Web Pixel Extensions
Web Pixels listen for customer-events (page_viewed, product_viewed,
product_added_to_cart, checkout_started, checkout_completed). They
are how analytics and marketing apps observe customer behavior without
direct DOM access to the store theme.
Runtime sandbox
Pixels run in a sandboxed runtime with no access to the host page — depending on where they run:- Storefront — the pixel runs in a dedicated Web Worker (created
from a Blob via
new Worker(url)). A Worker has no DOM, no cookies and nodocumentat all; the pixel script is loaded withimportScripts. - React checkout / cart / account — the pixel runs in an
srcdociframe loaded withsandbox="allow-scripts"(noallow-same-origin).
- Read or write cookies on the parent page.
- Read or write
localStorage/sessionStorageon the parent. - Make same-origin requests against the store.
- Read the parent DOM.
fetch, with CORS or mode: 'no-cors').
Event delivery is via postMessage from the host to the pixel runtime —
your pixel subscribes to a stable event API documented below.
Manifest
Declare Web Pixels underextensions.webPixels[] in your app’s
app.json (same manifest file as every other extension type):
*One of
scriptSrc or inlineScript is required. A pixel with neither
is never loaded.
Pixel runtime API
The sandbox exposes two globals:register and analytics. Both work
identically on both runtimes (storefront Worker and checkout iframe).
register(callback) — recommended
register hands your callback an object with the event bus and the
merchant’s saved settings for this pixel:
Semantics:
- The callback runs synchronously, before the host delivers any event — subscriptions made inside it never miss an event.
- If the callback throws, the error is caught and the sandbox keeps
running. On the iframe runtime the host also receives an
LMS_PIXEL_ERRORmessage withreason: 'register-callback-threw'. - Calling
registerwith a non-function is a safe no-op. - Calling
registermore than once is allowed; each callback runs once.
analytics.subscribe(eventName, cb) — top level
Subscribing at the top level of your script also works, and remains fully
supported. It has no access to configuration:
settings/init argument
and no browser proxy — the sandbox is a Web Worker (storefront) or a
same-origin-blocked iframe (checkout), so no host-page storage exists.
The event object
Every subscriber receives the same object on both runtimes:data holds the event’s payload (the fields listed below). For pixels
written against the bare payload, the payload’s fields are also copied to
the top level of the object (event.product equals event.data.product).
clientId is a random per-browser id; it is null where storage is
unavailable.
Event catalogue
Payloads use the shapes listed above, so a pixel can subscribe to event
names and read the listed fields without further translation.
For the full cart after an add, subscribe to
cart_updated — like
Shopify’s, product_added_to_cart is about the added line. The storefront
starts watching the theme’s cart calls once a pixel subscribes to a cart
event (pixels start when the page is idle), so a cart call made in the
first moments of a page load is not reported.
Installation
The pixel manifest (and anyinlineScript) is deployed by the platform
when the app is installed. Like Shopify’s webPixelsConfigList, the list
of the store’s pixels (with their settings) is written into each storefront
page — there is no separate request for it. Installing, uninstalling or
publishing a new version of your app refreshes the store’s pages, so a new
pixel receives events on the next page and an uninstalled one stops on the
next page. A change to a pixel’s settings reaches the storefront within
about a minute.
An inlineScript is served from the store’s domain at a content-versioned
URL (…/web-pixels/<handle>.js?v=<hash>) with a one-year cache; a new
version of the script gets a new URL.
Best practices
- Prefer
register. It is the only way to read the merchant’ssettingsfrom inside the sandbox. Reach for top-levelanalytics.subscribeonly when your pixel needs no configuration. - Don’t bake merchant config into
scriptSrc. One script is served to every store that installs your app; per-store values belong insettingsand arrive asconfiguration. - Send with
fetch(..., { keepalive: true }). The pixel runtime may be torn down during navigation. (navigator.sendBeaconis not available inside a Worker.) - Don’t expect cookies to persist between visits. The pixel runs in a Worker (storefront) or a sandboxed iframe (checkout) with no host-page storage.