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:

Visits to conversion 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=""></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:


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!

Setting up a custom report in Google Analytics

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.

Get blog posts via email

About the author
Will Critchlow

Will Critchlow

Will founded Distilled with Duncan in 2005. Since then, he has consulted with some of the world’s largest organisations and most famous websites, spoken at most major industry events and regularly appeared in local and national press. For the...   read more