Go Back

Creating a new dataLayer

January 29, 2018

Data layer object is just an array containing key/value pairs. Use following snippet to create new data layer:

window.dataLayer = window.dataLayer || [];

It is important to keep in mind position of the snippet in relation to containers code. It is strongly recommended to paste the data layer create snippet before both of them are included, like this:


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

In case data layer is created after the container is initialised, 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.

Good example would be pageCategory variable. If assigned on every page you could e.g. show a pop-up only to people interested in specific type of content like product browser, contact or homepage etc.