Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changeset/android-v4-major.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- **Breaking:** remove `PostHogConfig.onFeatureFlags`; call `PostHog.onFeatureFlags(callback)` before `setup()` instead
- **Breaking:** replace `PostHogOnFeatureFlags` with `PostHogFeatureFlagsCallback` in `reloadFeatureFlags()`, which receives a `PostHogFeatureFlagsLoaded` with the enabled flags, their values and `errorsLoading`
- **Breaking:** return `null` from `getFeatureFlagResult(key)?.payload` for a payload that isn't valid JSON, such as plain text, a malformed or empty string, or a JSON `null`
- **Breaking:** return `enabled = false` from `getFeatureFlagResult()` and leave the flag out of `PostHogFeatureFlagsLoaded` and `$active_feature_flags` for a feature flag whose variant is an empty string, as `isFeatureEnabled()` already does
- **Breaking:** remove wireframe session replay and the `screenshot`, `drawableConverter` and `debouncerDelayMs` options of `PostHogSessionReplayConfig` (use `throttleDelayMs` for the last); replay always records masked screenshots, so apps that relied on the default now upload screenshots and should mask custom-drawn views, maps and video, or turn replay off
- **Breaking:** apply `Modifier.postHogMask()` and `Modifier.postHogUnmask()` only to the element and its children, mask Compose text that draws past its layout box in full, and mask nodes using `Modifier.clearAndSetSemantics` while text or image masking is on
- **Breaking:** send a dialog's dim and a dialog masked whole as screenshot images instead of styled wireframe shapes
Expand Down
6 changes: 6 additions & 0 deletions docs/migration-3.x-to-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ If you read a payload as plain text, such as a payload saved as `hello` rather t

Apps upgrading from 3.13 or older didn't cache flag details, so until the app's first `/flags` response, a malformed payload cached by 3.x still reads as the raw string, as it did in 3.x.

### Empty variants read as disabled

A flag whose variant is an empty string now reads as disabled everywhere, as it already did from `isFeatureEnabled()`, with or without `defaultValue`. `getFeatureFlagResult()` and `getAllFeatureFlags()` return `enabled = false` for it, where 3.x returned `true`, `PostHogFeatureFlagsLoaded` leaves it out of `flags` and `variants`, and captured events leave it out of `$active_feature_flags`. `variant`, `getFeatureFlag()` and the event's `$feature/<key>` property still return `""`.

PostHog no longer accepts an empty variant for new flags, so only flags created earlier through the API, MCP or Terraform are affected.

### `$locale` and `$app_language`

