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 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
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- allow anonymous uuid generation to be customizable ([#132](https://github.com/PostHog/posthog-android/pull/132))

## 3.2.2 - 2024-05-21

- chore: register to sdk console ([#131](https://github.com/PostHog/posthog-android/pull/131))
Expand Down Expand Up @@ -91,7 +93,7 @@

## 3.1.0 - 2024-01-08

- chore: Add mutations support to Session Recording ([#72](https://github.com/PostHog/posthog-android/pull/72))
- chore: Add mutations support to Session Recording ([#72](https://github.com/PostHog/posthog-android/pull/72))
- chore: Session Recording as Experimental preview
- Check out the [USAGE](https://github.com/PostHog/posthog-android/blob/main/USAGE.md#android-session-recording) guide.

Expand Down Expand Up @@ -192,15 +194,15 @@ Check out the [USAGE](https://github.com/PostHog/posthog-android/blob/main/USAGE

## 2.0.3 - 2023-01-30

- Feature flags will be sent with payloads by default for capture and screen events.
- Feature flags will be sent with payloads by default for capture and screen events.

## 2.0.2 - 2023-02-21

- Revert: Feature flags will be sent with payloads by default. Default Options will be properly applied
- Revert: Feature flags will be sent with payloads by default. Default Options will be properly applied

## 2.0.1 - 2023-01-30

- Feature flags will be sent with payloads by default. Default Options will be properly applied
- Feature flags will be sent with payloads by default. Default Options will be properly applied

## 2.0.0 - 2022-08-29

Expand Down
6 changes: 4 additions & 2 deletions posthog/api/posthog.api
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public final class com/posthog/PostHog$Companion : com/posthog/PostHogInterface
public class com/posthog/PostHogConfig {
public static final field Companion Lcom/posthog/PostHogConfig$Companion;
public static final field DEFAULT_HOST Ljava/lang/String;
public fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogEncryption;Lcom/posthog/PostHogOnFeatureFlags;ZLcom/posthog/PostHogPropertiesSanitizer;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogEncryption;Lcom/posthog/PostHogOnFeatureFlags;ZLcom/posthog/PostHogPropertiesSanitizer;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogEncryption;Lcom/posthog/PostHogOnFeatureFlags;ZLcom/posthog/PostHogPropertiesSanitizer;Lkotlin/jvm/functions/Function1;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogEncryption;Lcom/posthog/PostHogOnFeatureFlags;ZLcom/posthog/PostHogPropertiesSanitizer;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun addIntegration (Lcom/posthog/PostHogIntegration;)V
public final fun getApiKey ()Ljava/lang/String;
public final fun getCachePreferences ()Lcom/posthog/internal/PostHogPreferences;
Expand All @@ -71,6 +71,7 @@ public class com/posthog/PostHogConfig {
public final fun getEncryption ()Lcom/posthog/PostHogEncryption;
public final fun getFlushAt ()I
public final fun getFlushIntervalSeconds ()I
public final fun getGetAnonymousId ()Lkotlin/jvm/functions/Function1;
public final fun getHost ()Ljava/lang/String;
public final fun getIntegrations ()Ljava/util/List;
public final fun getLegacyStoragePrefix ()Ljava/lang/String;
Expand Down Expand Up @@ -98,6 +99,7 @@ public class com/posthog/PostHogConfig {
public final fun setEncryption (Lcom/posthog/PostHogEncryption;)V
public final fun setFlushAt (I)V
public final fun setFlushIntervalSeconds (I)V
public final fun setGetAnonymousId (Lkotlin/jvm/functions/Function1;)V
public final fun setLegacyStoragePrefix (Ljava/lang/String;)V
public final fun setLogger (Lcom/posthog/internal/PostHogLogger;)V
public final fun setMaxBatchSize (I)V
Expand Down
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 getAnonymousId method is available, pass-through the value for modification
config?.getAnonymousId?.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 id (which as of now is just random UUID v4)
*/
public var getAnonymousId: ((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
15 changes: 15 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,19 @@ internal class PostHogTest {
sut.close()
}

@Test
fun `allows for modification of the uuid generation mechanism`() {
val expected = UUID.randomUUID()
val config =
PostHogConfig(API_KEY, getAnonymousId = {
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
Loading