Skip to content
This repository was archived by the owner on Jul 20, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pages/docs/featureflags.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ NOTE: Cohort membership is refreshed every 2 hours when targeting anything aside

## Implementation

Feature Flagging is supported on three client-side SDK's: Web, iOS, and Android and currently in "Beta" on one server-side SDK, python.
Feature Flagging is supported on three client-side SDK's: Web, iOS, and Android and currently in "Beta" on server-side SDKs: Python, Node.js, and Java.

See our developer guides on implementing feature flags on these platforms below:

Expand All @@ -198,10 +198,11 @@ See our developer guides on implementing feature flags on these platforms below:
<Cards.Card icon title="iOS" href="/docs/tracking-methods/sdks/swift/swift-flags" />
<Cards.Card icon title="Android" href="/docs/tracking-methods/sdks/android/android-flags" />
<Cards.Card icon title="Python" href="/docs/tracking-methods/sdks/python/python-flags" />
<Cards.Card icon title="Node.js" href="/docs/tracking-methods/sdks/nodejs/nodejs-flags" />
<Cards.Card icon title="Java" href="/docs/tracking-methods/sdks/java/java-flags" />
</Cards>

Coming soon: React Native, Ruby, Node.js, Go
Coming soon: React Native, Ruby, Go

<Callout type="info">
If you'd like to see Feature Flags availability in other SDKs, please [reach out to the Support team](https://mixpanel.com/get-support).
Expand Down
3 changes: 3 additions & 0 deletions pages/docs/tracking-methods/sdks/nodejs/_meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
"nodejs-flags": "Feature Flags (Node.js)",
}
122 changes: 122 additions & 0 deletions pages/docs/tracking-methods/sdks/nodejs/nodejs-flags.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { Callout } from 'nextra/components'

# Implement Feature Flags (Node.js)

<Callout type="info">
The Node.js feature flags implementation is currently in Beta.
</Callout>

## Overview

This developer guide will assist you in configuring your server-side Node.js platform for [Feature Flags](/docs/featureflags) using the [Mixpanel Node.js SDK](/docs/tracking-methods/sdks/nodejs). 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](/docs/featureflags), ensure:

- You are on an Enterprise subscription plan and have the [Mixpanel Node.js SDK](https://github.com/mixpanel/mixpanel-node) installed. If not, please follow [this doc](/docs/quickstart/install-mixpanel) to install the SDK.
- You have your Project Token from your [Mixpanel Project Settings](/docs/orgs-and-projects/managing-projects#find-your-project-tokens)

## 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

<Callout type="warning">
Targeting by Mixpanel cohorts and sticky variants are not supported in Local Evaluation mode.
</Callout>

- The SDK is configured with a `local_flags_config` object that specifies parameters:
1) `api_host` - If your project is in the EU/IN region, this should be set to route to `api-eu.mixpanel.com`/`api-in.mixpanel.com` respectively.
2) `enable_polling` - This should be set to `true` to enable polling for new flag configurations.
3) `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.

```javascript
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_config` object to use remote evaluation.

```javascript
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.

```javascript
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);
```