Until Google’s Universal Analytics is rolled out we have little to no capability in Google Analytics for tracking unique visitors. Here is a little trick to capture “visitors” to your website.

Waiting for Google Universal Analytics

While other digital analytics solutions offer a dedicated visitor ID variable, in Google Analytics (up until now) we have had to use/sacrifice a custom variable out of the 5 variables we’re allowed to use per page.

For all intents and purposes, we’re going to use the following basic Google Analytics tracking code – and don’t get me started on Google Tag Manager and Universal Analytics tagging, I’ll cover those in later posts. Custom variable #1 comes with a visitor scope and a “random” value of “1111-1111-1111-1111” which of course needs to be generated.

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(
	['_setAccount', 'UA-XXXXXXX-Y'],
	['_setCustomVar', 1,'Visitor ID', '1111-1111-1111-1111',1],
	['_anonymizeIp'],
	['_trackPageview']
  );

 (function() {
   var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
   ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 })();
</script>

A unique visitor number, huh?

Ideally, in this variable, we are going to store a unique identifier. Now, unless the entire population of the planet logs on to your website within the scope of 2 years, we should be safe with slightly-less-than-unique identifiers. You get my drift.

There are 3 ways you can generate a unique identifier:

  • Have the visitor log on to your website and grab their customer/account number and use it in your custom variable. Make sure is cannot personally identify someone, i.e. 1111-1111-1111-1111 should not be traceable to Mr. John Smith without hacking the customer database first.
  • Use Google Analytics’ own cookies by grabbing the value in your __utma cookie.
  • Use Javascript to generate a random series of numbers like that 1111 sequence from earlier but with way random-er numbers.

In this post we’ll focus on the latter case. Here is an example of Javascript unique ID generation code I found on the Interwebs that ties into custom variable generation:

function s4() {
  return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
};

function guid() {
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}

var uuid = guid();
_gaq.push(['_setCustomVar', 1,'Visitor ID', uuid,1]);

Note that your _setCustomVar instruction must always take place before a pageview, event or other GA hit type (eCommerce, social, timing, etc).

Install this code, visitors come to your website and receive a unique identifier passed on to the custom variable. You can add more code to handle returning visitors so that they preserve their unique identifier, using cookie as well as the _getVisitorCustomVar instruction to read custom variable values.

How do we use it in reports?

Great question! the easiest way is to build a custom report in explorer mode by using Custom Variable (value 01) as a first dimension. then stack on whatever dimension and metrics combo that you’d like.

Google Analytics unique visitors custom report

The idea is that the first level in this drill-down report is going to be your unique visitor. Of course you’ll have truckloads of visitors to dig through so have fun! Click here for a copy of the custom report builder configuration, in which I present visits by landing page by visitor.

And that’s pretty much it! Google Universal Analytics is going to be visitor-centric so we’ll have use of a dedicated variable for each visitor, using one of the 3 aforementioned methods.

Like this post? Say so in the comments and leave constructive criticism or contact me!