Repository navigation
fix: reset JavaScript-mode feature flags when identify changes the distinct id - #477
tylerjroach wants to merge 1 commit into
Conversation
…stinct id identify only reloaded flags, so a first-time event variant activated for the previous user stayed active for the new one. Reset the flags instead, as reset() does, and expose the refetch to flag readers right away. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
[Medium impact] This change appears safe to merge. Reviews (1) · Last reviewed commit: "fix: reset JavaScript-mode feature flags..." · Reviewed by Greptile |
rahul-mixpanel
left a comment
There was a problem hiding this comment.
Review notes. The fix is right for the sequential case the tests cover; most of the comments below are about the window between the distinct id switch and the flag reset.
Not anchored inline (outside the diff hunks):
javascript/mixpanel-main.js~L222: readers during identify still see the previous user's flags.updateDistinctIdruns before the awaits, but the reset only happens at the end. AgetVariantissued while identify is in progress (e.g.identify()not awaited in a login handler) underPERSISTENCE_UNTIL_NETWORK_SUCCESSseesareFlagsReady() === truewith user A's flags, returns A's variant and tracks$experiment_startedunder B's distinct id. The "getVariant right after identify waits for the new user" guarantee only holds when the caller awaits identify.javascript/mixpanel-flags-js.jsreset(): sync getters now return fallbacks after every identify.flags = nullplus clearing persistence meansgetVariantSync/isEnabledSync/getAllVariantsSyncreturn fallbacks from identify until/flagsreturns, and stay on fallback offline (no cached state left). Probably intended, but worth a changelog note since it's a visible behavior change for sync-API users.javascript/mixpanel-flags-js.js~L594: stale docstring._invalidateInFlightFetchstill says it's used "by identify() flows", and the comment infetchFlagslists identify as a direct caller; identify now goes throughreset().
| }); | ||
|
|
||
| // Flag state, including first-time event activations, belongs to the previous identity | ||
| if (this._flagsJS) { |
There was a problem hiding this comment.
The reset runs only after persistIdentity, identifyUserQueue and the $identify track have been awaited, while the distinct id is already the new user's. In that window the previous user's pendingFirstTimeEvents are still live.
Scenario: user A has a pending first-time event on Signed Up. App calls identify('user_b') and, without awaiting, track('Signed Up') (or $identify itself matches a first-time event). checkFirstTimeEvents activates A's pending variant, and recordFirstTimeEvent POSTs with distinct_id: user_b because it reads the current distinct id, so B gets credited server-side with A's activation. The later reset() wipes the local activation, so local state and server disagree.
Suggest clearing the flag state (the synchronous part of reset()) right after updateDistinctId, before the first await, and kicking off the refetch there or at the end.
| if (this._flagsJS) { | ||
| this._flagsJS._invalidateInFlightFetch(); | ||
| this._flagsJS.loadFlags().catch(() => { | ||
| this._flagsJS.reset().catch(() => { |
There was a problem hiding this comment.
Nit: MixpanelFlagsJS.reset() catches and logs everything internally, so this .catch can never fire and the "Failed to load flags for distinct Id" message will never show up. The real log is "Error during flags reset". Could drop the .catch or have reset() rethrow.
| $device_id: deviceId, | ||
| }); | ||
|
|
||
| // Flag state, including first-time event activations, belongs to the previous identity |
There was a problem hiding this comment.
Small consistency point: instance reset() wires the flags reset at the facade level (index.js ~L686, this._flags.reset()), while identify now does it inside MixpanelMain via the _flagsJS back-reference. Two identity-change paths reset flags through different layers; a single identity-changed hook in one place would keep them from drifting.
| this._loadedTtlMs = null; | ||
| this._invalidateInFlightFetch(); | ||
| // Readers wait on fetchPromise, so expose the refetch right away instead of after the clear | ||
| const resetPromise = this.persistence.clear().then(() => this.loadFlags()); |
There was a problem hiding this comment.
fetchPromise is set to resetPromise while _fetchStartTime is null. If the chain rejects before fetchFlags() reassigns fetchPromise (clear() swallows errors today, but e.g. a synchronous throw building the request from a non-serializable custom context), the rejected resetPromise stays as fetchPromise and every later getVariant returns the BACKEND_ERROR fallback until something else calls loadFlags(). Might be worth resetting fetchPromise to null in the catch if it's still resetPromise.
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("waits for the new user's flags when read right after identify", async () => { |
There was a problem hiding this comment.
Tests cover fully awaited, sequential identifies. Two cases that would be good to add:
- back-to-back identifies A→B→C without awaiting, so two
reset()calls overlap onfetchPromise/_fetchStartTime; assert the final context is C's and the flags are C's. track()of the first-time event whileidentify()is still in progress; assert the previous user's pending variant is not activated and no first-time-event POST is sent under the new distinct id.
Summary
In JavaScript mode (
useNative: false, or when the native module is unavailable, e.g. Expo Go or React Native for Web),identify()with a new distinct id only reloaded feature flags. It keptactivatedFirstTimeEvents, the pending first-time events and the stored flags from the previous user. A first-time event variant activated for user A therefore stayed active for user B, and B's own first-time event never fired.identify()now calls the flagsreset()when the distinct id changes, asreset()on the instance already does. It clears that state and the stored flags, then refetches. Repeating the same distinct id doesn't reset.reset()now setsfetchPromiseto the refetch right away, sogetVariant()called right afteridentify()waits for the new user's flags instead of reading during the storage clear.Native mode is unchanged: there the native Android and iOS SDKs already reset flags on an identity change.
Testing
New
__tests__/flags-identify.test.js:getVariant()right afteridentify()returns the new user's flagsThe first two fail without the fix. The full Jest suite passes.
🤖 Generated with Claude Code