Overview
This developer guide will assist you in configuring your web platform for Feature Flags using the Mixpanel JavaScript SDK. Feature Flags allow you to control the rollout of your features, conduct A/B testing, and manage application behavior without deploying new code. For complete JavaScript SDK documentation, see the JavaScript SDK guide.Prerequisites
Before implementing Feature Flags, ensure:- You are on an Enterprise subscription plan and have the latest version of the SDK installed (minimum supported version is
v2.71.0). If not, please follow this doc to install the SDK. - You have your Project Token from your Mixpanel Project Settings
Flag Initialization
Initializing the SDK with theflags option enables making an outbound request to Mixpanel servers with the current user context.
The server will assign the user context to a variant for each feature flag according to the how they are configured in the Mixpanel UX.
The response will include an assigned variant for each flag that the user context is in a rollout group for. If a flag is not returned, it most likely signifies that the user was either not in the rollout percentage for a flag or in the configured targeting cohort.
Example Usage
Javascript
distinct_id or device_id for any of the feature flags in your project, then the call to initialize feature flags must include those keys.
For example, for a Variant Assignment Key, company_id, you would setup the SDK as follows.
Javascript
custom_properties node within context:
Flag Persistence
Minimum supported SDK version:v2.79.0
By default, the SDK is in networkOnly persistence mode, where the network call for flag assignments are made and then we wait for the network response before async variant calls complete. You can enable persistence to store assignments in IndexedDB so they are available immediately on subsequent page loads.
Configure a variantLookupPolicy in the flags init options:
networkFirst — The SDK waits for the network on every launch. If the network call fails, the persisted value is returned as a fallback. Async getter calls block until the fetch completes (or the fallback is resolved).
persistenceUntilNetworkSuccess — Persisted variants are returned immediately on page load if they exist and are valid. Once a network call for flags succeeds, it will overwrite previously loaded persisted values.
persistenceTtlMs alongside variantLookupPolicy. By default, the TTL is 24 hours.
Variant source
Every variant carries avariant_source field indicating where the value came from: "network", "persistence", or "fallback". Use get_variant to receive the full variant object including this metadata.
$experiment_started properties
When a variant is served, the $experiment_started exposure event includes:
$variant_source—"network"or"persistence"$persisted_at_in_ms— epoch ms from when the persisted variant set was first stored.$ttl_in_ms— the configured TTL in ms, if the variant was from persistence
Persisted variants are tied to the current
distinct_id. Calling identify() with a new ID or calling reset() clears persisted data and triggers a fresh fetch.Flag Reload
Following initialization, you can reload feature flag assignments in a couple of ways:- After a user logs in or out of your application and you call
identify, a feature flag reload will be triggered.
- To refresh flag variants that may have changed during the lifetime of your app, you can manually reload flags with a new context:
- Calling
reset()clears all feature flag assignments from memory and triggers a new fetch for the updateddistinct_id. Requires SDK versionv2.79.0or later.
Flag Evaluation
Lookup the assigned value for a feature flag. This action triggers tracking an exposure event,$experiment_started to your Mixpanel project.
Asynchronous Flag Variant Retrieval
Experiment Flags: Get Variant ValueSynchronous Flag Variant Retrieval
Experiment Flags: Get Variant ValueFrequently Asked Questions
What if I’m not receiving any flags on SDK initialization?
-
Check your project token:
- Ensure you’re using the correct project token from your Mixpanel project settings
-
Review flag configuration:
- Make sure your feature flag is enabled
- Check the flag’s rollout percentage
- User contexts that are not assigned to the rollout percentage will not be assigned variant values
- If you are using a targeting cohort, verify on the mixpanel ‘Users’ page that the user’s
distinct_idis a member of that cohort.
-
Review SDK parameters:
- Ensure
flags: trueorflags: { context: {...} }is included in the init options - If using a custom Variant Assignment Key, ensure it is included in the
contextobject - If using Runtime Targeting, ensure all properties used in targeting are included in the
custom_propertiesobject withincontext
- Ensure
-
Enable debug mode: Use
debug: truein initialization to see detailed logs in the browser console