window.abtest

The storefront JavaScript API that lets your theme code check which variant a visitor is in and render the matching experience.

When the AB Test app embed is enabled, the storefront runtime attaches a small global object, window.abtest, to every page. Its main use is JavaScript API tests, where your own code renders variant B instead of the app redirecting the browser. This page documents the public members precisely as the runtime implements them.

Methods at a glance

MemberSignatureReturns
isHypothesisActive isHypothesisActive(hypothesisId) Promise<boolean>
getVisitorData getVisitorData() { visitorId, assignments } (synchronous)
setAnalyticsConsent setAnalyticsConsent(consent) Promise<void>

setAnalyticsConsent is covered on its own page — see Data & consent. The rest of this page covers the two members you use to drive variant rendering.

Assignments load asynchronously (from a cached cookie or the network). isHypothesisActive waits for them internally, so you should await it. getVisitorData is synchronous and returns only what is known at the moment you call it.

isHypothesisActive(hypothesisId)

Resolves to whether the current visitor is assigned to a specific variant. Use it to decide whether to render your variant experience.

Parameters

NameTypeDescription
hypothesisId string The variant (hypothesis) id you want to check, copied from the test's setup screen in the AB Test admin.

Return value

A Promise<boolean> that resolves once the visitor's assignments are known:

Because the runtime fails open, a network error while loading assignments resolves to false (the visitor sees your default/control experience) rather than rejecting.

For a manual-trigger test, the first call to isHypothesisActive for that experiment also enrols the visitor. The bucket is already decided; this call just persists that the visitor's code reached the test. Automatic-trigger tests enrol on page load, before your code runs.

getVisitorData()

Returns the current visitor id and the visitor's known assignments, synchronously.

{
  visitorId: "…",       // the anonymous _ab_vid for this visitor
  assignments: [
    { experimentId: "…", variantId: "…" }
    // one entry per experiment the visitor is enrolled in
  ]
}
FieldTypeDescription
visitorId string The pseudonymous visitor id (the _ab_vid cookie value). No personal data.
assignments array One { experimentId, variantId } per enrolled experiment. Empty until assignments finish loading.

getVisitorData does not wait. If you call it during initial page load, assignments may be an empty array even though the visitor will be enrolled once loading completes. For a reliable per-variant check, await isHypothesisActive instead of reading assignments yourself.

Usage example

A typical JavaScript API test renders variant B only for visitors assigned to it, and leaves everyone else on the control markup. Await isHypothesisActive with the variant id from the test setup screen:

// Variant id copied from the JS API test in the AB Test admin.
const VARIANT_B = "var_9f3c…";

async function renderExperiment() {
  if (await window.abtest.isHypothesisActive(VARIANT_B)) {
    // Visitor is in variant B — render your alternate experience.
    document.querySelector("#cta").textContent = "Start free trial";
  }
  // Otherwise leave the control markup untouched.
}

renderExperiment();

Reading the visitor id (for example, to tag your own analytics event) is synchronous:

const { visitorId, assignments } = window.abtest.getVisitorData();
console.log(visitorId, assignments);

See the JS test workflow for how to structure a full test, including anti-flicker handling.

How assignments load

The runtime resolves each visitor's assignments before it fulfils isHypothesisActive:

  1. On page load the runtime reads or creates the anonymous _ab_vid.
  2. It uses a cached assignment cookie when one is present for that visitor; otherwise it calls the assign endpoint.
  3. An internal promise resolves as soon as assignments are known — from cache or network — and every pending isHypothesisActive call resolves against them.

Because of this, you can call isHypothesisActive as early as you like; it will not resolve before assignments are available.

Requirements and scope

Related