Skip to content

Commit

Permalink
fix: allow changing person properties after identify (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Nov 12, 2024
1 parent bd1ec53 commit 135cf8f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
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`() {
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

0 comments on commit 135cf8f

Please sign in to comment.