Free SEO audit for new clients — claim yours
Home/Blog/Analytics/The Data Layer: What Marketers Need to Know
Analytics

The Data Layer: What Marketers Need to Know

The Data Layer: What Marketers Need to Know
Table of contents
  1. The data layer as a contract between your website and your tags
  2. Why scraping the page for tracking data keeps breaking
  3. Events worth standardising for lead generation and ecommerce
  4. Writing a data layer specification developers can build from
  5. Reading data layer values in Google Tag Manager
  6. Testing and debugging the data layer
  7. Worked example: specifying tracking for a roofing company's quote form
  8. Keeping the data layer from decaying after launch
  9. Questions marketers ask about the data layer
    1. Do I need a data layer if I use Google Tag Manager?
    2. Who should write the data layer specification?
    3. Can I put email addresses or phone numbers in the data layer?
    4. Why does my GTM variable show undefined?
    5. Does a data layer slow down my website?
  10. Briefing your developers on a data layer this month
  11. Further reading on data layer
  12. Sources for this data layer guide

The tracking worked perfectly at launch. Then the developers redesigned the checkout button, renamed a CSS class and moved the order total into a different element, and nobody told marketing. Three weeks later, someone notices that GA4 purchases have dropped to zero while the bank account says sales are fine. Google Ads has been bidding blind the whole time.

This happens because a lot of tracking is built by reading things off the page: clicking on a button with a certain class, scraping a price from the HTML, assuming a thank-you URL. A data layer replaces that guesswork with a deliberate agreement: the website publishes the facts that matter (a lead was submitted, an order was placed, for this value, with these products), and the tags read them from one stable place.

You don't need to be a developer to benefit from a data layer, but you do need to know what to ask for. This guide explains the concept in plain language, shows why page scraping breaks, lists the events worth standardising for lead generation and ecommerce, gives you a specification template your developers can build from, and covers how to use and test the values in Google Tag Manager.

The data layer as a contract between your website and your tags

Technically, the data layer is a JavaScript array on your web pages, by convention called dataLayer. The website adds messages to it; Google Tag Manager (GTM) listens for those messages and makes their values available to your tags. A typical message looks like this:

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
  event: 'generate_lead',
  form_name: 'quote_request',
  lead_type: 'roof_repair',
  value: 150,
  currency: 'GBP'
});

The first line makes sure the array exists without wiping anything already in it. The push adds a message. The event key is special: GTM treats it as something that happened, which tags can fire on. The other keys are data that tags can read.

It helps to think of it as a contract with three parties:

  • The website (developers) promises to push specific messages, with specific keys and value formats, at specific moments.
  • The tag manager (analysts or marketers) promises to read only from those keys, not from the page's HTML.
  • The specification document is the contract itself, listing every event and key, owned by someone, and versioned.

The point of the contract is independence. Developers can redesign pages freely as long as the pushes stay the same. Marketers can add a new ad platform tag without touching website code, because the data it needs is already published. If you're new to GTM itself, our Google Tag Manager guide covers containers, tags, triggers and variables first.

Why scraping the page for tracking data keeps breaking

Most broken tracking we see in audits comes from triggers and variables that depend on how the page looks rather than on what happened. Each method below works on launch day and fails on a predictable future day.

Scraping methodWhat breaks itData layer alternative
Click trigger on a CSS class like .btn-submitRedesigns, CSS refactors, A/B test variantsPush generate_lead after the server confirms success
Thank-you page URL as the conversionPage renamed, forms switched to inline success messages, page bookmarked and revisitedPush the event when the submission succeeds, wherever it happens
DOM element variable for order valuePrice moved, currency symbol added, discount line insertedPush value as a number from the order system
Form submit triggerFires on failed validation, misses AJAX formsPush only on confirmed success
Reading text such as product names from headingsCopy edits, translationsPush stable item_id and item_name
Custom JavaScript scraping cookies or URLsConsent changes, cookie renames, URL restructuresPush the needed value explicitly

