Experiments

A/B tests the merchant runs from the Experiments app. A storefront does two things with them: it shows each visitor their variant, and it tells the platform which experiments a visitor was shown. The platform does the rest: it judges every variant against the control on the visitor's real orders, carts and revenue.

How a visitor gets a variant

  • The visitor is the platform's visitor cookie, _cartbase_visitor, which the injected proxy sets on the first request; it is the device_id a cart and an order carry, which is what joins a visitor to what they bought.
  • The variant is decided by a hash of the experiment's seed and the visitor id, GrowthBook's hash version 2: fnv1a32(String(fnv1a32(seed + visitor))) % 10000 / 10000, then the variants' ranges laid end to end by weight, each coverage × weight long. The same visitor gets the same variant on every visit with nothing stored, and a visitor whose number falls in no range is outside the experiment and sees the control.
  • Show the control whenever the answer is null: no experiment by that key, a draft, an experiment that ended without a winner, a visitor outside the coverage, or no visitor id.
  • An ended experiment with a winner answers its winner_variant to every visitor: the winning version is live without a code change, until the storefront's code makes it the default.
  • Report once, from the browser. Call the exposures endpoint from the shopper's browser the first time a visitor is shown an experiment (the package remembers per browser); the platform keeps the first exposure and ignores the rest. A call from a server or a script carries no browser's user agent and is answered but never recorded, like a bot's.
  • A checkout rule can be the test. A checkout rule that names an experiment shows its option hidden to the control and greyed out with its reason to the other variant; the platform decides it per cart and no storefront code asks for a variant. The checkout's two listings, asked with unavailable=include, name the experiments that shaped their answer in experiments (the payment listing also per delivery option): report those keys like any other. The package's checkout does both on its own (checkout.md, the listings).

Preview

A link that shows one variant in one browser tab, never counted: {storefront}/?cartbase_preview=<experiment key>:<variant key> (several pairs comma separated). The package keeps it for the rest of the tab: useExperiment answers that variant, a draft's included, and the checkout passes it to its listings as preview=, where the platform forces it for that answer. Nothing a previewed experiment shows is reported. ?cartbase_preview=off ends it. The Experiments app gives each variant its link.

GET /api/store/experiments: what a storefront acts on

  • Purpose: the running experiments with what an assignment needs, and the ended ones with a winner.
  • Auth: anon (x-client-id or the publishable key).
  • Request: no params.
  • Response:
{
  "experiments": [
    {
      "key": "drawer-weight",          // what the storefront asks by
      "status": "running",             // "running" | "ended" (ended ones carry a winner)
      "hash_version": 2,
      "seed": "01J9Z…",                // never changes
      "coverage": 1,                   // the share of visitors included
      "variants": [                    // the control first
        { "key": "control", "weight": 0.5 },
        { "key": "kilos", "weight": 0.5 }
      ],
      "winner_variant": null           // set on an ended one: everyone sees it
    }
  ]
}
  • Errors: 400 missing_client_id.
  • SDK: experiments.listExperiments(client) (@cartbase/storefront/api/experiments).
  • Components: <ExperimentsProvider> reads it the first time a component asks; getExperimentVariant reads it on the server (components.md, family experiments).
# The store's experiments: a list, empty while nothing runs.
BODY=$(curl -sf "$BASE/api/store/experiments" -H "x-client-id: $CLIENT_ID")
echo "$BODY" | node -e 'const b=JSON.parse(require("fs").readFileSync(0,"utf8")); if(!Array.isArray(b.experiments)) process.exit(1)'

POST /api/store/experiments/exposures: what a visitor was shown

  • Purpose: record the first time a visitor is shown each experiment, and get the variants back from the platform's own computation.
  • Auth: anon (x-client-id or the publishable key).
  • Request:
{
  "device_id": "4f6c…",       // the visitor cookie, 1 to 128 characters
  "keys": ["drawer-weight"],  // 1 to 20 experiment keys
  "consent": true             // optional; false answers and records nothing
}
  • Response:
{
  "assignments": [
    { "key": "drawer-weight", "variant": "kilos" }  // null: show the control
  ]
}
  • The platform never takes a variant from the storefront: it computes each one from the visitor id and the seed. A visitor already recorded keeps the first record. A declared bot (a script and a server included: any user agent that is not a browser's) and a visitor who refused analytics (consent: false) get their answer and are never recorded, so neither counts in a result. Each new exposure also reaches the store's analytics as experiment_viewed, on the visitor's profile.
  • Errors: 400 invalid_data (a missing or empty device_id, no keys or more than 20), 400 missing_client_id.
  • SDK: experiments.recordExposures(client, { device_id, keys, consent }).
  • Components: useExperiment(key) reports through its provider, once per visitor per experiment.
# A key with no experiment behind it answers null: the storefront shows its control.
BODY=$(curl -sf -X POST "$BASE/api/store/experiments/exposures" \
  -H "x-client-id: $CLIENT_ID" -H "content-type: application/json" \
  -d "{\"device_id\":\"doc-visitor-$RUN\",\"keys\":[\"doc-none-$RUN\"]}")
echo "$BODY" | grep -q '"assignments"'
echo "$BODY" | grep -q '"variant":null'
# No visitor id is refused.
STATUS=$(curl -s -o /dev/null -w '%{http_code}' -X POST "$BASE/api/store/experiments/exposures" \
  -H "x-client-id: $CLIENT_ID" -H "content-type: application/json" \
  -d '{"keys":["doc-none"]}')
test "$STATUS" = 400

Settings

  • Experiments (admin, Apps): create, start and end experiments, read the results; the app's Settings hold the chance to win at which a variant is called, a new experiment's smallest change worth finding, and the days before any verdict.