Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix reset session when reset or close are called #97

Merged
merged 4 commits into from
Feb 19, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- fix reset session when reset or close are called ([#97](https://github.com/PostHog/posthog-android/pull/97))

## 3.1.7 - 2024-02-14

- recording: fix missing windowAttachCount method after minification ([#96](https://github.com/PostHog/posthog-android/pull/96))
Expand Down
3 changes: 2 additions & 1 deletion posthog/src/main/java/com/posthog/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public class PostHog private constructor(

featureFlagsCalled.clear()

sessionId = sessionIdNone
endSession()
} catch (e: Throwable) {
config?.logger?.log("Close failed: $e.")
}
Expand Down Expand Up @@ -542,6 +542,7 @@ public class PostHog private constructor(
queue?.clear()
replayQueue?.clear()
featureFlagsCalled.clear()
endSession()
}

private fun isEnabled(): Boolean {
Expand Down
52 changes: 52 additions & 0 deletions posthog/src/test/java/com/posthog/PostHogTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -850,4 +850,56 @@ internal class PostHogTest {

sut.close()
}

@Test
fun `does not set session id when reset is called`() {
val http = mockHttp()
val url = http.url("/")

val sut = getSut(url.toString(), preloadFeatureFlags = false)

sut.capture(
event,
distinctId,
props,
userProperties = userProps,
userPropertiesSetOnce = userPropsOnce,
groupProperties = groupProps,
)

queueExecutor.awaitExecution()

var request = http.takeRequest()

assertEquals(1, http.requestCount)
var content = request.body.unGzip()
var batch = serializer.deserialize<PostHogBatchEvent>(content.reader())

var theEvent = batch.batch.first()
assertNotNull(theEvent.properties!!["\$session_id"])

sut.reset()

sut.capture(
event,
distinctId,
props,
userProperties = userProps,
userPropertiesSetOnce = userPropsOnce,
groupProperties = groupProps,
)

queueExecutor.shutdownAndAwaitTermination()

request = http.takeRequest()

assertEquals(2, http.requestCount)
content = request.body.unGzip()
batch = serializer.deserialize<PostHogBatchEvent>(content.reader())

theEvent = batch.batch.first()
assertNull(theEvent.properties!!["\$session_id"])

sut.close()
}
}