David Vallejo - Web Analyst

Keeping your Google Analytics clientId value as long as possible

Web Analytics

If you are an analyst you may be concerned about your analytic tool (specifically talking about Google Analytics on this post) cookies tool integrity and about keeping that cookies as long as you can. Today I was working on a little audit for a client that was experimenting some cookies deletion problem on their intranet. They have, let say, 100 employees and they were seeing ~300 users on Google Analytics for one month, ~550 users when looking for the past 2 months data, they were wondering how could that be possible ? . It seems that everything time they were being unlogged and logged again into their local domain, their cookies were being reset.

Some users may clean their cookies, some cleaning tools does so too, but what is not usual is to clean the localStorage. So why not use it as a backup for our Google Analytics clientId ?

What's the localStorage ?

Since HTML5 it's possible to use the localStorage to save up to 5MB of data locally in our browser, unlike cookies this info won't be included along with our HTTP Requests, so it won't affect in any case our site performance. Browser's support for localStorage is widely present in the current browser's versions and it was since long ago.

web_storage_support
Image capture taken from: http://www.html5rocks.com/en/features/storage

This is why I decided to write a little hack to use it with Google Analytics to try to keep our's user's clientId as long as we can. This is the flow that our hack is going to follow:

  1. We will save our Google Analytics clientId in our browser's localStorage.
  2. If _ga cookie is not present, we will check our localStorage for a saved clientId, and if it's there we'll force that clientId on GA tracker/cookie using the universal.js API.
  3. If our browser doesn't support localStorage ( that's like not going to happen! ) or it's the first time the user's is visiting our site ( ie: no localStorage value, no _ga cookie ), Universal
    Analytics will act normal, this is creating a new cookie.

Below you can find a fully-commented JavaScript Snippet:


  // Let's check if LocalStorage is available
  if(typeof(Storage) !== "undefined") {
  // We only want to read the CID from the localStorage if the _ga cookie is not present
  // If _ga is not present, we will want to check if it's saved in our localStorage
    if(!document.cookie.match(new RegExp('_ga=([^;]+)'))){
    	var ga_cid_hash = localStorage.getItem('ua_cid');
    }
  }
  
  // if _ga was present ga_cid_hash will be undefined
  // if _ga was not present and it was neither available on our localStorage ga_cid_hash value will be undefined
  // as long as ga_cid_hash value is undefined a new cookie set would be set, ie: like if we weren't setting the clientId
  // on the create tracker call
  ga('create', 'UA-12804828-123', {'clientId': ga_cid_hash});
  ga('send', 'pageview');
  // Again we don't want to do anything is localStorage is not available
  if(typeof(Storage) !== "undefined") {
  	ga(function(tracker) {
	     // This will be ran right after GA has been loaded, 
	     // We'll check for a saved clientId in our localStorage, if not present, we will grab
	     // the current GA clientID and we will save it 
	     if(!localStorage.getItem('ua_cid'))
	     {
	         var clientId = tracker.get('clientId');
	         localStorage.setItem('ua_cid',clientId);
	     }
	  });
  }

There we go, our cid  will be restored from the localStorage, so even if the user cleans it's cookies , we will be able to track him as if he didn't clean them :).
Of course if the client's does a full history reset for their browse data, there's nothing we can do, but at least we can be sure that in some % we'll be able to keep ours user's history in our Google Analytics.

I've setup a demo page for this little hack on this url:

Link:  http://stage.tags.ninja/ga_persistent_clientid.php

So now visit the link, note your actual Google Analytics CID value, clean your cookies, reload the page, and Ta-da, your previous GA's client Id is back in town.

Any comments or suggestion will be appreciated :)