Create a data layer

Tag Manager

The data layer is a global JavaScript object that you can use to communicate with the Tag Manager’s container. Using it you can add information about events or variables, fire a trigger based on a chosen value — for example, when order_value > 100 — or on a specific event — for example, when the video was seen.

You can think of the data layer as of a universal data structure that contains essential data about events and information displayed on the page, in a format that can be utilized by third-party tools and scripts.

In this article, we’ll explain how to create a data layer.

Create a data layer

A data layer object is an array containing key/value pairs. Use the following snippet to create a new data layer:

<script>window.dataLayer = window.dataLayer || [];</script>

Note the position of the snippet in relation to the containers code. We strongly recommend to paste the data layer snippet before the containers code, like this:

	<script>window.dataLayer = window.dataLayer || [];</script>
	<!-- Piwik PRO Tag Manager synchronous container code -->
	<!-- Piwik PRO Tag Manager asynchronous container code -->

When a data layer is created after the container, some tags may behave inappropriately or not fire at all. The data layer content is not persisted between page views. This means you have to redeclare the variables on each separate page. It is also common to use different values for different pages.

A good example of a data layer would be a page category variable. If you assign it on every page, you could show a pop-up only to people interested in a specific type of content like a product, contact or homepage.

Was this article helpful?

Technical support

If you still have some questions, visit our community.

There’s always someone ready to help!