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: allow changing person properties after identify #205

Merged
merged 6 commits into from
Nov 12, 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: allow changing person properties after identify ([#205](https://github.com/PostHog/posthog-android/pull/205))

## 3.9.1 - 2024-11-11

- recording: fix observation on multiple threads in layout/draw is not supported for compose ([#204](https://github.com/PostHog/posthog-android/pull/204))
Expand Down
13 changes: 12 additions & 1 deletion posthog/src/main/java/com/posthog/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,8 @@ public class PostHog private constructor(
config?.logger?.log("identify called with invalid anonymousId: $anonymousId.")
}

if (previousDistinctId != distinctId && !isIdentified) {
val hasDifferentDistinctId = previousDistinctId != distinctId
if (hasDifferentDistinctId && !isIdentified) {
// this has to be set before capture since this flag will be read during the event
// capture
synchronized(identifiedLock) {
Expand All @@ -583,6 +584,16 @@ public class PostHog private constructor(
if (reloadFeatureFlags) {
reloadFeatureFlags()
}
// we need to make sure the user props update is for the same user
// otherwise they have to reset and identify again
} else if (!hasDifferentDistinctId && (userProperties?.isNotEmpty() == true || userPropertiesSetOnce?.isNotEmpty() == true)) {
capture(
"\$set",
distinctId = distinctId,
userProperties = userProperties,
userPropertiesSetOnce = userPropertiesSetOnce,
)
// Note we don't reload flags on property changes as these get processed async
} else {
config?.logger?.log("already identified with id: $distinctId.")
}
Expand Down
77 changes: 76 additions & 1 deletion posthog/src/test/java/com/posthog/PostHogTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -574,13 +574,88 @@ internal class PostHogTest {

sut.identify(
"anotherDistinctId",
)

queueExecutor.shutdownAndAwaitTermination()

assertEquals(1, http.requestCount)

sut.close()
}

@Test
fun `captures a set event if identified`() {
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
val http = mockHttp()
val url = http.url("/")

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

sut.identify(
DISTINCT_ID,
userProperties = userProps,
userPropertiesSetOnce = userPropsOnce,
)

val userProps = mapOf("user1" to "theResult")
val userPropsOnce = mapOf("logged" to false)

sut.identify(
DISTINCT_ID,
userProperties = userProps,
userPropertiesSetOnce = userPropsOnce,
)

queueExecutor.shutdownAndAwaitTermination()

assertEquals(1, http.requestCount)
val request = http.takeRequest()

val content = request.body.unGzip()
val batch = serializer.deserialize<PostHogBatchEvent>(content.reader())

val theEvent = batch.batch.last()

assertEquals("\$set", theEvent.event)
assertEquals(userProps, theEvent.properties!!["\$set"])
assertEquals(userPropsOnce, theEvent.properties!!["\$set_once"])

sut.close()
}

@Test
fun `does not capture a set event if different user`() {
val http = mockHttp()
val url = http.url("/")

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

sut.identify(
DISTINCT_ID,
userProperties = userProps,
userPropertiesSetOnce = userPropsOnce,
)

val userProps = mapOf("user1" to "theResult")
val userPropsOnce = mapOf("logged" to false)

sut.identify(
"different user",
userProperties = userProps,
userPropertiesSetOnce = userPropsOnce,
)

queueExecutor.shutdownAndAwaitTermination()

queueExecutor.shutdownAndAwaitTermination()

val request = http.takeRequest()

val content = request.body.unGzip()
val batch = serializer.deserialize<PostHogBatchEvent>(content.reader())

val theEvent = batch.batch.last()

assertEquals(1, batch.batch.size)
assertEquals("\$identify", theEvent.event)

sut.close()
}
Expand Down
Loading