The thank-you page approach deserves special mention because it is so common. It double counts when people refresh or bookmark the page, misses conversions when developers switch to inline confirmation messages, and carries no information about what was submitted. Our guide to tracking form submissions in GA4 and GTM covers the alternatives in detail; a data layer push after server-side success is the most robust.

Scraping also creates a hidden dependency on the CSS and JavaScript the page uses, which is exactly the kind of thing A/B tests change. If variant B moves the form, scraped tracking can make one variant look better simply because its conversions are counted differently.

Events worth standardising for lead generation and ecommerce

Don't invent names if a standard exists. GA4 has recommended event names for common actions, and using them unlocks built-in reports and makes your data easier for any future analyst to understand. The table below covers the core set most businesses need.

EventWhen to push itKey parameters
generate_leadA lead form, quote request or callback request succeedsform_name, lead_type, value, currency
sign_upAn account or free trial is createdmethod, plan
loginA user logs inmethod
view_itemA product page loadscurrency, value, items
add_to_cartAn item is added to the basketcurrency, value, items
begin_checkoutCheckout startscurrency, value, items, coupon
purchaseAn order is confirmedtransaction_id, value, currency, tax, shipping, items
Custom: phone_click, booking_completeBusiness-specific actionsWhatever your reporting needs, documented in the spec

For lead generation, also plan for what happens after the form: GA4 supports lead lifecycle events (such as qualifying and converting leads) that you can send later from your CRM. That's how you connect a form submission to revenue. See our guides to offline conversion tracking and getting UTM data into your CRM for that side.

For ecommerce, follow the GA4 ecommerce structure exactly: an ecommerce object containing values and an items array. Our GA4 ecommerce tracking guide covers the reports that depend on it.

Lead-generation sites should also decide early whether to capture ad click identifiers into hidden form fields, because offline conversion imports match on them. Google describes the identifier that auto-tagging adds:

“URLs that are auto-tagged with Google Ads parameters include the GCLID parameter.”

— Google Analytics Help, Traffic-source dimensions, manual tagging, and auto-tagging

Writing a data layer specification developers can build from

Developers can't build "track the leads". They can build a precise list of events, keys, value types and timing. A good specification takes an afternoon and saves weeks of back-and-forth.

  1. List the business questions. Start with what you need to report: leads by type and source, revenue by product category, trial sign-ups by plan. Every event should trace back to a question.
  2. Map questions to events. Use GA4 recommended event names wherever possible.
  3. Define each key. Name, data type (string, number, boolean, array), allowed values, example, and whether it's required.
  4. Define the trigger moment precisely. "After the server returns success", not "when the button is clicked".
  5. Define page-level data. Values that should be available on every page load, such as page_type, logged_in or content_group, pushed before the GTM container loads.
  6. State the privacy rules. No names, email addresses, phone numbers or other personal data in the data layer unless there's a specific, approved purpose (such as hashed data for enhanced conversions) handled correctly.
  7. Add acceptance tests. For each event, describe how to trigger it and what should appear in GTM preview mode.
  8. Version and own it. Give the document a version number and an owner; any change goes through them.

A spec entry can be as simple as this:

FieldExample entry
Eventgenerate_lead
Fires whenQuote form submission returns HTTP 200 from the server and the success message is shown
form_nameString, required. One of quote_request, callback, contact
lead_typeString, required. Service selected in the form, lowercase with underscores, e.g. roof_repair
valueNumber, optional. Estimated lead value from the lookup table, no currency symbol
currencyString, required if value set. ISO 4217 code, e.g. GBP
Must not includeName, email, phone, address, free-text message
Acceptance testSubmit a test quote; in GTM preview, a generate_lead message appears once with all required keys

Include one or two complete code examples in the spec as well. Developers appreciate seeing exactly what the push should look like, and it removes ambiguity about nesting and data types.

Reading data layer values in Google Tag Manager

