Customer Impact

Data & Tracking

Data layer: what it is and how to set it up for GTM and GA4

Copy for AI

A data layer is a JavaScript object that tells Google Tag Manager what happened.

Your website lays out structured information in it for GTM and the Google tag. GTM no longer has to guess from buttons and texts.

Instead, your site says it directly: “a quote request was submitted, for service X, via form Y”.

The verdict: if leads or revenue matter to you, track them on a data layer. It’s more reliable, easier to test and it survives changes to your site.

Below: how it works, how to set it up in six steps and which mistakes to avoid.

What is a data layer?

It’s a shared list your website writes to and Tag Manager reads from.

In its documentation for Tag Manager, Google calls it “an object used by Google Tag Manager and gtag.js to pass information to tags”.

Your website puts data and events into that object. GTM reads them via variables and uses them in triggers and tags.

What a push looks like

In code, it’s a list (array) named dataLayer. Every time something happens, you add an object with dataLayer.push().

Here’s an example for a B2B quote form:

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
  'event': 'lead_submit',
  'form_id': 'quote',
  'lead_type': 'quote_request',
  'service': 'tracking-audit'
});

The event key has a special role: GTM uses it to fire tags.

The other keys become variables you can pass on to GA4, Google Ads or Meta.

Where it sits in your setup

The data layer sits between your website and your tags, like a contract:

ARCHITECTURE The data layer as a contract 01 Website CMS, form, shop 02 dataLayer event + parameters 03 Tag Manager variables, triggers 04 Platforms GA4, Ads, Meta The website says what happened, Tag Manager decides where it goes
The data layer sits between your website and your tags: the website says what happened, GTM decides where it goes.

The website owns what happened. Tag Manager owns where it goes.

Why use a data layer?

Because tracking on what GTM sees on the page breaks the moment the page changes.

The problem with tracking on the page

Without a data layer, you build tracking on page details, such as:

  • a click on a button with a certain text
  • a CSS class
  • a thank-you page URL

That works until someone changes the button text, swaps the form plugin or removes the thank-you page.

Then your measurement stops, often without anyone noticing.

DOM tracking vs data layer tracking

Here’s the difference side by side:

Tracking on the page (DOM)Tracking via the data layer
Source of the dataButtons, texts, CSS classes, URLsExplicit pushes from the website
Sensitive to a redesignHighLow, as long as the push stays
Extra informationOnly what is visibleInternal values too: service, lead type, order value
Testable and documentableDifficultYes, it is a fixed contract
Work for developersNoneOne-off, plus maintenance

Rule of thumb: a site without a thank-you page needs a data layer even more. Our guide to form conversions without a thank-you page shows why.

Two cases where it made the difference

  • Suivo: a form update silently stopped lead measurement, with no error message. The fix was a robust form listener and a clean layer of events, so the next update can’t break it. Read the Suivo case.
  • Kaizo: the booked demo got its own event in the data layer. So GA4, Google Ads and Meta see exactly the same conversion. Read the Kaizo case.

Setting up a data layer in 6 steps

Plan first, code second, test always. Here are the six steps in order.

1. Write a measurement plan

First decide which actions you want to measure and which information belongs with them.

For a B2B site that’s often:

  • a quote request
  • a demo booking
  • a contact form
  • a download
  • a click on a phone number

For each event, record the name and the parameters. For example lead_submit with form_id, lead_type and service.

Where possible, use the recommended GA4 events, so your reports and Google Ads can work with them straight away.

2. Write a specification for your developer

For each event, document three things:

  • When: only after a successful submission, not on the button click
  • Keys: which keys the push contains
  • Values: which values are allowed

Do you use a CMS or CRM forms? For example WordPress with Gravity Forms, HubSpot, or forms linked to Teamleader or Odoo.

Then also note where the push technically comes from.

3. Declare the data layer before the GTM snippet

Google recommends declaring the object before Tag Manager loads, with window.dataLayer = window.dataLayer || [];.

That way you never overwrite data that was already there.

Watch out: never write dataLayer = [...] further down in your code. You’d wipe everything that was in it before.

4. Have the website push the events

The developer adds the pushes at the agreed moments.

For e-commerce, follow the structure of GA4 e-commerce. Google first clears the previous e-commerce object there, so old product data doesn’t piggyback on the next event:

window.dataLayer.push({ ecommerce: null });
window.dataLayer.push({
  'event': 'purchase',
  'ecommerce': {
    'transaction_id': 'T_12345',
    'value': 72.05,
    'currency': 'EUR',
    'items': [{ 'item_id': 'SKU_1', 'item_name': 'Product', 'price': 72.05, 'quantity': 1 }]
  }
});

For a purchase, four fields always belong in it: transaction_id, value, currency and items.

