When to use it
Use custom code for a change the test type cannot make on its own.- Add an element the theme does not have. A shipping test’s Variant A shows a free shipping progress bar in the cart drawer that fills as the cart grows. The script reads the visitor’s threshold from the JavaScript API, so each test group’s bar matches its own shipping rate.
- Change how the page behaves. A visual editor test’s Variant A shows a sticky Add to cart bar on mobile once the visitor scrolls past the Add to cart button on the blue snowboard product page.
- Restyle part of the page. A price test’s Variant A hides the compare-at price on the blue snowboard product page with three lines of CSS.
When NOT to use it
- You want to edit text, images, or links on the page. Use a visual editor test instead. Its changes are scoped to the element, hide it until the change lands, and need no code.
- The code changes a price, a shipping rate, or an offer. Those are the test itself. Use a price test, a shipping test, or an offer test, and read the result from the JavaScript API if you need to render it.
- You want to send assignments to another tool. A script in your theme reads every test’s assignment from the JavaScript API on every page, so you write it once instead of adding it to each test. See Send assignments to another tool.
How it works
Each test group has its own JavaScript and CSS. When a visitor lands on a page in the test, ABConvert assigns them to a test group and puts that test group’s code on the page. Only the assigned test group’s code runs; no other test group’s code reaches that visitor.- The test’s script resolves the visitor’s test group.
- The test group’s JavaScript runs at the time you chose: before the page shows, or after the page loads.
- The test group’s CSS is added to the page head at once.
location.pathname.startsWith('/products/').
When the code runs
When the code runs depends on when the test assigns the visitor to a test group:- Theme, template, URL redirect, visual editor, and price tests usually assign the visitor before the page shows. The code is on the page before the visitor sees anything.
- A test that targets visitors by country needs a country lookup on the visitor’s first visit. If any test group has CSS, or JavaScript set to run before the page shows, the page stays hidden for every test group, Control included, until the lookup returns. After a few seconds the page shows anyway, and the code runs once the lookup returns.
- Shipping, offer, and checkout tests assign the visitor after the page loads, to keep the storefront fast. Their code runs on the page the visitor already sees.
JavaScript set to run before the page shows holds the page until it finishes. Keep it short, and choose After the page loads for anything that does not change what the visitor first sees.
Setup
1
Open the changes step of any test
Open the Custom Code card in the step where you set up each test group.
2
Add code to a test group
Click Add code on a test group, and write JavaScript or CSS. Control is an ordinary test group here.
3
Choose when JavaScript runs
- Before the page shows: runs as soon as the visitor is assigned. On most tests that is before the page shows, so visitors see only the changed page. Use it for visual changes.
- After the page loads: runs once the page’s HTML has loaded (
DOMContentLoaded). The page is not hidden, so visitors may see it change. Use it for popups and event listeners.

The Custom Code card, where you add JavaScript and CSS to each test group.
body { outline: 4px solid red } to Variant A of a draft test, open the preview link, and switch test groups from the preview bar.
Reading the test from your code
Custom JavaScript can read the visitor’s test group, price, shipping rates, and offer throughwindow.ABConvert. Push your code into window.ABConvertQueue and it runs once every test on the page has assigned the visitor:

Custom code adds a free shipping bar above the header.
Common mistakes
- Reading an element that is not on the page yet. Code that runs before the page shows can run before the page has a
<body>. Build the elements you render into inside thewindow.ABConvertQueuecallback, which runs after the page has parsed. If the code does not change the layout, choose After the page loads instead. - Waiting for the page to load before changing what the visitor sees. The visitor sees the original until the wait is over. Put styling in CSS, which applies before the page shows, and change an element the moment it appears, for example with a
MutationObserver, rather than onDOMContentLoaded. - Running a popup or a listener before the page shows. It holds the page until it finishes, and on a test that targets visitors by country it keeps the page hidden until the country lookup returns, though it changes nothing the visitor first sees. Choose After the page loads for it.
FAQ
Does a JavaScript error break the test?
Does a JavaScript error break the test?
No. A script that throws is reported to ABConvert and the rest of the test runs as normal. A script that does not parse is refused when you save.
Is there a size limit?
Is there a size limit?
Each block is limited to 50,000 characters. The code is added to every page in the test, so keep it short.
Can I edit custom code on a running test?
Can I edit custom code on a running test?
No. Custom code is part of the test’s setup and is locked once the test is running, like a price or a shipping rate. End the test and create a new one to change it.