Once the website pushes messages, GTM needs three things to use them: variables that read the keys, triggers that fire on the events, and tags that send the data on.

  1. Create Data Layer Variables. In GTM, go to Variables → New → Data Layer Variable. Enter the key name exactly as pushed, for example lead_type. For nested values use dot notation, for example ecommerce.transaction_id. Key names are case sensitive.
  2. Create Custom Event triggers. Triggers → New → Custom Event, with the event name exactly as pushed, for example generate_lead.
  3. Build the GA4 event tag. Use a Google Analytics: GA4 Event tag, set the event name, and map parameters to your Data Layer Variables. For ecommerce, the tag can read the standard ecommerce object directly.
  4. Reuse the same trigger for ad platform tags. Google Ads conversions, Meta and LinkedIn tags can all fire on the same generate_lead event and read the same value, so every platform counts the same thing.
  5. Register custom parameters in GA4. Parameters like lead_type only appear in GA4 reports after you create custom dimensions for them in the GA4 admin.

One behaviour catches people out. GTM keeps a running internal model of everything pushed on the page, and a value stays there until it's overwritten. If one push includes ecommerce data and a later push doesn't, the later tag may read stale values from the earlier one. That's why the standard ecommerce pattern clears the object first:

window.dataLayer.push({ ecommerce: null });
window.dataLayer.push({
  event: 'purchase',
  ecommerce: {
    transaction_id: 'T-10482',
    value: 129.00,
    currency: 'GBP',
    items: [{ item_id: 'SKU-221', item_name: 'Trail jacket', price: 129.00, quantity: 1 }]
  }
});

The same consistency matters for campaign data. If your site captures UTM values into the data layer for hidden form fields, keep them in the format GA4 expects; Google is explicit that parameter values are case sensitive, so normalise them before pushing.

“Parameter values are case sensitive, e.g. utm_source=google is different from utm_source=Google.”

— Google Analytics Help, URL builders: Collect campaign data with custom URLs

Testing and debugging the data layer

Test every event before release and again after any site change that touches the relevant pages. GTM's preview mode (Tag Assistant) is the main tool.

  1. Open GTM, click Preview, and enter the page URL. A debug session opens alongside your site.
  2. Perform the action: submit the form, add to basket, complete a test purchase.
  3. In Tag Assistant, select the event in the left-hand timeline and open the Data Layer tab to see exactly what was pushed and the resulting state.
  4. Check the Variables tab to confirm each Data Layer Variable resolved to the expected value, with the right type (a number, not a string with a currency symbol).
  5. Check the Tags tab: the right tags fired once, and nothing fired that shouldn't.
  6. Open GA4 DebugView and confirm the event arrived with its parameters.
  7. Repeat on mobile and in a second browser, and with consent both accepted and declined, to confirm behaviour matches your consent setup.

The bugs that show up most often:

  • The push happens on button click rather than on confirmed success, so failed submissions count.
  • The event fires twice because both a page script and a plugin push it.
  • value arrives as a string such as "£129.00", so GA4 records no revenue.
  • Key names differ in case between spec and code (transactionId versus transaction_id).
  • Page-level data is pushed after the GTM container loads, so the first page view tag can't see it.
  • Personal data slips into a key, often from a form field captured "just in case".
  • On single-page sites, virtual page views aren't pushed, so only the first page is tracked.

Make a test checklist from your spec's acceptance tests and run it after every release. Some teams automate this with browser tests that check the data layer contents; even a manual monthly run catches most regressions before they cost money. Data layer checks also belong in every GA4 audit.

Worked example: specifying tracking for a roofing company's quote form

This example is hypothetical. A roofing company runs Google Ads and Meta campaigns to a quote form. Tracking currently fires on the thank-you page, which shows 210 conversions last month. The CRM recorded 164 quote requests. The gap comes from refreshes, bookmarked thank-you pages and a contact form that also redirects to the same page.

The marketing manager writes a one-page spec: a generate_lead push on server-confirmed success, with form_name (quote or contact) and lead_type (repair, replacement, gutter), and an estimated value from a simple table (repair 120, replacement 400, gutter 60). The developer implements it in two hours. In GTM, one Custom Event trigger feeds the GA4 event tag, the Google Ads conversion tag (filtered to form_name equals quote) and the Meta tag.

