Implement Feature Flags (Node.js)
The Node.js feature flags implementation is currently in Beta.
Overview
This developer guide will assist you in configuring your server-side Node.js platform for Feature Flags using the Mixpanel Node.js SDK. Feature Flags allow you to control the rollout of your features, conduct A/B testing, and manage application behavior without deploying new code.
Prerequisites
Before implementing Feature Flags, ensure:
- You are on an Enterprise subscription plan and have the Mixpanel Node.js SDK installed. If not, please follow this doc to install the SDK. The minimum supported version is v0.19.0
- You have your Project Token from your Mixpanel Project Settings
Flag Evaluation Scenarios
There are two scenarios available for using the Node.js SDK for feature flagging: Local Evaluation and Remote Evaluation.
For local evaluation, the SDK will poll Mixpanel servers for feature flag configurations. Assignment of user contexts to variants will be done locally within the SDK. This mode is recommended for low latency since there is no network call made at assignment time.
For remote evaluation, the SDK will make a network call to Mixpanel servers at assignment time. This mode is recommended for use cases where you want to leverage Mixpanel cohorts for user targeting or sticky variants for persistent variant assignments.
In either case there is also the capability to evaluate all flags for a given user context at once, to avoid needing to make multiple calls to get individual flag variants for the same user. This is particularly useful for remote evaluation to avoid incurring additional network calls.
Local Evaluation
Targeting by Mixpanel cohorts and sticky variants are not supported in Local Evaluation mode.
-
The SDK is configured with a
local_flags_configobject that specifies parameters:api_host- If your project is in the EU/IN region, this should be set to route toapi-eu.mixpanel.com/api-in.mixpanel.comrespectively.enable_polling- This should be set totrueto enable polling for new flag configurations.polling_interval_in_seconds- This is the interval in seconds at which the SDK will poll Mixpanel servers for feature flag configurations.
-
The SDK will continue to poll for the lifetime of the SDK instance or until stopped.
const Mixpanel = require('mixpanel');
const mixpanel = Mixpanel.init('YOUR_PROJECT_TOKEN', {
local_flags_config: {
api_host: 'api.mixpanel.com',
enable_polling: true,
polling_interval_in_seconds: 60
}
});
// If enable_polling is set to false, this will fetch definitions only once for the lifetime of the SDK.
await mixpanel.local_flags.startPollingForDefinitions();
// This should be the 'key' of the feature flag from Mixpanel's UX.
const flagKey = 'sample-flag';
// This is the fallback variant to return if the user context is not in a rollout group for the flag.
const fallbackValue = 'control';
// Current user context for evaluation.
// At minimum, this needs to include the user's distinct_id.
// If any of your feature flags use a Variant Assignment Key other than 'distinct_id', this should also include those keys for evaluation. For example, 'company_id' below
// If any of your feature flags use Runtime targeting, this should also include 'custom_properties' for evaluation
const userContext = {
distinct_id: '1234',
company_id: 'X',
custom_properties: {
platform: 'node'
}
};
// Gets the assigned variant for the flag for the given user context.
// This will return the fallback_variant if the user context is not in an assignment group for the flag.
const variantValue = mixpanel.local_flags.getVariantValue(flagKey, fallbackValue, userContext);Remote Evaluation
- The SDK is configured with a
remote_flags_configobject to use remote evaluation.
const Mixpanel = require('mixpanel');
const mixpanel = Mixpanel.init('YOUR_PROJECT_TOKEN', {
remote_flags_config: {
api_host: 'api.mixpanel.com',
request_timeout_in_seconds: 5
}
});
// getVariantValue usage is the same as for local evaluation, but will make a network call to Mixpanel servers at assignment time.
const variantValue = await mixpanel.remote_flags.getVariantValue(flagKey, fallbackValue, userContext);Evaluate all flags at once
Below is a remote evaluation sample of evaluating all flags for a given user context at once.
const Mixpanel = require('mixpanel');
const userContext = {
distinct_id: '1234',
};
const mixpanel = Mixpanel.init('YOUR_PROJECT_TOKEN', {
remote_flags_config: {
api_host: 'api.mixpanel.com',
request_timeout_in_seconds: 5
}
});
// Returns a dictionary, mapping flag keys to assigned variants ONLY for flags that the user context is in an assignment group for.
// By default, this will not track an exposure event.
const variants = await mixpanel.remote_flags.getAllVariants(userContext);
// Given a flag key and the selected variant for that key, manually track an exposure event for a given flag and assigned variant, after exposing the user to the variant
mixpanel.remote_flags.trackExposureEvent(flagKey, selectedVariant, userContext);Was this page useful?