The data layer

Google Tag Manager functions best when deployed alongside a data layer. A data layer is a JavaScript object that is used to pass information from your website to your Tag Manager container. You can then use that information to populate variables and activate triggers in your tag configurations.

Developers should read the developer documentation for more information.

Rather than referencing variables, transaction information, page categories, and other important signals scattered throughout your page, Tag Manager is designed to easily reference information that you include in your data layer source code. Implementation of a data layer with variables and associated values, ensures that they will be available as soon as you need them to fire tags.

When you set up your website or mobile app to work with Tag Manager and a data layer, spend some time to think about what types of information the data layer should handle. Types of data you may want to add to a data layer include:

  • Product data: Product name, price, category
  • Marketing campaign information: Source of the traffic, medium
  • Transaction data: Cart value, date of checkout
  • Customer information: New or returning customer

Create a data layer variable

Data layer variables enable Tag Manager to read values from your data layer implementation and pass those values to tags, triggers, and other variables. A data layer object is made up of a list of key/value pairs. A key is a category of things, e.g. book category, title, or author. Each key could have different values, e.g. a title key could have a value of 'Ulysses', 'War in Peace', 'A Brief History of Time', and so on.

To set up a data layer variable in Tag Manager, you need to use the key as it is written in your JavaScript code.

<script>
  dataLayer = [{
    'bookCategory': 'fiction',
    'bookTitle': 'One Hundred Years of Solitude',
    'bookAuthor': 'Gabriel García Márquez',
  }];
</script>

In the above example, bookCategory, bookTitle, and bookAuthor are all keys.

Once you have the key that you would like to work with, you can proceed to create a data layer variable:

  1. Click Variables.
  2. Under User-Defined Variables, click New.
  3. Select Data Layer Variable as the variable type.
  4. In the Data Layer Variable Name field, enter the key exactly as it was written in the code, e.g. bookTitle, not book title.
  5. Most use cases will leave the Data Layer Version set to Version 2. Learn more
  6. Save your tag and publish the container.
Note: Data layer variables only work on a per-page basis. If you would like the data layer values to persist through multiple page views, you will need to add your own code to pass data layer values between those pages.

Additional resources

Was this article helpful?
How can we improve it?