dataLayer
Definition
The dataLayer is a global array that page code pushes events and context into and a tag manager reads from. It's a convention rather than a browser API, defined by Google Tag Manager, and it exists so that page code and marketing tags can be deployed and changed independently of one another.
Documented at Google's tag platform, the data layer.
How We've Used It
The rule we work to is that the theme owns the events and the tag manager owns the tags. Page context, meaning template, market, locale, signed-in state and loyalty tier, is seeded from Liquid in the head before any script, so the first push isn't a guess. Commerce events come from one place rather than every button: where six components each call the cart API directly, we've wrapped fetch once and reported the add from there, which is the only way to catch the ones rendered after a section re-render. Sign-in and sign-up events are read from redirect parameters and then stripped from the address, so a refresh can't fire them twice.
Builds Using It
- GA4 ecommerce dataLayer instrumentation across a Rebuy cart — Three Ships
- Server-rendered page-context dataLayer, declared in the head and pushed at browser idle — Three Ships
- Homepage hero carousel v2 - video and split-image slides with per-slide typography control — Nudestix
- Swell VIP tier and email-subscriber segmentation resolved in Liquid — Three Ships
- Title-matched mega menu with a full-depth link-list fallback — Nudestix
- Grouped shade swatch picker with shade-finder dialog and GA4 shade-finder events — Nudestix
One further gated build uses it; its page follows.
Related Entities
- Customer accounts — the segmentation values it publishes.
- Cart AJAX API — the calls commerce events are derived from.
- content_for_header — the rest of what loads in the head.