This page describes how to instrument your browser JavaScript application with the Datadog Feature Flags SDK. Datadog feature flags provide a unified way to remotely control feature availability in your app, experiment safely, and deliver new experiences with confidence.
The Datadog Feature Flags SDK for JavaScript is built on OpenFeature, an open standard for feature flag management. This guide explains how to install the SDK, configure the Datadog provider, and evaluate flags in your application.
Installation
Install the Datadog OpenFeature provider and the OpenFeature Web SDK using your preferred package manager:
Define who or what the flag evaluation applies to using an evaluation context. The evaluation context includes user or session information used to determine which flag variations should be returned. Reference these attributes in your targeting rules to control who sees each variant.
The targetingKey is used as the randomization subject for percentage-based targeting. When a flag targets a percentage of subjects (for example, 50%), the targetingKey determines which "bucket" a user falls into. Users with the same targetingKey always receive the same variant for a given flag.
Evaluate flags
After the provider is initialized, you can evaluate flags anywhere in your application. Flag evaluation is local and instantaneous—the SDK uses locally cached data, so no network requests occur when evaluating flags.
Get a client
Retrieve the OpenFeature client to evaluate flags:
constclient=OpenFeature.getClient();
Boolean flags
Use getBooleanValue(key, defaultValue) for flags that represent on/off or true/false conditions:
When you need more than just the flag value, use the detail methods. These return both the evaluated value and metadata explaining the evaluation:
constdetails=client.getBooleanDetails('checkout_new',false);console.log(details.value);// Evaluated value (true or false)
console.log(details.variant);// Variant name, if applicable
console.log(details.reason);// Why this value was chosen
console.log(details.errorCode);// Error code, if evaluation failed
Complete example
Here’s a complete example showing how to set up and use Datadog Feature Flags in a JavaScript application:
import{DatadogProvider}from'@datadog/openfeature-browser';import{OpenFeature}from'@openfeature/web-sdk';// Initialize the Datadog provider
constprovider=newDatadogProvider({applicationId:'<APPLICATION_ID>',clientToken:'<CLIENT_TOKEN>',env:'<ENV_NAME>',});// Set the evaluation context
constevaluationContext={targetingKey:'user-123',user_id:'123',user_role:'admin',};awaitOpenFeature.setProviderAndWait(provider,evaluationContext);// Get the client and evaluate flags
constclient=OpenFeature.getClient();constshowNewFeature=client.getBooleanValue('new_feature',false);if(showNewFeature){console.log('New feature is enabled!');}
Update the evaluation context
To update the evaluation context after initialization (for example, when a user logs in), use OpenFeature.setContext():