Skip to main content
  1. Blog Post/

Keeping your Google Analytics clientId value as long as possible

3 min · 882 words
Table of Contents

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.

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:

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