Skip to main content

Experimentation

OpenFeature provides built-in support for experimentation use cases such as A/B testing, multivariate tests, and gradual rollouts. This page explains how three existing capabilities namely the tracking API, hooks, and targeting key, work together to support experimentation workflows.

How OpenFeature Supports Experimentation

Three OpenFeature mechanisms combine to form a complete experimentation pipeline:

  • Tracking API : Records business events (conversions, clicks, sign-ups) and links them back to the flag evaluation context so your analytics platform can measure per-variant outcomes.
  • Hooks : Extend the SDK at well-defined points of the flag evaluation lifecycle to capture metrics and forward data to analytics platforms without modifying application code.
  • Targeting Key : A stable user identifier (passed via evaluation context) that your provider uses to assign users to variants consistently across sessions, and that analytics platforms use as the join key between flag evaluations and tracking events.

Experimentation Workflow

Step 1 : Set the Evaluation Context

The targetingKey is the user identifier your provider uses to assign the user to a variant consistently across sessions. Additional attributes (plan, geo, device) can be used by the provider for more advanced targeting rules.

import { OpenFeature } from '@openfeature/server-sdk';

// Set global context — targetingKey is the primary experiment identifier
OpenFeature.setContext({
targetingKey: 'user-456',
plan: 'premium',
geo: 'EU',
});

Step 2 : Evaluate the Experiment Flag

Use the standard evaluation API to determine which variant the user receives. The provider uses the targetingKey from the context to return a consistent variant for that user.

const client = OpenFeature.getClient();

// getBooleanValue: false = control group, true = variant group
const useNewCheckout = await client.getBooleanValue('checkout-experiment', false);

if (useNewCheckout) {
renderNewCheckoutFlow();
} else {
renderExistingCheckoutFlow();
}

Step 3 : Track Conversions

Call track when a business event occurs (purchase, sign-up, click). The provider automatically links this event to the flag evaluation context — including the targetingKey — so your analytics platform can segment conversion rates by variant.

// When the user completes a purchase, emit a tracking event
client.track('checkout-completed', { value: 29.99, currencyCode: 'USD' });

Step 4 : Analyze Results

Your analytics platform uses the targetingKey as the join key between flag evaluation data and tracking events. This lets it compute per-variant conversion rates and determine statistical significance, so you can decide whether to roll out the winning variant.

The Targeting Key

The targetingKey is the single most important field for experimentation. It serves three roles:

  • Consistent assignment : The same user always receives the same variant as long as the flag configuration does not change.
  • Unbiased splits : Providers use consistent hashing on the targeting key to distribute users evenly across variants.
  • Join key : Analytics platforms join flag evaluation records with tracking events using this identifier.

Use a stable, unique user identifier as the targeting key — typically a user ID, session ID, or device ID.