

News Archive
March 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
September 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
How to do First Touch Tracking in Google Analytics
February 17, 2010, 10:49 amRecently, I have found myself talking quite a lot about tracking whole visitor paths in Google Analytics. Rand and I discussed the issue in a Whiteboard Friday kill the head or chase the tail and I covered how to get past last click attribution in a subsequent post. These have tended to cover the why and the (broad-brush) why. If you’re interested in what first touch, last touch or multi-touch tracking is or why you’d want to do them, the resources above are a good place to start.
The main point can be summed up by the chart below showing the visits to convert report from Google Analytics:

The point is not what those exact values are, but simply that a significant number (in this case more than half) of all conversions come from visits that aren’t the first!
Today’s post, however, is all about the details: how to get first touch tracking working to get you actionable data about real acquisition costs from Google Analytics.
You will need to be able to:
- Import a custom .js file
- Modify the Google Analytics embed code across your website
- Create custom reports in GA
Note that this is going to set custom variables. If you are already using this functionality, you should be very careful with how you integrate this. Oh, and all of this is provided as is, with no warranty. I hope it will help you out, but only you are responsible for changes you make to your website and tracking code.
By default, GA attributes conversions to the last touch – i.e. the source of the visit that led to the conversion. I’m going to show you how to get the source of their first visit to your site.
Step 1
Embed a JavaScript file defining three functions:
- distilledCheckAnalyticsCookie – in order to track first touch information, we only want to record details to custom variables on someone’s first visit. This function checks for the __utma visitor cookie
- distilledTruncate – as I discuss in more detail over on SearchEngineLand, Google won’t allow you to set custom variables of longer than 64 characters (including the variable name) after URL encoding so this function is a slightly long way round of truncating the variable information
- distilledFirstTouch – the heavy lifting – this is the function that sets the four variables outlined in more detail below
You can embed this with the following code anywhere above the Google Analytics code script in your page code:
<script type="text/javascript" src="http://attributiontrackingga.googlecode.com/svn/trunk/distilled.FirstTouch.js"></script>
It’s a little clunky at the moment and I want to refine it a little to cope better with combinations of Google Analytics and Website Optimizer. If anyone has any good ideas for this, feel free to drop me a line or raise issues over at Google Code.
Step 2
Move your GA code above any Website Optimizer code or anything from Google that might write a visitor (__utma) cookie and look for:
var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
pageTracker._trackPageview();
In between those two lines, you want to put the following code:
distilledFirstTouch(pageTracker);
So that your trackpageview code looks like this:
var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
// Distilled first touch tracking
distilledFirstTouch(pageTracker);
pageTracker._trackPageview();
Make sure you use your own UA-XXXXXXX-X identifier string!
This writes 4 custom variables (apologies for the ridiculous naming conventions – Google limits the whole of the variable name + value to 64 characters!):
- l : original landing page (no query string)
- s : original landing page query string
- r : original referrer
- q : if q=keyword+keyword is found, this contains that part of the referrer (it’s actually more complicated than that – I have taken the full list of keyword delimiters from Google help and attempted to pull them out into the fourth variable in case the full referrer is truncated by the character limit).
Step 3
The detail of this is probably best reserved for another day / another post, but suffice it to say that I have found that custom reports exported to Excel are probably the best way of analysing the data this method produces. Far be it from me to tell you what reports to create, but I suggest something like conversions or revenue by original referring keywords might be interesting!

I have found the Visitors –> Custom Variables report in GA to be flaky at best. I would advise avoiding that and creating your own reports.
Step 4
Work a tiny bit of Excel magic.
Because (as described above) Google encodes the data on the way into GA, you need to decode it to make real sense of it. I have made the assumption that Google’s URLEncode function works like JavaScript’s encodeURIComponent() function and written an Excel formula to help:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"%2F","/"), "%2C", ","), "%3F", "?"), "%3A", ":"), "%40", "@"), "%26", "&"), "%3D", "="), "%2B", "+"), "%24", "$"), "%23", "#")
Just paste this formula into an empty cell and place the information you want to decode into cell A1. Manipulate as necessary. You’re welcome. And yes, I’m a (tiny bit) sorry for the nastiness of that formula.
On a side note
I was amused to see the following tweet from @jaamit:
First click attribution is a bit like crediting your first girlfriend with your current marriage
(Attributed to @avinashkaushik’s keynote).
Swiftly rebutted by @Philipbuxton:
Last-click attribution is like selling Alonso because he doesn’t score enough goals.
On that note, I’ll leave you to go ahead and implement and let me know what you think.
This documentation also appears on the Google code wiki.




