The Google Tag Manager (GTM) is awesome! It enables me to manage all my 3rd party tags in one place, and it seems like the Google team has made every effort to come up with a robust solution that could actually be an alternative to the way we work today.
Although there are additional features that I would have liked to see in the GTM, I realize that this is just the first version of this platform, and I am sure Google will continue to improve it.
One such missing feature relates to the data layer object, and so I have created my own extension in the meantime, and thought it will be a good idea to spread it around.
The Data Layer Object: In order to send information to the GTM, or to configure when a tag should be fired, the GTM is designed to easily reference information placed in an object called “dataLayer”.
For example, you may want a specific tag to only be fired when a user logs onto your site. In order to do so, you would:
- Define a new variable such as “visitorType”
- Set it to “loggedin” when the user logs onto your site
- Configure the tag to only be fired if “visitorType” equals “loggedin”
So what’s missing?
The problem is that the data layer object only persists as long as the visitor remains on the current page. Variables that are relevant across pages (such as visitor type) must be declared in the data layer on each and every website page.
Before I explain how to get around this, following is an example of why and how you might want to set a variable on a session or visitor level. In my article about how registered users affect your conversion rate over time on acquisition-related goals, I suggested creating a separate GA account for each visitor type. With GTM this can be achieved relatively easily. You just have to define the event that will change the visitor type and make sure GTM fires the right tag. But as mentioned above – you have to set the visitor type on every separate webpage.
The method I have created to get around this is a DataLayer Wrapper that stores per-session and per-visitor variables in cookies, and automatically pushes them into the dataLayer.
By adding the following JavaScript code before the container snippet and after the data layer snippet, you can set data layer variables and define whether you want it to be stored on a page, session or visitor scope (as with the _setcustomrvar method).
[javascript] var __dlw={rc:function(d){var f=new RegExp("(?:^| )"+d+"=([^;]*)","i");var e=document.cookie.match(f);return(e&&e.length==2)?decodeURIComponent(e[1]):null},sc:function(g,f,j){var i="";if(j){var h=new Date();h.setTime(h.getTime()+(((typeof(j)!="undefined")?j:3)*24*60*60*1000));i="; expires="+h.toGMTString()}document.cookie=g+"="+encodeURIComponent(f)+i+"; path=/;"},store:function(e,d,k){k=k||3;var j={};j[e]=d;dataLayer.push(j);if(k!=3){var f=__dlw.rc("cdl"+k);var h=[];if(f!=null){h=f.split(";");var g=h.length;while(g--){if(h[g].split(":")[0]==e){h.splice(g,1);break}}}h.push(e+":"+d.replace(/;/g,""));__dlw.sc("cdl"+k,h.join(";"),k==1?1000:false)}},init:function(){var f=__dlw.rc("cdl1");var c=__dlw.rc("cdl2");var a=(f||"")+";"+(c||"");if(a==";"){return}var e=a.split(";");var b=e.length;var d={};while(b--){if(e[b]==""){continue}d[e[b].split(":")[0]]=e[b].split(":")[1]}dataLayer.push(d)}}; __dlw.init(); [/javascript]
This code, which defines a wrapper for the data layer object (called __dlw), provides you with a storing method:
- store(name, value, scope) – adds a variable (name and value) to the dataLayer. The scope parameter sets the scope of this variable (1 – visitor, 2 – session, 3 – page).
Using the following code, you can now set variables on a visitor or session scope and not worry about redefining them on each and every page of your site:
[javascript] __dlw.store("visitorType", "registered", 1); [/javascript]
In my next post I will show you how you can add an opt-in and opt-out feature to your site using the GTM and the above code.
FAQ about Google Tag Manager
What is GTM used for?
Google Tag Manager (GTM) is a powerful tool that allows you to manage all your third-party tags in one place. It simplifies the process of adding and updating tags on your website without needing to modify the code directly.
What does GTM mean in tracking?
GTM, in terms of tracking, refers to the functionality it provides for implementing tracking codes and managing data collection across your website. It streamlines the tracking process and offers flexibility in how you track user interactions.
How does GTM handle the data layer?
The data layer in GTM is a crucial component for passing information between your website and GTM. It allows you to define variables and trigger tags based on specific data points, enhancing the customization and accuracy of your tracking.
What are the GTM session and visitor data layer limitations?
GTM’s data layer has a limitation where it persists data only for the duration of a user’s session on a specific page. This means that variables set in the data layer are not automatically carried over to subsequent pages or across sessions.
Is there a workaround for persistent data layer variables in GTM?
Yes, you can implement a workaround using a DataLayer Wrapper that stores per-session and per-visitor variables in cookies. This allows you to maintain variables across pages and sessions, providing more flexibility in tracking and customization.
How does the DataLayer Wrapper work?
The DataLayer Wrapper is a JavaScript code snippet that you add to your website. It stores variables in cookies and automatically pushes them into the data layer, enabling persistent tracking of session and visitor-level data.