Tags need information: which plan someone chose, what the order was worth, whether the person is signed in. Without a data layer, each tag digs that information out of the page itself, reading a price from a heading or a product name from a button. When the design changes, those tags break. A data layer separates the two: your site writes the facts into one agreed place, and every tag reads them from there.
How Google Tag Manager's data layer works
Google's data layer documentation (opens in a new tab) describes it as an object that Tag Manager and gtag.js use to pass information to tags. It is created once, as high on the page as possible, without overwriting any existing one:
window.dataLayer = window.dataLayer || [];Your code then pushes facts and events into it:
dataLayer.push({ event: 'signup', plan: 'pro' });In Tag Manager, a Custom Event trigger can listen for signup, and Data Layer Variables can read plan. Values pushed before the container snippet are available as soon as it loads.
Google's own cautions are worth repeating:
- The name is case-sensitive:
dataLayer, notdatalayer. - Use the same key for the same thing on every page (
visitorTypeeverywhere, notvisitor_typeon some pages). - Add with
push(). Assigning a new array discards what was there. - Keep one data layer per page.
Designing a data layer
Write down the keys before anyone writes code. A simple plan names each event (signup, checkout_started, purchase), the properties each one carries, and who is responsible for pushing it. Treat it like an interface between your developers and whoever manages tags. Many teams keep the plan in a short shared document.
Using a data layer with Reveata
Reveata does not need a data layer. Its script records page views on its own, and you send goals with reveata('goal', …) or HTML attributes (Custom goals).
If your site already has a data layer and Google Tag Manager, you can reuse it. The Reveata Analytics tag template loads the script, and further tags of the same template can Track a goal or Identify the visitor from any trigger. For identify, fill the user id, and optionally email and name, from your data layer variables (Install with Google Tag Manager). A signup event in the data layer can then fire a Reveata signup goal with the plan as a property.
Pitfalls
- Personal data. A data layer is readable by every tag on the page, including third-party ones. Do not push names, emails or other personal data unless each tag that reads it is allowed to have it.
- Timing. A push after a tag fires does not reach that tag. Push the facts before the event that triggers it.
- Drift. Data layers decay when nobody owns them. Review them when you change checkout or sign-up flows.