A missing currency is a classic. Revenue then arrives incorrectly or not at all.

5. Build variables and triggers in Google Tag Manager

In GTM, build two things per event:

  • a variable of the “Data Layer Variable” type for each key, for example form_id
  • a trigger of the “Custom Event” type with exactly the same name as in the push

Then connect them to your tags: a GA4 event, a Google Ads conversion, a Meta event.

In GTM, the variable setup looks like this:

The Google Ads end of the chain is a conversion action. Here’s one set as Primary in our own account:

Google Ads conversion action settings for the GA4 contact form on customerimpact.be, set as Contacts, Primary action, the conversion a data layer lead event feeds The contact form conversion action in our own Google Ads account, October 2026. It’s set as primary, so campaigns optimise on real enquiries.

Read more about the container itself in our article on Google Tag Manager, and about the ads side in Google Ads conversion tracking.

6. Test and document

Open preview mode and check in Google Tag Assistant, on the Data Layer tab. Does the push arrive at the right moment, with the right values?

Look for your event name and its keys:

For a quick look, use your browser’s console: type dataLayer and you see the full list.

In our projects we record the result in a shared document. Whoever works on the site later then knows which pushes mustn’t be removed.

The key rules for a clean data layer

Five rules keep it clean:

  • Case-sensitive. The object is called dataLayer, not datalayer. Your keys and event names must also match exactly what GTM expects.
  • Use quotes around keys. Google recommends {'variable_name': 'value'}.
  • Per page. According to Google, such a variable only persists while the visitor stays on the current page. Need a value on every page? Push it again on every page load.
  • One data layer per page. You can rename the object, but Google supports only one dataLayer object per page.
  • Consistent names. Choose one naming convention, for example lowercase with underscores, and stick to it across all events.

For example: lead_submit, demo_booked and file_download follow one convention. leadSubmit, Demo-Booked and download don’t.

No personal data in your data layer to GA4

It’s tempting to push a lead’s email address or phone number along.

For GA4 that’s not allowed. Google’s policy on personal data says you mustn’t send data Google could recognise as personally identifiable information, including email addresses and phone numbers.

The allowed route for enhanced conversions

Want to use that data for enhanced conversions in Google Ads? That happens via the dedicated, hashed route, and only with consent.

Without consent, your tags may not pass the data on with cookies, even if it’s ready in the object.

More in consent mode v2 for Google Ads. A consent tool such as Cookiebot sets that state first; see our Cookiebot setup guide.

Common mistakes with the data layer

These five come up again and again:

  • The push happens on the click, not after the submission. Every failed or half-completed form then counts as a lead.
  • The object is overwritten after the GTM snippet. A dataLayer = [] in a theme or plugin wipes everything that was there.
  • Different names in the push than in GTM. leadSubmit in the code, lead_submit in the trigger: the tag never fires.
  • Old e-commerce data that lingers. Without ecommerce: null, a purchase can carry products from a previous step.
  • Single-page apps without virtual page views. A site in React or Vue doesn’t load new pages, so push an event yourself on every route change.

Similar problems on the GA4 side are covered in common mistakes in Google Analytics tracking.

Frequently asked questions

What is the difference between a data layer and Google Tag Manager?

The data layer is the information your website lays out. Google Tag Manager is the tool that reads it and decides which tags fire and what they pass on. The object also works without GTM, with the Google tag (gtag.js), but only reaches its full potential with GTM.

Do I need a developer for a data layer?

For the pushes from your website, usually yes, or a plugin that generates them for your CMS or webshop. A marketer or tracking specialist can set up the variables, triggers and tags in GTM themselves.

How do I view the data layer in Chrome?

Open the developer tools, go to the console and type dataLayer. You then see all pushes on that page. GTM preview mode gives a clearer overview, with a Data Layer tab that shows per moment what came in.

What is a datalayer.push?

dataLayer.push() is the function you use to add a new object to the data layer. If that object contains an event key, GTM can fire a trigger on it.

Does the data layer have anything to do with a data warehouse?

No. In data engineering, people also talk about “data layers” (for example bronze, silver and gold in a data warehouse). That’s a different concept. In marketing tracking, we mean the dataLayer object on your website.

Want a data layer set up for you?

A well-designed dataLayer is the foundation under your GA4, Google Ads, Meta and server-side tagging.

Our tracking specialist writes the measurement plan and the developer spec, builds the container and tests every event.

Not sure where your setup stands? Start with a tracking audit. The bigger picture is in conversion tracking, or get in touch and we’ll review your current events first.

Free website scan

Enter your website and get an automatic scan within minutes, with concrete technical and SEO improvements. No sales pitch.

Where should we send your report?

We only use your details for your scan. No spam, unsubscribe anytime.