-
Notifications
You must be signed in to change notification settings - Fork 36
feat: add device bucketing support #481
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
055337f
feat: add device bucketing support for stable feature flag assignment
dmarticus 9f9baa9
fix: format PostHogFeatureFlags.kt to pass spotless check
dmarticus 4c4952f
chore: update API declarations for device bucketing
dmarticus 9ec4d78
refactor: address review feedback on device bucketing
dmarticus 6dea9ee
chore: add changeset for device bucketing
dmarticus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "posthog": minor | ||
| --- | ||
|
|
||
| feat: add device bucketing support for stable feature flag assignment across identity changes | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import com.posthog.internal.PostHogOnRemoteConfigLoaded | |
| import com.posthog.internal.PostHogPreferences.Companion.ALL_INTERNAL_KEYS | ||
| import com.posthog.internal.PostHogPreferences.Companion.ANONYMOUS_ID | ||
| import com.posthog.internal.PostHogPreferences.Companion.BUILD | ||
| import com.posthog.internal.PostHogPreferences.Companion.DEVICE_ID | ||
| import com.posthog.internal.PostHogPreferences.Companion.DISTINCT_ID | ||
| import com.posthog.internal.PostHogPreferences.Companion.GROUPS | ||
| import com.posthog.internal.PostHogPreferences.Companion.IS_IDENTIFIED | ||
|
|
@@ -53,6 +54,7 @@ public class PostHog private constructor( | |
| private val reloadFeatureFlags: Boolean = true, | ||
| ) : PostHogInterface, PostHogStateless() { | ||
| private val anonymousLock = Any() | ||
| private val deviceIdLock = Any() | ||
| private val identifiedLock = Any() | ||
| private val groupsLock = Any() | ||
| private val personProcessingLock: Any = Any() | ||
|
|
@@ -175,6 +177,11 @@ public class PostHog private constructor( | |
|
|
||
| legacyPreferences(config, config.serializer) | ||
|
|
||
| // Initialize device_id if not already set. getDeviceId() handles lazy init | ||
| // by seeding from the anonymous ID, providing a stable identifier for | ||
| // device-level feature flag bucketing that survives identify() and reset(). | ||
| getDeviceId() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmarticus does this have any effect since the sdk is still not enabled? This should be called after enabled=true |
||
|
|
||
| super.enabled = true | ||
|
|
||
| queue.start() | ||
|
|
@@ -1224,9 +1231,10 @@ public class PostHog private constructor( | |
| return | ||
| } | ||
|
|
||
| // only remove properties, preserve BUILD and VERSION keys in order to fix over-sending | ||
| // of 'Application Installed' events and under-sending of 'Application Updated' events | ||
| val except = mutableListOf(VERSION, BUILD) | ||
| // Preserve BUILD and VERSION to prevent over-sending "Application Installed" events | ||
| // and under-sending "Application Updated" events. Preserve DEVICE_ID to maintain | ||
| // stable feature flag bucketing across identity changes. | ||
| val except = mutableListOf(VERSION, BUILD, DEVICE_ID) | ||
| // preserve the ANONYMOUS_ID if reuseAnonymousId is enabled (for preserving a guest user | ||
| // account on the device) | ||
| if (config?.reuseAnonymousId == true) { | ||
|
|
@@ -1283,6 +1291,25 @@ public class PostHog private constructor( | |
| return distinctId | ||
| } | ||
|
|
||
| override fun getDeviceId(): String { | ||
| if (!isEnabled()) { | ||
| return "" | ||
| } | ||
| synchronized(deviceIdLock) { | ||
| val deviceId = getPreferences().getValue(DEVICE_ID) as? String | ||
| if (deviceId.isNullOrBlank()) { | ||
| // Lazy init for upgrades: existing installs won't have a device_id yet | ||
| val anonId = anonymousId | ||
| if (anonId.isNotBlank()) { | ||
| getPreferences().setValue(DEVICE_ID, anonId) | ||
| return anonId | ||
| } | ||
| return "" | ||
| } | ||
| return deviceId | ||
| } | ||
| } | ||
|
|
||
| override fun startSession() { | ||
| if (!isEnabled()) { | ||
| return | ||
|
|
@@ -1627,6 +1654,8 @@ public class PostHog private constructor( | |
|
|
||
| override fun distinctId(): String = shared.distinctId() | ||
|
|
||
| override fun getDeviceId(): String = shared.getDeviceId() | ||
|
|
||
| override fun debug(enable: Boolean) { | ||
| shared.debug(enable) | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dmarticus you also need to release the android sdk here otherwise it wont have any effect