A data layer is a JavaScript object on a website that holds information about the page and user interactions so that tags can read it. In Google Tag Manager and gtag.js it is an array called dataLayer, and the site adds data to it with dataLayer.push().
How does the data layer work?
The site pushes key-value pairs into the array, such as a page type, a product ID, or a login state. Tag Manager reads these values as data layer variables and can use them in tags and triggers. A push that contains the key event can fire a trigger.
Google documents that Tag Manager processes messages first-in, first-out, one at a time in the order they arrive. Values persist only while the visitor stays on the current page, so anything needed on every page has to be pushed again on each page load. Variables that should be available when the container loads belong above the container snippet.
For SEO teams, the data layer is often where analytics gets page types or conversion data for organic traffic (assessment).
Example
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'pageType': 'product',
'event': 'view_item'
});
</script>
The first line reuses an existing dataLayer array instead of replacing it.
Common mistakes
- Assigning instead of pushing.
dataLayer = [...]overwrites values that are already there. - Wrong capitalization. The object name is case-sensitive, so
datalayeris a different variable. - Unquoted keys. Google asks for variable names in quotes, for example
'new-variable'. - Inconsistent names. The same information should use the same variable name on every page.
Sources
- The data layer – Google Tag Manager, Google for Developersdevelopers.google.com
Last updated: