# Getting Started

The Shoplift JavaScript API gives you programmatic control over A/B test entry criteria and experiences, visitor consent, and test data retrieval. Use it to build custom test experiences that go beyond what template, theme, and URL tests offer.

### How the Script Loads

Shoplift automatically injects and maintains its script in your theme's `<head>` section. You don't need to add any script tags — the script is managed for you and:

* Loads before other scripts to minimize flicker
* Initializes automatically on every page load
* Provides the global `window.shoplift` object
* Updates automatically when new versions are released

### The `window.shoplift` Object

Once the page loads, three methods are available on the global `window.shoplift` object:

```typescript
interface ShopliftPublic {
  isHypothesisActive(hypothesisId: string): Promise<boolean>;
  setAnalyticsConsent(consent: boolean): Promise<void>;
  getVisitorData(): ExternalVisitorData;
}

declare global {
  interface Window {
    shoplift?: ShopliftPublic;
  }
}
```

<table><thead><tr><th width="204.76171875">Method</th><th>Purpose</th><th>Returns</th><th>Async</th></tr></thead><tbody><tr><td><code>isHypothesisActive()</code></td><td>Check if visitor is assigned to a specific test variant</td><td><code>Promise&#x3C;boolean></code></td><td>Yes</td></tr><tr><td><code>setAnalyticsConsent()</code></td><td>Sets the visitor's analytics consent state</td><td><code>Promise&#x3C;void></code></td><td>Yes</td></tr><tr><td><code>getVisitorData()</code></td><td>Returns visitor info and all test assignments</td><td><code>ExternalVisitorData</code></td><td>No</td></tr></tbody></table>

### Quick Start

Most JavaScript API test implementations only need `isHypothesisActive()`. Here's the minimal pattern:

```javascript
// 1. Check that Shoplift is loaded
if (window.shoplift) {

  // 2. Check the visitor's variant assignment
  const isVariant = await window.shoplift.isHypothesisActive('your-hypothesis-id');

  // 3. Render the appropriate experience
  if (isVariant) {
    // Variant (B) experience
  } else {
    // Control (A) experience
  }
}
```

The `hypothesisId` is the unique identifier for your test variant. You'll find it in the Shoplift dashboard when you create a JavaScript API test:

<figure><img src="https://573960906-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoHLmrXEftH3xhn1oPwUH%2Fuploads%2FgtaNpsAmBtcMMUpbZkIk%2FhypothesisId.png?alt=media&#x26;token=97347086-a455-4d37-bf9c-0a61f6d7c9fa" alt="" width="325"><figcaption></figcaption></figure>

### Safely Checking for Shoplift

The `window.shoplift` object may not be available immediately (e.g., if your script runs before Shoplift's script has loaded). You can use a polling pattern to wait for it:

```javascript
function waitForShoplift(callback, maxAttempts = 50) {
  let attempts = 0;

  function check() {
    if (window.shoplift) {
      callback(window.shoplift);
    } else if (attempts < maxAttempts) {
      attempts++;
      setTimeout(check, 100);
    } else {
      console.warn('Shoplift did not load within the expected time.');
    }
  }

  check();
}

// Usage
waitForShoplift(async (shoplift) => {
  const isVariant = await shoplift.isHypothesisActive('your-hypothesis-id');
  // ...
});
```

### When to Use the JavaScript API

The JavaScript API is the right choice when you need to:

* **Control trigger timing** — assign visitors to tests only when they perform a specific action (open a cart, click a tab, scroll to a section)
* **Modify dynamic content** — elements that load after the initial page render (AJAX carts, React components, lazy-loaded sections)
* **Set third-party consent management state —** if your consent management platform doesn't integrate with Shopify's Customer Privacy API, you can set consent state
* **Combine targeting conditions** — factor in more advanced targeting conditions like cookie presence or user behavior before including a visitor in a test and showing a variant
* **Integrate with third-party tools** — pass test data to analytics platforms or interact with other apps programmatically
