The ROI Revolution Blog
Tracking Transactions back to the Initial Referrer with Google Analytics
May 21, 2009
Google Analytics, by default, will attribute transactions to the last referrer. While this is all fine and good, there are some situations where you would really like to be able to track these transactions back to the initial referrer rather than the last referrer.
For example, you may be spending money on AdWords traffic to get visitors to the site initially, but many of the actual transactions aren’t occurring until later when they’ve returned to the site organically. You can change your Google Analytics Tracking Code so that it credits these transactions to the initial referrer rather than the last referrer, allowing you to get a better handle on the return for your paid marketing efforts.
One issue with changing your Google Analytics code so that it gives transaction credit to the first referrer rather than the last referrer, however, is that this is a permanent change affecting all profiles. You can’t have one profile that gives first referrer credit and another profile that gives last referrer credit because both profiles will use the same set of cookies, even if those profiles use separate account numbers.
You can work around this, however, by using a local, modified version of ga.js. The original ga.js modification and idea comes from John Henson at Lunametrics, though I’ve tweaked a few things for my own purposes. His post that I’m referencing isn’t directly related to this modification, but there are some tie-ins to the overall idea of using different cookies.
If you want to switch all of your profiles over to track initial referrer rather than last referrer, you can just use the following code:
If, however, you want to be able to track first referrer in a separate profile, you will need to follow these steps:
1. Upload ga.js file to your site.
Get the file here. Remember where you uploaded the file as you’ll need this later.
2. Create a profile with a new account number:
To do this, start with the Analytics Settings page, click “Add Website Profile”, then select “Add a Profile for a new domain”. You can enter the same URL as your current profiles, but selecting this option means that this new profile will use a new account number, separate from all of your existing profiles. After you create this profile, make a note of the account number or “Web Property ID” as we’ll use this later.
3. Add the following code to your site, either before or after your Google Analytics Tracking Code:
You will need to supply the correct path for the local ga.js file that you uploaded to your site back at step one and the account number for the profile that you noted when you created the new profile at step 2.
That’s all there is to it. To see best the effect on your ecommerce attribution, I would also add Shawn’s transaction filter set to your profiles. This filter set will add the source, medium, and keyword to the end of each transaction ID. You can then compare the Transactions report for both profiles to see both the initial referrer and the latest referrer for each of your transactions.
I feel obligated at this point to offer an explanation for this code and how it works. You shouldn’t feel obligated to read it, however, so feel free to drop out at any time.
Again, what follows is rather technical in nature.
The second code I gave you differs from the code above it in a few important ways:
1. I use the firstTracker object instead of pageTracker. This is important if we want to be able to register events or virtual pageviews for one or both profile types.
2. Instead of using _gat._getTracker, I use _roit._getTracker. I modified the local ga.js file to use roit instead so that the original tracker object, pageTracker, would not be affected by the local ga.js code. This is important in case the standard Google Analytics Tracking Code happens to be placed below the initial referrer tracking code.
3. The code checks for the presence of __roia instead of __utma. That’s because the local ga.js file uses an entirely separate set of cookies than the standard Google Analytics Tracking Code. That’s how we’re able to attribute transactions to the initial referrer in one profile and the last referrer in other profiles.
There are several important aspects to what both of these codes do:
1. They check to see whether or not this is the first visit based on the existence of the __utma or __roia cookie. If it exists, we make sure that the original referrer won’t be overridden by anything new. If it’s missing, we let the tracking code run as normal so that all usual referral data is captured in the __utmz cookie.
2. The first thing we do to prevent the original referral from being overwritten is to use the _setReferrerOverride method. This method tells Google Analytics to use the referrer we pass to it rather than the actual referrer. In our case, we want Google Analytics to treat this as a direct visit, so we pass the empty string to this function.
There’s no official documentation available for _setReferrerOverride, but I believe credit for discovering this method goes to Ophir. He also gives an excellent use for the function in his post.
3. We set all of the usual UTM Campaign variables to use a bogus parameter name. I use the parameter name “zzz” here, but you can use something else if zzz is a legit parameter name for you or you think it might become one in the future.
Also note that you can technically call all of these methods without parameters with the same result:
This can be done based on a how the methods handle parameters, however, and does not indicate how these methods are normally used.
Most of the methods used to override parameters are officially documented. I found the _setCampCIdKey method on my own, though it’s likely others know about it.
This method tells Google Analytics to use something other than the gclid parameter for tracking AdWords hits when autotagging. There’s probably not much use for it other than preventing a second AdWords hit from overriding the referral data for a previous hit.
That just about wraps things up. Feel free to leave comments if you want additional explanations on a particular aspect of these methods, or for any other reason. Again, we’ll be going over techniques like this and so much more in our Google Analytics Training Seminars.