From 46a67ddb350dcbf94d14cf686f96b398e2e600ed Mon Sep 17 00:00:00 2001 From: Phil Haack Date: Tue, 1 Dec 2026 14:20:29 -0800 Subject: [PATCH 1/8] fix(flags): keep session properties on minimal flag called events Keep $os_name, $os_version, $device_type, $app_version, $referring_domain and the utm and click-ID campaign keys that posthog-js keeps. The sessions table takes a session's initial OS, device type and attribution from its first event, and a minimal flag call can be that first event. --- .../minimal-flag-called-session-props.md | 6 ++ .../main/java/com/posthog/PostHogStateless.kt | 39 ++++++++- .../src/test/java/com/posthog/PostHogTest.kt | 84 +++++++++++++++++++ 3 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 .changeset/minimal-flag-called-session-props.md diff --git a/.changeset/minimal-flag-called-session-props.md b/.changeset/minimal-flag-called-session-props.md new file mode 100644 index 000000000..8e1a8acfc --- /dev/null +++ b/.changeset/minimal-flag-called-session-props.md @@ -0,0 +1,6 @@ +--- +"posthog": patch +"posthog-android": patch +--- + +Keep `$os_name`, `$os_version`, `$device_type`, `$app_version`, `$referring_domain` and the utm and click-ID campaign properties on minimal `$feature_flag_called` events, so a session that starts with a flag call keeps its initial OS, device type and attribution. diff --git a/posthog/src/main/java/com/posthog/PostHogStateless.kt b/posthog/src/main/java/com/posthog/PostHogStateless.kt index 6ee867c09..fe1e5dfe1 100644 --- a/posthog/src/main/java/com/posthog/PostHogStateless.kt +++ b/posthog/src/main/java/com/posthog/PostHogStateless.kt @@ -689,8 +689,8 @@ public open class PostHogStateless protected constructor( private const val GROUP_IDENTIFY = "\$groupidentify" // Strict allowlist for minimal $feature_flag_called events, defined by the cross-SDK - // contract: everything not listed is stripped, including registered super properties, the - // static and dynamic context envelope, and the $feature_flag_bootstrapped_* / + // contract: everything not listed is stripped, including other registered super properties, + // the rest of the context envelope, and the $feature_flag_bootstrapped_* / // $used_bootstrap_value fields this SDK otherwise sets. The list follows the shared // contract, so it may include properties this SDK does not set yet (e.g. locally_evaluated, // set only by posthog-server). @@ -712,6 +712,41 @@ public open class PostHogStateless protected constructor( "\$window_id", "\$lib", "\$lib_version", + // The sessions table takes a session's initial OS, device type and attribution from + // its first event, and a flag call can be that first event. Ingestion copies $os_name + // into $os. The attribution keys are present only when the app registered them as + // super properties. + "\$os_name", + "\$os_version", + "\$device_type", + "\$app_version", + "\$referring_domain", + "utm_source", + "utm_medium", + "utm_campaign", + "utm_content", + "utm_term", + "gad_source", + "mc_cid", + "gclid", + "gclsrc", + "dclid", + "gbraid", + "wbraid", + "fbclid", + "msclkid", + "twclid", + "li_fat_id", + "igshid", + "ttclid", + "rdt_cid", + "epik", + "qclid", + "sccid", + "oppref", + "bfcid", + "irclid", + "_kx", ) private val apiKeys = mutableSetOf() diff --git a/posthog/src/test/java/com/posthog/PostHogTest.kt b/posthog/src/test/java/com/posthog/PostHogTest.kt index b3d391da0..69a59830a 100644 --- a/posthog/src/test/java/com/posthog/PostHogTest.kt +++ b/posthog/src/test/java/com/posthog/PostHogTest.kt @@ -587,6 +587,10 @@ internal class PostHogTest { "\$session_id", "\$lib", "\$lib_version", + "\$os_name", + "\$os_version", + "\$device_type", + "\$app_version", ) assertEquals(expectedKeys, theEvent.properties!!.keys) @@ -601,6 +605,86 @@ internal class PostHogTest { sut.close() } + @Test + fun `minimal feature flag called event keeps registered session attribution properties`() { + val file = File("src/test/resources/json/basic-flags-minimal-flag-called-events.json") + val responseFlagsApi = file.readText() + + val http = + mockHttp( + response = + MockResponse() + .setBody(responseFlagsApi), + ) + http.enqueue( + MockResponse() + .setBody(""), + ) + val url = http.url("/") + + val sut = + getSut( + url.toString(), + preloadFeatureFlags = false, + context = TestPostHogContext(), + ) + + // posthog-js MINIMAL_FLAG_CALLED_EVENT_CAMPAIGN_PROPERTIES plus \$referring_domain + val attributionKeys = + listOf( + "\$referring_domain", + "utm_source", + "utm_medium", + "utm_campaign", + "utm_content", + "utm_term", + "gad_source", + "mc_cid", + "gclid", + "gclsrc", + "dclid", + "gbraid", + "wbraid", + "fbclid", + "msclkid", + "twclid", + "li_fat_id", + "igshid", + "ttclid", + "rdt_cid", + "epik", + "qclid", + "sccid", + "oppref", + "bfcid", + "irclid", + "_kx", + ) + attributionKeys.forEach { sut.register(it, "value-$it") } + sut.register("\$referrer", "https://example.com/full/path") + + sut.reloadFeatureFlags() + + remoteConfigExecutor.shutdownAndAwaitTermination() + + // remove from the http queue + http.takeRequest() + + assertFalse(sut.getFeatureFlag("IAmInactive") as Boolean) + + queueExecutor.shutdownAndAwaitTermination() + + val request = http.takeRequest() + val content = request.body.unGzip() + val batch = serializer.deserialize(content.reader()) + + val properties = batch.batch.first().properties!! + attributionKeys.forEach { assertEquals("value-$it", properties[it], "missing $it") } + assertFalse(properties.containsKey("\$referrer")) + + sut.close() + } + @Test fun `feature flag called event keeps the full shape when gated and the flag has an experiment`() { val file = File("src/test/resources/json/basic-flags-minimal-flag-called-events.json") From 3a5468a11b6ea9d48c8ec6915959c125ef9bb59f Mon Sep 17 00:00:00 2001 From: Phil Haack Date: Tue, 1 Dec 2026 14:28:42 -0800 Subject: [PATCH 2/8] Continue work on minimal-flag-called-session-props --- posthog/src/main/java/com/posthog/PostHogStateless.kt | 6 +++--- posthog/src/test/java/com/posthog/PostHogTest.kt | 6 +----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/posthog/src/main/java/com/posthog/PostHogStateless.kt b/posthog/src/main/java/com/posthog/PostHogStateless.kt index fe1e5dfe1..a05a00c96 100644 --- a/posthog/src/main/java/com/posthog/PostHogStateless.kt +++ b/posthog/src/main/java/com/posthog/PostHogStateless.kt @@ -713,9 +713,9 @@ public open class PostHogStateless protected constructor( "\$lib", "\$lib_version", // The sessions table takes a session's initial OS, device type and attribution from - // its first event, and a flag call can be that first event. Ingestion copies $os_name - // into $os. The attribution keys are present only when the app registered them as - // super properties. + // its first event. A flag call can be that first event. Ingestion copies $os_name into + // $os. The attribution keys are present only when the app registered them as super + // properties. "\$os_name", "\$os_version", "\$device_type", diff --git a/posthog/src/test/java/com/posthog/PostHogTest.kt b/posthog/src/test/java/com/posthog/PostHogTest.kt index 69a59830a..4499a3316 100644 --- a/posthog/src/test/java/com/posthog/PostHogTest.kt +++ b/posthog/src/test/java/com/posthog/PostHogTest.kt @@ -674,11 +674,7 @@ internal class PostHogTest { queueExecutor.shutdownAndAwaitTermination() - val request = http.takeRequest() - val content = request.body.unGzip() - val batch = serializer.deserialize(content.reader()) - - val properties = batch.batch.first().properties!! + val properties = firstBatchEvent(http).properties!! attributionKeys.forEach { assertEquals("value-$it", properties[it], "missing $it") } assertFalse(properties.containsKey("\$referrer")) From f9f26eb0556ad89de916d0ab78159ca39fa97170 Mon Sep 17 00:00:00 2001 From: Phil Haack Date: Tue, 1 Dec 2026 14:33:27 -0800 Subject: [PATCH 3/8] Address review findings --- posthog/src/test/java/com/posthog/PostHogTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posthog/src/test/java/com/posthog/PostHogTest.kt b/posthog/src/test/java/com/posthog/PostHogTest.kt index 4499a3316..32ed01abc 100644 --- a/posthog/src/test/java/com/posthog/PostHogTest.kt +++ b/posthog/src/test/java/com/posthog/PostHogTest.kt @@ -629,7 +629,7 @@ internal class PostHogTest { context = TestPostHogContext(), ) - // posthog-js MINIMAL_FLAG_CALLED_EVENT_CAMPAIGN_PROPERTIES plus \$referring_domain + // posthog-js MINIMAL_FLAG_CALLED_EVENT_CAMPAIGN_PROPERTIES plus $referring_domain val attributionKeys = listOf( "\$referring_domain", From 301b3613248a0f8d7081dd33c024b940c7358aa5 Mon Sep 17 00:00:00 2001 From: Phil Haack Date: Wed, 2 Dec 2026 08:45:22 -0800 Subject: [PATCH 4/8] Keep ingestion inputs and limit device properties to the client on minimal flag calls --- .../minimal-flag-called-session-props.md | 6 +- posthog/src/main/java/com/posthog/PostHog.kt | 8 +++ .../main/java/com/posthog/PostHogStateless.kt | 30 +++++++--- .../java/com/posthog/PostHogStatelessTest.kt | 58 +++++++++++++++++++ .../src/test/java/com/posthog/PostHogTest.kt | 58 +++++++++++++++++++ 5 files changed, 150 insertions(+), 10 deletions(-) diff --git a/.changeset/minimal-flag-called-session-props.md b/.changeset/minimal-flag-called-session-props.md index 8e1a8acfc..6df1afab2 100644 --- a/.changeset/minimal-flag-called-session-props.md +++ b/.changeset/minimal-flag-called-session-props.md @@ -3,4 +3,8 @@ "posthog-android": patch --- -Keep `$os_name`, `$os_version`, `$device_type`, `$app_version`, `$referring_domain` and the utm and click-ID campaign properties on minimal `$feature_flag_called` events, so a session that starts with a flag call keeps its initial OS, device type and attribution. +Keep more properties on minimal `$feature_flag_called` events: + +- The ingestion inputs `$ip`, `$raw_user_agent`, `$host`, `$timezone` and `$cookieless_extra` now stay on every minimal event, so GeoIP and cookieless processing read the same values they read on other events. +- `$referring_domain` and the utm and click-ID campaign properties stay, so a session that starts with a flag call keeps its attribution. +- On posthog-android, `$os_name`, `$os_version`, `$device_type` and `$app_version` stay, so a session that starts with a flag call keeps its initial OS and device type. Server SDKs strip these, because on a server they describe the request, not the host. diff --git a/posthog/src/main/java/com/posthog/PostHog.kt b/posthog/src/main/java/com/posthog/PostHog.kt index 9126471f9..9574a330b 100644 --- a/posthog/src/main/java/com/posthog/PostHog.kt +++ b/posthog/src/main/java/com/posthog/PostHog.kt @@ -2885,6 +2885,11 @@ public class PostHog private constructor( return remoteConfig?.isMinimalFlagCalledEventsEnabled() == true } + // The sessions table takes a session's initial OS and device type from its first event. A flag + // call can be that first event. Ingestion copies $os_name into $os. + override val minimalFlagCalledDeviceProperties: Set + get() = MINIMAL_FEATURE_FLAG_CALLED_DEVICE_PROPERTIES + public override fun getFeatureFlag( key: String, sendFeatureFlagEvent: Boolean?, @@ -3466,6 +3471,9 @@ public class PostHog private constructor( private val IDENTITY_EVENTS = setOf("\$identify", "\$create_alias", "\$merge_dangerously", "\$groupidentify", "\$set", "\$set_once") + private val MINIMAL_FEATURE_FLAG_CALLED_DEVICE_PROPERTIES = + setOf("\$os_name", "\$os_version", "\$device_type", "\$app_version") + /** * Captures application log records into PostHog's logs product * (separate from product analytics events). Forwards to the shared diff --git a/posthog/src/main/java/com/posthog/PostHogStateless.kt b/posthog/src/main/java/com/posthog/PostHogStateless.kt index d4707f5ba..8c20fa061 100644 --- a/posthog/src/main/java/com/posthog/PostHogStateless.kt +++ b/posthog/src/main/java/com/posthog/PostHogStateless.kt @@ -704,9 +704,19 @@ public open class PostHogStateless protected constructor( return properties } - return properties.filterKeys { it in MINIMAL_FEATURE_FLAG_CALLED_PROPERTIES }.toMutableMap() + return properties + .filterKeys { it in MINIMAL_FEATURE_FLAG_CALLED_PROPERTIES || it in minimalFlagCalledDeviceProperties } + .toMutableMap() } + /** + * Device properties that a client SDK also keeps on a minimal `$feature_flag_called` event. + * A server SDK strips them, because on a server they describe the request it handles, not its + * own host. + */ + internal open val minimalFlagCalledDeviceProperties: Set + get() = emptySet() + /** * Whether the server enabled minimal `$feature_flag_called` events for this team. The client * SDK stores its remote config on [PostHogConfig] rather than on [featureFlags], so it @@ -959,14 +969,16 @@ public open class PostHogStateless protected constructor( "\$window_id", "\$lib", "\$lib_version", - // The sessions table takes a session's initial OS, device type and attribution from - // its first event. A flag call can be that first event. Ingestion copies $os_name into - // $os. The attribution keys are present only when the app registered them as super - // properties. - "\$os_name", - "\$os_version", - "\$device_type", - "\$app_version", + // Ingestion reads these for GeoIP and for the cookieless distinct id and session id, + // so they stay whether or not the event is in cookieless mode. + "\$ip", + "\$raw_user_agent", + "\$host", + "\$timezone", + "\$cookieless_extra", + // The sessions table takes a session's initial attribution from its first event. A + // flag call can be that first event. The attribution keys are present only when the + // app registered them as super properties. "\$referring_domain", "utm_source", "utm_medium", diff --git a/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt b/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt index 27d2a416b..e3748e0ec 100644 --- a/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt +++ b/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt @@ -2,6 +2,7 @@ package com.posthog import com.posthog.internal.FeatureFlag import com.posthog.internal.FeatureFlagMetadata +import com.posthog.internal.PostHogContext import com.posthog.internal.PostHogFeatureFlagCalledCache import com.posthog.internal.PostHogFeatureFlagsInterface import com.posthog.internal.PostHogMemoryPreferences @@ -101,6 +102,7 @@ internal class PostHogStatelessTest { } private class MockFeatureFlags : PostHogFeatureFlagsInterface { + var minimalFlagCalledEvents = false private val flags = mutableMapOf() private val payloads = mutableMapOf() private val details = mutableMapOf() @@ -214,6 +216,8 @@ internal class PostHogStatelessTest { return details[key] } + override fun isMinimalFlagCalledEventsEnabled(): Boolean = minimalFlagCalledEvents + override fun clear() { flags.clear() payloads.clear() @@ -788,6 +792,60 @@ internal class PostHogStatelessTest { assertEquals(false, mockQueue.events[1].properties!!["\$feature_flag_has_experiment"]) } + @Test + fun `minimal feature flag called event strips caller device properties and keeps ingestion inputs`() { + val mockQueue = MockQueue() + val mockFeatureFlags = MockFeatureFlags() + mockFeatureFlags.minimalFlagCalledEvents = true + mockFeatureFlags.setFlagDetails( + "plain_flag", + FeatureFlag( + key = "plain_flag", + enabled = true, + variant = null, + metadata = FeatureFlagMetadata(id = 2, payload = null, version = 1, hasExperiment = false), + reason = null, + ), + ) + + sut = createStatelessInstance() + config = + createConfig(sendFeatureFlagEvent = true).apply { + // posthog-server passes request context properties to events through the dynamic context. + context = + object : PostHogContext { + override fun getStaticContext(): Map = emptyMap() + + override fun getDynamicContext(): Map = + mapOf( + "\$device_type" to "Mobile", + "\$os_name" to "iOS", + "\$os_version" to "17.0", + "\$app_version" to "2.0.0", + "\$raw_user_agent" to "Mozilla/5.0 (iPhone)", + "\$ip" to "203.0.113.7", + ) + + override fun getSdkInfo(): Map = emptyMap() + } + } + + sut.setup(config) + sut.setMockQueue(mockQueue) + sut.setMockFeatureFlags(mockFeatureFlags) + + sut.getFeatureFlagResultStateless("user123", "plain_flag") + + val properties = mockQueue.events.single().properties!! + assertEquals(false, properties["\$feature_flag_has_experiment"]) + assertFalse(properties.containsKey("\$device_type")) + assertFalse(properties.containsKey("\$os_name")) + assertFalse(properties.containsKey("\$os_version")) + assertFalse(properties.containsKey("\$app_version")) + assertEquals("Mozilla/5.0 (iPhone)", properties["\$raw_user_agent"]) + assertEquals("203.0.113.7", properties["\$ip"]) + } + @Test fun `feature flag called event omits has_experiment when metadata does not report it`() { val mockQueue = MockQueue() diff --git a/posthog/src/test/java/com/posthog/PostHogTest.kt b/posthog/src/test/java/com/posthog/PostHogTest.kt index 4329100b6..e45f22c8a 100644 --- a/posthog/src/test/java/com/posthog/PostHogTest.kt +++ b/posthog/src/test/java/com/posthog/PostHogTest.kt @@ -1064,6 +1064,64 @@ internal class PostHogTest { sut.close() } + @Test + fun `minimal feature flag called event keeps the ingestion inputs the context attaches`() { + val file = File("src/test/resources/json/basic-flags-minimal-flag-called-events.json") + val responseFlagsApi = file.readText() + + val http = + mockHttp( + response = + MockResponse() + .setBody(responseFlagsApi), + ) + http.enqueue( + MockResponse() + .setBody(""), + ) + val url = http.url("/") + + val context = + object : PostHogContext { + override fun getStaticContext(): Map = emptyMap() + + override fun getDynamicContext(): Map = + mapOf( + "\$raw_user_agent" to "Dalvik/2.1.0 (Linux; U; Android 14)", + "\$timezone" to "Europe/London", + "\$user_agent" to "Dalvik/2.1.0 (Linux; U; Android 14)", + "\$locale" to "en-GB", + ) + + override fun getSdkInfo(): Map = emptyMap() + } + val sut = + getSut( + url.toString(), + preloadFeatureFlags = false, + context = context, + ) + + sut.reloadFeatureFlags() + + remoteConfigExecutor.shutdownAndAwaitTermination() + + // remove from the http queue + http.takeRequest() + + assertFalse(sut.getFeatureFlag("IAmInactive") as Boolean) + + queueExecutor.shutdownAndAwaitTermination() + + val properties = firstBatchEvent(http).properties!! + assertEquals("Dalvik/2.1.0 (Linux; U; Android 14)", properties["\$raw_user_agent"]) + assertEquals("Europe/London", properties["\$timezone"]) + assertFalse(properties.containsKey("\$user_agent")) + assertFalse(properties.containsKey("\$locale")) + + sut.close() + } + @Test fun `minimal feature flag called event keeps the registered legacy option properties`() { val file = File("src/test/resources/json/basic-flags-minimal-flag-called-events.json") From 28239c3332fa6368632bce49f53f04e05e1f4af7 Mon Sep 17 00:00:00 2001 From: Phil Haack Date: Wed, 2 Dec 2026 08:51:48 -0800 Subject: [PATCH 5/8] Cover $host and $cookieless_extra in the minimal flag call test --- .changeset/minimal-flag-called-session-props.md | 2 +- posthog/src/main/java/com/posthog/PostHogStateless.kt | 6 +++--- posthog/src/test/java/com/posthog/PostHogTest.kt | 4 ++++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.changeset/minimal-flag-called-session-props.md b/.changeset/minimal-flag-called-session-props.md index 6df1afab2..da186b8e5 100644 --- a/.changeset/minimal-flag-called-session-props.md +++ b/.changeset/minimal-flag-called-session-props.md @@ -7,4 +7,4 @@ Keep more properties on minimal `$feature_flag_called` events: - The ingestion inputs `$ip`, `$raw_user_agent`, `$host`, `$timezone` and `$cookieless_extra` now stay on every minimal event, so GeoIP and cookieless processing read the same values they read on other events. - `$referring_domain` and the utm and click-ID campaign properties stay, so a session that starts with a flag call keeps its attribution. -- On posthog-android, `$os_name`, `$os_version`, `$device_type` and `$app_version` stay, so a session that starts with a flag call keeps its initial OS and device type. Server SDKs strip these, because on a server they describe the request, not the host. +- On posthog-android, `$os_name`, `$os_version`, `$device_type` and `$app_version` stay, so a session that starts with a flag call keeps its initial OS and device type. posthog-server does not send minimal flag call events today. When it does, it will strip these, because on a server they describe the request, not the host. diff --git a/posthog/src/main/java/com/posthog/PostHogStateless.kt b/posthog/src/main/java/com/posthog/PostHogStateless.kt index 8c20fa061..a4d0f4039 100644 --- a/posthog/src/main/java/com/posthog/PostHogStateless.kt +++ b/posthog/src/main/java/com/posthog/PostHogStateless.kt @@ -941,9 +941,9 @@ public open class PostHogStateless protected constructor( private const val GROUP_IDENTIFY = "\$groupidentify" // Strict allowlist for minimal $feature_flag_called events, defined by the cross-SDK - // contract: everything not listed is stripped, including other registered super properties, - // the rest of the context envelope, and the $feature_flag_bootstrapped_* / - // $used_bootstrap_value fields this SDK otherwise sets. The list follows the shared + // contract: everything not listed here or in minimalFlagCalledDeviceProperties is stripped, + // including other registered super properties, the rest of the context envelope, and the + // $feature_flag_bootstrapped_* / $used_bootstrap_value fields this SDK otherwise sets. The list follows the shared // contract, so it may include properties this SDK does not set yet (e.g. locally_evaluated, // set only by posthog-server). private val MINIMAL_FEATURE_FLAG_CALLED_PROPERTIES = diff --git a/posthog/src/test/java/com/posthog/PostHogTest.kt b/posthog/src/test/java/com/posthog/PostHogTest.kt index e45f22c8a..a1558b0b3 100644 --- a/posthog/src/test/java/com/posthog/PostHogTest.kt +++ b/posthog/src/test/java/com/posthog/PostHogTest.kt @@ -1089,6 +1089,8 @@ internal class PostHogTest { mapOf( "\$raw_user_agent" to "Dalvik/2.1.0 (Linux; U; Android 14)", "\$timezone" to "Europe/London", + "\$host" to "app.example.com", + "\$cookieless_extra" to "extra", "\$user_agent" to "Dalvik/2.1.0 (Linux; U; Android 14)", "\$locale" to "en-GB", ) @@ -1116,6 +1118,8 @@ internal class PostHogTest { val properties = firstBatchEvent(http).properties!! assertEquals("Dalvik/2.1.0 (Linux; U; Android 14)", properties["\$raw_user_agent"]) assertEquals("Europe/London", properties["\$timezone"]) + assertEquals("app.example.com", properties["\$host"]) + assertEquals("extra", properties["\$cookieless_extra"]) assertFalse(properties.containsKey("\$user_agent")) assertFalse(properties.containsKey("\$locale")) From 1a34e31489518c0cfb5f1467fab80d6d5f002b85 Mon Sep 17 00:00:00 2001 From: Phil Haack Date: Wed, 2 Dec 2026 09:01:11 -0800 Subject: [PATCH 6/8] Keep the spec's full client device and URL list on minimal flag calls --- .../minimal-flag-called-session-props.md | 2 +- posthog/src/main/java/com/posthog/PostHog.kt | 18 +++++- .../main/java/com/posthog/PostHogStateless.kt | 2 +- .../java/com/posthog/PostHogStatelessTest.kt | 4 ++ .../src/test/java/com/posthog/PostHogTest.kt | 58 +++++++++++++++++++ 5 files changed, 79 insertions(+), 5 deletions(-) diff --git a/.changeset/minimal-flag-called-session-props.md b/.changeset/minimal-flag-called-session-props.md index da186b8e5..69d56df90 100644 --- a/.changeset/minimal-flag-called-session-props.md +++ b/.changeset/minimal-flag-called-session-props.md @@ -7,4 +7,4 @@ Keep more properties on minimal `$feature_flag_called` events: - The ingestion inputs `$ip`, `$raw_user_agent`, `$host`, `$timezone` and `$cookieless_extra` now stay on every minimal event, so GeoIP and cookieless processing read the same values they read on other events. - `$referring_domain` and the utm and click-ID campaign properties stay, so a session that starts with a flag call keeps its attribution. -- On posthog-android, `$os_name`, `$os_version`, `$device_type` and `$app_version` stay, so a session that starts with a flag call keeps its initial OS and device type. posthog-server does not send minimal flag call events today. When it does, it will strip these, because on a server they describe the request, not the host. +- On posthog-android, `$os_name`, `$os_version`, `$device_type`, `$app_version`, `$os`, `$browser`, `$browser_version`, `$viewport_width`, `$viewport_height` and `$current_url` stay when the event carries them, so a session that starts or ends with a flag call keeps its initial device properties and its entry and end URL. posthog-server does not send minimal flag call events today. When it does, it will strip these, because on a server they describe the request, not the host. diff --git a/posthog/src/main/java/com/posthog/PostHog.kt b/posthog/src/main/java/com/posthog/PostHog.kt index 9574a330b..6e72f186e 100644 --- a/posthog/src/main/java/com/posthog/PostHog.kt +++ b/posthog/src/main/java/com/posthog/PostHog.kt @@ -2885,8 +2885,9 @@ public class PostHog private constructor( return remoteConfig?.isMinimalFlagCalledEventsEnabled() == true } - // The sessions table takes a session's initial OS and device type from its first event. A flag - // call can be that first event. Ingestion copies $os_name into $os. + // The sessions table takes a session's initial browser, OS, device type, viewport and entry URL + // from its first event, and its end URL from its last event. A flag call can be either event. + // Ingestion copies $os_name into $os. $app_version is static platform identity, like the OS. override val minimalFlagCalledDeviceProperties: Set get() = MINIMAL_FEATURE_FLAG_CALLED_DEVICE_PROPERTIES @@ -3472,7 +3473,18 @@ public class PostHog private constructor( setOf("\$identify", "\$create_alias", "\$merge_dangerously", "\$groupidentify", "\$set", "\$set_once") private val MINIMAL_FEATURE_FLAG_CALLED_DEVICE_PROPERTIES = - setOf("\$os_name", "\$os_version", "\$device_type", "\$app_version") + setOf( + "\$browser", + "\$browser_version", + "\$os", + "\$os_name", + "\$os_version", + "\$device_type", + "\$viewport_width", + "\$viewport_height", + "\$current_url", + "\$app_version", + ) /** * Captures application log records into PostHog's logs product diff --git a/posthog/src/main/java/com/posthog/PostHogStateless.kt b/posthog/src/main/java/com/posthog/PostHogStateless.kt index a4d0f4039..47191e3e2 100644 --- a/posthog/src/main/java/com/posthog/PostHogStateless.kt +++ b/posthog/src/main/java/com/posthog/PostHogStateless.kt @@ -710,7 +710,7 @@ public open class PostHogStateless protected constructor( } /** - * Device properties that a client SDK also keeps on a minimal `$feature_flag_called` event. + * Device and URL properties that a client SDK also keeps on a minimal `$feature_flag_called` event. * A server SDK strips them, because on a server they describe the request it handles, not its * own host. */ diff --git a/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt b/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt index e3748e0ec..b37e46160 100644 --- a/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt +++ b/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt @@ -822,6 +822,8 @@ internal class PostHogStatelessTest { "\$os_name" to "iOS", "\$os_version" to "17.0", "\$app_version" to "2.0.0", + "\$browser" to "Chrome", + "\$current_url" to "https://api.example.com/api/checkout", "\$raw_user_agent" to "Mozilla/5.0 (iPhone)", "\$ip" to "203.0.113.7", ) @@ -842,6 +844,8 @@ internal class PostHogStatelessTest { assertFalse(properties.containsKey("\$os_name")) assertFalse(properties.containsKey("\$os_version")) assertFalse(properties.containsKey("\$app_version")) + assertFalse(properties.containsKey("\$browser")) + assertFalse(properties.containsKey("\$current_url")) assertEquals("Mozilla/5.0 (iPhone)", properties["\$raw_user_agent"]) assertEquals("203.0.113.7", properties["\$ip"]) } diff --git a/posthog/src/test/java/com/posthog/PostHogTest.kt b/posthog/src/test/java/com/posthog/PostHogTest.kt index a1558b0b3..5e588f124 100644 --- a/posthog/src/test/java/com/posthog/PostHogTest.kt +++ b/posthog/src/test/java/com/posthog/PostHogTest.kt @@ -1126,6 +1126,64 @@ internal class PostHogTest { sut.close() } + @Test + fun `minimal feature flag called event keeps the session device properties and current url`() { + val file = File("src/test/resources/json/basic-flags-minimal-flag-called-events.json") + val responseFlagsApi = file.readText() + + val http = + mockHttp( + response = + MockResponse() + .setBody(responseFlagsApi), + ) + http.enqueue( + MockResponse() + .setBody(""), + ) + val url = http.url("/") + + val sessionProperties = + mapOf( + "\$browser" to "Chrome", + "\$browser_version" to "120", + "\$os" to "Android", + "\$viewport_width" to 412, + "\$viewport_height" to 915, + "\$current_url" to "https://app.example.com/pricing", + ) + val context = + object : PostHogContext { + override fun getStaticContext(): Map = emptyMap() + + override fun getDynamicContext(): Map = sessionProperties + + override fun getSdkInfo(): Map = emptyMap() + } + val sut = + getSut( + url.toString(), + preloadFeatureFlags = false, + context = context, + ) + + sut.reloadFeatureFlags() + + remoteConfigExecutor.shutdownAndAwaitTermination() + + // remove from the http queue + http.takeRequest() + + assertFalse(sut.getFeatureFlag("IAmInactive") as Boolean) + + queueExecutor.shutdownAndAwaitTermination() + + val properties = firstBatchEvent(http).properties!! + sessionProperties.forEach { (key, value) -> assertEquals(value, properties[key], "missing $key") } + + sut.close() + } + @Test fun `minimal feature flag called event keeps the registered legacy option properties`() { val file = File("src/test/resources/json/basic-flags-minimal-flag-called-events.json") From ec2e6054a9c373d70702b744857d4232f417245c Mon Sep 17 00:00:00 2001 From: Phil Haack Date: Wed, 2 Dec 2026 09:08:20 -0800 Subject: [PATCH 7/8] Assert the server path strips every client-only key --- posthog/src/test/java/com/posthog/PostHogStatelessTest.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt b/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt index b37e46160..5b88993c9 100644 --- a/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt +++ b/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt @@ -822,7 +822,11 @@ internal class PostHogStatelessTest { "\$os_name" to "iOS", "\$os_version" to "17.0", "\$app_version" to "2.0.0", + "\$os" to "iOS", "\$browser" to "Chrome", + "\$browser_version" to "120", + "\$viewport_width" to 390, + "\$viewport_height" to 844, "\$current_url" to "https://api.example.com/api/checkout", "\$raw_user_agent" to "Mozilla/5.0 (iPhone)", "\$ip" to "203.0.113.7", @@ -844,7 +848,11 @@ internal class PostHogStatelessTest { assertFalse(properties.containsKey("\$os_name")) assertFalse(properties.containsKey("\$os_version")) assertFalse(properties.containsKey("\$app_version")) + assertFalse(properties.containsKey("\$os")) assertFalse(properties.containsKey("\$browser")) + assertFalse(properties.containsKey("\$browser_version")) + assertFalse(properties.containsKey("\$viewport_width")) + assertFalse(properties.containsKey("\$viewport_height")) assertFalse(properties.containsKey("\$current_url")) assertEquals("Mozilla/5.0 (iPhone)", properties["\$raw_user_agent"]) assertEquals("203.0.113.7", properties["\$ip"]) From 4ca94efb6cae5bd78ff6f2147db426f916908d4e Mon Sep 17 00:00:00 2001 From: Phil Haack Date: Wed, 2 Dec 2026 14:54:13 -0800 Subject: [PATCH 8/8] Stop the real queue when a stateless test swaps in a mock --- posthog/src/test/java/com/posthog/PostHogStatelessTest.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt b/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt index 5b88993c9..3d687b422 100644 --- a/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt +++ b/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt @@ -48,6 +48,8 @@ internal class PostHogStatelessTest { fun isEnabledPublic(): Boolean = isEnabled() fun setMockQueue(queue: PostHogQueueInterface) { + // setup() started a real queue with a repeating flush timer, and teardown stops only the mock. + this.queue?.stop() this.queue = queue }