A data layer is a JavaScript object on your website that holds the information your tracking tools need, such as page type, user status, product details and order totals, in a clean, predictable format. Your tags read from it instead of scraping the page.
In Google Tag Manager the data layer is an array called window.dataLayer. Your site pushes messages into it, and GTM listens. This guide covers how it works, what to put in it, and copy-ready GA4 ecommerce examples.
Why bother with a data layer?
Without one, tags pull data from the page itself: a CSS selector for the price, the URL for the page type, the confirmation text for the order ID. It works until someone changes the template, and then your revenue tracking silently breaks.
With a data layer:
- Tracking survives redesigns. Developers own one contract (the data layer), not dozens of CSS selectors.
- The data is correct at the source. Prices, IDs and currencies come straight from the backend, not from formatted text like
"$1,299.00". - One push feeds every tool. GA4, Google Ads, Meta, TikTok and your warehouse all read the same event.
- Debugging takes minutes. You open GTM Preview and see exactly what the site sent.
How dataLayer.push works
Every message is an object pushed into the array:
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'sign_up',
method: 'email'
});
Three rules cover most of what you need:
window.dataLayer = window.dataLayer || []first. It creates the array if GTM hasn't loaded yet, without wiping it if GTM already has. Never writedataLayer = []after GTM loads, because that disconnects GTM from the array.- The
eventkey is what triggers tags. A push witheventcan fire a Custom Event trigger in GTM. A push withouteventjust stores values for later. - GTM merges pushes into a running state. Each push updates GTM's internal data model, and later pushes overwrite earlier keys with the same name. That's why ecommerce objects need clearing (more on that below).
Is your GA4 & GTM setup collecting the right data?
Get a senior review of your tags, triggers, data layer and consent setup, with a prioritized fix list your developers can act on.
Page-level data: push it before GTM loads
Information that describes the page, like page type, language or logged-in state, should be pushed above the GTM container snippet. That way it's available to the very first tags, including your GA4 configuration:
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
page_type: 'product',
site_language: 'en',
user_logged_in: true,
user_id: 'u_83421' // internal ID only, never an email address
});
</script>
<!-- Google Tag Manager snippet goes here -->
In GTM, read each key with a Data Layer Variable (Variables → New → Data Layer Variable → name page_type). Then use it in tags and triggers, or send it to GA4 as an event parameter or user property.
Never put personal data in the data layer: no emails, phone numbers or names, not even hashed in a way that tags send to GA4. Google's terms forbid sending personally identifiable information to Analytics, and every vendor script on the page can read window.dataLayer.
Event data: push when something happens
User actions get a push at the moment they happen, with an event name:
// Newsletter form submitted successfully (after the server confirms)
window.dataLayer.push({
event: 'generate_lead',
form_id: 'newsletter_footer',
lead_type: 'newsletter'
});
Push after success, not on click. A form push that fires before validation counts every failed attempt as a lead.
GA4 ecommerce: the examples you'll actually use
GA4 has a fixed ecommerce schema. If you follow it exactly, the GA4 tag in GTM can send the whole ecommerce object automatically. Tick "Send Ecommerce data" and choose Data Layer as the source.
view_item (product page)
window.dataLayer.push({ ecommerce: null }); // clear the previous ecommerce object
window.dataLayer.push({
event: 'view_item',
ecommerce: {
currency: 'USD',
value: 129.00,
items: [{
item_id: 'SKU_12345',
item_name: 'Trail Running Shoe',
item_brand: 'Northpeak',
item_category: 'Shoes',
item_category2: 'Running',
item_variant: 'Blue / 42',
price: 129.00,
quantity: 1
}]
}
});
add_to_cart
window.dataLayer.push({ ecommerce: null });
window.dataLayer.push({
event: 'add_to_cart',
ecommerce: {
currency: 'USD',
value: 258.00, // price × quantity
items: [{
item_id: 'SKU_12345',
item_name: 'Trail Running Shoe',
item_variant: 'Blue / 42',
price: 129.00,
quantity: 2
}]
}
});
begin_checkout
window.dataLayer.push({ ecommerce: null });
window.dataLayer.push({
event: 'begin_checkout',
ecommerce: {
currency: 'USD',
value: 258.00,
coupon: 'WINTER10',
items: [ /* every item in the cart, same shape as above */ ]
}
});
purchase (order confirmation)
window.dataLayer.push({ ecommerce: null });
window.dataLayer.push({
event: 'purchase',
ecommerce: {
transaction_id: 'ORD-100482', // must be unique per order
currency: 'USD',
value: 232.20, // revenue as you define it, usually after discounts
tax: 19.35,
shipping: 0,
coupon: 'WINTER10',
items: [{
item_id: 'SKU_12345',
item_name: 'Trail Running Shoe',
item_variant: 'Blue / 42',
price: 116.10, // unit price after item-level discount
discount: 12.90,
quantity: 2
}]
}
});
The full funnel uses the same pattern for view_item_list, select_item, view_cart, remove_from_cart, add_shipping_info, add_payment_info and refund.
The seven mistakes we find in almost every audit
- Not clearing
ecommerce. Because GTM merges pushes, items fromview_itemcan leak into the nextadd_to_cart. Push{ ecommerce: null }before every ecommerce event. - Prices as strings with formatting.
"$1,299.00"is not a number. Send1299.00, without currency symbols or thousands separators. valuethat doesn't match the items. Decide whethervalueincludes tax and shipping, document it, and use the same rule on every event.- Missing
currency. GA4 drops revenue from events that havevaluebut nocurrency. - Duplicate purchases. The thank-you page reloads, or the user bookmarks it, and the purchase fires again. Fire
purchaseonce per order, either server-side or with a "sent" flag tied to thetransaction_id. Reusing the sametransaction_idhelps GA4 dedupe, but don't rely on it alone. - Two sources sending the same event. A platform integration (Shopify's Google channel, a WordPress plugin) and GTM both send
purchase, so revenue doubles. Pick one. - Inconsistent naming.
addToCart,add_to_cartandAdd To Cartall appear in the same property. Write a tracking plan, use GA4's recommended event names, and stick to snake_case.
How to check your data layer in 2 minutes
- GTM Preview mode: connect to your site, perform the action, and click the event in the left panel. The Data Layer tab shows exactly what was pushed and the merged state after it.
- Browser console: type
dataLayerand press Enter to see every push since the page loaded. - GA4 DebugView: confirms the event arrived in GA4 with the parameters you expect. See our GA4 DebugView troubleshooting guide.
Data layer vs. "the data layer" in other contexts
You'll see "data layer" used for other things too: the data link layer in networking (OSI layer 2), or the "data access layer" in software architecture. Those are unrelated. In web analytics, the data layer means exactly what's described here, the structured object your tag manager reads.
Get it right once
A clean data layer is the single highest-leverage fix in most tracking setups. Every downstream report, audience and bid strategy depends on it. If you'd like a second pair of eyes, our GA4 & GTM audit checks every push against the GA4 schema and gives your developers a prioritized fix list.