Shopify Hydrogen UTM tracking: setup for headless stores
By Devendra Variya, co-founder of Virling. Published 2026-11-24, updated 2026-12-01.
Shopify Hydrogen UTM tracking takes three pieces. Load a tracking script in app/root.tsx and allow its host in your content security policy, so the UTM tags of each landing are stored. Write the visitor ID to the cart as an attribute, named virling_vid, from your cart code. Shopify then copies cart attributes onto the order as note attributes, where Virling reads the ID and credits the order to that visitor's tagged visit. Theme snippets do not work on a headless store. Orders with no visitor ID and no email match stay Unattributed.
Why theme snippets do not apply
The usual Shopify setup pastes a snippet into theme.liquid that posts the visitor ID to /cart/update.js, part of the Ajax Cart API. A Hydrogen storefront, or any custom storefront on the Storefront API, has no theme.liquid, and /cart/update.js only exists on the Online Store. You set the attribute from your own cart code instead. The order is read the same way in both cases. For a theme store, see Shopify UTM tracking.
Which Hydrogen version the code is for
The script, policy and cart route code below is for Hydrogen 2026-04 and earlier. Version 2026-10 replaced CartForm and the cart action route, and Shopify's migration guide moves the content security policy into your own code. The idea does not change: load the script, allow its host, and set the cart attribute. Each step says what to do on 2026-10.
Step 1: add the script
Put the tag in the <head> of app/root.tsx, with the nonce from useNonce:
// app/root.tsx, inside <head>; nonce comes from useNonce() in @shopify/hydrogen.
<script defer src="https://app.virling.com/v.js"
data-site="yourstore.com" nonce={nonce}></script>Hydrogen navigates with the History API, so route changes are tracked with no extra code. On 2026-10, use the nonce from your own policy code, or leave the attribute out if you have none.
Step 2: allow the host in the content security policy
Without this the browser blocks the script and its events. In app/entry.server.tsx, add your Virling host to createContentSecurityPolicy:
const { nonce, header, NonceProvider } = createContentSecurityPolicy({
shop: { checkoutDomain: context.env.PUBLIC_CHECKOUT_DOMAIN, storeDomain: context.env.PUBLIC_STORE_DOMAIN },
scriptSrc: ["'self'", "https://cdn.shopify.com", "https://app.virling.com"],
connectSrc: ["'self'", "https://app.virling.com"],
});Keep https://cdn.shopify.com in scriptSrc: once you set that directive, the default script sources no longer apply, and Shopify loads its own scripts from there. Self-hosting, use your own Virling address in place of app.virling.com here and in the script tag. On 2026-10, add the same hosts to script-src and connect-src in your own policy.
Step 3: put the visitor ID on the cart
The script stores the visitor ID in the virling_vid cookie on your storefront's domain, so the Hydrogen server receives it with every cart request. In the action of app/routes/cart.tsx, set it after lines are added, with updateAttributes from the cart handler:
case CartForm.ACTIONS.LinesAdd: {
result = await cart.addLines(inputs.lines);
// Store the Virling visitor ID on the cart; Shopify copies it onto the order.
const vid = request.headers.get("Cookie")?.match(/(?:^|;\s*)virling_vid=([^;]+)/)?.[1];
const cartId = result.cart?.id;
if (vid && cartId) {
result = await cart.updateAttributes([{ key: "virling_vid", value: vid }], { cartId });
}
break;
}If you set other cart attributes, pass them in the same call. On Hydrogen 2026-10, or on a custom storefront without Hydrogen, use the Storefront API: pass attributes in the input of cartCreate, or call cartAttributesUpdate on an existing cart:
mutation SetVisitor($cartId: ID!, $attributes: [AttributeInput!]!) {
cartAttributesUpdate(cartId: $cartId, attributes: $attributes) {
cart { id }
userErrors { field message }
}
}Send attributes: [{ key: "virling_vid", value: vid }], with the ID from the cookie on your server or from window.virling?.vid?.() in the browser.
Step 4: read the order's note attributes
Shopify copies the cart attribute onto the order. No checkout script is involved; see what still works for tracking under Shopify checkout extensibility. In the Admin API's order resource it looks like this, with a made-up visitor ID:
"note_attributes": [
{ "name": "virling_vid", "value": "vexample123" }
]Connect the store to Virling with an Admin API token that has only the read_orders scope, as described on the Shopify integration page. Virling imports the last 60 days of orders on the first run, then new orders and refunds every minute. It looks for virling_vid first. For an order without one, it compares the order's email with emails your site sent as identify events, and an exact match shows as Matched by email. UTM tags that Shopify stores on the order, and the landing page, are not read. The method is explained in how to track Shopify revenue by traffic source.
What each step keeps
The UTM tags only live on the storefront. What reaches the order is the visitor ID, and Virling joins the order to the visit through it. The table follows one test order through a Hydrogen store; the order is example data, made up to show the fields. Check the tagged link with the UTM checker before you start.
| Step | What it keeps | How it reaches the order |
|---|---|---|
| Storefront page | The Virling script records the landing's UTM tags and referrer against a visitor ID, and sets the virling_vid cookie, for example vexample123. | It does not on its own. The tags stay with the visit in Virling, and nothing is sent to Shopify yet. |
| Cart | The lines, and the attribute virling_vid that your cart code sets from the cookie. | The attribute is part of the cart, so it travels with it to checkout. |
| Shopify checkout | The cart and its attributes, plus the email the buyer types in. The Virling script does not run here, and does not need to. | Shopify turns the cart into the order and copies its attributes onto it. |
| Order | note_attributes with virling_vid, the email, total_price, currency, financial_status and any refunds. | Virling reads the order with the read_orders token and credits it to the visits behind virling_vid. With no ID, an exact email match is used, and otherwise the order is Unattributed. |
Example numbers: the test order has two items at $31.60 and $22.85, $5.35 shipping and no tax, so Shopify's total_price is 59.80. Virling records $59.80, the total with shipping, dated when the order was processed. It imports orders that are paid, partially refunded or refunded, and keeps a voided order as failed; an order still pending or only authorized is skipped until its payment is captured. When you refund the whole test order, it stays in the list as refunded.
What stays Unattributed
These orders still count toward revenue, but with no visitor ID they are credited to a visit only if the email matches:
- Carts made outside your storefront, such as cart permalinks, Buy Button embeds and other sales channels, have no
virling_vid - Cookieless mode has no cookie and no visitor ID to store, so leave
data-cookielessoff for a store - A blocked script, by a browser or an ad blocker, leaves no visitor ID
- A cart created before the script loaded, on a visitor's first page, has no attribute until the next line is added
- Orders from before the setup carry no visitor ID, and a visitor ID with no visit inside the lookback window is not credited either
For orders with no source in Shopify's own reports, see why Shopify orders show up as Direct.
Test one order
- Turn on Shopify Payments test mode or the Bogus Gateway. For local development, add
data-devto the script tag - Open a private window and visit the storefront through a tagged link, such as
https://yourstore.com/?utm_source=test&utm_medium=email - Add a product in the same window and check the cart's attributes, for example with
cart { attributes { key value } }on the Storefront API'sCartobject - Go to checkout from that cart and place a test order with a new customer email, since a later order by the same customer keeps the first order's channel
- Open the order in the Shopify admin and check that its note attributes, shown as additional details, include
virling_vid - Wait a minute or two for the next sync, then open the payments list in Virling: the order shows the
testsource and the Email channel instead of Unattributed
Cancel and refund the test order afterwards. The full setup is in the Shopify docs.
Virling is open source. Self-host it free, or start the hosted version with a 14-day free trial.
Frequently asked questions
Does Shopify Hydrogen track UTM parameters?
With the setup on this page, yes. The script records the UTM tags of each landing on your Hydrogen storefront, and the visitor ID on the cart joins that visit to the order, so revenue can be grouped by UTM campaign.
Can I use a theme.liquid tracking snippet on a Hydrogen store?
No. A Hydrogen storefront has no theme.liquid, and /cart/update.js only exists on the Online Store. Set the cart attribute from your own cart code through the Storefront API instead.
How do cart attributes get to a Shopify order?
Attributes set on a cart through the Storefront API are copied onto the order as note attributes, the same as on theme carts. Virling reads virling_vid from there.
Does Shopify checkout on another domain break the tracking?
No. The visitor ID travels on the cart, not in the browser, so checkout can stay on Shopify's checkout domain. Keep the storefront on your own domain, where the script runs.
Why is my Hydrogen order Unattributed?
Usually its note attributes have no virling_vid: the script was blocked or cookieless, or the cart was made outside your storefront. An order with a visitor ID is also Unattributed when no visit of that visitor falls inside the lookback window.
Does this work without Hydrogen?
Yes. Any storefront built on the Storefront API can pass the attribute to cartCreate or call cartAttributesUpdate on an existing cart. Virling reads the finished order through the Admin API either way.
See revenue per UTM campaign for your Hydrogen store
Virling is open source under AGPL. Self-host it free, or use the hosted version with a 14-day free trial. It connects to Stripe, Paddle, Shopify, WooCommerce, Polar, LemonSqueezy, Dodo, Creem and RevenueCat.