Analytics & measurementIntermediate

Data layer

A data layer is a JavaScript object that passes structured information, like events and page details, from your site to tag managers and analytics tools.

Also called: dataLayer

A data layer is a shared JavaScript array, usually window.dataLayer, where a website pushes structured information for tags to read. Instead of scraping values from the page's HTML, Google Tag Manager and other tools listen to the data layer for events and variables. It separates what the site knows (plan selected, form submitted, user logged in) from how marketing tools use it, which makes tracking more reliable when designs change.

How it works

The site pushes objects onto the array. Each push can carry an event name, which GTM can use as a trigger, and any number of key-value pairs, which GTM reads as data layer variables.

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
  event: 'trial_start',
  plan: 'team',
  billing_period: 'annual'
});

What to put in it

Push values the page knows that tags cannot reliably read from the markup.

  • Page context: page type, content category, author
  • User context: logged-in state, plan tier, internal user ID (never email or name)
  • Actions: form submitted successfully, trial started, plan changed
  • Ecommerce: items, values and currency for checkout events

Keeping it reliable

Document the data layer like an API: event names, keys, types and when each push happens. Push on success, not on button click, so a failed form does not count as a lead. For ecommerce objects, clear the previous object before pushing a new one so values do not carry over.

Example

Surviving a redesign

Imagine Brackenlight, an e-signature tool, used GTM click triggers on a button class for its sign-up tracking. A redesign renamed the class and sign-ups vanished from GA4 for two weeks. After moving to a sign_up push in the data layer, triggered when the account is created, the next redesign changed nothing in tracking.

Common mistakes

Overwriting the array

Writing window.dataLayer = [...] after GTM loads breaks it. Always use the || [] pattern and push.

Sending personal data

Emails and names in the data layer can leak into third-party tags. Use internal IDs.

Questions

Not strictly, but it is the most reliable way to send custom events and parameters, especially through GTM.

Know the terms. Now win the searches.

We find the searches your buyers make, then build, publish and improve the pages that win them.