by Doug


Google Analytics

Super Cookie – How to guide and source code (a guide for techies)

This is a how to guide (+ source code) for the new Google Analytics Super Cookie developed by ConversionWorks. What’s the Super Cookie?

Wouldn’t it be great if we could track all the website touch-points before a website visitor converts?  Well…we can, with a Super Cookie!

What we set out to do and what it does (an abstract)

Tracking where a visitor comes from is not a big deal. It can be done a number of ways, usually involving the referrer part of HTTP headers which can be accessed by any web scripting language, from PHP to JavaScript.

Understanding a list of referral paths however allows us to identify the effectiveness of our marketing budget and how they accessed our site on their way to a conversion.

We have produced a Javascript solution which gives us all the data we want in an easy to read format:

organic (google) (search term) | referral (referral) (referrer) | cpc (google) (search term)

This tells us that the visitor originally came to the site through an organic Google search, then came via a referring site and finally converted after clicking a CPC ad on Google.

Using a standard Google Analytics (GA) install, the credit for the conversion will go to the CPC ad (the last click) but why should the organic search not get any credit, or even the referrer?

How it’s done – the __utmz cookie & Javascript

So how do we create a record of multiple referrers? We use cookies.  We can make use of the fact that a cookie will typically stay on a user’s machine for several weeks or months, to keep track of their visits to our site from first click, all the way through to conversion and beyond.

To make the referral data as accurate as possible we make use of the __utmz cookie which is set by GA.js to gather referral data. By combining this with a set of custom Javascript functions which are executed after trackPageview(), we build a referral path string and store it in a cookie on the user’s machine. The cookie is given a lifetime of 6 months to match the default lifetime of the __utmz cookie.

Every time a user visits our site the cookie string is updated and then passed to setVar()  – a user defined value function from the GATC  so we can access it via the Google Analytics interface.  We can then combine this with a converting traffic segment to find our top converting referral paths.

Side-note: If you need to configure __utmz to have a greater lifespan, you can configure the campaign tracking cookie length to suit your business using _setCookieTimeout().

A problem with sessions – Google’s override

Important: If you don’t close your browser window and visit the site at a later point in the same session, the referral data might not be entirely accurate.

Consider the following example;

Visit 1: Referral from
Visit 2: Google organic search for “custom usb flash drives”
Visit 3: Referral from

This is what the __utmz cookie will look like:

Visit 1: Referral from
Visit 2: Google organic search for “custom usb flash drives”
Visit 3: Google organic “custom usb flash drives” – Ooops! The referrer has been overridden!

Our data is inaccurate.

If you haven’t closed your browser then technically you are in the same browsing session and as a result, Google Analytics  gives credit to the most important traffic source which it believes to be organic (as in the example above).

However with the advent of tabbed browsing, a new browser application instance isn’t necessarily what signifies a new session. I can open your site in one tab and a different one in another. I may then close your site’s tab and continue browsing for an unknown period of time – potentially weeks given the ability of PC’s to suspend/hibernate – and then return & convert.

To me it’s a different session but Google Analytics doesn’t know that.

Solving the problem – document.referrer & ga.js’ list of search engines

The problem arose because we trusted in what __utmz was telling us about the referral data. We do have access to another source for the referrer data though,  Javascript’s document.referrer.  We can then compare this with __utmz. If the two don’t agree we know that an override has occurred and can compensate, using document.referrer rather than the value stored in __utmz.

We compared __utmz with document.referrer by checking the referrer’s domain and query string against GA’s own list of search engines which you can extract from GA.js.  We also needed to make sure we exclude the site’s own domain from being tracked as a self referrer so we define a variable with this value set.

Where do we go next

The Super Cookie script (in its downloadable format) will only track visitors coming from the standard locations – direct, referral, organic and CPC.  The Super Cookie can be modified using Javascript which will allow for advanced customisation which will analyze visitors who come from various traffic sources such as email shots.  Additionally, you may wish to adjust the script to ignore other referrers on your website network (if you have one).

Get the code

Download and try the super cookie now!

The download link contains two Javascript files and one HTML file. The HTML file shows you an example implementation of how the two Javascript files and the GATC integrate with eachother.

[ConversionWorks are a Google Analytics Authorized Consultant (image missing)]

Commercial Services – would you like to start tracking the full referral path of your campaigns?

As a Google Analytics Authorized Consultant we can help you better understand and manage your Adwords marketing using the Super Cookie.


Leave a comment

Your email address will not be published.

ConversionWorks is now Media.MonksVisit us at