feat: Add DeviceIdProvider for custom device ID generation#880
Merged
tylerjroach merged 5 commits intomasterfrom Feb 6, 2026
Merged
feat: Add DeviceIdProvider for custom device ID generation#880tylerjroach merged 5 commits intomasterfrom
tylerjroach merged 5 commits intomasterfrom
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.
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
Collaborator
Author
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
approved these changes
Jan 13, 2026
Contributor
rahul-mixpanel
left a comment
There was a problem hiding this comment.
LGTM 👍 Clean solution. Love it!
tylerjroach
approved these changes
Jan 13, 2026
Resolve merge conflict in MixpanelOptions.java by keeping both deviceIdProvider (from this branch) and serverURL/proxyServerInteractor (from master).
tylerjroach
approved these changes
Feb 6, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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.