# Common Issues

#### `window.shoplift` is `undefined`

**Cause:** Your script is running before Shoplift's script has loaded.

**Fix:** Use a polling pattern to wait:

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

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

  check();
}
```

#### `isHypothesisActive()` always returns `false`

**Possible causes:**

* The test is paused or inactive in the Shoplift dashboard
* The hypothesis ID is incorrect (check for typos, copy it fresh from the dashboard)
* The visitor doesn't meet the test's targeting criteria (e.g., wrong device type or country)
* Merchant preview mode is active (check for `?isShopliftMerchant=true` in the URL or sessionStorage key `Shoplift_Session`)

**Debug:**

```javascript
// Force the variant via URL to confirm the hypothesis ID is correct
// https://yourstore.com/?slVariant=your-hypothesis-id

// Then check in console
await window.shoplift.isHypothesisActive('your-hypothesis-id');
```

If the URL parameter override works but normal assignment doesn't, the test configuration in the dashboard needs attention.

#### Variant shows briefly then disappears (flicker)

**Cause:** Your code applies the variant, but another script or page re-render overwrites it.

**Fix:**

* Ensure your test code runs after the DOM element is stable
* Use CSS transitions instead of immediate DOM manipulation
* For elements loaded via AJAX, listen for the appropriate load/render event before applying the variant

#### Test data not appearing in analytics

**Possible causes:**

* `getVisitorData()` is called before the visitor has been assigned to any tests
* Your analytics platform's script hasn't loaded yet
* Consent hasn't been granted (if using `setAnalyticsConsent()`)

**Fix:** Ensure both Shoplift and your analytics platform are loaded before sending data:

```javascript
function init(maxAttempts = 50) {
  let attempts = 0;

  function check() {
    if (window.shoplift && window.yourAnalytics) {
      const data = window.shoplift.getVisitorData();
      if (data.visitorTests.length > 0) {
        // Send data
      }
    } else if (attempts < maxAttempts) {
      attempts++;
      setTimeout(check, 100);
    }
  }

  check();
}

init();
```

#### Consent integration not working

**Debug steps:**

1. Verify your CMP is loaded: `console.log(typeof OneTrust)` or `console.log(typeof Cookiebot)`
2. Check consent state: `console.log(window.OnetrustActiveGroups)` or `console.log(Cookiebot.consent)`
3. Manually test: `await window.shoplift.setAnalyticsConsent(true)`
4. Verify: `console.log(window.shoplift.getVisitorData())`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.shoplift.ai/api-reference/debugging-and-testing/common-issues.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
