diff --git a/posthog/src/main/java/com/posthog/PostHog.kt b/posthog/src/main/java/com/posthog/PostHog.kt index 5c19ebdac..ae287057e 100644 --- a/posthog/src/main/java/com/posthog/PostHog.kt +++ b/posthog/src/main/java/com/posthog/PostHog.kt @@ -1293,12 +1293,15 @@ public class PostHog private constructor( * the wrapper's hook can rename the event, and a build changes no SDK state. * [deferredReplayDebug] holds the replay debug values from the build, sent only if the send * claims the window; null when the event can't carry them. + * [builtAsScreen] is true for a `$screen` build: a plain event the wrapper names `$screen` doesn't + * move the current screen. */ private class WrapperBuild( val deferredReplayDebug: Map?, val requiresPersonProcessing: Boolean, val sdkDistinctId: String, val resetGeneration: Long, + val builtAsScreen: Boolean, ) /** @@ -1503,8 +1506,9 @@ public class PostHog private constructor( return null } // The wrapper may drop the event after this, so state changes wait for the send. + val builtAsScreen = !ai && event == PostHogEventName.SCREEN.event val eventProperties = - if (!ai && event == PostHogEventName.SCREEN.event) { + if (builtAsScreen) { val screenTitle = properties?.get("\$screen_name") as? String ?: return null screenProperties(screenTitle, properties, cacheScreenName = false) ?: return null } else { @@ -1555,6 +1559,7 @@ public class PostHog private constructor( requiresPersonProcessing = requiresPersonProcessing(userProperties, userPropertiesSetOnce, groups), sdkDistinctId = sdkDistinctId, resetGeneration = buildResetGeneration, + builtAsScreen = builtAsScreen, ) synchronized(pendingWrapperBuilds) { pendingWrapperBuilds[uuid] = build @@ -1628,7 +1633,7 @@ public class PostHog private constructor( ) } // A reset() since the build cleared the screen name, so the old one isn't restored. - if (!ai && build.resetGeneration == resetGeneration && event.event == PostHogEventName.SCREEN.event) { + if (!ai && build.builtAsScreen && build.resetGeneration == resetGeneration && event.event == PostHogEventName.SCREEN.event) { (properties["\$screen_name"] as? String)?.trim()?.takeIf { it.isNotEmpty() }?.let { cacheScreenName(it) } } // The build added only the required replay debug keys. The window is claimed here, so a diff --git a/posthog/src/main/java/com/posthog/PostHogInterface.kt b/posthog/src/main/java/com/posthog/PostHogInterface.kt index 92b40577e..d0aa7bf4d 100644 --- a/posthog/src/main/java/com/posthog/PostHogInterface.kt +++ b/posthog/src/main/java/com/posthog/PostHogInterface.kt @@ -657,7 +657,7 @@ public interface PostHogInterface : PostHogCoreInterface { * the event is sent, as for [capture]. * Surveys and session replay event triggers see [event] as passed in, after the wrapper's hook. * Like [capture], it updates the person properties used for flags from `$set` and `$set_once`, - * caches a `$screen` event's `$screen_name` as the current screen, turns person processing on + * caches the `$screen_name` of an event built and sent as `$screen` as the current screen, turns person processing on * when the build had user properties or groups, and may add the replay debug values snapshotted * at build that the event lacks. Keys the wrapper's hook removed aren't added back. * diff --git a/posthog/src/test/java/com/posthog/PostHogWrapperEventTest.kt b/posthog/src/test/java/com/posthog/PostHogWrapperEventTest.kt index 2ec40faf0..7f82b1afd 100644 --- a/posthog/src/test/java/com/posthog/PostHogWrapperEventTest.kt +++ b/posthog/src/test/java/com/posthog/PostHogWrapperEventTest.kt @@ -249,6 +249,17 @@ internal class PostHogWrapperEventTest { assertEquals(mapOf("plan" to "pro"), prefs.getValue(PERSON_PROPERTIES_FOR_FLAGS)) } + @Test + fun `an event built under another name and sent as a screen doesn't cache its name`() { + val sut = getSut(httpServers.mockHttp()) + val built = assertNotNull(sut.buildEventForWrapper("\$captured_screen", properties = mapOf("\$screen_name" to "Home"))) + + assertNotNull(sut.sendBuiltEventForWrapper(built.copy(event = "\$screen"))) + val next = assertNotNull(sut.buildEventForWrapper("tap")) + + assertNull(next.properties?.get("\$screen_name")) + } + @Test fun `send after a reset delivers the screen without restoring its name`() { val sut = getSut(httpServers.mockHttp())