> ## Documentation Index
> Fetch the complete documentation index at: https://docs.abtestly.com/llms.txt
> Use this file to discover all available pages before exploring further.

# window.abtestly API

> Every method and event on the runtime object. Stable surface, you can call these from variant JS or from your own scripts.

Once the snippet has loaded, `window.abtestly` (and its alias
`window.__abtestly`) is available on the page. This is the runtime's
public surface. It is designed to be called from variant JavaScript, from
your own scripts, and, for `debug`, from the DevTools console.

> [!NOTE]
>
> Writing the variant code itself? The dashboard's [variation
> editor](https://docs.abtestly.com/build/variations.md) is Monaco (VS Code) with linting and
> Cmd+S save, and you can [attach curated
> libraries](https://docs.abtestly.com/build/dev-libraries.md) like Splide or GLightbox from a
> picker instead of pasting `<script>` tags into every variant.

## The methods

### `trackGoal(key, valueOrOpts?)`

Fire a custom goal.

```js theme={null}
// simple
abtestly.trackGoal('newsletter_signup');

// with revenue (number sugar)
abtestly.trackGoal('purchase', 129.00);

// full form for revenue with dedup and refunds
abtestly.trackGoal('purchase', {
  value: 129.00,
  orderId: 'order-9182',
  currency: 'USD',
});

// refund
abtestly.trackGoal('purchase', {
  refund: true,
  refundId: 'refund-3341',
  orderId: 'order-9182',
});
```

**Returns:** `boolean`. `true` if accepted, `false` if the visitor was
opted-out or the goal key is not one your experiments care about.

`orderId` enables revenue deduplication. If two beacons arrive with the
same `expId:goalId:orderId`, the second is netted server-side so a
double-fire from a retry does not double-count revenue.

### `onApply(experimentIdOrKey, fn)`

Register a callback that fires when a specific experiment applies. If the
experiment already applied on this pageview, the callback fires
immediately.

```js theme={null}
abtestly.onApply('checkout-simplify', (ctx) => {
  console.log('checkout-simplify applied for', ctx.variantKey);
});
```

The callback receives an `ApplyContext` with the variant key and a
route revision.

### `onCleanup(experimentIdOrKey, fn)`

Register a teardown callback. It fires on SPA route changes only, for
every experiment that was active on the previous route, including the
ones still active on the new one, so a re-apply never stacks on top of
the last one. Handlers run in registration order. See [SPA
lifecycle](https://docs.abtestly.com/developer/spa-lifecycle.md).

```js theme={null}
abtestly.onCleanup('checkout-simplify', () => {
  // undo any DOM mutations you made in onApply
  document.querySelector('.mount').remove();
});
```

### `waitFor(target, cb, timeoutMs = 5000)`

Wait for an element or a predicate. Polls at 50 ms. Fires `cb` when the
condition is met, or times out silently after `timeoutMs`.

```js theme={null}
// single selector
abtestly.waitFor('.cart-total', (el) => {
  el.textContent = 'You save $X';
});

// array of selectors — cb fires once all resolve
abtestly.waitFor(['.header', '.footer'], ([h, f]) => { … });

// custom predicate
abtestly.waitFor(() => window.__ready === true, () => { … });
```

### `activate(key)`

Fire a **manual** activation trigger. See
[Activation triggers](https://docs.abtestly.com/targeting/activation-triggers.md) for when to use
this.

The `key` is the immutable published experiment key (the
`scr003`-style code), not the editable display name.

Returns a discriminated object:

```ts theme={null}
type ActivationResult =
  | { ok: true;  status: 'activated' | 'already_active' }
  | { ok: false; reason:
        'unknown_experiment'
      | 'not_eligible'
      | 'not_bucketed'
      | 'wrong_activation_mode'
    };
```

```js theme={null}
const result = abtestly.activate('scr003');
if (result.ok) {
  // status is 'activated' (fired just now) or 'already_active'
} else {
  // reason explains why nothing fired
  // 'unknown_experiment'  — no experiment with that key in the live config
  // 'not_eligible'        — targeting excludes this route/visitor
  // 'not_bucketed'        — eligible but not served (traffic holdback or degraded)
  // 'wrong_activation_mode' — configured activation is not 'manual'
}
```

Callers may safely ignore the return value; the loader queue does. The
shape exists so GTM/dataLayer integrations can branch on why an
activation did or did not fire.

### `dev.apply(experimentId)`

**Development only.** Apply an experiment's variant to the current
pageview without bucketing and without firing a beacon. Useful for
prototyping. Do not ship code that calls this.

```js theme={null}
abtestly.dev.apply('checkout-simplify');
```

### `debug(opts?)`

Return a snapshot of what the runtime knows: loaded experiments, the
last handful of events (buckets picked, variants applied, beacons sent),
and any errors caught. See [Dev debugger](https://docs.abtestly.com/preview/dev-debugger.md) for the
full shape.

```js theme={null}
abtestly.debug();          // human-readable
abtestly.debug({ json: true });  // JSON, for pasting into an issue
```

## Events

### `abtestly:routechange`

Fires on `window` whenever ABTestly detects an SPA route change and
finishes re-evaluating. The `detail` object includes:

* `url`, the new URL
* `activeExperiments`, experiment ids currently applied on the new
  route
* `deactivatedExperiments`, experiment ids that were torn down

```js theme={null}
window.addEventListener('abtestly:routechange', (e) => {
  console.log('now on', e.detail.url);
});
```

## Runtime bindings inside variant JS

Every variant's JavaScript runs inside a function that gets a few names
passed in as arguments. In addition to the raw code you wrote, you can
reference:

* `ABTESTLY_EXP_KEY`, the experiment's short code (e.g. `checkout-simplify`)
* `ABTESTLY_EXP_ID`, the experiment's UUID
* `ABTESTLY_VARIANT_KEY`, the variant's short key (`control`, `variant-1`, …)

These are useful for logging or for keying your own storage per-variant.
See [runtime bindings](https://docs.abtestly.com/developer/runtime-bindings.md) for the full list
and the exact positions.

## Stability

Everything on this page is part of the **stable public API**. Method
signatures do not change without a major version bump; deprecated
methods stick around for at least one major cycle after their
replacement lands.

Internals not documented here, `_handlers`, `_pendingActivations`,
private queues, are exactly that: internal. If a piece of your code
reaches for one of those, expect it to break.

## Related

> [card] [Variation editor (Monaco)](https://docs.abtestly.com/build/variations.md)
>
> Real editor, real lints. Cmd+S save, fullscreen, theme & font.

> [card] [Dev libraries](https://docs.abtestly.com/build/dev-libraries.md)
>
> Curated Splide, GLightbox, and friends via a picker.

> [card] [Dev debugger](https://docs.abtestly.com/preview/dev-debugger.md)
>
> Console snapshot of assignments, applies, beacons, errors.

> [card] [Runtime bindings](https://docs.abtestly.com/developer/runtime-bindings.md)
>
> ABTESTLY\_EXP\_KEY and friends inside variant code.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.