How to set up the storefront pixel on a headless Shopify store

Shopify only runs Web Pixels on pages Shopify hosts. If your store uses a custom storefront (such as Next.js, Hydrogen, or Astro) with Shopify Checkout, the standard Reportslayer pixel only sees checkout events until you add the storefront pixel to your site.

Once added, the storefront pixel captures landing-page visits, UTM parameters, ad IDs, click IDs, and shopper funnel events on your storefront and ties them to completed orders in Shopify Checkout.

Before you start

Turn on the Reportslayer pixel for your Shopify store first.

  • In Claude or ChatGPT: Ask your assistant to check your pixel status and give you the headless storefront script URL for your store.
  • In the app: Go to Attribution → Pixel and model and make sure the pixel is turned on. Once active, a Headless storefront section appears with your store's script tag.

Step 1: Load the storefront pixel on every page

Copy the script tag from Attribution → Pixel and model → Headless storefront (or ask Claude or ChatGPT for your storefront script URL and load it as an async script) into the <head> of every storefront page.

Once loaded, the pixel automatically records visits along with UTM parameters, ad IDs, and click IDs, including across client-side route changes in single-page apps.

Step 2: Tie storefront visits to Shopify Checkout

When you create a cart and when updating existing carts, set the Shopify cart attribute _rs_browser to the shopper's browser ID:

  • In the browser: Guard the call until the async script finishes loading, such as window.reportslayer?.browser?.(). It returns an empty string until consent is granted.
  • On your server: Read the first-party _rs_browser cookie (available after the shopper's first page load).

When the shopper reaches Shopify Checkout, Reportslayer reads _rs_browser from the cart attributes and connects the checkout and order back to the storefront's visit history. Because _rs_browser is saved as a standard Shopify cart attribute, it also appears in order additional details in Shopify.

Step 3: Send shopper events and customer identity

Because Shopify does not fire storefront events on custom sites, push shopper events from your storefront code using window.reportslayer:

window.reportslayer = window.reportslayer || []
reportslayer.push(['product_viewed', { product_id, variant_id, product_title, variant_title, sku, price, currency, url, image_url, event_id }])
reportslayer.push(['added_to_cart', { product_id, variant_id, product_title, variant_title, sku, price, currency, url, image_url, quantity, event_id }])
reportslayer.push(['identify', { customer_id, email, phone, country }])
reportslayer.push(['consent', { analytics: true, marketing: true, sale_of_data: false }])
  • Identify signed-in shoppers or email sign-ups: Push identify with the Shopify customer_id, email, phone (with country when the number has no calling code), or any combination of them. Emails and phone numbers are hashed in the browser before leaving the page, so plain values are never sent.
  • Product and cart events: Push product_viewed and added_to_cart for destinations like Klaviyo, Attentive, Postscript, Meta, and TikTok. Funnel events send for identified shoppers by default, and for anonymous browsers only when anonymous funnel sending is turned on for Meta or TikTok.
  • Skip checkout starts on Shopify Checkout: Shopify Checkout already fires checkout start events automatically using Shopify's event ID, so do not push checkout_started from your storefront or it will be counted twice. If you do push checkout_started on a custom checkout flow, it also accepts event_id.

Deduplicate funnel events with your own Meta or TikTok browser pixel

If your headless storefront runs its own Meta or TikTok browser pixel alongside Reportslayer's server-side funnel events, pass the exact event ID your browser pixel generated for that event as event_id (a string) on product_viewed, added_to_cart, or checkout_started:

  • Exact match for deduplication: Reportslayer forwards your event_id verbatim on the server-side event so Meta and TikTok match the server copy to your browser event and count it once.
  • Bundles and multi-line cart adds: Give each added_to_cart line its own event_id matching the corresponding browser pixel event.
  • Optional field: If you omit event_id, Reportslayer generates its own server-side ID, which will not match a custom browser pixel's ID.
  • Turn on server-side funnel events after wiring event_id: Ask Claude or ChatGPT to enable Meta or TikTok funnel events for your store once your storefront is passing event_id, or turn them on in Attribution → Destinations.

Optional: Keep returning Safari shoppers past 7 days

Safari deletes cookies written by JavaScript after 7 days, which causes returning Safari shoppers on a headless storefront to be counted as new visitors. To keep the browser ID past 7 days:

  • Append &cookie=server to the end of your storefront pixel script URL so the pixel does not overwrite _rs_browser from JavaScript.
  • Have the server that serves your storefront pages echo the _rs_browser cookie back in the response whenever a request already includes it.

For Next.js, add this to middleware.ts (replace yourstore.com with your store's domain):

// middleware.ts in Next.js
import { NextResponse, type NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
  const response = NextResponse.next()
  const browser = request.cookies.get('_rs_browser')?.value
  if (browser && /^[\w-]{8,64}$/.test(browser)) {
    response.cookies.set('_rs_browser', browser, { maxAge: 34560000, path: '/', domain: 'yourstore.com', sameSite: 'lax', secure: true })
  }
  return response
}

On any other server framework, respond to page requests that already carry an _rs_browser cookie with this header (replacing yourstore.com with your store's domain):

Set-Cookie: _rs_browser=<value>; Max-Age=34560000; Path=/; Domain=yourstore.com; SameSite=Lax; Secure

Because your server only echoes a cookie that the pixel already set, your consent banner still controls whether the cookie is created.

  • If your storefront uses Shopify's Customer Privacy API, load it before the storefront pixel script so the pixel follows its analytics, marketing, and data-sale permissions from the first page view.
  • If your storefront does not load Shopify's Customer Privacy API, only load the storefront pixel script after the shopper grants analytics and marketing consent, and push a consent command whenever their choice changes. When Shopify's Customer Privacy API is not present, the script also respects Global Privacy Control (GPC) for data-sale opt-outs.

Verify your setup

  • In Claude or ChatGPT: Ask your assistant to check your pixel activity and confirm that storefront visits, click IDs, and funnel events are coming in.
  • In the app: Open Attribution → Pixel and model to see live 7-day visits, orders seen percentage, and match key coverage. Orders seen updates after the next sync and builds up over a rolling 7-day window as new orders arrive.