Nov 30 12

Google Tag Manager – Persistent Data Layer (per-session and per-visitor)

By

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).

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();

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:

  __dlw.store("visitorType", "registered", 1);

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.

Subscribe for Our Latest SharePoint Analytics Updates

Signup to get the latest SharePoint Analytics posts straight to your inbox!