RenderContextDocumentation

Experiments

Run two-variant A/B tests with deterministic assignment, automatic exposure tracking, and conversion measurement in the dashboard.

Prerequisites

Complete the quickstart first. You should be sending pageviews and custom events before running an experiment.

1. Create an experiment

In the console, open your project and go to Experiments Create experiment. Set:

  • Key — identifier used in code (e.g. checkout_test)
  • Variants — two names, typically control and treatment
  • Allocation % — share of in-rollout visitors assigned to the treatment variant
  • Rollout % — share of all visitors entered into the experiment
  • Primary conversion event — custom event name that counts as a conversion (e.g. purchase_completed)

Use the experiment key in your code, not the internal document ID shown in dashboard URLs.

2. Get a variant assignment

analytics.variant returns the variant name, or null when the visitor is outside rollout, the experiment is disabled, or render context times out.

const variant = await analytics.variant("checkout_test");

if (variant === "treatment") {
  renderTreatmentCheckout();
} else if (variant === "control") {
  renderControlCheckout();
} else {
  renderDefaultCheckout(); // not in experiment
}

The first variant() call per experiment per page load automatically records an exposure event.

3. Record a conversion

When the visitor completes your goal action, fire a track event with the same name you set as the primary conversion event. No special experiment API is required.

analytics.track("purchase_completed");

4. View results

Open the experiment in the console and click Results to compare exposures and conversions across variants. Results update as new events arrive.

Manual exposure (optional)

If you need to record an exposure without calling variant() first:

await analytics.exposure("checkout_test", "treatment");

Related

Feature flags share the same render context. See Feature flags for rollout and targeting without a conversion goal.

Back to docs