Before you begin
- The JavaScript SDK is installed on your site. See Mixpanel SDKs: Javascript.
- You have a feature flag in your project and know its flag key. See Feature Flags.
- Your consent management tool gives you a callback or event when the visitor accepts and when they decline.
- Feature Flags availability depends on your plan. See pricing. For the permissions you need to create flags, see Roles and permissions.
EU data residency and consent rules
If your project uses EU data residency, setapi_host: 'https://api-eu.mixpanel.com' in every init() call on this page, including the tracking instance in Path B. The flag request uses the same host as events, so this one setting sends both to Mixpanel’s EU servers. See EU Residency and EU Data Residency in the JavaScript SDK reference.
EU residency controls where Mixpanel stores and processes your data. It doesn’t change what the SDK stores on the visitor’s device before consent. In France, the CNIL requires consent before a site reads or writes an identifier in a cookie or local storage, unless the storage is strictly necessary or falls under one of the CNIL’s exemptions. Other EU regulators apply similar rules under the ePrivacy Directive. This setup stores a distinct ID and queued events and sends a flag request before consent, so your privacy or legal team needs to decide whether it fits your consent basis. If it doesn’t, evaluate flags after the visitor accepts.
Settings that break pre-consent flags
Don’t use these settings on the instance that evaluates flags. Each one stops flags from working until the visitor accepts.Choose your setup
Pick the path that matches how you load Mixpanel on the page.Path A: one instance
In Path A, a single JavaScript SDK instance loads before the consent banner, evaluates flags, queues events in local storage, and starts sending them when the visitor accepts.Path A settings
Set up Path A
Initialize the SDK before the consent banner loads
YOUR_PROJECT_TOKEN with the token from your project settings. On the visitor’s first page load, the SDK creates a device ID and stores it. Later page loads reuse it. The flag request goes out as soon as the SDK loads.Read the flag where the visitor sees the feature
YOUR_FLAG_KEY with the flag key from the flag’s page in Mixpanel. The SDK tracks a $experiment_started exposure event and adds it to the queue in local storage. It doesn’t send it. Any other events you track before consent wait in the same queue.If you load the SDK from the CDN snippet, the library loads asynchronously, and mixpanel.flags doesn’t exist until it finishes. Read flags inside the loaded callback in your init() options so the call runs after the SDK is ready:mixpanel.flags right after init().Start sending events when the visitor accepts
hasStoredConsent with a check of your consent tool’s saved choice, and onConsentGranted with its accept callback. batch_autostart: false applies on every page load, and start_batch_senders() only lasts for the current page, so call it on every page load where consent is already stored. Once it runs, the SDK sends the queued events, including the exposure event, and sends later events as they happen. opt_in_tracking() clears the opt-out that opt_out_tracking() saved if the visitor declined earlier and now accepts. Without it, their events stay blocked. It also sends an $opt_in event.Stop storing data when the visitor declines
onConsentDeclined with your consent tool’s decline callback. opt_out_tracking() stops the queue, deletes the queued events, and stops storing the visitor’s distinct ID. If you’ve called identify() for this visitor, it also deletes their user profile. To keep the profile, call mixpanel.opt_out_tracking({delete_user: false}).Verify Path A
- Open your site in a private browser window and don’t answer the consent banner.
- In your browser’s developer tools, open the Network tab and filter for
mixpanel. You should see a request to/flags/and no request to/track/. - Open the Application tab. Under Local storage, the
__mpq_YOUR_PROJECT_TOKEN_eventry should hold a$experiment_startedevent. Under Cookies, themp_YOUR_PROJECT_TOKEN_mixpanelcookie should hold adistinct_id. - Reload the page. The
distinct_idand the variant should stay the same. - Accept the consent banner. You should see a request to
/track/that includes$experiment_started, and the queue entry should empty.
distinct_id changes on reload, or you see a /track/ request before you accept, go to Troubleshooting.
Path B: separate flag and tracking instances
In Path B, a flag instance loads before the consent banner and evaluates flags. A tracking instance loads after the visitor accepts. Both instances use the same project token and the same stored identity, so the exposure event and the visitor’s later events share a distinct ID. The SDK stores the identity undermp_ plus the persistence_name value, or under mp_YOUR_PROJECT_TOKEN_mixpanel if persistence_name is unset. Two instances share an identity only when they use the same persistence and persistence_name values.
Path B settings
Flag instance:Set up Path B
Initialize the flag instance before the consent banner loads
YOUR_PROJECT_TOKEN with the token from your project settings. The third argument names the instance, so you reach it as mixpanel.preconsent. Don’t name an instance flags, since mixpanel.flags is already the flags API.Read the flag from the flag instance
YOUR_FLAG_KEY with the flag key from the flag’s page in Mixpanel. The exposure event waits in the queue.If you load the SDK from the CDN snippet, the library loads asynchronously, and mixpanel.preconsent doesn’t exist until it finishes. Read flags inside the loaded callback of the flag instance’s init() options. The callback receives the flag instance, so use its flags property directly:mixpanel.preconsent.flags right after init().When the visitor accepts, initialize the tracking instance with the same storage settings
persistence and persistence_name values in the tag’s init options.Send the flag instance's queued events
$insert_id.opt_in_tracking() clears the opt-out that opt_out_tracking() saved if the visitor declined earlier and now accepts. The opt-out is stored per project token, so it covers both instances, and one call clears it for both. Without it, their events stay blocked. It also sends an $opt_in event. start_batch_senders() only lasts for the current page, so call it on every page load where consent is already stored, and initialize the tracking instance on those page loads too.When the visitor declines, stop the flag instance from storing data
flags: { context: { distinct_id: 'YOUR_USER_ID' } }. A distinct_id or device_id in context replaces the value the SDK would send in the flag request only. It doesn’t change the ID stored by either instance, so the exposure event and later events still carry the stored ID. To make them match, call identify('YOUR_USER_ID') on both instances, for example mixpanel.preconsent.identify('YOUR_USER_ID') and, after consent, mixpanel.tracking.identify('YOUR_USER_ID').Verify Path B
- Open your site in a private browser window and don’t answer the consent banner.
- In the Network tab, you should see a request to
/flags/and no request to/track/. - Reload the page. In the console,
mixpanel.preconsent.get_distinct_id()should return the same value as before the reload. - Accept the consent banner. In the console,
mixpanel.tracking.get_distinct_id()should return the same value asmixpanel.preconsent.get_distinct_id(). - You should see a request to
/track/that includes$experiment_started.
Troubleshooting
The distinct ID changes on every page load
opt_out_tracking_by_default is on, or the browser holds an opt-out from an earlier visit. Remove opt_out_tracking_by_default from the init options. To test again, clear the site’s data in the browser, which removes the stored __mp_opt_in_out_YOUR_PROJECT_TOKEN status.
The visitor gets no variant before consent
consent_mode: 'require_consent' is set, which blocks the flag request until opt_in_tracking(). Remove it. If it’s not set, check that flags is in the init options.
Events send before the visitor accepts
Check these causes:batch_autostartisn’tfalseon the instance that tracks the exposure. Set it tofalse.- A
track()call passes{send_immediately: true}, which skips the queue. Remove that option from events you track before consent. - The browser can’t use local storage. The SDK then turns off queueing, sends each event right away, and logs
Turning off Mixpanel request-queueingto the console. - Saving to the queue failed, for example because local storage is full. The SDK sends that event right away instead of dropping it.
get_variant_value() means no exposure event is tracked:
The flag instance and tracking instance have different distinct IDs
The instances use differentpersistence or persistence_name values, so each stores its own identity. Set the same values in both init calls, including in the tag manager tag that loads the tracking instance.
Next steps
- Implement Feature Flags (Web) for flag context, persistence, and exposure event details.
- Opt out of tracking in the JavaScript SDK reference.
- Protecting user data for other privacy controls.