Getting Started with Headless
This guide covers the shared setup steps for integrating Elevate A/B Testing into any headless storefront — Shopify Hydrogen, Next.js, or Remix. Complete these steps first, then follow the guide for your specific framework.
Using a standard Shopify theme? This section is for headless storefronts only. For standard Shopify theme integration, see Setup & Installation.
Installation
Install the SDK via npm:
npm install @elevateab/sdkIf you run into peer dependency conflicts (common with stores using older package versions), use the --legacy-peer-deps flag:
npm install @elevateab/sdk --legacy-peer-depsPeer dependencies:
react>= 18.0.0@shopify/hydrogen>= 2023.10.0 (Hydrogen only)next>= 13.0.0 (Next.js only)
Storefront Access Token
The Storefront Access Token allows the SDK to tag cart attributes with A/B test data so that orders are attributed to the correct variant. Without it, your experiments will still run (visitors will see variants), but you won't be able to measure conversion lift per variant in your dashboard.
How to find your token
In your Shopify Admin, go to Settings → Apps and sales channels → Develop apps.
If you don't have a custom app yet, click Create an app and give it a name.
Under Configuration, make sure the app has the Storefront API access scope enabled with at least
unauthenticated_read_product_listingsandunauthenticated_write_checkoutspermissions.Go to the API credentials tab and copy the Storefront API access token.
This token is safe to use client-side. It's a public Shopify token with limited, read-only permissions — it cannot access admin data, customer information, or anything sensitive.
Where to store it
We recommend using environment variables rather than hardcoding the token:
How you reference this variable depends on your framework:
Hydrogen:
context.env.PUBLIC_STOREFRONT_API_TOKENNext.js:
process.env.NEXT_PUBLIC_STOREFRONT_TOKENRemix: Access via your loader context
Anti-Flicker
Set preventFlickering={true} on your ElevateProvider to prevent visitors from briefly seeing the wrong variant before the test configuration loads. This is especially important for above-the-fold content like hero banners, headlines, and pricing.
When enabled, the SDK briefly hides the page content (up to 3 seconds by default) until the test configuration is loaded and the visitor is assigned a variant. If the configuration fails to load within the timeout, the page renders normally with the control experience.
You can customize the timeout with the flickerTimeout prop:
Creating an Experiment
Experiments are created and configured on the Elevate dashboard. The SDK doesn't run experiments on its own — it reads the experiment configuration from Elevate, which handles visitor assignment, traffic allocation, and result tracking.
Elevate supports three experiment types that work with headless storefronts:
Split URL Experiment — Redirects visitors between different URLs (e.g.,
/products/shirtvs/products/shirt-v2).Custom Code Experiment — Runs custom JavaScript on the page.
Content Editor Experiment — Uses the Elevate visual editor to modify on-page content.
All three experiment types work out of the box once you've set up the provider. You create the experiment on Elevate, configure your variants there, and the SDK handles the rest. No code changes are needed in your storefront for these experiments.
Using the useExperiment Hook (Optional)
The useExperiment hook is completely optional. You only need it if you want to conditionally render content directly in your codebase rather than configuring the experiment entirely through Elevate.
For example, if you want to test two different hero banner components that live in your code, you could create an empty custom code experiment on Elevate (no custom code needed — just set up the variations), then use the hook to render different content based on the assigned variant.
Finding your experiment ID
After creating an experiment, you can find its ID in the URL on your Elevate dashboard:
This test ID is what you pass to the useExperiment hook.
Basic usage
Available variant flags
The hook supports up to five variants:
isControl
Visitor is in the control group
isA
Visitor is in Variant A
isB
Visitor is in Variant B
isC
Visitor is in Variant C
isD
Visitor is in Variant D
isLoading
Test configuration is still loading
Handling the loading state
While the SDK fetches the test configuration, isLoading will be true and all variant flags will be false. You have a few options for handling this:
Show a skeleton/placeholder — Best for above-the-fold content where layout shift would be noticeable:
Show the control as default — Works well for minor content changes where a brief flash is acceptable:
Use preventFlickering — If you've enabled preventFlickering on the provider, the page is hidden until loading completes, so isLoading will effectively never be true when your component renders.
Accessing the full variant object
For advanced use cases, you can access the full variant object:
Next Steps
Now that you've installed the SDK and understand the basics, follow the setup guide for your framework:
Shopify Hydrogen — Automatic event tracking via Hydrogen's analytics system
Next.js & Remix — Manual event tracking with helper functions
For a full list of props, hooks, and tracking functions, see the SDK Reference.
Last updated