{"id":5204,"date":"2022-07-28T16:32:06","date_gmt":"2022-07-28T13:32:06","guid":{"rendered":"https:\/\/trackingchef.com\/?p=5204"},"modified":"2022-07-28T16:32:08","modified_gmt":"2022-07-28T13:32:08","slug":"resetting-the-data-layer-in-google-tag-manager","status":"publish","type":"post","link":"https:\/\/trackingchef.com\/google-tag-manager\/resetting-the-data-layer-in-google-tag-manager\/","title":{"rendered":"Resetting the Data Layer in Google Tag Manager"},"content":{"rendered":"\n

The data layer in Google Tag Manager (GTM) is probably one of its best features. It helps easily pass data to your tags and triggers in a simple key:value method that creates a powerful data store.<\/p>\n\n\n\n

The flip side to using this method is that it can create a data bloat that can quickly get out of hand. For example, look at this screenshot from Next<\/a> – they pushed 37 different data layer events \ud83d\ude31<\/p>\n\n\n

\n
\"\"<\/figure><\/div>\n\n\n

Why does this matter?<\/h2>\n\n\n\n

Website speed is a critical factor for conversion rates<\/a>. I don’t think I need to convince you of this at this point. So removing any extra fat that can slow down your site is important. The Next example is rather radical; they keep pushing these events with every click on the website, which can amass a large data set.<\/p>\n\n\n\n

A more common example is Single Page Applications (SPA) or Progressive Web Apps (PWA) which are popular today, especially in the field of Headless Commerce. In these types of websites, the user doesn’t navigate to a new page, which persists the data layer between pages.<\/p>\n\n\n\n

This can result in a bloated data layer that contains data from dozens of pages visited throughout the current sessions and the events triggered on them.<\/p>\n\n\n\n

Additionally, this can lead to misattributed events – i.e. event data from a previous page sent on the current page.<\/p>\n\n\n\n

Solving the bloat<\/h2>\n\n\n\n

Luckily enough, the solution is relatively simple. A the script below can be triggered to reset the data layer.<\/p>\n\n\n\n

<script>\nwindow.dataLayer.push(function() {\r\n  this.reset();\r\n})\n<\/script><\/code><\/pre>\n\n\n\n

Manual implementation<\/h3>\n\n\n\n

One option is to ask your developer to trigger this on a page load of your SPA\/PWA site (aka route change). This script should be fired before<\/strong> the pageview event you’re triggering (if you’re using a triggered pageview event<\/a> as I recommend).<\/p>\n\n\n\n

If you’re tracking pageviews using the history change trigger in GTM, make sure to trigger the script before<\/strong> the route change action.<\/p>\n\n\n\n

Google Tag Manager Implementation<\/h3>\n\n\n\n

Since you’re already using GTM you can obviously use it to trigger this script. A simple way to go about this is to set the script as a Custom HTML tag that fires immediately before the Google Analytics Pageview tag. This way the new page loaded get a fresh start.<\/p>\n\n\n

\n
\"\"<\/figure><\/div>\n\n\n

This tag should have no trigger attached to it.<\/p>\n\n\n\n

In your Google Analytics Pageview tag, expand the Advanced Settings<\/em> section and the expand Tag Sequencing<\/em>. Check Fire a tag before…<\/em> and then select the tag you’ve created in the previous step.<\/p>\n\n\n

\n
\"\"<\/figure><\/div>\n\n\n

In the Custom HTML tag it will reflect in this way:<\/p>\n\n\n

\n
\"\"<\/figure><\/div>","protected":false},"excerpt":{"rendered":"

The data layer in Google Tag Manager (GTM) is probably one of its best features. It helps easily pass data to your tags and triggers in a simple key:value method that creates a powerful data store. The flip side to using this method is that it can create a data bloat that can quickly get […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[],"_links":{"self":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/5204"}],"collection":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/comments?post=5204"}],"version-history":[{"count":2,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/5204\/revisions"}],"predecessor-version":[{"id":5211,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/5204\/revisions\/5211"}],"wp:attachment":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/media?parent=5204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/categories?post=5204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/tags?post=5204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}