Skip to content
Merged
10 changes: 10 additions & 0 deletions .changeset/minimal-flag-called-session-props.md
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 20 additions & 0 deletions posthog/src/main/java/com/posthog/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
get() = MINIMAL_FEATURE_FLAG_CALLED_DEVICE_PROPERTIES

public override fun getFeatureFlag(
key: String,
sendFeatureFlagEvent: Boolean?,
Expand Down Expand Up @@ -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
Expand Down
55 changes: 51 additions & 4 deletions posthog/src/main/java/com/posthog/PostHogStateless.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
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
Expand Down Expand Up @@ -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 =
Expand All @@ -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<String>()
Expand Down
72 changes: 72 additions & 0 deletions posthog/src/test/java/com/posthog/PostHogStatelessTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,6 +48,8 @@ internal class PostHogStatelessTest {
fun isEnabledPublic(): Boolean = isEnabled()

fun setMockQueue(queue: PostHogQueueInterface<PostHogEvent>) {
// setup() started a real queue with a repeating flush timer, and teardown stops only the mock.
this.queue?.stop()
this.queue = queue
}

Expand Down Expand Up @@ -101,6 +104,7 @@ internal class PostHogStatelessTest {
}

private class MockFeatureFlags : PostHogFeatureFlagsInterface {
var minimalFlagCalledEvents = false
private val flags = mutableMapOf<String, Any>()
private val payloads = mutableMapOf<String, Any?>()
private val details = mutableMapOf<String, FeatureFlag>()
Expand Down Expand Up @@ -214,6 +218,8 @@ internal class PostHogStatelessTest {
return details[key]
}

override fun isMinimalFlagCalledEventsEnabled(): Boolean = minimalFlagCalledEvents

override fun clear() {
flags.clear()
payloads.clear()
Expand Down Expand Up @@ -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<String, Any> = emptyMap()

override fun getDynamicContext(): Map<String, Any> =
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<String, Any> = 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()
Expand Down
Loading
Loading