> For the complete documentation index, see [llms.txt](https://docs.shoplift.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.shoplift.ai/api-reference/guides/javascript-api-tests/code-examples/add-your-own-code-to-any-test.md). Agents can also search and read this documentation through its [MCP server](https://docs.shoplift.ai/~gitbook/mcp).

# Add Your Own Code to Any Test

Read which group a visitor is in for a price, template, theme or URL redirect test, and make your own change for that group.

Make an extra change for visitors in one group of a price, template, theme or URL redirect test. Shoplift measures the test's change and yours together, as one variant. This guide is for the developer who adds the code to the theme.

**You need:** a live or draft test · access to your theme code · about 30 minutes

**Not a developer?** To decide whether a combo test fits your idea, read [Combo tests](https://docs.shoplift.ai/learn/guides/combo-tests).

### Before you start

* Create the test in Shoplift. See [Create a price test](https://docs.shoplift.ai/test/price-testing/create-a-price-test) or [Create a template test](https://docs.shoplift.ai/test/template-testing/create-a-template-test).
* Have your Shoplift API key ready, if you'll look up the group IDs yourself. See Shoplift API key.

### Steps

#### 1. Get the variant's hypothesis ID

Each group in a test has an ID called a hypothesis ID. For a template, theme or URL redirect test, copy it in Shoplift: hover over the info icon next to the variant's name on the test's draft, or in the test report once it's live. For a price test before launch, look it up with the Get test request.

```bash
export SHOPLIFT_API_KEY="YOUR_API_KEY"
curl https://app.shoplift.ai/api/public/v1/tests/YOUR_TEST_ID \
  -H "Authorization: Bearer $SHOPLIFT_API_KEY"
```

* `YOUR_API_KEY`: your Shoplift API key.
* `YOUR_TEST_ID`: the test's ID from the List tests response.

Expected result: a JSON test object. `variant.id` is the variant's hypothesis ID, and `control.id` is the original's.

#### 2. Add the check to your theme

Add this script to `layout/theme.liquid` in your live theme, just before the closing `</body>` tag, so it runs on every storefront page that uses your main layout (all of them, on most themes). For a theme test, add it to the variant theme as well, so it runs for both groups. It waits for Shoplift, then checks whether the visitor is in the variant.

```javascript
function whenShopliftReady(timeoutMs = 5000) {
  return new Promise((resolve) => {
    const started = Date.now();
    (function check() {
      if (window.shoplift) return resolve(window.shoplift);
      if (Date.now() - started >= timeoutMs) return resolve(null);
      setTimeout(check, 100);
    })();
  });
}

async function applyComboChange() {
  const shoplift = await whenShopliftReady(15000);
  let inVariant = false;
  try {
    inVariant = shoplift
      ? shoplift
          .getVisitorData()
          .visitorTests.some((test) => test.hypothesisId === "YOUR_VARIANT_HYPOTHESIS_ID")
      : false;
  } catch (error) {
    inVariant = false;
  }
  if (inVariant) {
    document.body.classList.add("combo-variant");
  }
}

applyComboChange();
```

* `YOUR_VARIANT_HYPOTHESIS_ID`: the `variant.id` from step 1. It works the same way for every test type.

Expected result: for visitors in the variant, the page's `body` gets the `combo-variant` class. Style your change with that class, or replace the `classList` line with your own code. Visitors in the original, visitors outside the test, and visitors whose page can't load Shoplift keep your normal store. If Shoplift loads after the 15-second wait, the visitor sees your normal store on that page, and the change appears from the next page.

The check uses [`getVisitorData()`](https://docs.shoplift.ai/api-reference/javascript-api/javascript-api/getvisitordata.md) and only reads the visitor's assignment. Match on `hypothesisId`, not `testId`.

{% hint style="warning" %}
**Don't use `isHypothesisActive()` for a price, template, theme or URL redirect test.** Calling it with one of their hypothesis IDs can put visitors into the test before they reach it, which skews your results. Read the assignment with `getVisitorData()` instead, as above.
{% endhint %}

#### 3. Check again when it matters

A visitor joins a test at a set moment, and your check only finds them after that:

| Test type         | When a visitor joins                          |
| ----------------- | --------------------------------------------- |
| Price test        | When a tested price first scrolls into view   |
| Theme test        | On their first page view                      |
| Template test     | When they first reach the tested type of page |
| URL redirect test | When they first land on the tested URL        |

For a price test, a visitor joins when a tested price first scrolls into view, which can be after your check runs. On that page, call `applyComboChange()` again at the moment your change matters, such as in your cart drawer's open handler. For template, theme and URL redirect tests, the visitor's group is already set when Shoplift is ready, so the check in step 2 finds it.

#### 4. Check both groups

Open your store in a private browser window, not Shoplift's preview: the preview shows the test's own change only. For a theme test, first confirm the script is in the variant theme too. Reach the point where visitors join the test, then run this in your browser's console:

```javascript
window.shoplift.getVisitorData().visitorTests.map((test) => test.hypothesisId).filter(Boolean);
```

Expected result: a list of the hypothesis IDs for the groups you're in. If the variant's ID is there, your change should show. If not, close all private windows and open a new one to try for the other group. In most browsers, private windows share one session until you close them all.

Visits you make this way are counted in the test's results, like any visitor's.

### Verify it worked

In a private window where the console lists the variant's ID, your change shows on every page where you added the script. In a window where it doesn't, your normal store shows.

### Troubleshooting

**The change never shows**: the visitor hasn't joined the test yet, or the ID doesn't match. Run the console check from step 4 on the same page, and compare the IDs with `variant.id`.

**The change shows on later pages but not the first one**: the visitor joined the test after your check ran. Call `applyComboChange()` again at the moment the change matters, as in step 3.

**The change never shows for the variant of a theme test**: the script is only in your live theme. Add it to the variant theme as well.

**The change still shows while the test is paused**: your check can still find the visitor's group while the test is paused. Remove or turn off your change while the test is paused.

**`window.shoplift` is undefined in the console**: Shoplift hasn't loaded on the page. See [Common issues](https://docs.shoplift.ai/api-reference/debugging-and-testing/common-issues.md).

### Next steps

* [Give each test group its own offer at checkout](https://docs.shoplift.ai/api-reference/guides/javascript-api-tests/code-examples/offers-at-checkout.md)
* [getVisitorData()](https://docs.shoplift.ai/api-reference/javascript-api/javascript-api/getvisitordata.md)

**Related articles**

* Get test
* [window.shoplift](https://docs.shoplift.ai/api-reference/javascript-api/javascript-api/window.shoplift.md)

***

**Still need help?** [Contact support](https://docs.shoplift.ai/support/get-help) to chat with the Shoplift team in the app, or email <help@shoplift.ai>. Include your store URL and the name of your test.
