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
9 changes: 7 additions & 2 deletions posthog/src/main/java/com/posthog/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any>?,
val requiresPersonProcessing: Boolean,
val sdkDistinctId: String,
val resetGeneration: Long,
val builtAsScreen: Boolean,
)

/**
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion posthog/src/main/java/com/posthog/PostHogInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
11 changes: 11 additions & 0 deletions posthog/src/test/java/com/posthog/PostHogWrapperEventTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Loading