diff --git a/.changeset/minimal-flag-called-session-props.md b/.changeset/minimal-flag-called-session-props.md new file mode 100644 index 000000000..69d56df90 --- /dev/null +++ b/.changeset/minimal-flag-called-session-props.md @@ -0,0 +1,10 @@ +--- +"posthog": patch +"posthog-android": patch +--- + +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`, `$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 9126471f9..6e72f186e 100644 --- a/posthog/src/main/java/com/posthog/PostHog.kt +++ b/posthog/src/main/java/com/posthog/PostHog.kt @@ -2885,6 +2885,12 @@ public class PostHog private constructor( return remoteConfig?.isMinimalFlagCalledEventsEnabled() == true } + // 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 + public override fun getFeatureFlag( key: String, sendFeatureFlagEvent: Boolean?, @@ -3466,6 +3472,20 @@ 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( + "\$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 * (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 6f2a180e4..47191e3e2 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 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. + */ + 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 @@ -931,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 registered super properties, the - // static and dynamic 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 = @@ -959,6 +969,43 @@ public open class PostHogStateless protected constructor( "\$window_id", "\$lib", "\$lib_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", + "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 projectTokens = mutableSetOf() diff --git a/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt b/posthog/src/test/java/com/posthog/PostHogStatelessTest.kt index 27d2a416b..3d687b422 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 @@ -47,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 } @@ -101,6 +104,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 +218,8 @@ internal class PostHogStatelessTest { return details[key] } + override fun isMinimalFlagCalledEventsEnabled(): Boolean = minimalFlagCalledEvents + override fun clear() { flags.clear() payloads.clear() @@ -788,6 +794,72 @@ 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", + "\$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", + ) + + 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")) + 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"]) + } + @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 5cd21e164..5e588f124 100644 --- a/posthog/src/test/java/com/posthog/PostHogTest.kt +++ b/posthog/src/test/java/com/posthog/PostHogTest.kt @@ -968,6 +968,10 @@ internal class PostHogTest { "\$groups", "\$lib", "\$lib_version", + "\$os_name", + "\$os_version", + "\$device_type", + "\$app_version", ) assertEquals(expectedKeys, theEvent.properties!!.keys) assertFalse(theEvent.properties!!.containsKey("\$recording_status")) @@ -984,6 +988,202 @@ 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 properties = firstBatchEvent(http).properties!! + attributionKeys.forEach { assertEquals("value-$it", properties[it], "missing $it") } + assertFalse(properties.containsKey("\$referrer")) + + 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", + "\$host" to "app.example.com", + "\$cookieless_extra" to "extra", + "\$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"]) + assertEquals("app.example.com", properties["\$host"]) + assertEquals("extra", properties["\$cookieless_extra"]) + assertFalse(properties.containsKey("\$user_agent")) + assertFalse(properties.containsKey("\$locale")) + + 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")