The next month, GA4 shows 171 quote leads against 168 in the CRM; the small difference is consent and ad blockers. Google Ads now optimises towards value, so replacement leads count more than gutter enquiries, and the team can compare cost per lead by lead type. Nothing about the campaigns changed except the quality of the signal they learn from, which is also the foundation that enhanced conversions and profit-based bidding build on.

Keeping the data layer from decaying after launch

A data layer is only as reliable as the process around it. A few habits keep it healthy:

  • Put the spec where developers work, such as the code repository or the team wiki, not in a marketer's inbox.
  • Add data layer checks to the release checklist for any change touching forms, checkout or templates.
  • Version changes. When you add a key, note the date so analysts know when data starts.
  • Review quarterly. Remove events nobody uses; unused tracking still costs page performance and attention.
  • Treat privacy as a requirement, not a review. Check every new key against your privacy policy; our guide to marketing privacy laws and the Consent Mode v2 guide cover the obligations and the consent signals tags must respect.
  • Consider server-side tagging once the data layer is stable; a clean data layer makes server-side tagging much easier to adopt.

Questions marketers ask about the data layer

Do I need a data layer if I use Google Tag Manager?

You can use GTM without one, but anything important (leads, purchases, values) should come from the data layer. Scraped triggers and DOM variables are fine for minor engagement tracking but break too easily for conversions.

Who should write the data layer specification?

The person responsible for measurement, usually a marketer or analyst, writes the business requirements and event list. Developers review it for feasibility and implement it. One named owner should approve every change.

Can I put email addresses or phone numbers in the data layer?

Generally no. Google Analytics policies prohibit sending personal data, and anything in the data layer is visible in the browser. Specific features such as enhanced conversions handle user data in defined ways; follow their documentation and your privacy policy.

Why does my GTM variable show undefined?

Usually the key name or nesting does not match the push exactly (names are case sensitive), the push happens after the tag fires, or the value was never pushed on that page. Check the Data Layer tab in preview mode at the moment the tag fires.

Does a data layer slow down my website?

The data layer itself is just a small JavaScript array and has negligible impact. Performance costs come from the tags that read it, so keep the container lean.

Briefing your developers on a data layer this month

List your three most important conversions, write one spec entry for each using the template above, and ask developers to push them on confirmed success. Rebuild the matching GTM triggers on Custom Events, test in preview mode, and retire the old click and thank-you page triggers once the new ones match your CRM.

If you'd like a data layer specification written for your site, or an audit of tracking that has drifted, our PPC management and conversion optimization teams do this routinely. Get a quote to discuss your tracking.

Further reading on data layer

Sources for this data layer guide

  1. Google Analytics Help: URL builders – Collect campaign data with custom URLs
  2. Google Analytics Help: Traffic-source dimensions, manual tagging, and auto-tagging
  3. Google Analytics Help: Set up cross-domain measurement
  4. Google Analytics Help: Custom channel groups
Rohit Choudhary
Written byRohit ChoudharyCo-Founder, My SEO Experts

Rohit Choudhary is the co-founder of My SEO Experts and has more than 14 years of hands-on experience in SEO, paid advertising and digital marketing. Since starting out in 2011 he has helped hundreds of businesses grow through practical, data-driven search strategies, and he also serves as a consulting Digital Marketing Director for several US-based companies.

Keep reading

Related articles

Bot Traffic in Analytics: How to Spot and Filter It
Analytics

Bot Traffic in Analytics: How to Spot and Filter It

Spam bots, scrapers and headless browsers inflate traffic and distort conversion rates. This guide shows how to recognise bot traffic in GA4, what Google filters for you, and how to clean your reports and protect your forms.

Email Open Rates After Apple Mail Privacy: What to Measure Instead
Analytics

Email Open Rates After Apple Mail Privacy: What to Measure Instead

Apple Mail Privacy Protection preloads tracking pixels, so open rates now overstate engagement and break open-based automations. This guide explains what an open means today and which metrics, tests and hygiene rules to use instead.

Ready to grow your business online?

Get a free, no-obligation proposal tailored to your goals and budget.

CallFree Quote