Data layer
Definition
The data layer is a JavaScript object (window.dataLayer) that serves as an interface between the website and the tag management system. It centralizes all contextual information (current page, displayed product, login status, funnel step) in a structured format that GTM can read and use to fire the appropriate tags.
How it works
The data layer is a JavaScript array into which the site “pushes” objects via dataLayer.push(). Each push can contain variables (static page information) and/or an event (a user-triggered action). GTM listens for these pushes and uses them as triggers and data sources for tags.
Practical example
On an e-commerce product page, the data layer typically contains the product name, price, category and availability. When the user adds the product to cart, a dataLayer.push with the add_to_cart event and product details is fired. GTM captures this event and sends the data to GA4 and advertising platforms.
Why it matters
Without a data layer, tracking relies on DOM scraping (reading HTML elements from the page), a fragile method that breaks with every design change. A well-structured data layer is the foundation of a reliable analytics setup.