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

# JavaScript API reference

> Every global, method, event, and object on window.ABConvert.

This page lists every global, method, event, and object on `window.ABConvert`. For what the API is for and how to use it, read [JavaScript API](https://docs.abconvert.io/api-reference/browser-api.md) first.

## Globals

| Global | Defined when | Use it for |
| - | - | - |
| `window.ABConvertQueue` | Your code or ABConvert creates it; ABConvert keeps one you created | Running a callback when ABConvert is ready. Before ready, callbacks wait; after ready, `push` runs them at once. |
| `window.ABConvert` | The ABConvert app embed has run | Every method below. Code that runs after the app embed can call it directly, but assignments are not known before ready. |

Do not define your own `window.ABConvert`: ABConvert replaces it when it runs. The ABConvert object reaches your `window.ABConvertQueue` callback as its argument.

## Methods

| Method | Defined when | Returns | Empty result when |
| - | - | - | - |
| `onReady(callback)` | Always | `void` | Never |
| `getAssignments()` | Always | `Assignment[]` | `[]`: the visitor is in no test |
| `getAssignment(experimentId)` | Always | `Assignment \| null` | `null`: the visitor is not in that test |
| `getPriceByVariantId(variantId, options?)` | Price test | `Price \| null` | `null`: no test price applies to this visitor. [See the rules](https://docs.abconvert.io/api-reference/browser-api.md#prices) |
| `getPriceByProductId(productId, options?)` | Price test | `Price \| null` | `null`: the same cases as `getPriceByVariantId`, per product |
| `formatPrice(amount, currency?)` | Always | `string` | Never |
| `getShippingRates(options?)` | Shipping test | `ShippingRate[] \| null` | `null`: the visitor is in no shipping test `[]`: the test group has no rates, or `zone` names a zone the test does not have |
| `getFreeShippingThreshold(options?)` | Shipping test | `FreeShippingThreshold \| null` | `null`: the visitor is in no shipping test, or no single threshold exists. [See the rules](https://docs.abconvert.io/api-reference/browser-api.md#read-the-free-shipping-threshold) |
| `getOffers()` | Offer test | `Offer[]` | `[]`: the visitor is in no offer test, or their test group has no offer |
| `getSubscriptionOffers()` | Subscription test | `SubscriptionOffer[]` | `[]`: no assigned subscription plans |
| `track(name, payload?)` | Always | `void` | Never |
| `getVisitorId()` | Always | `string` | Never |
| `getCountry()` | Always | `string` | `''`: the country is not known yet |
| `forceTestGroup(experimentId, index)` | Always | `void` | Never |
| `clearForcedTestGroup(experimentId)` | Always | `void` | Never |

A method marked with a test type exists only while your store has a test of that type running or in preview.

### onReady

Runs `callback` when ABConvert is ready, or at once if it already is. The same as pushing onto `window.ABConvertQueue`; use it from code that already holds the object.

**callback** `(ABConvert) =\> void` *required*

Receives the `ABConvert` object.

### getAssignments

Returns one [`Assignment`](#assignment) per test the visitor is in. Tests that excluded the visitor are absent.

### getAssignment

Returns the visitor's [`Assignment`](#assignment) for one test, or `null`.

**experimentId** `string` *required*

The test ID shown in the ABConvert admin, as a string: `'49603'`.

### getPriceByVariantId

Returns the visitor's [`Price`](#price) for one product variant, or `null` when no test price applies. See the [null rules](https://docs.abconvert.io/api-reference/browser-api.md#prices).

**variantId** `string` *required*

The product variant ID as a numeric string: `'47522361606401'`.

**options** `PriceOptions`

**fields**

**country** `string`

ISO 3166-1 alpha-2 code. Reads that country's price. Defaults to the country the page rendered for.

### getPriceByProductId

Returns one [`Price`](#price) for a product, or `null`, under the same rules as `getPriceByVariantId`.

**productId** `string` *required*

The product ID as a numeric string: `'6654464491584'`.

**options** `PriceOptions`

**fields**

**country** `string`

As in `getPriceByVariantId`.

**aggregate** `'min' | 'max' | 'first'` (default: 'min')

Which product variant's price to return. `'min'` and `'max'` pick among the product variants the test prices for this visitor. `'first'` picks the product's first product variant, or the first one the test prices.

### formatPrice

Returns `amount` formatted in `currency` for the visitor's locale, such as `'$62.70'`. For a currency code the browser does not know, returns the code and the amount with two decimals, as in `'NOPE 49.50'`.

**amount** `number` *required*

**currency** `string`

ISO 4217 code. Defaults to the page currency, then to `'USD'`.

### getShippingRates

Returns the [`ShippingRate`](#shippingrate)s the visitor's test group offers, cheapest first within each zone and currency. Returns `null` when the visitor is in no shipping test, and `[]` when the test group has no rates.

**options** `ShippingOptions`

**fields**

**zone** `string`

A shipping zone name, as in the Shopify admin. Returns that zone's rates only, and `[]` for a zone the test does not have. Defaults to every zone.

### getFreeShippingThreshold

Returns the lowest order subtotal at which the visitor's test group gets a free rate in a zone, as a [`FreeShippingThreshold`](#freeshippingthreshold), or `null` when the visitor is in no shipping test or no single threshold exists. See the [null rules](https://docs.abconvert.io/api-reference/browser-api.md#read-the-free-shipping-threshold).

**options** `ShippingOptions`

**fields**

**zone** `string`

A shipping zone name. Required when the visitor's shipping tests cover more than one zone.

### getOffers

Returns one [`Offer`](#offer) per offer test the visitor is in. A test group with no offer contributes none.

### getSubscriptionOffers

Returns one [`SubscriptionOffer`](#subscriptionoffer) per product in each assigned subscription test group. Includes Control and one-time rows. The result can also describe a subscription change in a combined test or personalization. It describes the assignment; it does not confirm the change was applied to a product form or cart.

### track

Records a [custom JS event](https://docs.abconvert.io/analytics/custom-events.md#custom-js-event) for this visitor. At most once per page load; an unknown or archived slug, or a page outside the event's page scope, records nothing.

**name** `string` *required*

The event's [slug](https://docs.abconvert.io/analytics/custom-events.md#event-names-and-slugs): `'product_builder_completed'`.

**payload** `\{ value?: unknown \}`

`value` is stored with the event.

### getVisitorId

Returns the ID ABConvert records this visitor's assignments under, creating it if needed. Use it to match your own logs with ABConvert data. When the browser blocks `localStorage`, every call returns a new ID, so read it once and reuse it. To segment other tools by test group, send `testGroup.index`, not the visitor ID.

### getCountry

Returns the visitor's country as an ISO 3166-1 alpha-2 code such as `'US'`, or `''` while it is still unknown. It is the country ABConvert detected, not the market the page rendered for, so it can differ from `Price.country`. Preview with a chosen country returns that country.

### forceTestGroup

Puts you in a test group from the next page load. For seeing a test group yourself; see [Force a test group](https://docs.abconvert.io/api-reference/browser-api.md#force-a-test-group).

**experimentId** `string` *required*

The test ID. Throws a `TypeError` when empty.

**index** `number` *required*

The test group's position, counting from 0. Throws unless it is an integer of 0 or more. A position past the last test group shows Control.

### clearForcedTestGroup

Undoes `forceTestGroup` for one test, from the next page load.

**experimentId** `string` *required*

The test ID. Throws a `TypeError` when empty.

## Events

ABConvert fires two events on `window`. `abconvert:assignment-ready` fires before ABConvert changes the page; `abconvert:ready` fires after.

| Event | When | `event.detail` |
| - | - | - |
| `abconvert:ready` | Once per page load, when ABConvert is ready. Fires even when the visitor is in no test. | The `ABConvert` object |
| `abconvert:assignment-ready` | Once per assignment on every page load, before `abconvert:ready`. Fires for an assignment kept from an earlier visit, too. | The `Assignment` |

Rules for `abconvert:ready`:

* **It fires once and does not replay.** Prefer `window.ABConvertQueue`, which runs your callback whether you push before or after the event.
* **It does not fire again on client-side navigation.** A theme that swaps page content without a full load gets one event, on the first load.

Rules for `abconvert:assignment-ready`:

* **It fires before your queue callback runs.** Add the listener at the top level of your script, not inside a `window.ABConvertQueue` callback, or it never runs. To read assignments after the fact, call `getAssignments()` instead.
* **It does not fire for a forced or preview assignment.** A visit forced with `forceTestGroup`, a link, or Preview appears in `getAssignments()` but sends no event.
* **It fires once per test.** A combined test's changes register as one assignment and send one event.

When you need one DOM event per assignment, for example as a tag manager trigger, use the events:

```js theme={null}
window.addEventListener('abconvert:ready', function (event) {
  const ABConvert = event.detail;
  console.log(ABConvert.getAssignments());
});

window.addEventListener('abconvert:assignment-ready', function (event) {
  const assignment = event.detail;
  console.log(assignment.experimentId, assignment.testGroup.index);
});
```

## Objects

### Assignment

**experimentId** `string`

The test ID shown in the ABConvert admin, as a string.

**experimentName** `string`

**type** `string`

One of `price`, `shipping`, `theme`, `template`, `url_redirect`, `checkout`, `offer`, `subscription`, `visual_editor`, `combined`, `personalization`.

**status** `'active' | 'preview'`

The [test lifecycle](https://docs.abconvert.io/experiments/lifecycle.md#what-each-state-means) states a visitor can see.

**testGroup** `TestGroup`

The visitor's [test group](#testgroup).

**reason** `string`

How the visitor got the test group:

| `reason` | Set when |
| - | - |
| `random_split` | [Random group assignment](https://docs.abconvert.io/targeting/overview.md#random-split) picked the test group |
| `force_assign_group` | A [targeting rule](https://docs.abconvert.io/targeting/overview.md#targeting-rules) whose action is **Assign to test group** set the test group |
| `preview_override` | [Preview](https://docs.abconvert.io/experiments/lifecycle.md#preview-before-you-launch) set the test group |
| `url_force_assign` | `forceTestGroup` or a [link](https://docs.abconvert.io/experiments/lifecycle.md#see-one-test-group-with-a-link) set the test group |

### TestGroup

**index** `number`

The test group's position in the test, counting from 0. Use it as the identifier.

**name** `string`

A display label. Can change at any time.

**control** `boolean`

`true` for Control. `false` for every test group of a personalization.

**split** `number`

The test group's share of traffic, an integer from 0 to 100.

### Price

**experimentId** `string`

**testGroup** `TestGroup`

**amount** `number`

**compareAtAmount** `number | null`

`null` when the test group sets no compare-at price.

**currency** `string`

ISO 4217. The currency this price is stated in.

**country** `string`

ISO 3166-1 alpha-2. The country this price is for. Empty when unknown.

**productId** `string`

**variantId** `string`

### ShippingRate

**experimentId** `string`

**testGroup** `TestGroup`

**zone** `string`

The shipping zone name, as in the Shopify admin.

**name** `string`

The rate name shown at checkout.

**amount** `number`

`0` for a free rate.

**currency** `string`

The currency you set the rate in, which can differ from the page currency.

**condition** `object | null`

`null` for a rate with no condition.

**fields**

**type** `'price' | 'weight'`

**minimum** `number | null`

**maximum** `number | null`

**unit** `string | null`

The weight unit for a `weight` condition.

### FreeShippingThreshold

**experimentId** `string`

**testGroup** `TestGroup`

**zone** `string`

**amount** `number`

`0` for a free rate with no condition.

**currency** `string`

The currency you set the rate in, which can differ from the page currency.

### Offer

**experimentId** `string`

**testGroup** `TestGroup`

**title** `string`

**discounts** `Discount[]`

The offer's discounts. The `Discount` shapes are in the [TypeScript](#typescript) block below.

### SubscriptionOffer

**experimentId** `string`

**testGroup** `TestGroup`

**provider** `'recharge' | 'stay' | 'skio'`

**semantics** `'acquisition-only' | 'new-recurring-price'`

**purchaseType** `'one-time' | 'subscription'`

One-time rows have no plan IDs.

**productId** `string`

Numeric Shopify product ID.

**providerPlanId** `string`

The connected provider's plan ID, for subscription rows.

**shopifySellingPlanId** `string`

Shopify selling plan ID, when the provider supplies one.

**productPath** `string`

Optional product page path.

**hideOtherPlans** `boolean`

Whether ABConvert hides the other plans on the matching product page.

## TypeScript

Copy this into a `.d.ts` file for autocompletion and type checking. Methods marked `?` exist only while your store has that test type.

```ts theme={null}
interface ABConvertAPI {
  onReady(callback: (api: ABConvertAPI) => void): void;
  getAssignments(): Assignment[];
  getAssignment(experimentId: string): Assignment | null;
  getPriceByVariantId?(variantId: string, options?: PriceOptions): Price | null;
  getPriceByProductId?(productId: string, options?: PriceOptions): Price | null;
  formatPrice(amount: number, currency?: string): string;
  getShippingRates?(options?: ShippingOptions): ShippingRate[] | null;
  getFreeShippingThreshold?(options?: ShippingOptions): FreeShippingThreshold | null;
  getOffers?(): Offer[];
  getSubscriptionOffers?(): SubscriptionOffer[];
  track(name: string, payload?: { value?: unknown }): void;
  getVisitorId(): string;
  getCountry(): string;
  forceTestGroup(experimentId: string, index: number): void;
  clearForcedTestGroup(experimentId: string): void;
}

interface Window {
  ABConvert?: ABConvertAPI;
  ABConvertQueue?: Array<(api: ABConvertAPI) => void>;
}

interface Assignment {
  experimentId: string;
  experimentName: string;
  type: 'price' | 'shipping' | 'theme' | 'template' | 'url_redirect' | 'checkout' | 'offer' | 'subscription' | 'visual_editor' | 'combined' | 'personalization';
  status: 'active' | 'preview';
  testGroup: TestGroup;
  reason: 'random_split' | 'force_assign_group' | 'preview_override' | 'url_force_assign';
}

interface TestGroup {
  index: number;
  name: string;
  control: boolean;
  split: number;
}

interface Price {
  experimentId: string;
  testGroup: TestGroup;
  amount: number;
  compareAtAmount: number | null;
  currency: string;
  country: string;
  productId: string;
  variantId: string;
}

interface PriceOptions {
  country?: string;
  aggregate?: 'min' | 'max' | 'first';
}

interface ShippingRate {
  experimentId: string;
  testGroup: TestGroup;
  zone: string;
  name: string;
  amount: number;
  currency: string;
  condition: { type: 'price' | 'weight'; minimum: number | null; maximum: number | null; unit: string | null } | null;
}

interface FreeShippingThreshold {
  experimentId: string;
  testGroup: TestGroup;
  zone: string;
  amount: number;
  currency: string;
}

interface ShippingOptions {
  zone?: string;
}

interface Offer {
  experimentId: string;
  testGroup: TestGroup;
  title: string;
  discounts: Discount[];
}

interface SubscriptionOffer {
  experimentId: string;
  testGroup: TestGroup;
  provider: 'recharge' | 'stay' | 'skio';
  semantics: 'acquisition-only' | 'new-recurring-price';
  purchaseType: 'one-time' | 'subscription';
  productId: string;
  providerPlanId?: string;
  shopifySellingPlanId?: string;
  productPath?: string;
  hideOtherPlans: boolean;
}

type Discount =
  | { type: 'product_discount';   value: DiscountValue; scope?: DiscountScope; condition?: DiscountCondition }
  | { type: 'order_discount';     value: DiscountValue; condition?: DiscountCondition }
  | { type: 'shipping_discount';  value: DiscountValue; scope?: ShippingDiscountScope; condition?: DiscountCondition }
  | { type: 'volume_discount';    tiers: VolumeTier[]; scope?: DiscountScope; condition?: DiscountCondition }
  | { type: 'threshold_discount'; tiers: ThresholdTier[]; condition?: DiscountCondition };

type DiscountValue =
  | { unit: 'percentage'; value: number; maxDiscountAmount?: Money }   // percent off, capped at maxDiscountAmount
  | { unit: 'fixed'; amount: Money };

type DiscountScope =                                            // omitted = all products
  | { type: 'all_products' }
  | { type: 'specific_products'; productIds: string[] }
  | { type: 'specific_collections'; collectionIds: string[] };

type ShippingDiscountScope =                                    // omitted = all rates
  | { type: 'all_rates' }
  | { type: 'specific_rates'; rateNames: string[] };

type DiscountCondition =
  | { type: 'order_price'; minimum: Money }
  | { type: 'quantity'; minimum: number };

type VolumeTier = { threshold: number; value: DiscountValue };      // threshold: item count
type ThresholdTier = { threshold: Money; value: DiscountValue };    // threshold: order subtotal
type Money = { amount: number; currency: string };
```


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