Skip to content

fix: reset JavaScript-mode feature flags when identify changes the distinct id - #477

Open
tylerjroach wants to merge 1 commit into
masterfrom
fix/flags-reset-on-identify
Open

tylerjroach wants to merge 1 commit into
masterfrom
fix/flags-reset-on-identify

Conversation

@tylerjroach

Copy link
Copy Markdown
Contributor

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 kept activatedFirstTimeEvents, 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 flags reset() when the distinct id changes, as reset() 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 sets fetchPromise to the refetch right away, so getVariant() called right after identify() 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:

  • a first-time variant activated for the previous user is dropped, and the new user's first-time event fires
  • getVariant() right after identify() returns the new user's flags
  • identify with the same user keeps the activated variant

The first two fail without the fix. The full Jest suite passes.

🤖 Generated with Claude Code

…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>
@tylerjroach
tylerjroach requested review from a team and rahul-mixpanel November 17, 2026 13:46
@greptile-apps

greptile-apps Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This change appears safe to merge.

Reviews (1) · Last reviewed commit: "fix: reset JavaScript-mode feature flags..." · Reviewed by Greptile

@rahul-mixpanel rahul-mixpanel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. updateDistinctId runs before the awaits, but the reset only happens at the end. A getVariant issued while identify is in progress (e.g. identify() not awaited in a login handler) under PERSISTENCE_UNTIL_NETWORK_SUCCESS sees areFlagsReady() === true with user A's flags, returns A's variant and tracks $experiment_started under 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.js reset(): sync getters now return fallbacks after every identify. flags = null plus clearing persistence means getVariantSync / isEnabledSync / getAllVariantsSync return fallbacks from identify until /flags returns, 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. _invalidateInFlightFetch still says it's used "by identify() flows", and the comment in fetchFlags lists identify as a direct caller; identify now goes through reset().

});

// Flag state, including first-time event activations, belongs to the previous identity
if (this._flagsJS) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 on fetchPromise / _fetchStartTime; assert the final context is C's and the flags are C's.
  • track() of the first-time event while identify() 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants