Skip to content

Commit 6d357ec

Browse files
authored
active_feature_flags event should filter non active flags (#41)
1 parent eea1e33 commit 6d357ec

4 files changed

Lines changed: 52 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Next
22

3-
- Upsert flags when loading feature flags with computed errors ([#38](https://github.com/PostHog/posthog-android/pull/38))
3+
- Upsert flags when loading feature flags with computed errors ([#38](https://github.com/PostHog/posthog-android/pull/38))
4+
- `$active_feature_flags` event should filter non active flags ([#41](https://github.com/PostHog/posthog-android/pull/41))
45

56
## 3.0.0-alpha.5 - 2023-10-04
67

posthog/src/main/java/com/posthog/PostHog.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,13 @@ public class PostHog private constructor(
209209
val keys = mutableListOf<String>()
210210
for (entry in it.entries) {
211211
props["\$feature/${entry.key}"] = entry.value
212-
keys.add(entry.key)
212+
213+
// only add active feature flags
214+
val active = entry.value as? Boolean ?: true
215+
216+
if (active) {
217+
keys.add(entry.key)
218+
}
213219
}
214220
props["\$active_feature_flags"] = keys
215221
}

posthog/src/test/java/com/posthog/PostHogTest.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ internal class PostHogTest {
216216

217217
@Test
218218
fun `getFeatureFlag captures feature flag event if enabled`() {
219+
val file = File("src/test/resources/json/basic-decide-with-non-active-flags.json")
220+
val responseDecideApi = file.readText()
221+
219222
val http = mockHttp(
220223
response =
221224
MockResponse()
@@ -237,6 +240,8 @@ internal class PostHogTest {
237240
http.takeRequest()
238241

239242
assertTrue(sut.getFeatureFlag("4535-funnel-bar-viz") as Boolean)
243+
assertFalse(sut.getFeatureFlag("IAmInactive") as Boolean)
244+
assertEquals("SplashV2", sut.getFeatureFlag("splashScreenName") as String)
240245

241246
queueExecutor.shutdownAndAwaitTermination()
242247

@@ -250,12 +255,15 @@ internal class PostHogTest {
250255
assertNotNull(theEvent.timestamp)
251256
assertNotNull(theEvent.uuid)
252257

253-
// {$feature/4535-funnel-bar-viz=true, $active_feature_flags=[4535-funnel-bar-viz], $feature_flag=4535-funnel-bar-viz, $feature_flag_response=true}
254258
assertEquals(true, theEvent.properties!!["\$feature/4535-funnel-bar-viz"])
259+
assertEquals(false, theEvent.properties!!["\$feature/IAmInactive"])
260+
assertEquals("SplashV2", theEvent.properties!!["\$feature/splashScreenName"])
255261

256262
@Suppress("UNCHECKED_CAST")
257263
val theFlags = theEvent.properties!!["\$active_feature_flags"] as List<String>
258-
assertEquals("4535-funnel-bar-viz", theFlags[0])
264+
assertTrue(theFlags.contains("4535-funnel-bar-viz"))
265+
assertTrue(theFlags.contains("splashScreenName"))
266+
assertFalse(theFlags.contains("IAmInactive"))
259267

260268
assertEquals("4535-funnel-bar-viz", theEvent.properties!!["\$feature_flag"])
261269
assertEquals(true, theEvent.properties!!["\$feature_flag_response"])
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"autocaptureExceptions": false,
3+
"toolbarParams": {},
4+
"errorsWhileComputingFlags": false,
5+
"capturePerformance": true,
6+
"autocapture_opt_out": false,
7+
"isAuthenticated": false,
8+
"supportedCompression": [
9+
"gzip",
10+
"gzip-js"
11+
],
12+
"config": {
13+
"enable_collect_everything": true
14+
},
15+
"featureFlagPayloads": {
16+
"thePayload": true
17+
},
18+
"featureFlags": {
19+
"4535-funnel-bar-viz": true,
20+
"IAmInactive": false,
21+
"splashScreenName": "SplashV2"
22+
},
23+
"sessionRecording": false,
24+
"siteApps": [
25+
{
26+
"id": 21039.0,
27+
"url": "/site_app/21039/EOsOSePYNyTzHkZ3f4mjrjUap8Hy8o2vUTAc6v1ZMFP/576ac89bc8aed72a21d9b19221c2c626/"
28+
}
29+
],
30+
"editorParams": {
31+
32+
}
33+
}

0 commit comments

Comments
 (0)