Repository navigation
feat(analytics): generate random IDs in the SDK and drop react-native-get-random-values - #472
Conversation
[Medium impact] The PR appears safe to merge; the remaining sample setup documentation issue is non-blocking. Findings
|
| ## Run | ||
|
|
||
| ```sh | ||
| npm install |
There was a problem hiding this comment.
Fresh-clone setup misses dependencies
These instructions install only the sample's dependencies. mixpanel-react-native links to the repository root, whose dependencies are not installed by this command. On a fresh clone, Metro therefore cannot resolve imports such as react-native-get-random-values. Add a root npm install step before the sample install, as CI already does.
There was a problem hiding this comment.
removed the react-native-get-random-values library and implemented the feature in the SDK.
There was a problem hiding this comment.
You're right—the react-native-get-random-values reference in my comment is outdated. The sample's npm install also installs the local SDK's declared runtime dependencies, so a separate root install isn't needed for those imports. Please disregard my comment.
Tip: You can customize Greptile's behavior for this repo with .greptile/rules.md and .greptile/config.json.
A crypto polyfill without its native module, such as react-native-get-random-values imported by another dependency, throws from getRandomValues. randomUUID then rejected instead of trying the SDK's native module, so JavaScript-mode init failed for a new device ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rahul-mixpanel
left a comment
There was a problem hiding this comment.
Nice cleanup. The source order in mixpanel-random.js makes sense, existing device IDs load unchanged, and e5b258a handles a polyfill that throws. Plain React Native CLI apps in JavaScript mode also get secure device IDs now instead of Math.random.
The main test workflow doesn't run on this PR because it only triggers for PRs into master, so I ran Jest locally on e5b258a: 528 passed, 4 skipped.
A few things before merge:
- Release notes reminder: when the CHANGELOG for this release is generated, please add the breaking change by hand. Importing the SDK no longer installs
crypto.getRandomValuesfor the whole app, so apps that relied on that needreact-native-get-random-valuesand an import at the top of their entry file. The migration steps in this PR's description could go in as they are. - Expo + SwiftPM: this PR exists to fix that build, but it's listed as not yet tested. Could you run one Expo + SwiftPM build (e.g.
Samples/MixpanelExpowith"experiments": { "swiftPackageManager": true }) before merging, to confirm thePackage.swift is missingerror is gone?
Nits (optional):
Samples/MixpanelExpo/ios/Podfile.lockstill listsreact-native-get-random-valuesfrommixpanel-react-native/node_modules. Runningpod installwould refresh it.Samples/MixpanelStarterstill installsreact-native-get-random-valuesand has a Metro alias for it. That's harmless, but it means the sample never exercises the new fallback path.
TestingSetup: Expo SDK 58 (
The Nits are addressed in the c6e2e8b commit |
rahul-mixpanel
left a comment
There was a problem hiding this comment.
Thanks for the thorough testing, Ketan. Expo + SwiftPM on iOS, Android, and web in both modes covers it, and the sample cleanup (including the Starter metro.config.js fix) looks good. LGTM.
Reminder for the release: please add the breaking change to the generated CHANGELOG by hand. Importing the SDK no longer installs crypto.getRandomValues for the whole app, so apps that relied on it need react-native-get-random-values and an import at the top of their entry file.
Summary
Removes
react-native-get-random-valuesanduuid. The SDK now generates random IDs itself injavascript/mixpanel-random.js, using the same order of sources the library used, with a newrandomUUIDmethod on our native module as the secure source for device IDs when JavaScript has none.Linear: SDK-456 (December CocoaPods removal / next major)
Why
"experiments": { "swiftPackageManager": true }linkreact-native-get-random-values1.11.0, which has noPackage.swift:error: Package.swift is missing for library "react-native-get-random-values"Package.swift, but it builds on 2.0.0, which is New-Architecture-only and needs React Native ≥ 0.81. On React Native 0.79, npm silently installs a second React Native 0.88 under our package instead of failing.Math.randomfor device IDs.Changes
javascript/mixpanel-random.js(new):randomUUID()(async) tries, in order:globalThis.crypto.getRandomValues);global.expo.modules.ExpoCrypto);MixpanelReactNative.randomUUID();Math.random, with a one-timeconsole.warn.randomHex(n)(sync) is a best-effort source for trace IDs. It uses the same JavaScript sources, thenMath.random.randomUUIDon iOS (UUID(), inMixpanelReactNative.swiftand.m) and on Android (UUID.randomUUID()), both backed by the system's secure random generator.mixpanel-persistent.js: the device ID usesrandomUUID(). Existing stored device IDs are loaded unchanged.mixpanel-flags-js.js:traceparentusesrandomHex, so the header is now always sent. Previously it was skipped whenuuidhad no crypto source.package.json: removesreact-native-get-random-valuesanduuid, and drops them from Jest'stransformIgnorePatterns.__tests__/random.test.jscovers each source and the one-time warning.uuidand polyfill mocks are removed..github/copilot-instructions.md: dependency list updated.package-lock.json: besides removing the packages, npm 11 added"dev": truemarkers and corrected the stale root version (3.8.1 → 4.0.0).Breaking change
Importing the SDK no longer sets
global.crypto.getRandomValuesfor the whole app. Apps that useuuidor other crypto libraries and relied on our import must add the polyfill themselves:Test setups that call
jest.mock('react-native-get-random-values')without the package installed will also fail with "Cannot find module".Unchanged:
randomUUIDfalls back without crashing.Testing
npm test: 526 passed, 4 skipped (skipped before this change).isRecording=true. JavaScript mode got its device ID from the nativerandomUUID, since Hermes has no Web Crypto.Podfile.lockno longer containsreact-native-get-random-values.console.warn. The ID logic is unchanged.Not yet tested:
expo-crypto).