`$locale` now means the first language in the device settings, as a full language tag such as `de-CH` or `zh-Hant-TW`. In 3.x it came from `Locale.getDefault()`, which could be a secondary language, a per-app language or a value your app set with `Locale.setDefault()`, and it dropped the script (`zh-TW`). It's left out when the device reports no language. On older Android versions, 3.x also sent the legacy codes `iw`, `in` and `ji`, so Hebrew, Indonesian and Yiddish values change, such as `in-ID` to `id-ID`. Update filters that match those. Other values mostly stay the same, so otherwise only check filters if your users change these settings.
Expand Down
4 changes: 2 additions & 2 deletions posthog/src/main/java/com/posthog/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -908,8 +908,8 @@ public class PostHog private constructor(
for (entry in it.entries) {
props["\$feature/${entry.key}"] = entry.value

// only add active feature flags
val active = entry.value as? Boolean ?: true
// only add active feature flags; an empty variant reads as disabled
val active = entry.value != false && entry.value != ""

if (active) {
keys.add(entry.key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class PostHogFeatureFlagsLoaded
*/
public val variants: Map<String, Any> =
featureFlags.entries
.mapNotNull { (key, value) -> if (value == true || value is String) key to value else null }
.mapNotNull { (key, value) -> if (value == true || (value is String && value.isNotEmpty())) key to value else null }
.toMap()

/**
Expand Down
4 changes: 2 additions & 2 deletions posthog/src/main/java/com/posthog/PostHogStateless.kt
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ public open class PostHogStateless protected constructor(
for (entry in it.entries) {
props["\$feature/${entry.key}"] = entry.value

// only add active feature flags
val active = entry.value as? Boolean ?: true
// only add active feature flags; an empty variant reads as disabled
val active = entry.value != false && entry.value != ""

if (active) {
keys.add(entry.key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ public class PostHogRemoteConfig(
val (enabled, variant) =
when (value) {
is Boolean -> value to null
is String -> true to value
is String -> value.isNotEmpty() to value
else -> return null
}

Expand Down
20 changes: 20 additions & 0 deletions posthog/src/test/java/com/posthog/PostHogFeatureFlagsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,26 @@ internal class PostHogFeatureFlagsTest {
assertFalse(sut.isFeatureEnabled("IAmInactive", defaultValue = true))
}

@Test
fun `an empty variant reads as disabled`() {
val flags =
File("src/test/resources/json/basic-flags-with-non-active-flags.json").readText()
.replace("\"variant\": \"SplashV2\"", "\"variant\": \"\"")
val http = mockHttp(response = MockResponse().setBody(flags))
val sut = getSut(http.url("/").toString(), preloadFeatureFlags = false)
val updates = LinkedBlockingQueue<PostHogFeatureFlagsLoaded>()

sut.reloadFeatureFlags { updates.add(it) }
val loaded = updates.poll(5, TimeUnit.SECONDS)!!

assertFalse(sut.isFeatureEnabled("splashScreenName"))
assertFalse(sut.isFeatureEnabled("splashScreenName", defaultValue = true))
val result = sut.getFeatureFlagResult("splashScreenName")!!
assertFalse(result.enabled)
assertEquals("", result.variant)
assertEquals(listOf("4535-funnel-bar-viz"), loaded.flags)
}

@Test
fun `disabled SDK returns null from getFeatureFlag and the default from isFeatureEnabled`() {
val http =
Expand Down
2 changes: 2 additions & 0 deletions posthog/src/test/java/com/posthog/PostHogStatelessTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ internal class PostHogStatelessTest {
val mockQueue = MockQueue()
val mockFeatureFlags = MockFeatureFlags()
mockFeatureFlags.setFlag("test_flag", true)
mockFeatureFlags.setFlag("empty_variant_flag", "")

sut = createStatelessInstance()
config = createConfig(sendFeatureFlagEvent = true)
Expand All @@ -596,6 +597,7 @@ internal class PostHogStatelessTest {
assertEquals(1, mockQueue.events.size)
val event = mockQueue.events.first()
assertEquals(true, event.properties!!["\$feature/test_flag"])
assertEquals("", event.properties!!["\$feature/empty_variant_flag"])
assertEquals(listOf("test_flag"), event.properties!!["\$active_feature_flags"])
}

Expand Down
24 changes: 24 additions & 0 deletions posthog/src/test/java/com/posthog/PostHogTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,30 @@ internal class PostHogTest {
sut.close()
}

@Test
fun `captured events leave a flag with an empty variant out of active_feature_flags`() {
val flags =
File("src/test/resources/json/basic-flags-with-non-active-flags.json").readText()
.replace("\"variant\": \"SplashV2\"", "\"variant\": \"\"")
val http = mockHttp(response = MockResponse().setBody(flags))
http.enqueue(MockResponse().setBody(""))
val sut = getSut(http.url("/").toString(), preloadFeatureFlags = false)

sut.reloadFeatureFlags()
remoteConfigExecutor.shutdownAndAwaitTermination()
http.takeRequest()

sut.capture(EVENT, DISTINCT_ID)
queueExecutor.shutdownAndAwaitTermination()

val batch = serializer.deserialize<PostHogBatchEvent>(http.takeRequest().body.unGzip().reader())
val properties = batch.batch.first().properties!!
assertEquals("", properties["\$feature/splashScreenName"])
assertEquals(listOf("4535-funnel-bar-viz"), properties["\$active_feature_flags"])

sut.close()
}

@Test
fun `feature flag called event reports has_experiment from flag metadata`() {
val file = File("src/test/resources/json/basic-flags-with-non-active-flags.json")
Expand Down
Loading