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

allow anonymous uuid generation to be customizable #132

Merged
merged 7 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion posthog/src/main/java/com/posthog/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ public class PostHog private constructor(
synchronized(anonymousLock) {
anonymousId = getPreferences().getValue(ANONYMOUS_ID) as? String
if (anonymousId.isNullOrBlank()) {
anonymousId = UUID.randomUUID().toString()
var uuid = UUID.randomUUID()
// when getDeviceId method is available, pass-through the value
config?.getDeviceId?.let { uuid = it(uuid) }
anonymousId = uuid.toString()
this.anonymousId = anonymousId ?: ""
}
}
Expand Down
6 changes: 6 additions & 0 deletions posthog/src/main/java/com/posthog/PostHogConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.posthog.internal.PostHogNetworkStatus
import com.posthog.internal.PostHogPreferences
import com.posthog.internal.PostHogPrintLogger
import com.posthog.internal.PostHogSerializer
import java.util.UUID

/**
* The SDK Config
Expand Down Expand Up @@ -94,6 +95,11 @@ public open class PostHogConfig(
* The hook is called before the event is cached or sent over the wire
*/
public var propertiesSanitizer: PostHogPropertiesSanitizer? = null,
/**
* Hook that allows for modification of the default mechanism for
* generating anonymous device id (which as of now is just random UUID v4)
*/
public var getDeviceId: ((UUID) -> UUID) = { it },
) {
// fix me: https://stackoverflow.com/questions/53866865/leaking-this-in-constructor-warning-should-apply-to-final-classes-as-well-as
@PostHogInternal
Expand Down
14 changes: 14 additions & 0 deletions posthog/src/test/java/com/posthog/PostHogTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import okhttp3.mockwebserver.MockResponse
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import java.io.File
import java.util.UUID
import java.util.concurrent.Executors
import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
Expand Down Expand Up @@ -852,6 +854,18 @@ internal class PostHogTest {
sut.close()
}

@Test
fun `allows for modification of the uuid generation mechanism`() {
val expected = UUID.randomUUID()
val config = PostHogConfig(API_KEY, getDeviceId = {
assertNotEquals(it, expected, "Expect two unique UUIDs")
expected
})
// now generate an event
val sut = PostHog.with(config)
assertEquals(expected.toString(), sut.distinctId(), "It should use the injected uuid instead")
}

@Test
fun `enables debug mode`() {
val config = PostHogConfig(API_KEY)
Expand Down