askvity

What is a data layer variable?

Published in GTM Data Variables 3 mins read

A data layer variable is a tool that lets you extract specific values from the data layer and use them as variables in your tags, triggers, and other variables. In essence, it bridges the gap between the data stored in the data layer and the actionable elements within your Google Tag Manager (GTM) setup.

Understanding the Data Layer

Before diving into data layer variables, let's quickly look at the data layer itself:

  • The data layer is like a central hub for information. It is a JavaScript object that holds data that you want to use across your website, such as user actions, product details, or page attributes.
  • This data layer is an array where you can store information using the dataLayer.push() method.
  • You can see the content of your data layer in the Data Layer tab of the GTM Preview and Debug mode.

How Data Layer Variables Work

Here's how data layer variables function in GTM:

  • Fetching Values: A data layer variable is configured to access a particular piece of data that exists within the data layer, by referencing the exact key/value pair.
  • Creating Variables: Once fetched, the value becomes a variable within GTM, allowing it to be referenced in your tags (like Google Analytics or conversion tracking tags), triggers (that decide when to fire these tags), and even other variables.
  • Dynamic Usage: These variables make your tracking setup dynamic and responsive to the user interactions on the website.

Practical Uses of Data Layer Variables

Here are a few practical examples of how data layer variables can be used:

  • Tracking Product Views: If the data layer contains information about product viewed, a data layer variable can grab the product ID to use it in a Google Analytics event tag.
  • User Engagement: Track logged-in user or user categories. A data layer variable can access these attributes from the data layer and feed them into the correct tag.
  • Form Submission Data: Use data layer variables to grab form inputs (like user name or email), as they are passed to the data layer, and pass them to tracking tags upon form submission.

Example

Let's imagine the following data layer entry:

dataLayer.push({
 'event' : 'productView',
 'productID' : '12345',
 'productName' : 'Amazing Widget'
});

In GTM, you could then create two data layer variables:

  1. productID which points to productID in the data layer
  2. productName which points to productName in the data layer

You can then use these two variables in other parts of GTM such as tags, triggers or other variables.

Key takeaways

  • Data Layer Variables enable you to fetch a value from the Data Layer and turn it into a variable (which can be used in Tags, Triggers, and even other variables).

In short, a data layer variable is an essential tool that lets you effectively use the data stored in the data layer, making your analytics and tracking more comprehensive and responsive.

Related Articles