How to do First Touch Tracking in Google Analytics
Recently, 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.
If you have enjoyed this post you can subscribe to the rss feed to read more about how you can monitor and protect your brand online


Comment link
Michael Whitaker on Wed (17 Feb) @ 7:23 pm
Hi Will,
Many thanks for sharing your approach. Attribution management is definitely something a lot of people are interested in, but I would like to play devil’s advocate and ask how you would make attribution management actionable.
First of all I think you will need a lot of data to actually see patterns emerging, so I am not sure this is all that applicable for smaller sites. Many people don’t even use segmentation so moving on to attribution modeling is quite a step up…
It is undoubtedly valuable to find assist keywords and referrers that come earlier in the sales cycle and which may have been hidden by branded search terms that are closer to the transaction. If anything you can use this type of data to explain with Adwords conversion tracking can give you different results than GA. However, for modeling, don’t you need to analyze ALL visit histories, including those that did NOT result in a transaction? That’s a lot of data to crunch.
Which brings me to my last point. How would you approach the modeling? Presumably the reason for doing this is so that you can come up with a model that would allow you to allocate marketing spend better. But is first-click attribution always appropriate? If the first visit is Google Adwords and the converting visit is my email campaign, should the email campaign not get at least some credit? Also, how far back in time do you go? Should a referral from a shopping comparison engine that sent a visit 364 days ago get any credit?
I don’t think there is a right or wrong way, and a model is just a model. Would be great to continue a conversation about the model itself.
Cheers,
Michael
Comment link
Will Critchlow on Wed (17 Feb) @ 8:26 pm
Excellent points Michael. You could definitely build some pretty complex models, and maybe we’ll get into that more in future posts…
I also agree that first touch is not always the right answer (especially a long way in the past as you point out). One of the things I want to do next is get closer to quantifying ‘assists’.
At its most basic, however, I want to just clear up a single conundrum we see a lot which is clients not seeing the value of long-tail unbranded searches as “they don’t convert”. If most conversions appear to come via direct / email / branded search, there is clearly an acquisition channel not getting all the credit it could!
As I build out some excel models, I’ll be sure to share what I can. In the meantime, I’d love to hear more of your thoughts.
Comment link
Brian on Thu (18 Feb) @ 9:26 am
I’ve not even read this post yet but I’m so excited about it!
I’ve been waiting for a post like this for ages, so thanks Will. Now if only I wasn’t so damn busy over the next few days I could actually read the thing and dive into my analytics…
Comment link
netclick on Thu (18 Feb) @ 3:46 pm
Thanks for sharing this info. I was waiting for that, since you first addressed it in the Whiteboard Friday.
This is just for the first touch tracking and not for the entire path, right?
Comment link
Ricardo on Sun (21 Feb) @ 1:52 am
Would this code:
be ok to use on site with SSL?
Thanks!
Comment link
Derek on Mon (22 Feb) @ 2:21 pm
Can you please briefly elaborate on whether this code can work with web site optimizer. I’m under the impression I can only use one or the other on a page, but not both.
Thanks for all the great info.
-Derek
Comment link
Jim Gianoglio on Fri (26 Feb) @ 9:54 pm
Will – excellent information!
One potential problem I see is with GA only allowing 5 slots for the custom variables. With the first touch tracking filling up four of those slots, you’re left with only 1 additional visitor-level slot. (This is merely a limitation of GA – nothing against your first touch tracking method!)
So I would be able to do first touch tracking, and set a custom variable for member = yes/no, but if I wanted to set a visitor-level custom variable for anything else, say membership level, I’d be out of luck.
Any thoughts on this? Am I overlooking some (easy) solution?
(By the way, can I blame you when my wife asks why I’m getting home so late on a Friday? )
Happy weekend!
Comment link
Daniil Azovskih on Tue (2 Mar) @ 6:12 am
Kevin Hillstrom (minethatdata.com) once said: “Sales attribution is like focusing on measuring every grain of sand. We lose sight of the whole beach when we do this! And just when we’ve attributed every grain of sand, a rogue wave comes in and we have to start attribution all over from scratch!”
Comment link
Luke Jones on Tue (9 Mar) @ 2:17 pm
Looks like I’ll be helping to implement first-touch tracking soon!!! Quite excited… Is that sad?
Comment link
Michael Zukewich on Thu (11 Mar) @ 4:38 pm
Will,
Thanks for providing this info. I’d be very interested in hearing more about step #3 but I also have a question in regards to one of your statements:
“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!”
I’m looking to solve the high conversion issue with branded terms. So when you create the custom report in Google Analytics, are you saying you will not be able to find out what the exact keyphrase the person initially searched for? If that is correct, is there a way to find out what the person’s initial kephrase search was to help the long tail analysis?
Thanks!
Michael Zukewich
Comment link
Rob Smith on Thu (15 Apr) @ 7:33 pm
Will,
We’ve just taken over a site that uses the model you have outlined (found this page by googling the code used in the page!).
A question though – I’m looking at the results and they vary wildly in terms of the number of visits that have this information attached. For instance looking into a custom report using the Dimension (l : original landing page (no query string)) we only get 15,000 visits reported for it out of 41,000 actual visits.
Could you help explain that to me a little? I love the idea of first touch to aid decisions, but there’s a big data gap there!
Thanks Will.
Comment link
Will Critchlow on Mon (19 Apr) @ 12:46 pm
Hi Rob,
I am not 100% certain I have tracked down the issue, but I believe it is arising because of a truncation problem in the javascript (i.e. long referring URLs were causing an error in my code).
I have uploaded a new version that I believe fixes this issue and so hopefully the data should start coming through better. I’ll come back here and update if I catch additional issues. Let me know what you see.
I am also working on a project to define reports that turn the captured data into something useful – subscribe to the blog to get the announcement when I have managed that.
Thanks for stopping by – I hope we can get this working how we want it.
Comment link
Rob Smith on Tue (20 Apr) @ 2:27 pm
I’ll look out for that Will thanks.
Comment link
Raymond Wong on Thu (20 May) @ 5:19 pm
I refined the script by adding a line in distilledTruncate() to replace ‘%20′ or ‘+’ or ‘ ‘ with ‘_’. This way it will reduced the string size and be readable in GA.
function distilledTruncate(input) {
var byteLength = 63;
input = input.replace(/s|%20|+/ig,”_”);
…
Comment link
Will Critchlow on Mon (24 May) @ 10:45 pm
Hi Raymond,
I don’t think that will help as Google urlencodes everything on the way in… Have you tested that change?
Thanks for the idea…
Comment link
Raymond Wong on Tue (25 May) @ 2:07 am
Yes. It works. My keyword(custom var) are displayed as “this_is_my_keyword”. I looked up the url spec. “_” will not be encoded.
Comment link
Will Critchlow on Tue (25 May) @ 11:21 am
You read the manual? Isn’t that cheating?
Nice idea – I’ll try to make that change.
Comment link
» State of Search radioshow – episode 12: SMX London, A4UMunich, Newspapers selling SEO and opt out Analytics - State of Search on Fri (28 May) @ 1:47 pm
[...] Top 10 analytics reports – http://www.distilled.co.uk/blog/seo/first-touch-tracking-in-google-analytics/ [...]
Comment link
6 cool things YOU can do with Google Analytics Custom Segments | Distilled blog on Thu (10 Jun) @ 4:25 pm
[...] Lest we forget, Distilled has a great post about tracking how visitors first discovered your site, rather than how they most recently discovered your site – for more info read Will’s post about first touch tracking in Google Analytics. [...]
Comment link
AG on Mon (26 Jul) @ 3:19 pm
Hi Will,
Awesome post and thanks for sharing the code it is great. I am currently testing it to measure the real ROI of PPC advertising and especially to outline the number of goals/sales achieved from first PPC click. For example if someone comes to a site through paid search initially, then leave and convert through organic search.
I have found your method really useful, but I am kind of struggling to easily get the actual source/medium (cpc, email, organic) from the first touch.
Would you have any suggestions to achieve this?
Many Thanks
Comment link
Reiner on Wed (28 Jul) @ 3:51 pm
Thank you a lot for another groundbreaking post, Will!
Yet there are still two questions I’m crunching on. First: how to use this code together with the asyncronus GA Code? While with the non-asyncronus GATC your code is placed between gat.getTracker and trackPageview I doubt that – when using asyncronus code – it would be enough to place it between _setAccount and trackPageview.
In case I’m less interested in “l” and “s” for the initial landing page information but rather want to leave these two slots for other purposes: as I understand in your code I just need to allocate “r” to the first and “q” for the second slot and allocate no other slots. Correct?
Would be great if you could shed some light on these questions.
Reiner