Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resetting Data Layer variables in Single page Applications ( Google Tag manager)

Trying to achieve content grouping in mobile site which is SPA.

The scenario goes like this: When I am visiting a PDP page for the first time, category variable is set and this is sent to content groups in Universal Analytics Tag. If after this I go back to the homepage, the category variable doesn't update and homepage is also grouped under the same content group of previous PDP page.

This variable only updates when I visit another PDP page.

I am using a custom event PageLoad to mimic the pageView event. Just want the data layer varaibles to refresh within the consecutive PageLoad event.

Thanks

like image 205
Pranay Avatar asked Apr 28 '17 06:04

Pranay


2 Answers

There is now a documented way to reset the data layer. See Reset in the Data layer docs.

If you push a function to the dataLayer, it'll give you access to an object with get, set, and reset semantics. For this question in particular, reset is the relevant bit.

like image 137
diminishedprime Avatar answered Jan 04 '23 01:01

diminishedprime


There's an undocumented way to cleanup the whole dataLayer using this method:

var gtm = window.google_tag_manager['GTM-XXXXXX'];
gtm.dataLayer.reset();

Now you don't necessarily need to do that since you can set the values you don't want anymore to undefined in your dataLayer:

dataLayer.push({
  'please_go_away': undefined
});
like image 39
cheesemacfly Avatar answered Jan 04 '23 00:01

cheesemacfly