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:

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

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:

<head>

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

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.