-
Notifications
You must be signed in to change notification settings - Fork 370
feat: Add DeviceIdProvider for custom device ID generation #880
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
base: master
Are you sure you want to change the base?
Conversation
Allows customers to supply their own device ID generation logic via the DeviceIdProvider interface: - Control where device IDs are stored (e.g., server-side, Keychain) - Control whether device IDs persist across reset()/optOutTracking() - Provider returning same value = persistent ID; different value = ephemeral ID Identity continuity protection: when adding a provider to an existing app, persisted IDs take precedence with a warning log if values differ. Handles null/empty returns and exceptions gracefully by falling back to UUID generation.
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.
Pull request overview
This PR adds a DeviceIdProvider interface to enable custom device ID generation in the Mixpanel Android SDK, porting functionality from the Swift SDK. This allows developers to control device ID generation instead of relying on the SDK's default UUID-based approach.
Changes:
- Introduces
DeviceIdProviderfunctional interface for custom device ID generation with comprehensive documentation - Adds
deviceIdProvider()configuration method toMixpanelOptions.Builder - Integrates provider into
PersistentIdentitywith defensive error handling, fallback logic, and identity continuity protection - Updates all existing test classes to accommodate the new 5-parameter
getPersistentIdentity()signature - Adds 15 comprehensive instrumented tests covering basic functionality, reset behavior, opt-out, migration, persistence, and edge cases
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
DeviceIdProvider.java |
New functional interface with detailed documentation on usage patterns and threading considerations |
MixpanelOptions.java |
Adds deviceIdProvider field and builder method with comprehensive documentation |
PersistentIdentity.java |
Implements device ID generation logic with provider support, error handling, and identity continuity checks |
MixpanelAPI.java |
Updates getPersistentIdentity() method signatures to pass provider through initialization chain |
TestUtils.java |
Updates CleanMixpanelAPI to support new constructor signature |
PersistentIdentityTest.java |
Passes null for new deviceIdProvider parameter |
OptOutTest.java |
Updates all getPersistentIdentity() overrides to include new parameter |
MixpanelBasicTest.java |
Updates test implementation overrides for new signature |
AutomaticEventsTest.java |
Updates getPersistentIdentity() override |
MixpanelDeviceIdProviderTest.java |
New comprehensive test suite with 15 tests covering all scenarios |
src/main/java/com/mixpanel/android/mpmetrics/PersistentIdentity.java
Outdated
Show resolved
Hide resolved
- Cache provider result in readIdentities() to avoid calling provider twice - Remove eager loading for DeviceIdProvider (use lazy loading like default UUID) - Add @FunctionalInterface annotation to DeviceIdProvider interface - Add @nullable annotation to MixpanelOptions.getDeviceIdProvider() - Add @after cleanup method to MixpanelDeviceIdProviderTest - Update test to verify lazy loading behavior
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
The test was asserting provider call count before accessing identity, but due to lazy loading the provider is only called on first identity access.
rahul-mixpanel
left a comment
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.
LGTM 👍 Clean solution. Love it!
Summary
Ports the
deviceIdProviderfeature from the Swift SDK (PR #692) to Android, allowing customers to supply their own device ID generation logic.Key Behaviors
API
Test Plan
MixpanelDeviceIdProviderTest.javaDocumentation
See
context/device-id-provider.mdfor complete API reference and usage examples.GitHub Copilot Summary
This pull request introduces support for custom device ID generation in the Mixpanel Android SDK, enabling developers to control how device IDs are created and persisted. The main change is the addition of the
DeviceIdProviderinterface, which allows for flexible device ID strategies, including persistence across resets or ephemeral IDs that change on reset. Related updates ensure that this new provider is correctly integrated into the SDK and its tests.Custom Device ID Provider Feature:
DeviceIdProviderinterface, allowing developers to specify custom logic for device ID generation, with documentation and usage examples. (src/main/java/com/mixpanel/android/mpmetrics/DeviceIdProvider.java)context/device-id-provider.md)Test and Internal API Updates:
getPersistentIdentityto accept and pass through the newDeviceIdProviderparameter, ensuring compatibility with the new feature. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]Constructor Enhancements:
MixpanelOptions, allowing injection of a custom device ID provider. (src/androidTest/java/com/mixpanel/android/mpmetrics/TestUtils.java)These changes provide developers with full control over device identity management in Mixpanel, improving flexibility and supporting advanced privacy requirements.