The ROI Revolution Blog

Tracking Transactions back to the Initial Referrer with Google Analytics

May 21, 2009

first touch

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. For additional advanced javascript techniques and other Google Analytics know-how to help you get the most out of Google Analytics, join Shawn Purtell and Michael Harrison in Miami for our Google Analytics Seminar for Success. 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: pageTracker._setReferrerOverride(); pageTracker._setCampNameKey(); pageTracker._setCampMediumKey(); pageTracker._setCampSourceKey(); pageTracker._setCampTermKey(); pageTracker._setCampContentKey(); pageTracker._setCampCIdKey(); 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.

Google Analytics for Online Advertisers
Here at ROI Revolution, we consider Google Analytics tracking essential for paid search, so it's included in our PPC Campaign Management service.

Comments

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would make the instructions in this post obsolete. I have not made any recent updates to my ga.js, but it should still work fine. You shouldn't be missing anything by using it.

September 8, 2009 4:08 PM

PJ said:

Hi,

Thanks alot for such a great post. I tried following your instructions but seems like have made some mistake in implementing. I am not passing "zzz", will this affect the functionality of the code? here is how the code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("");
pageTracker._setCampMediumKey("");
pageTracker._setCampSourceKey("");
pageTracker._setCampTermKey("");
pageTracker._setCampContentKey("");
pageTracker._setCampCIdKey("");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

Also, do i need to make changes on the shopping cart related pages as well? My site uses a 3rd party shopping cart.

September 30, 2009 10:24 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:

1. If you call the functions and pass nothing, that works fine:

pageTracker._setReferrerOverride();
pageTracker._setCampNameKey();
pageTracker._setCampMediumKey();
pageTracker._setCampSourceKey();
pageTracker._setCampTermKey();
pageTracker._setCampContentKey();
pageTracker._setCampCIdKey();

But I wouldn't pass the empty string as this can cause unexpected issues.

2. Most likely you won't need to make the same changes on the 3rd party shopping cart because there shouldn't be any traffic going directly to the shopping cart. As long as you have your tracking code set up correctly for multiple domains with all the proper modifications for links between domains, that should be sufficient.

October 1, 2009 8:13 AM

PJ said:

Hi Jeremy,

I tried the suggested code by passing "zzz" but it seems like every time i visit the page, the source information in the cookie is getting overwritten. Here is my entire code, can you please have a look and help me out in correcting the mistake i am making.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
"http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) { pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

October 1, 2009 2:13 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:I don't see anything wrong with the code in your comment. Do you have any additional code on your page such as Google Website Optimizer code?

October 1, 2009 2:42 PM

PJ said:

Hi Jeremy,

Just figured out that there are 2 different google analytics code on the website. Is this the reason why the source is getting owerwritten in then cookie? The second code has not been customized according to your suggestions.

Thanks & Regards

October 1, 2009 11:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ: Yes, that's why the source is getting overwritten. It's a limitation of the customization that it has to be done for Google Analytics (and Google Website Optimizer) tracking code snippet on the site, otherwise it won't work.

This is one of the advantages of using the local ga.js file I provide: since it uses a separate set of cookies, those cookies won't get overwritten unless you have another piece of tracking code that also uses the local ga.js file.

October 2, 2009 7:59 AM

Levi Page said:

What happens when their is a second purchase though? Wouldn't the visits to purchase be incorrect? Would it know show visits going back even before the first purchase?

October 10, 2009 10:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Levi: Yes, it should show all the visits, even before the first purchase. If you're using last referrer tracking, this could still happen if the referrer happens not to change. So using initial referrer instead doesn't make this more incorrect, if by correct you mean that it should only count the number of visits since the last purchase.

October 13, 2009 7:43 PM

Matthias Galica said:

In the event that you've setup adwords to pass GCLID into Analytics in a way that's intelligible as a Traffic Source (we've arbitrarily called it wtm_ coding) then can all the same functionality detailed in this post be achieved by utilizing one of the new Custom Variables through the method _setCustomVar ?

November 18, 2009 1:21 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mattias: You could get the data into Google Analytics by passing parameter values into custom variables. The downside is that you'll now have two ways to look at referral data: one that uses the standard reports (last touch) and one that uses custom variables (first touch). The fact that custom variables give you a lot of flexibility also means that they lack the structure that the standard reports will give you, so it may not be as clear what's being reported on at any given point.

The main problem with using custom variables to track initial referral data is that impressions, clicks, cost, etc., data is not associated in any way with custom variables. If you wanted to track ROI, you would have to do this manually. With the method given in this post, initial referrer data is automatically tied to AdWords cost data just as last referrer data is.

November 18, 2009 1:36 PM

Boaz said:

Hi Jeremy,
1st of all thanks a lot for your post. I've been looking for this type of solutions for one of my clients, as I want to show him a comparison of an initial referrer profile against a last referrer (default) profile.

I've implemented your solutions a couple of months ago. My original profile stayed the same, with each transaction and its value being associated with the last referrer.

In the Initial Referrer profile ALL transactions are now associated to "(direct) / (none)". I checked again and again and cannot understand what the problem is.

Here is the relevant code on my confirmation page:


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-1227832-1");
pageTracker._setDomainName("danhotels.com");
pageTracker._initData();
pageTracker._trackPageview();




try {
var firstTracker = _roit._getTracker("UA-1227832-6");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();

pageTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
pageTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
pageTracker._trackTrans();

firstTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
firstTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
firstTracker._trackTrans();

}catch(err) {}


December 10, 2009 6:46 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Boaz: I noticed that you have the following line in your Google Analytics Tracking Code:

pageTracker._setDomainName("danhotels.com");

Usually when you set the domain name, it's for a good reason, like having a site with subdomains. When you have multiple tracking objects (pageTracker and firstTracker), it's best to make the same type of changes to each. In this case, that means you'd want to add the following line to your firstTracker code:

firstTracker._setDomainName("danhotels.com");

This is almost certainly the culprit. Normally in situations with subdomains where you don't set the domain name, you would see all transactions showing up as self-referred. In this case, however, that referrer is being ignored because the code on the subdomain is able to find the __roia cookie and override the referrer with "". When it sets its own cookies, however, it's setting them to the full domain, including the subdomain, rather than the root level domain, danhotels.com.

December 10, 2009 7:33 AM

Boaz said:

Thanks Jeremy.
You are right, the reason is a subdomain.
I'll give it a go and let you know.

Boaz

December 13, 2009 3:11 AM

Catherine said:

Great post - I am trying to figure this out and have read through a couple of times. I have a yahoo store and the shopping cart is on another domain- in GA it shows "my domain' as the top referrer - since the shopping cart is on a different domain. I have installed your code on my domain pages but am wondering what code needs to be installed on the shopping cart pages to preserve the "true source" of the traffic, not my domain... I think I am getting close. Also, I have installed GA tracking code for "multiple domains" only on the shopping cart pages and have your code on my website.

Any help would be fantastic.

December 13, 2009 5:48 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Catherine: We recommend that anyone wishing to track ecommerce for Yahoo Store in Google Analytics use Monitus. This will give you the most complete ecommerce tracking solution with Yahoo Store. Most likely, however, you'll have to choose between initial referrer and last referrer. It's unlikely you'll be able to do both with Yahoo Store.

December 14, 2009 9:07 AM

Boaz said:

Jeremy - Thanks a lot for your help. Looks like we resolved the issue, or at least very close to it!!!

Much appreciated.
Boaz

December 15, 2009 3:43 AM

Thomas G. said:

I have put the first code on all our pages as described. Just changed the "UA-XXXXXXX-X" but it does not work.
Every transactions shows our OWN domain as the source or referrer.
Any ideas why?

February 4, 2010 6:03 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Thomas: Most likely you have a third-party shopping cart. You need to modify your tracking code appropriately for multiple domains. There's an article on this in the Analytics Help Center:

http://google.com/support/analytics/bin/answer.py?hl=en&answer=55532

February 8, 2010 12:40 PM

Mark said:

Any update on this for the asynch code?

June 11, 2010 3:49 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mark: Not yet. I've made a note to write an update. I'm aiming to post it sometime this month.

June 11, 2010 7:20 AM

Robert said:

Great article and also works a charm when implemented in Magento.

However, the latest GA paradigm is to use the async code instead of _pageTracker so what would it take to adjust this code to the async tracker? Any news on that?

July 25, 2010 8:47 PM

מכונות קפה said:

Really liked to read what you wrote. I was at the Google's ambassadors program and I learned a lot. But of course it is important to practice to practice and practice again. I would like to recommend the option Analytics few know and really important. Is the segment that allows to isolate specific entrance channel and compare it to other users who enter the site. It brings a great advantage to traffic from different places.
Try it!

מכונות

November 8, 2010 5:14 AM

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would make the instructions in this post obsolete. I have not made any recent updates to my ga.js, but it should still work fine. You shouldn't be missing anything by using it.

September 8, 2009 4:08 PM

PJ said:

Hi,

Thanks alot for such a great post. I tried following your instructions but seems like have made some mistake in implementing. I am not passing "zzz", will this affect the functionality of the code? here is how the code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("");
pageTracker._setCampMediumKey("");
pageTracker._setCampSourceKey("");
pageTracker._setCampTermKey("");
pageTracker._setCampContentKey("");
pageTracker._setCampCIdKey("");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

Also, do i need to make changes on the shopping cart related pages as well? My site uses a 3rd party shopping cart.

September 30, 2009 10:24 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:

1. If you call the functions and pass nothing, that works fine:

pageTracker._setReferrerOverride();
pageTracker._setCampNameKey();
pageTracker._setCampMediumKey();
pageTracker._setCampSourceKey();
pageTracker._setCampTermKey();
pageTracker._setCampContentKey();
pageTracker._setCampCIdKey();

But I wouldn't pass the empty string as this can cause unexpected issues.

2. Most likely you won't need to make the same changes on the 3rd party shopping cart because there shouldn't be any traffic going directly to the shopping cart. As long as you have your tracking code set up correctly for multiple domains with all the proper modifications for links between domains, that should be sufficient.

October 1, 2009 8:13 AM

PJ said:

Hi Jeremy,

I tried the suggested code by passing "zzz" but it seems like every time i visit the page, the source information in the cookie is getting overwritten. Here is my entire code, can you please have a look and help me out in correcting the mistake i am making.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
"http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) { pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

October 1, 2009 2:13 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:I don't see anything wrong with the code in your comment. Do you have any additional code on your page such as Google Website Optimizer code?

October 1, 2009 2:42 PM

PJ said:

Hi Jeremy,

Just figured out that there are 2 different google analytics code on the website. Is this the reason why the source is getting owerwritten in then cookie? The second code has not been customized according to your suggestions.

Thanks & Regards

October 1, 2009 11:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ: Yes, that's why the source is getting overwritten. It's a limitation of the customization that it has to be done for Google Analytics (and Google Website Optimizer) tracking code snippet on the site, otherwise it won't work.

This is one of the advantages of using the local ga.js file I provide: since it uses a separate set of cookies, those cookies won't get overwritten unless you have another piece of tracking code that also uses the local ga.js file.

October 2, 2009 7:59 AM

Levi Page said:

What happens when their is a second purchase though? Wouldn't the visits to purchase be incorrect? Would it know show visits going back even before the first purchase?

October 10, 2009 10:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Levi: Yes, it should show all the visits, even before the first purchase. If you're using last referrer tracking, this could still happen if the referrer happens not to change. So using initial referrer instead doesn't make this more incorrect, if by correct you mean that it should only count the number of visits since the last purchase.

October 13, 2009 7:43 PM

Matthias Galica said:

In the event that you've setup adwords to pass GCLID into Analytics in a way that's intelligible as a Traffic Source (we've arbitrarily called it wtm_ coding) then can all the same functionality detailed in this post be achieved by utilizing one of the new Custom Variables through the method _setCustomVar ?

November 18, 2009 1:21 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mattias: You could get the data into Google Analytics by passing parameter values into custom variables. The downside is that you'll now have two ways to look at referral data: one that uses the standard reports (last touch) and one that uses custom variables (first touch). The fact that custom variables give you a lot of flexibility also means that they lack the structure that the standard reports will give you, so it may not be as clear what's being reported on at any given point.

The main problem with using custom variables to track initial referral data is that impressions, clicks, cost, etc., data is not associated in any way with custom variables. If you wanted to track ROI, you would have to do this manually. With the method given in this post, initial referrer data is automatically tied to AdWords cost data just as last referrer data is.

November 18, 2009 1:36 PM

Boaz said:

Hi Jeremy,
1st of all thanks a lot for your post. I've been looking for this type of solutions for one of my clients, as I want to show him a comparison of an initial referrer profile against a last referrer (default) profile.

I've implemented your solutions a couple of months ago. My original profile stayed the same, with each transaction and its value being associated with the last referrer.

In the Initial Referrer profile ALL transactions are now associated to "(direct) / (none)". I checked again and again and cannot understand what the problem is.

Here is the relevant code on my confirmation page:


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-1227832-1");
pageTracker._setDomainName("danhotels.com");
pageTracker._initData();
pageTracker._trackPageview();




try {
var firstTracker = _roit._getTracker("UA-1227832-6");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();

pageTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
pageTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
pageTracker._trackTrans();

firstTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
firstTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
firstTracker._trackTrans();

}catch(err) {}


December 10, 2009 6:46 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Boaz: I noticed that you have the following line in your Google Analytics Tracking Code:

pageTracker._setDomainName("danhotels.com");

Usually when you set the domain name, it's for a good reason, like having a site with subdomains. When you have multiple tracking objects (pageTracker and firstTracker), it's best to make the same type of changes to each. In this case, that means you'd want to add the following line to your firstTracker code:

firstTracker._setDomainName("danhotels.com");

This is almost certainly the culprit. Normally in situations with subdomains where you don't set the domain name, you would see all transactions showing up as self-referred. In this case, however, that referrer is being ignored because the code on the subdomain is able to find the __roia cookie and override the referrer with "". When it sets its own cookies, however, it's setting them to the full domain, including the subdomain, rather than the root level domain, danhotels.com.

December 10, 2009 7:33 AM

Boaz said:

Thanks Jeremy.
You are right, the reason is a subdomain.
I'll give it a go and let you know.

Boaz

December 13, 2009 3:11 AM

Catherine said:

Great post - I am trying to figure this out and have read through a couple of times. I have a yahoo store and the shopping cart is on another domain- in GA it shows "my domain' as the top referrer - since the shopping cart is on a different domain. I have installed your code on my domain pages but am wondering what code needs to be installed on the shopping cart pages to preserve the "true source" of the traffic, not my domain... I think I am getting close. Also, I have installed GA tracking code for "multiple domains" only on the shopping cart pages and have your code on my website.

Any help would be fantastic.

December 13, 2009 5:48 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Catherine: We recommend that anyone wishing to track ecommerce for Yahoo Store in Google Analytics use Monitus. This will give you the most complete ecommerce tracking solution with Yahoo Store. Most likely, however, you'll have to choose between initial referrer and last referrer. It's unlikely you'll be able to do both with Yahoo Store.

December 14, 2009 9:07 AM

Boaz said:

Jeremy - Thanks a lot for your help. Looks like we resolved the issue, or at least very close to it!!!

Much appreciated.
Boaz

December 15, 2009 3:43 AM

Thomas G. said:

I have put the first code on all our pages as described. Just changed the "UA-XXXXXXX-X" but it does not work.
Every transactions shows our OWN domain as the source or referrer.
Any ideas why?

February 4, 2010 6:03 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Thomas: Most likely you have a third-party shopping cart. You need to modify your tracking code appropriately for multiple domains. There's an article on this in the Analytics Help Center:

http://google.com/support/analytics/bin/answer.py?hl=en&answer=55532

February 8, 2010 12:40 PM

Mark said:

Any update on this for the asynch code?

June 11, 2010 3:49 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mark: Not yet. I've made a note to write an update. I'm aiming to post it sometime this month.

June 11, 2010 7:20 AM

Robert said:

Great article and also works a charm when implemented in Magento.

However, the latest GA paradigm is to use the async code instead of _pageTracker so what would it take to adjust this code to the async tracker? Any news on that?

July 25, 2010 8:47 PM

מכונות קפה said:

Really liked to read what you wrote. I was at the Google's ambassadors program and I learned a lot. But of course it is important to practice to practice and practice again. I would like to recommend the option Analytics few know and really important. Is the segment that allows to isolate specific entrance channel and compare it to other users who enter the site. It brings a great advantage to traffic from different places.
Try it!

מכונות

November 8, 2010 5:14 AM

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would make the instructions in this post obsolete. I have not made any recent updates to my ga.js, but it should still work fine. You shouldn't be missing anything by using it.

September 8, 2009 4:08 PM

PJ said:

Hi,

Thanks alot for such a great post. I tried following your instructions but seems like have made some mistake in implementing. I am not passing "zzz", will this affect the functionality of the code? here is how the code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("");
pageTracker._setCampMediumKey("");
pageTracker._setCampSourceKey("");
pageTracker._setCampTermKey("");
pageTracker._setCampContentKey("");
pageTracker._setCampCIdKey("");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

Also, do i need to make changes on the shopping cart related pages as well? My site uses a 3rd party shopping cart.

September 30, 2009 10:24 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:

1. If you call the functions and pass nothing, that works fine:

pageTracker._setReferrerOverride();
pageTracker._setCampNameKey();
pageTracker._setCampMediumKey();
pageTracker._setCampSourceKey();
pageTracker._setCampTermKey();
pageTracker._setCampContentKey();
pageTracker._setCampCIdKey();

But I wouldn't pass the empty string as this can cause unexpected issues.

2. Most likely you won't need to make the same changes on the 3rd party shopping cart because there shouldn't be any traffic going directly to the shopping cart. As long as you have your tracking code set up correctly for multiple domains with all the proper modifications for links between domains, that should be sufficient.

October 1, 2009 8:13 AM

PJ said:

Hi Jeremy,

I tried the suggested code by passing "zzz" but it seems like every time i visit the page, the source information in the cookie is getting overwritten. Here is my entire code, can you please have a look and help me out in correcting the mistake i am making.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
"http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) { pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

October 1, 2009 2:13 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:I don't see anything wrong with the code in your comment. Do you have any additional code on your page such as Google Website Optimizer code?

October 1, 2009 2:42 PM

PJ said:

Hi Jeremy,

Just figured out that there are 2 different google analytics code on the website. Is this the reason why the source is getting owerwritten in then cookie? The second code has not been customized according to your suggestions.

Thanks & Regards

October 1, 2009 11:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ: Yes, that's why the source is getting overwritten. It's a limitation of the customization that it has to be done for Google Analytics (and Google Website Optimizer) tracking code snippet on the site, otherwise it won't work.

This is one of the advantages of using the local ga.js file I provide: since it uses a separate set of cookies, those cookies won't get overwritten unless you have another piece of tracking code that also uses the local ga.js file.

October 2, 2009 7:59 AM

Levi Page said:

What happens when their is a second purchase though? Wouldn't the visits to purchase be incorrect? Would it know show visits going back even before the first purchase?

October 10, 2009 10:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Levi: Yes, it should show all the visits, even before the first purchase. If you're using last referrer tracking, this could still happen if the referrer happens not to change. So using initial referrer instead doesn't make this more incorrect, if by correct you mean that it should only count the number of visits since the last purchase.

October 13, 2009 7:43 PM

Matthias Galica said:

In the event that you've setup adwords to pass GCLID into Analytics in a way that's intelligible as a Traffic Source (we've arbitrarily called it wtm_ coding) then can all the same functionality detailed in this post be achieved by utilizing one of the new Custom Variables through the method _setCustomVar ?

November 18, 2009 1:21 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mattias: You could get the data into Google Analytics by passing parameter values into custom variables. The downside is that you'll now have two ways to look at referral data: one that uses the standard reports (last touch) and one that uses custom variables (first touch). The fact that custom variables give you a lot of flexibility also means that they lack the structure that the standard reports will give you, so it may not be as clear what's being reported on at any given point.

The main problem with using custom variables to track initial referral data is that impressions, clicks, cost, etc., data is not associated in any way with custom variables. If you wanted to track ROI, you would have to do this manually. With the method given in this post, initial referrer data is automatically tied to AdWords cost data just as last referrer data is.

November 18, 2009 1:36 PM

Boaz said:

Hi Jeremy,
1st of all thanks a lot for your post. I've been looking for this type of solutions for one of my clients, as I want to show him a comparison of an initial referrer profile against a last referrer (default) profile.

I've implemented your solutions a couple of months ago. My original profile stayed the same, with each transaction and its value being associated with the last referrer.

In the Initial Referrer profile ALL transactions are now associated to "(direct) / (none)". I checked again and again and cannot understand what the problem is.

Here is the relevant code on my confirmation page:


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-1227832-1");
pageTracker._setDomainName("danhotels.com");
pageTracker._initData();
pageTracker._trackPageview();




try {
var firstTracker = _roit._getTracker("UA-1227832-6");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();

pageTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
pageTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
pageTracker._trackTrans();

firstTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
firstTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
firstTracker._trackTrans();

}catch(err) {}


December 10, 2009 6:46 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Boaz: I noticed that you have the following line in your Google Analytics Tracking Code:

pageTracker._setDomainName("danhotels.com");

Usually when you set the domain name, it's for a good reason, like having a site with subdomains. When you have multiple tracking objects (pageTracker and firstTracker), it's best to make the same type of changes to each. In this case, that means you'd want to add the following line to your firstTracker code:

firstTracker._setDomainName("danhotels.com");

This is almost certainly the culprit. Normally in situations with subdomains where you don't set the domain name, you would see all transactions showing up as self-referred. In this case, however, that referrer is being ignored because the code on the subdomain is able to find the __roia cookie and override the referrer with "". When it sets its own cookies, however, it's setting them to the full domain, including the subdomain, rather than the root level domain, danhotels.com.

December 10, 2009 7:33 AM

Boaz said:

Thanks Jeremy.
You are right, the reason is a subdomain.
I'll give it a go and let you know.

Boaz

December 13, 2009 3:11 AM

Catherine said:

Great post - I am trying to figure this out and have read through a couple of times. I have a yahoo store and the shopping cart is on another domain- in GA it shows "my domain' as the top referrer - since the shopping cart is on a different domain. I have installed your code on my domain pages but am wondering what code needs to be installed on the shopping cart pages to preserve the "true source" of the traffic, not my domain... I think I am getting close. Also, I have installed GA tracking code for "multiple domains" only on the shopping cart pages and have your code on my website.

Any help would be fantastic.

December 13, 2009 5:48 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Catherine: We recommend that anyone wishing to track ecommerce for Yahoo Store in Google Analytics use Monitus. This will give you the most complete ecommerce tracking solution with Yahoo Store. Most likely, however, you'll have to choose between initial referrer and last referrer. It's unlikely you'll be able to do both with Yahoo Store.

December 14, 2009 9:07 AM

Boaz said:

Jeremy - Thanks a lot for your help. Looks like we resolved the issue, or at least very close to it!!!

Much appreciated.
Boaz

December 15, 2009 3:43 AM

Thomas G. said:

I have put the first code on all our pages as described. Just changed the "UA-XXXXXXX-X" but it does not work.
Every transactions shows our OWN domain as the source or referrer.
Any ideas why?

February 4, 2010 6:03 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Thomas: Most likely you have a third-party shopping cart. You need to modify your tracking code appropriately for multiple domains. There's an article on this in the Analytics Help Center:

http://google.com/support/analytics/bin/answer.py?hl=en&answer=55532

February 8, 2010 12:40 PM

Mark said:

Any update on this for the asynch code?

June 11, 2010 3:49 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mark: Not yet. I've made a note to write an update. I'm aiming to post it sometime this month.

June 11, 2010 7:20 AM

Robert said:

Great article and also works a charm when implemented in Magento.

However, the latest GA paradigm is to use the async code instead of _pageTracker so what would it take to adjust this code to the async tracker? Any news on that?

July 25, 2010 8:47 PM

מכונות קפה said:

Really liked to read what you wrote. I was at the Google's ambassadors program and I learned a lot. But of course it is important to practice to practice and practice again. I would like to recommend the option Analytics few know and really important. Is the segment that allows to isolate specific entrance channel and compare it to other users who enter the site. It brings a great advantage to traffic from different places.
Try it!

מכונות

November 8, 2010 5:14 AM

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would make the instructions in this post obsolete. I have not made any recent updates to my ga.js, but it should still work fine. You shouldn't be missing anything by using it.

September 8, 2009 4:08 PM

PJ said:

Hi,

Thanks alot for such a great post. I tried following your instructions but seems like have made some mistake in implementing. I am not passing "zzz", will this affect the functionality of the code? here is how the code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("");
pageTracker._setCampMediumKey("");
pageTracker._setCampSourceKey("");
pageTracker._setCampTermKey("");
pageTracker._setCampContentKey("");
pageTracker._setCampCIdKey("");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

Also, do i need to make changes on the shopping cart related pages as well? My site uses a 3rd party shopping cart.

September 30, 2009 10:24 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:

1. If you call the functions and pass nothing, that works fine:

pageTracker._setReferrerOverride();
pageTracker._setCampNameKey();
pageTracker._setCampMediumKey();
pageTracker._setCampSourceKey();
pageTracker._setCampTermKey();
pageTracker._setCampContentKey();
pageTracker._setCampCIdKey();

But I wouldn't pass the empty string as this can cause unexpected issues.

2. Most likely you won't need to make the same changes on the 3rd party shopping cart because there shouldn't be any traffic going directly to the shopping cart. As long as you have your tracking code set up correctly for multiple domains with all the proper modifications for links between domains, that should be sufficient.

October 1, 2009 8:13 AM

PJ said:

Hi Jeremy,

I tried the suggested code by passing "zzz" but it seems like every time i visit the page, the source information in the cookie is getting overwritten. Here is my entire code, can you please have a look and help me out in correcting the mistake i am making.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
"http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) { pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

October 1, 2009 2:13 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:I don't see anything wrong with the code in your comment. Do you have any additional code on your page such as Google Website Optimizer code?

October 1, 2009 2:42 PM

PJ said:

Hi Jeremy,

Just figured out that there are 2 different google analytics code on the website. Is this the reason why the source is getting owerwritten in then cookie? The second code has not been customized according to your suggestions.

Thanks & Regards

October 1, 2009 11:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ: Yes, that's why the source is getting overwritten. It's a limitation of the customization that it has to be done for Google Analytics (and Google Website Optimizer) tracking code snippet on the site, otherwise it won't work.

This is one of the advantages of using the local ga.js file I provide: since it uses a separate set of cookies, those cookies won't get overwritten unless you have another piece of tracking code that also uses the local ga.js file.

October 2, 2009 7:59 AM

Levi Page said:

What happens when their is a second purchase though? Wouldn't the visits to purchase be incorrect? Would it know show visits going back even before the first purchase?

October 10, 2009 10:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Levi: Yes, it should show all the visits, even before the first purchase. If you're using last referrer tracking, this could still happen if the referrer happens not to change. So using initial referrer instead doesn't make this more incorrect, if by correct you mean that it should only count the number of visits since the last purchase.

October 13, 2009 7:43 PM

Matthias Galica said:

In the event that you've setup adwords to pass GCLID into Analytics in a way that's intelligible as a Traffic Source (we've arbitrarily called it wtm_ coding) then can all the same functionality detailed in this post be achieved by utilizing one of the new Custom Variables through the method _setCustomVar ?

November 18, 2009 1:21 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mattias: You could get the data into Google Analytics by passing parameter values into custom variables. The downside is that you'll now have two ways to look at referral data: one that uses the standard reports (last touch) and one that uses custom variables (first touch). The fact that custom variables give you a lot of flexibility also means that they lack the structure that the standard reports will give you, so it may not be as clear what's being reported on at any given point.

The main problem with using custom variables to track initial referral data is that impressions, clicks, cost, etc., data is not associated in any way with custom variables. If you wanted to track ROI, you would have to do this manually. With the method given in this post, initial referrer data is automatically tied to AdWords cost data just as last referrer data is.

November 18, 2009 1:36 PM

Boaz said:

Hi Jeremy,
1st of all thanks a lot for your post. I've been looking for this type of solutions for one of my clients, as I want to show him a comparison of an initial referrer profile against a last referrer (default) profile.

I've implemented your solutions a couple of months ago. My original profile stayed the same, with each transaction and its value being associated with the last referrer.

In the Initial Referrer profile ALL transactions are now associated to "(direct) / (none)". I checked again and again and cannot understand what the problem is.

Here is the relevant code on my confirmation page:


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-1227832-1");
pageTracker._setDomainName("danhotels.com");
pageTracker._initData();
pageTracker._trackPageview();




try {
var firstTracker = _roit._getTracker("UA-1227832-6");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();

pageTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
pageTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
pageTracker._trackTrans();

firstTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
firstTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
firstTracker._trackTrans();

}catch(err) {}


December 10, 2009 6:46 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Boaz: I noticed that you have the following line in your Google Analytics Tracking Code:

pageTracker._setDomainName("danhotels.com");

Usually when you set the domain name, it's for a good reason, like having a site with subdomains. When you have multiple tracking objects (pageTracker and firstTracker), it's best to make the same type of changes to each. In this case, that means you'd want to add the following line to your firstTracker code:

firstTracker._setDomainName("danhotels.com");

This is almost certainly the culprit. Normally in situations with subdomains where you don't set the domain name, you would see all transactions showing up as self-referred. In this case, however, that referrer is being ignored because the code on the subdomain is able to find the __roia cookie and override the referrer with "". When it sets its own cookies, however, it's setting them to the full domain, including the subdomain, rather than the root level domain, danhotels.com.

December 10, 2009 7:33 AM

Boaz said:

Thanks Jeremy.
You are right, the reason is a subdomain.
I'll give it a go and let you know.

Boaz

December 13, 2009 3:11 AM

Catherine said:

Great post - I am trying to figure this out and have read through a couple of times. I have a yahoo store and the shopping cart is on another domain- in GA it shows "my domain' as the top referrer - since the shopping cart is on a different domain. I have installed your code on my domain pages but am wondering what code needs to be installed on the shopping cart pages to preserve the "true source" of the traffic, not my domain... I think I am getting close. Also, I have installed GA tracking code for "multiple domains" only on the shopping cart pages and have your code on my website.

Any help would be fantastic.

December 13, 2009 5:48 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Catherine: We recommend that anyone wishing to track ecommerce for Yahoo Store in Google Analytics use Monitus. This will give you the most complete ecommerce tracking solution with Yahoo Store. Most likely, however, you'll have to choose between initial referrer and last referrer. It's unlikely you'll be able to do both with Yahoo Store.

December 14, 2009 9:07 AM

Boaz said:

Jeremy - Thanks a lot for your help. Looks like we resolved the issue, or at least very close to it!!!

Much appreciated.
Boaz

December 15, 2009 3:43 AM

Thomas G. said:

I have put the first code on all our pages as described. Just changed the "UA-XXXXXXX-X" but it does not work.
Every transactions shows our OWN domain as the source or referrer.
Any ideas why?

February 4, 2010 6:03 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Thomas: Most likely you have a third-party shopping cart. You need to modify your tracking code appropriately for multiple domains. There's an article on this in the Analytics Help Center:

http://google.com/support/analytics/bin/answer.py?hl=en&answer=55532

February 8, 2010 12:40 PM

Mark said:

Any update on this for the asynch code?

June 11, 2010 3:49 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mark: Not yet. I've made a note to write an update. I'm aiming to post it sometime this month.

June 11, 2010 7:20 AM

Robert said:

Great article and also works a charm when implemented in Magento.

However, the latest GA paradigm is to use the async code instead of _pageTracker so what would it take to adjust this code to the async tracker? Any news on that?

July 25, 2010 8:47 PM

מכונות קפה said:

Really liked to read what you wrote. I was at the Google's ambassadors program and I learned a lot. But of course it is important to practice to practice and practice again. I would like to recommend the option Analytics few know and really important. Is the segment that allows to isolate specific entrance channel and compare it to other users who enter the site. It brings a great advantage to traffic from different places.
Try it!

מכונות

November 8, 2010 5:14 AM

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would make the instructions in this post obsolete. I have not made any recent updates to my ga.js, but it should still work fine. You shouldn't be missing anything by using it.

September 8, 2009 4:08 PM

PJ said:

Hi,

Thanks alot for such a great post. I tried following your instructions but seems like have made some mistake in implementing. I am not passing "zzz", will this affect the functionality of the code? here is how the code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("");
pageTracker._setCampMediumKey("");
pageTracker._setCampSourceKey("");
pageTracker._setCampTermKey("");
pageTracker._setCampContentKey("");
pageTracker._setCampCIdKey("");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

Also, do i need to make changes on the shopping cart related pages as well? My site uses a 3rd party shopping cart.

September 30, 2009 10:24 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:

1. If you call the functions and pass nothing, that works fine:

pageTracker._setReferrerOverride();
pageTracker._setCampNameKey();
pageTracker._setCampMediumKey();
pageTracker._setCampSourceKey();
pageTracker._setCampTermKey();
pageTracker._setCampContentKey();
pageTracker._setCampCIdKey();

But I wouldn't pass the empty string as this can cause unexpected issues.

2. Most likely you won't need to make the same changes on the 3rd party shopping cart because there shouldn't be any traffic going directly to the shopping cart. As long as you have your tracking code set up correctly for multiple domains with all the proper modifications for links between domains, that should be sufficient.

October 1, 2009 8:13 AM

PJ said:

Hi Jeremy,

I tried the suggested code by passing "zzz" but it seems like every time i visit the page, the source information in the cookie is getting overwritten. Here is my entire code, can you please have a look and help me out in correcting the mistake i am making.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
"http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) { pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

October 1, 2009 2:13 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:I don't see anything wrong with the code in your comment. Do you have any additional code on your page such as Google Website Optimizer code?

October 1, 2009 2:42 PM

PJ said:

Hi Jeremy,

Just figured out that there are 2 different google analytics code on the website. Is this the reason why the source is getting owerwritten in then cookie? The second code has not been customized according to your suggestions.

Thanks & Regards

October 1, 2009 11:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ: Yes, that's why the source is getting overwritten. It's a limitation of the customization that it has to be done for Google Analytics (and Google Website Optimizer) tracking code snippet on the site, otherwise it won't work.

This is one of the advantages of using the local ga.js file I provide: since it uses a separate set of cookies, those cookies won't get overwritten unless you have another piece of tracking code that also uses the local ga.js file.

October 2, 2009 7:59 AM

Levi Page said:

What happens when their is a second purchase though? Wouldn't the visits to purchase be incorrect? Would it know show visits going back even before the first purchase?

October 10, 2009 10:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Levi: Yes, it should show all the visits, even before the first purchase. If you're using last referrer tracking, this could still happen if the referrer happens not to change. So using initial referrer instead doesn't make this more incorrect, if by correct you mean that it should only count the number of visits since the last purchase.

October 13, 2009 7:43 PM

Matthias Galica said:

In the event that you've setup adwords to pass GCLID into Analytics in a way that's intelligible as a Traffic Source (we've arbitrarily called it wtm_ coding) then can all the same functionality detailed in this post be achieved by utilizing one of the new Custom Variables through the method _setCustomVar ?

November 18, 2009 1:21 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mattias: You could get the data into Google Analytics by passing parameter values into custom variables. The downside is that you'll now have two ways to look at referral data: one that uses the standard reports (last touch) and one that uses custom variables (first touch). The fact that custom variables give you a lot of flexibility also means that they lack the structure that the standard reports will give you, so it may not be as clear what's being reported on at any given point.

The main problem with using custom variables to track initial referral data is that impressions, clicks, cost, etc., data is not associated in any way with custom variables. If you wanted to track ROI, you would have to do this manually. With the method given in this post, initial referrer data is automatically tied to AdWords cost data just as last referrer data is.

November 18, 2009 1:36 PM

Boaz said:

Hi Jeremy,
1st of all thanks a lot for your post. I've been looking for this type of solutions for one of my clients, as I want to show him a comparison of an initial referrer profile against a last referrer (default) profile.

I've implemented your solutions a couple of months ago. My original profile stayed the same, with each transaction and its value being associated with the last referrer.

In the Initial Referrer profile ALL transactions are now associated to "(direct) / (none)". I checked again and again and cannot understand what the problem is.

Here is the relevant code on my confirmation page:


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-1227832-1");
pageTracker._setDomainName("danhotels.com");
pageTracker._initData();
pageTracker._trackPageview();




try {
var firstTracker = _roit._getTracker("UA-1227832-6");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();

pageTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
pageTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
pageTracker._trackTrans();

firstTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
firstTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
firstTracker._trackTrans();

}catch(err) {}


December 10, 2009 6:46 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Boaz: I noticed that you have the following line in your Google Analytics Tracking Code:

pageTracker._setDomainName("danhotels.com");

Usually when you set the domain name, it's for a good reason, like having a site with subdomains. When you have multiple tracking objects (pageTracker and firstTracker), it's best to make the same type of changes to each. In this case, that means you'd want to add the following line to your firstTracker code:

firstTracker._setDomainName("danhotels.com");

This is almost certainly the culprit. Normally in situations with subdomains where you don't set the domain name, you would see all transactions showing up as self-referred. In this case, however, that referrer is being ignored because the code on the subdomain is able to find the __roia cookie and override the referrer with "". When it sets its own cookies, however, it's setting them to the full domain, including the subdomain, rather than the root level domain, danhotels.com.

December 10, 2009 7:33 AM

Boaz said:

Thanks Jeremy.
You are right, the reason is a subdomain.
I'll give it a go and let you know.

Boaz

December 13, 2009 3:11 AM

Catherine said:

Great post - I am trying to figure this out and have read through a couple of times. I have a yahoo store and the shopping cart is on another domain- in GA it shows "my domain' as the top referrer - since the shopping cart is on a different domain. I have installed your code on my domain pages but am wondering what code needs to be installed on the shopping cart pages to preserve the "true source" of the traffic, not my domain... I think I am getting close. Also, I have installed GA tracking code for "multiple domains" only on the shopping cart pages and have your code on my website.

Any help would be fantastic.

December 13, 2009 5:48 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Catherine: We recommend that anyone wishing to track ecommerce for Yahoo Store in Google Analytics use Monitus. This will give you the most complete ecommerce tracking solution with Yahoo Store. Most likely, however, you'll have to choose between initial referrer and last referrer. It's unlikely you'll be able to do both with Yahoo Store.

December 14, 2009 9:07 AM

Boaz said:

Jeremy - Thanks a lot for your help. Looks like we resolved the issue, or at least very close to it!!!

Much appreciated.
Boaz

December 15, 2009 3:43 AM

Thomas G. said:

I have put the first code on all our pages as described. Just changed the "UA-XXXXXXX-X" but it does not work.
Every transactions shows our OWN domain as the source or referrer.
Any ideas why?

February 4, 2010 6:03 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Thomas: Most likely you have a third-party shopping cart. You need to modify your tracking code appropriately for multiple domains. There's an article on this in the Analytics Help Center:

http://google.com/support/analytics/bin/answer.py?hl=en&answer=55532

February 8, 2010 12:40 PM

Mark said:

Any update on this for the asynch code?

June 11, 2010 3:49 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mark: Not yet. I've made a note to write an update. I'm aiming to post it sometime this month.

June 11, 2010 7:20 AM

Robert said:

Great article and also works a charm when implemented in Magento.

However, the latest GA paradigm is to use the async code instead of _pageTracker so what would it take to adjust this code to the async tracker? Any news on that?

July 25, 2010 8:47 PM

מכונות קפה said:

Really liked to read what you wrote. I was at the Google's ambassadors program and I learned a lot. But of course it is important to practice to practice and practice again. I would like to recommend the option Analytics few know and really important. Is the segment that allows to isolate specific entrance channel and compare it to other users who enter the site. It brings a great advantage to traffic from different places.
Try it!

מכונות

November 8, 2010 5:14 AM

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would make the instructions in this post obsolete. I have not made any recent updates to my ga.js, but it should still work fine. You shouldn't be missing anything by using it.

September 8, 2009 4:08 PM

PJ said:

Hi,

Thanks alot for such a great post. I tried following your instructions but seems like have made some mistake in implementing. I am not passing "zzz", will this affect the functionality of the code? here is how the code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("");
pageTracker._setCampMediumKey("");
pageTracker._setCampSourceKey("");
pageTracker._setCampTermKey("");
pageTracker._setCampContentKey("");
pageTracker._setCampCIdKey("");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

Also, do i need to make changes on the shopping cart related pages as well? My site uses a 3rd party shopping cart.

September 30, 2009 10:24 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:

1. If you call the functions and pass nothing, that works fine:

pageTracker._setReferrerOverride();
pageTracker._setCampNameKey();
pageTracker._setCampMediumKey();
pageTracker._setCampSourceKey();
pageTracker._setCampTermKey();
pageTracker._setCampContentKey();
pageTracker._setCampCIdKey();

But I wouldn't pass the empty string as this can cause unexpected issues.

2. Most likely you won't need to make the same changes on the 3rd party shopping cart because there shouldn't be any traffic going directly to the shopping cart. As long as you have your tracking code set up correctly for multiple domains with all the proper modifications for links between domains, that should be sufficient.

October 1, 2009 8:13 AM

PJ said:

Hi Jeremy,

I tried the suggested code by passing "zzz" but it seems like every time i visit the page, the source information in the cookie is getting overwritten. Here is my entire code, can you please have a look and help me out in correcting the mistake i am making.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
"http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) { pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

October 1, 2009 2:13 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:I don't see anything wrong with the code in your comment. Do you have any additional code on your page such as Google Website Optimizer code?

October 1, 2009 2:42 PM

PJ said:

Hi Jeremy,

Just figured out that there are 2 different google analytics code on the website. Is this the reason why the source is getting owerwritten in then cookie? The second code has not been customized according to your suggestions.

Thanks & Regards

October 1, 2009 11:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ: Yes, that's why the source is getting overwritten. It's a limitation of the customization that it has to be done for Google Analytics (and Google Website Optimizer) tracking code snippet on the site, otherwise it won't work.

This is one of the advantages of using the local ga.js file I provide: since it uses a separate set of cookies, those cookies won't get overwritten unless you have another piece of tracking code that also uses the local ga.js file.

October 2, 2009 7:59 AM

Levi Page said:

What happens when their is a second purchase though? Wouldn't the visits to purchase be incorrect? Would it know show visits going back even before the first purchase?

October 10, 2009 10:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Levi: Yes, it should show all the visits, even before the first purchase. If you're using last referrer tracking, this could still happen if the referrer happens not to change. So using initial referrer instead doesn't make this more incorrect, if by correct you mean that it should only count the number of visits since the last purchase.

October 13, 2009 7:43 PM

Matthias Galica said:

In the event that you've setup adwords to pass GCLID into Analytics in a way that's intelligible as a Traffic Source (we've arbitrarily called it wtm_ coding) then can all the same functionality detailed in this post be achieved by utilizing one of the new Custom Variables through the method _setCustomVar ?

November 18, 2009 1:21 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mattias: You could get the data into Google Analytics by passing parameter values into custom variables. The downside is that you'll now have two ways to look at referral data: one that uses the standard reports (last touch) and one that uses custom variables (first touch). The fact that custom variables give you a lot of flexibility also means that they lack the structure that the standard reports will give you, so it may not be as clear what's being reported on at any given point.

The main problem with using custom variables to track initial referral data is that impressions, clicks, cost, etc., data is not associated in any way with custom variables. If you wanted to track ROI, you would have to do this manually. With the method given in this post, initial referrer data is automatically tied to AdWords cost data just as last referrer data is.

November 18, 2009 1:36 PM

Boaz said:

Hi Jeremy,
1st of all thanks a lot for your post. I've been looking for this type of solutions for one of my clients, as I want to show him a comparison of an initial referrer profile against a last referrer (default) profile.

I've implemented your solutions a couple of months ago. My original profile stayed the same, with each transaction and its value being associated with the last referrer.

In the Initial Referrer profile ALL transactions are now associated to "(direct) / (none)". I checked again and again and cannot understand what the problem is.

Here is the relevant code on my confirmation page:


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-1227832-1");
pageTracker._setDomainName("danhotels.com");
pageTracker._initData();
pageTracker._trackPageview();




try {
var firstTracker = _roit._getTracker("UA-1227832-6");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();

pageTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
pageTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
pageTracker._trackTrans();

firstTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
firstTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
firstTracker._trackTrans();

}catch(err) {}


December 10, 2009 6:46 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Boaz: I noticed that you have the following line in your Google Analytics Tracking Code:

pageTracker._setDomainName("danhotels.com");

Usually when you set the domain name, it's for a good reason, like having a site with subdomains. When you have multiple tracking objects (pageTracker and firstTracker), it's best to make the same type of changes to each. In this case, that means you'd want to add the following line to your firstTracker code:

firstTracker._setDomainName("danhotels.com");

This is almost certainly the culprit. Normally in situations with subdomains where you don't set the domain name, you would see all transactions showing up as self-referred. In this case, however, that referrer is being ignored because the code on the subdomain is able to find the __roia cookie and override the referrer with "". When it sets its own cookies, however, it's setting them to the full domain, including the subdomain, rather than the root level domain, danhotels.com.

December 10, 2009 7:33 AM

Boaz said:

Thanks Jeremy.
You are right, the reason is a subdomain.
I'll give it a go and let you know.

Boaz

December 13, 2009 3:11 AM

Catherine said:

Great post - I am trying to figure this out and have read through a couple of times. I have a yahoo store and the shopping cart is on another domain- in GA it shows "my domain' as the top referrer - since the shopping cart is on a different domain. I have installed your code on my domain pages but am wondering what code needs to be installed on the shopping cart pages to preserve the "true source" of the traffic, not my domain... I think I am getting close. Also, I have installed GA tracking code for "multiple domains" only on the shopping cart pages and have your code on my website.

Any help would be fantastic.

December 13, 2009 5:48 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Catherine: We recommend that anyone wishing to track ecommerce for Yahoo Store in Google Analytics use Monitus. This will give you the most complete ecommerce tracking solution with Yahoo Store. Most likely, however, you'll have to choose between initial referrer and last referrer. It's unlikely you'll be able to do both with Yahoo Store.

December 14, 2009 9:07 AM

Boaz said:

Jeremy - Thanks a lot for your help. Looks like we resolved the issue, or at least very close to it!!!

Much appreciated.
Boaz

December 15, 2009 3:43 AM

Thomas G. said:

I have put the first code on all our pages as described. Just changed the "UA-XXXXXXX-X" but it does not work.
Every transactions shows our OWN domain as the source or referrer.
Any ideas why?

February 4, 2010 6:03 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Thomas: Most likely you have a third-party shopping cart. You need to modify your tracking code appropriately for multiple domains. There's an article on this in the Analytics Help Center:

http://google.com/support/analytics/bin/answer.py?hl=en&answer=55532

February 8, 2010 12:40 PM

Mark said:

Any update on this for the asynch code?

June 11, 2010 3:49 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mark: Not yet. I've made a note to write an update. I'm aiming to post it sometime this month.

June 11, 2010 7:20 AM

Robert said:

Great article and also works a charm when implemented in Magento.

However, the latest GA paradigm is to use the async code instead of _pageTracker so what would it take to adjust this code to the async tracker? Any news on that?

July 25, 2010 8:47 PM

מכונות קפה said:

Really liked to read what you wrote. I was at the Google's ambassadors program and I learned a lot. But of course it is important to practice to practice and practice again. I would like to recommend the option Analytics few know and really important. Is the segment that allows to isolate specific entrance channel and compare it to other users who enter the site. It brings a great advantage to traffic from different places.
Try it!

מכונות

November 8, 2010 5:14 AM

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would make the instructions in this post obsolete. I have not made any recent updates to my ga.js, but it should still work fine. You shouldn't be missing anything by using it.

September 8, 2009 4:08 PM

PJ said:

Hi,

Thanks alot for such a great post. I tried following your instructions but seems like have made some mistake in implementing. I am not passing "zzz", will this affect the functionality of the code? here is how the code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("");
pageTracker._setCampMediumKey("");
pageTracker._setCampSourceKey("");
pageTracker._setCampTermKey("");
pageTracker._setCampContentKey("");
pageTracker._setCampCIdKey("");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

Also, do i need to make changes on the shopping cart related pages as well? My site uses a 3rd party shopping cart.

September 30, 2009 10:24 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:

1. If you call the functions and pass nothing, that works fine:

pageTracker._setReferrerOverride();
pageTracker._setCampNameKey();
pageTracker._setCampMediumKey();
pageTracker._setCampSourceKey();
pageTracker._setCampTermKey();
pageTracker._setCampContentKey();
pageTracker._setCampCIdKey();

But I wouldn't pass the empty string as this can cause unexpected issues.

2. Most likely you won't need to make the same changes on the 3rd party shopping cart because there shouldn't be any traffic going directly to the shopping cart. As long as you have your tracking code set up correctly for multiple domains with all the proper modifications for links between domains, that should be sufficient.

October 1, 2009 8:13 AM

PJ said:

Hi Jeremy,

I tried the suggested code by passing "zzz" but it seems like every time i visit the page, the source information in the cookie is getting overwritten. Here is my entire code, can you please have a look and help me out in correcting the mistake i am making.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
"http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) { pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

October 1, 2009 2:13 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:I don't see anything wrong with the code in your comment. Do you have any additional code on your page such as Google Website Optimizer code?

October 1, 2009 2:42 PM

PJ said:

Hi Jeremy,

Just figured out that there are 2 different google analytics code on the website. Is this the reason why the source is getting owerwritten in then cookie? The second code has not been customized according to your suggestions.

Thanks & Regards

October 1, 2009 11:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ: Yes, that's why the source is getting overwritten. It's a limitation of the customization that it has to be done for Google Analytics (and Google Website Optimizer) tracking code snippet on the site, otherwise it won't work.

This is one of the advantages of using the local ga.js file I provide: since it uses a separate set of cookies, those cookies won't get overwritten unless you have another piece of tracking code that also uses the local ga.js file.

October 2, 2009 7:59 AM

Levi Page said:

What happens when their is a second purchase though? Wouldn't the visits to purchase be incorrect? Would it know show visits going back even before the first purchase?

October 10, 2009 10:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Levi: Yes, it should show all the visits, even before the first purchase. If you're using last referrer tracking, this could still happen if the referrer happens not to change. So using initial referrer instead doesn't make this more incorrect, if by correct you mean that it should only count the number of visits since the last purchase.

October 13, 2009 7:43 PM

Matthias Galica said:

In the event that you've setup adwords to pass GCLID into Analytics in a way that's intelligible as a Traffic Source (we've arbitrarily called it wtm_ coding) then can all the same functionality detailed in this post be achieved by utilizing one of the new Custom Variables through the method _setCustomVar ?

November 18, 2009 1:21 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mattias: You could get the data into Google Analytics by passing parameter values into custom variables. The downside is that you'll now have two ways to look at referral data: one that uses the standard reports (last touch) and one that uses custom variables (first touch). The fact that custom variables give you a lot of flexibility also means that they lack the structure that the standard reports will give you, so it may not be as clear what's being reported on at any given point.

The main problem with using custom variables to track initial referral data is that impressions, clicks, cost, etc., data is not associated in any way with custom variables. If you wanted to track ROI, you would have to do this manually. With the method given in this post, initial referrer data is automatically tied to AdWords cost data just as last referrer data is.

November 18, 2009 1:36 PM

Boaz said:

Hi Jeremy,
1st of all thanks a lot for your post. I've been looking for this type of solutions for one of my clients, as I want to show him a comparison of an initial referrer profile against a last referrer (default) profile.

I've implemented your solutions a couple of months ago. My original profile stayed the same, with each transaction and its value being associated with the last referrer.

In the Initial Referrer profile ALL transactions are now associated to "(direct) / (none)". I checked again and again and cannot understand what the problem is.

Here is the relevant code on my confirmation page:


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-1227832-1");
pageTracker._setDomainName("danhotels.com");
pageTracker._initData();
pageTracker._trackPageview();




try {
var firstTracker = _roit._getTracker("UA-1227832-6");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();

pageTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
pageTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
pageTracker._trackTrans();

firstTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
firstTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
firstTracker._trackTrans();

}catch(err) {}


December 10, 2009 6:46 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Boaz: I noticed that you have the following line in your Google Analytics Tracking Code:

pageTracker._setDomainName("danhotels.com");

Usually when you set the domain name, it's for a good reason, like having a site with subdomains. When you have multiple tracking objects (pageTracker and firstTracker), it's best to make the same type of changes to each. In this case, that means you'd want to add the following line to your firstTracker code:

firstTracker._setDomainName("danhotels.com");

This is almost certainly the culprit. Normally in situations with subdomains where you don't set the domain name, you would see all transactions showing up as self-referred. In this case, however, that referrer is being ignored because the code on the subdomain is able to find the __roia cookie and override the referrer with "". When it sets its own cookies, however, it's setting them to the full domain, including the subdomain, rather than the root level domain, danhotels.com.

December 10, 2009 7:33 AM

Boaz said:

Thanks Jeremy.
You are right, the reason is a subdomain.
I'll give it a go and let you know.

Boaz

December 13, 2009 3:11 AM

Catherine said:

Great post - I am trying to figure this out and have read through a couple of times. I have a yahoo store and the shopping cart is on another domain- in GA it shows "my domain' as the top referrer - since the shopping cart is on a different domain. I have installed your code on my domain pages but am wondering what code needs to be installed on the shopping cart pages to preserve the "true source" of the traffic, not my domain... I think I am getting close. Also, I have installed GA tracking code for "multiple domains" only on the shopping cart pages and have your code on my website.

Any help would be fantastic.

December 13, 2009 5:48 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Catherine: We recommend that anyone wishing to track ecommerce for Yahoo Store in Google Analytics use Monitus. This will give you the most complete ecommerce tracking solution with Yahoo Store. Most likely, however, you'll have to choose between initial referrer and last referrer. It's unlikely you'll be able to do both with Yahoo Store.

December 14, 2009 9:07 AM

Boaz said:

Jeremy - Thanks a lot for your help. Looks like we resolved the issue, or at least very close to it!!!

Much appreciated.
Boaz

December 15, 2009 3:43 AM

Thomas G. said:

I have put the first code on all our pages as described. Just changed the "UA-XXXXXXX-X" but it does not work.
Every transactions shows our OWN domain as the source or referrer.
Any ideas why?

February 4, 2010 6:03 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Thomas: Most likely you have a third-party shopping cart. You need to modify your tracking code appropriately for multiple domains. There's an article on this in the Analytics Help Center:

http://google.com/support/analytics/bin/answer.py?hl=en&answer=55532

February 8, 2010 12:40 PM

Mark said:

Any update on this for the asynch code?

June 11, 2010 3:49 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mark: Not yet. I've made a note to write an update. I'm aiming to post it sometime this month.

June 11, 2010 7:20 AM

Robert said:

Great article and also works a charm when implemented in Magento.

However, the latest GA paradigm is to use the async code instead of _pageTracker so what would it take to adjust this code to the async tracker? Any news on that?

July 25, 2010 8:47 PM

מכונות קפה said:

Really liked to read what you wrote. I was at the Google's ambassadors program and I learned a lot. But of course it is important to practice to practice and practice again. I would like to recommend the option Analytics few know and really important. Is the segment that allows to isolate specific entrance channel and compare it to other users who enter the site. It brings a great advantage to traffic from different places.
Try it!

מכונות

November 8, 2010 5:14 AM

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would make the instructions in this post obsolete. I have not made any recent updates to my ga.js, but it should still work fine. You shouldn't be missing anything by using it.

September 8, 2009 4:08 PM

PJ said:

Hi,

Thanks alot for such a great post. I tried following your instructions but seems like have made some mistake in implementing. I am not passing "zzz", will this affect the functionality of the code? here is how the code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("");
pageTracker._setCampMediumKey("");
pageTracker._setCampSourceKey("");
pageTracker._setCampTermKey("");
pageTracker._setCampContentKey("");
pageTracker._setCampCIdKey("");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

Also, do i need to make changes on the shopping cart related pages as well? My site uses a 3rd party shopping cart.

September 30, 2009 10:24 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:

1. If you call the functions and pass nothing, that works fine:

pageTracker._setReferrerOverride();
pageTracker._setCampNameKey();
pageTracker._setCampMediumKey();
pageTracker._setCampSourceKey();
pageTracker._setCampTermKey();
pageTracker._setCampContentKey();
pageTracker._setCampCIdKey();

But I wouldn't pass the empty string as this can cause unexpected issues.

2. Most likely you won't need to make the same changes on the 3rd party shopping cart because there shouldn't be any traffic going directly to the shopping cart. As long as you have your tracking code set up correctly for multiple domains with all the proper modifications for links between domains, that should be sufficient.

October 1, 2009 8:13 AM

PJ said:

Hi Jeremy,

I tried the suggested code by passing "zzz" but it seems like every time i visit the page, the source information in the cookie is getting overwritten. Here is my entire code, can you please have a look and help me out in correcting the mistake i am making.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
"http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) { pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

October 1, 2009 2:13 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:I don't see anything wrong with the code in your comment. Do you have any additional code on your page such as Google Website Optimizer code?

October 1, 2009 2:42 PM

PJ said:

Hi Jeremy,

Just figured out that there are 2 different google analytics code on the website. Is this the reason why the source is getting owerwritten in then cookie? The second code has not been customized according to your suggestions.

Thanks & Regards

October 1, 2009 11:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ: Yes, that's why the source is getting overwritten. It's a limitation of the customization that it has to be done for Google Analytics (and Google Website Optimizer) tracking code snippet on the site, otherwise it won't work.

This is one of the advantages of using the local ga.js file I provide: since it uses a separate set of cookies, those cookies won't get overwritten unless you have another piece of tracking code that also uses the local ga.js file.

October 2, 2009 7:59 AM

Levi Page said:

What happens when their is a second purchase though? Wouldn't the visits to purchase be incorrect? Would it know show visits going back even before the first purchase?

October 10, 2009 10:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Levi: Yes, it should show all the visits, even before the first purchase. If you're using last referrer tracking, this could still happen if the referrer happens not to change. So using initial referrer instead doesn't make this more incorrect, if by correct you mean that it should only count the number of visits since the last purchase.

October 13, 2009 7:43 PM

Matthias Galica said:

In the event that you've setup adwords to pass GCLID into Analytics in a way that's intelligible as a Traffic Source (we've arbitrarily called it wtm_ coding) then can all the same functionality detailed in this post be achieved by utilizing one of the new Custom Variables through the method _setCustomVar ?

November 18, 2009 1:21 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mattias: You could get the data into Google Analytics by passing parameter values into custom variables. The downside is that you'll now have two ways to look at referral data: one that uses the standard reports (last touch) and one that uses custom variables (first touch). The fact that custom variables give you a lot of flexibility also means that they lack the structure that the standard reports will give you, so it may not be as clear what's being reported on at any given point.

The main problem with using custom variables to track initial referral data is that impressions, clicks, cost, etc., data is not associated in any way with custom variables. If you wanted to track ROI, you would have to do this manually. With the method given in this post, initial referrer data is automatically tied to AdWords cost data just as last referrer data is.

November 18, 2009 1:36 PM

Boaz said:

Hi Jeremy,
1st of all thanks a lot for your post. I've been looking for this type of solutions for one of my clients, as I want to show him a comparison of an initial referrer profile against a last referrer (default) profile.

I've implemented your solutions a couple of months ago. My original profile stayed the same, with each transaction and its value being associated with the last referrer.

In the Initial Referrer profile ALL transactions are now associated to "(direct) / (none)". I checked again and again and cannot understand what the problem is.

Here is the relevant code on my confirmation page:


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-1227832-1");
pageTracker._setDomainName("danhotels.com");
pageTracker._initData();
pageTracker._trackPageview();




try {
var firstTracker = _roit._getTracker("UA-1227832-6");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();

pageTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
pageTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
pageTracker._trackTrans();

firstTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
firstTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
firstTracker._trackTrans();

}catch(err) {}


December 10, 2009 6:46 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Boaz: I noticed that you have the following line in your Google Analytics Tracking Code:

pageTracker._setDomainName("danhotels.com");

Usually when you set the domain name, it's for a good reason, like having a site with subdomains. When you have multiple tracking objects (pageTracker and firstTracker), it's best to make the same type of changes to each. In this case, that means you'd want to add the following line to your firstTracker code:

firstTracker._setDomainName("danhotels.com");

This is almost certainly the culprit. Normally in situations with subdomains where you don't set the domain name, you would see all transactions showing up as self-referred. In this case, however, that referrer is being ignored because the code on the subdomain is able to find the __roia cookie and override the referrer with "". When it sets its own cookies, however, it's setting them to the full domain, including the subdomain, rather than the root level domain, danhotels.com.

December 10, 2009 7:33 AM

Boaz said:

Thanks Jeremy.
You are right, the reason is a subdomain.
I'll give it a go and let you know.

Boaz

December 13, 2009 3:11 AM

Catherine said:

Great post - I am trying to figure this out and have read through a couple of times. I have a yahoo store and the shopping cart is on another domain- in GA it shows "my domain' as the top referrer - since the shopping cart is on a different domain. I have installed your code on my domain pages but am wondering what code needs to be installed on the shopping cart pages to preserve the "true source" of the traffic, not my domain... I think I am getting close. Also, I have installed GA tracking code for "multiple domains" only on the shopping cart pages and have your code on my website.

Any help would be fantastic.

December 13, 2009 5:48 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Catherine: We recommend that anyone wishing to track ecommerce for Yahoo Store in Google Analytics use Monitus. This will give you the most complete ecommerce tracking solution with Yahoo Store. Most likely, however, you'll have to choose between initial referrer and last referrer. It's unlikely you'll be able to do both with Yahoo Store.

December 14, 2009 9:07 AM

Boaz said:

Jeremy - Thanks a lot for your help. Looks like we resolved the issue, or at least very close to it!!!

Much appreciated.
Boaz

December 15, 2009 3:43 AM

Thomas G. said:

I have put the first code on all our pages as described. Just changed the "UA-XXXXXXX-X" but it does not work.
Every transactions shows our OWN domain as the source or referrer.
Any ideas why?

February 4, 2010 6:03 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Thomas: Most likely you have a third-party shopping cart. You need to modify your tracking code appropriately for multiple domains. There's an article on this in the Analytics Help Center:

http://google.com/support/analytics/bin/answer.py?hl=en&answer=55532

February 8, 2010 12:40 PM

Mark said:

Any update on this for the asynch code?

June 11, 2010 3:49 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mark: Not yet. I've made a note to write an update. I'm aiming to post it sometime this month.

June 11, 2010 7:20 AM

Robert said:

Great article and also works a charm when implemented in Magento.

However, the latest GA paradigm is to use the async code instead of _pageTracker so what would it take to adjust this code to the async tracker? Any news on that?

July 25, 2010 8:47 PM

מכונות קפה said:

Really liked to read what you wrote. I was at the Google's ambassadors program and I learned a lot. But of course it is important to practice to practice and practice again. I would like to recommend the option Analytics few know and really important. Is the segment that allows to isolate specific entrance channel and compare it to other users who enter the site. It brings a great advantage to traffic from different places.
Try it!

מכונות

November 8, 2010 5:14 AM

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would make the instructions in this post obsolete. I have not made any recent updates to my ga.js, but it should still work fine. You shouldn't be missing anything by using it.

September 8, 2009 4:08 PM

PJ said:

Hi,

Thanks alot for such a great post. I tried following your instructions but seems like have made some mistake in implementing. I am not passing "zzz", will this affect the functionality of the code? here is how the code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("");
pageTracker._setCampMediumKey("");
pageTracker._setCampSourceKey("");
pageTracker._setCampTermKey("");
pageTracker._setCampContentKey("");
pageTracker._setCampCIdKey("");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

Also, do i need to make changes on the shopping cart related pages as well? My site uses a 3rd party shopping cart.

September 30, 2009 10:24 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:

1. If you call the functions and pass nothing, that works fine:

pageTracker._setReferrerOverride();
pageTracker._setCampNameKey();
pageTracker._setCampMediumKey();
pageTracker._setCampSourceKey();
pageTracker._setCampTermKey();
pageTracker._setCampContentKey();
pageTracker._setCampCIdKey();

But I wouldn't pass the empty string as this can cause unexpected issues.

2. Most likely you won't need to make the same changes on the 3rd party shopping cart because there shouldn't be any traffic going directly to the shopping cart. As long as you have your tracking code set up correctly for multiple domains with all the proper modifications for links between domains, that should be sufficient.

October 1, 2009 8:13 AM

PJ said:

Hi Jeremy,

I tried the suggested code by passing "zzz" but it seems like every time i visit the page, the source information in the cookie is getting overwritten. Here is my entire code, can you please have a look and help me out in correcting the mistake i am making.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
"http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) { pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

October 1, 2009 2:13 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:I don't see anything wrong with the code in your comment. Do you have any additional code on your page such as Google Website Optimizer code?

October 1, 2009 2:42 PM

PJ said:

Hi Jeremy,

Just figured out that there are 2 different google analytics code on the website. Is this the reason why the source is getting owerwritten in then cookie? The second code has not been customized according to your suggestions.

Thanks & Regards

October 1, 2009 11:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ: Yes, that's why the source is getting overwritten. It's a limitation of the customization that it has to be done for Google Analytics (and Google Website Optimizer) tracking code snippet on the site, otherwise it won't work.

This is one of the advantages of using the local ga.js file I provide: since it uses a separate set of cookies, those cookies won't get overwritten unless you have another piece of tracking code that also uses the local ga.js file.

October 2, 2009 7:59 AM

Levi Page said:

What happens when their is a second purchase though? Wouldn't the visits to purchase be incorrect? Would it know show visits going back even before the first purchase?

October 10, 2009 10:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Levi: Yes, it should show all the visits, even before the first purchase. If you're using last referrer tracking, this could still happen if the referrer happens not to change. So using initial referrer instead doesn't make this more incorrect, if by correct you mean that it should only count the number of visits since the last purchase.

October 13, 2009 7:43 PM

Matthias Galica said:

In the event that you've setup adwords to pass GCLID into Analytics in a way that's intelligible as a Traffic Source (we've arbitrarily called it wtm_ coding) then can all the same functionality detailed in this post be achieved by utilizing one of the new Custom Variables through the method _setCustomVar ?

November 18, 2009 1:21 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mattias: You could get the data into Google Analytics by passing parameter values into custom variables. The downside is that you'll now have two ways to look at referral data: one that uses the standard reports (last touch) and one that uses custom variables (first touch). The fact that custom variables give you a lot of flexibility also means that they lack the structure that the standard reports will give you, so it may not be as clear what's being reported on at any given point.

The main problem with using custom variables to track initial referral data is that impressions, clicks, cost, etc., data is not associated in any way with custom variables. If you wanted to track ROI, you would have to do this manually. With the method given in this post, initial referrer data is automatically tied to AdWords cost data just as last referrer data is.

November 18, 2009 1:36 PM

Boaz said:

Hi Jeremy,
1st of all thanks a lot for your post. I've been looking for this type of solutions for one of my clients, as I want to show him a comparison of an initial referrer profile against a last referrer (default) profile.

I've implemented your solutions a couple of months ago. My original profile stayed the same, with each transaction and its value being associated with the last referrer.

In the Initial Referrer profile ALL transactions are now associated to "(direct) / (none)". I checked again and again and cannot understand what the problem is.

Here is the relevant code on my confirmation page:


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-1227832-1");
pageTracker._setDomainName("danhotels.com");
pageTracker._initData();
pageTracker._trackPageview();




try {
var firstTracker = _roit._getTracker("UA-1227832-6");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();

pageTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
pageTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
pageTracker._trackTrans();

firstTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
firstTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
firstTracker._trackTrans();

}catch(err) {}


December 10, 2009 6:46 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Boaz: I noticed that you have the following line in your Google Analytics Tracking Code:

pageTracker._setDomainName("danhotels.com");

Usually when you set the domain name, it's for a good reason, like having a site with subdomains. When you have multiple tracking objects (pageTracker and firstTracker), it's best to make the same type of changes to each. In this case, that means you'd want to add the following line to your firstTracker code:

firstTracker._setDomainName("danhotels.com");

This is almost certainly the culprit. Normally in situations with subdomains where you don't set the domain name, you would see all transactions showing up as self-referred. In this case, however, that referrer is being ignored because the code on the subdomain is able to find the __roia cookie and override the referrer with "". When it sets its own cookies, however, it's setting them to the full domain, including the subdomain, rather than the root level domain, danhotels.com.

December 10, 2009 7:33 AM

Boaz said:

Thanks Jeremy.
You are right, the reason is a subdomain.
I'll give it a go and let you know.

Boaz

December 13, 2009 3:11 AM

Catherine said:

Great post - I am trying to figure this out and have read through a couple of times. I have a yahoo store and the shopping cart is on another domain- in GA it shows "my domain' as the top referrer - since the shopping cart is on a different domain. I have installed your code on my domain pages but am wondering what code needs to be installed on the shopping cart pages to preserve the "true source" of the traffic, not my domain... I think I am getting close. Also, I have installed GA tracking code for "multiple domains" only on the shopping cart pages and have your code on my website.

Any help would be fantastic.

December 13, 2009 5:48 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Catherine: We recommend that anyone wishing to track ecommerce for Yahoo Store in Google Analytics use Monitus. This will give you the most complete ecommerce tracking solution with Yahoo Store. Most likely, however, you'll have to choose between initial referrer and last referrer. It's unlikely you'll be able to do both with Yahoo Store.

December 14, 2009 9:07 AM

Boaz said:

Jeremy - Thanks a lot for your help. Looks like we resolved the issue, or at least very close to it!!!

Much appreciated.
Boaz

December 15, 2009 3:43 AM

Thomas G. said:

I have put the first code on all our pages as described. Just changed the "UA-XXXXXXX-X" but it does not work.
Every transactions shows our OWN domain as the source or referrer.
Any ideas why?

February 4, 2010 6:03 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Thomas: Most likely you have a third-party shopping cart. You need to modify your tracking code appropriately for multiple domains. There's an article on this in the Analytics Help Center:

http://google.com/support/analytics/bin/answer.py?hl=en&answer=55532

February 8, 2010 12:40 PM

Mark said:

Any update on this for the asynch code?

June 11, 2010 3:49 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mark: Not yet. I've made a note to write an update. I'm aiming to post it sometime this month.

June 11, 2010 7:20 AM

Robert said:

Great article and also works a charm when implemented in Magento.

However, the latest GA paradigm is to use the async code instead of _pageTracker so what would it take to adjust this code to the async tracker? Any news on that?

July 25, 2010 8:47 PM

מכונות קפה said:

Really liked to read what you wrote. I was at the Google's ambassadors program and I learned a lot. But of course it is important to practice to practice and practice again. I would like to recommend the option Analytics few know and really important. Is the segment that allows to isolate specific entrance channel and compare it to other users who enter the site. It brings a great advantage to traffic from different places.
Try it!

מכונות

November 8, 2010 5:14 AM

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would make the instructions in this post obsolete. I have not made any recent updates to my ga.js, but it should still work fine. You shouldn't be missing anything by using it.

September 8, 2009 4:08 PM

PJ said:

Hi,

Thanks alot for such a great post. I tried following your instructions but seems like have made some mistake in implementing. I am not passing "zzz", will this affect the functionality of the code? here is how the code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("");
pageTracker._setCampMediumKey("");
pageTracker._setCampSourceKey("");
pageTracker._setCampTermKey("");
pageTracker._setCampContentKey("");
pageTracker._setCampCIdKey("");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

Also, do i need to make changes on the shopping cart related pages as well? My site uses a 3rd party shopping cart.

September 30, 2009 10:24 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:

1. If you call the functions and pass nothing, that works fine:

pageTracker._setReferrerOverride();
pageTracker._setCampNameKey();
pageTracker._setCampMediumKey();
pageTracker._setCampSourceKey();
pageTracker._setCampTermKey();
pageTracker._setCampContentKey();
pageTracker._setCampCIdKey();

But I wouldn't pass the empty string as this can cause unexpected issues.

2. Most likely you won't need to make the same changes on the 3rd party shopping cart because there shouldn't be any traffic going directly to the shopping cart. As long as you have your tracking code set up correctly for multiple domains with all the proper modifications for links between domains, that should be sufficient.

October 1, 2009 8:13 AM

PJ said:

Hi Jeremy,

I tried the suggested code by passing "zzz" but it seems like every time i visit the page, the source information in the cookie is getting overwritten. Here is my entire code, can you please have a look and help me out in correcting the mistake i am making.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
"http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) { pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

October 1, 2009 2:13 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:I don't see anything wrong with the code in your comment. Do you have any additional code on your page such as Google Website Optimizer code?

October 1, 2009 2:42 PM

PJ said:

Hi Jeremy,

Just figured out that there are 2 different google analytics code on the website. Is this the reason why the source is getting owerwritten in then cookie? The second code has not been customized according to your suggestions.

Thanks & Regards

October 1, 2009 11:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ: Yes, that's why the source is getting overwritten. It's a limitation of the customization that it has to be done for Google Analytics (and Google Website Optimizer) tracking code snippet on the site, otherwise it won't work.

This is one of the advantages of using the local ga.js file I provide: since it uses a separate set of cookies, those cookies won't get overwritten unless you have another piece of tracking code that also uses the local ga.js file.

October 2, 2009 7:59 AM

Levi Page said:

What happens when their is a second purchase though? Wouldn't the visits to purchase be incorrect? Would it know show visits going back even before the first purchase?

October 10, 2009 10:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Levi: Yes, it should show all the visits, even before the first purchase. If you're using last referrer tracking, this could still happen if the referrer happens not to change. So using initial referrer instead doesn't make this more incorrect, if by correct you mean that it should only count the number of visits since the last purchase.

October 13, 2009 7:43 PM

Matthias Galica said:

In the event that you've setup adwords to pass GCLID into Analytics in a way that's intelligible as a Traffic Source (we've arbitrarily called it wtm_ coding) then can all the same functionality detailed in this post be achieved by utilizing one of the new Custom Variables through the method _setCustomVar ?

November 18, 2009 1:21 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mattias: You could get the data into Google Analytics by passing parameter values into custom variables. The downside is that you'll now have two ways to look at referral data: one that uses the standard reports (last touch) and one that uses custom variables (first touch). The fact that custom variables give you a lot of flexibility also means that they lack the structure that the standard reports will give you, so it may not be as clear what's being reported on at any given point.

The main problem with using custom variables to track initial referral data is that impressions, clicks, cost, etc., data is not associated in any way with custom variables. If you wanted to track ROI, you would have to do this manually. With the method given in this post, initial referrer data is automatically tied to AdWords cost data just as last referrer data is.

November 18, 2009 1:36 PM

Boaz said:

Hi Jeremy,
1st of all thanks a lot for your post. I've been looking for this type of solutions for one of my clients, as I want to show him a comparison of an initial referrer profile against a last referrer (default) profile.

I've implemented your solutions a couple of months ago. My original profile stayed the same, with each transaction and its value being associated with the last referrer.

In the Initial Referrer profile ALL transactions are now associated to "(direct) / (none)". I checked again and again and cannot understand what the problem is.

Here is the relevant code on my confirmation page:


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-1227832-1");
pageTracker._setDomainName("danhotels.com");
pageTracker._initData();
pageTracker._trackPageview();




try {
var firstTracker = _roit._getTracker("UA-1227832-6");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();

pageTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
pageTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
pageTracker._trackTrans();

firstTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
firstTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
firstTracker._trackTrans();

}catch(err) {}


December 10, 2009 6:46 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Boaz: I noticed that you have the following line in your Google Analytics Tracking Code:

pageTracker._setDomainName("danhotels.com");

Usually when you set the domain name, it's for a good reason, like having a site with subdomains. When you have multiple tracking objects (pageTracker and firstTracker), it's best to make the same type of changes to each. In this case, that means you'd want to add the following line to your firstTracker code:

firstTracker._setDomainName("danhotels.com");

This is almost certainly the culprit. Normally in situations with subdomains where you don't set the domain name, you would see all transactions showing up as self-referred. In this case, however, that referrer is being ignored because the code on the subdomain is able to find the __roia cookie and override the referrer with "". When it sets its own cookies, however, it's setting them to the full domain, including the subdomain, rather than the root level domain, danhotels.com.

December 10, 2009 7:33 AM

Boaz said:

Thanks Jeremy.
You are right, the reason is a subdomain.
I'll give it a go and let you know.

Boaz

December 13, 2009 3:11 AM

Catherine said:

Great post - I am trying to figure this out and have read through a couple of times. I have a yahoo store and the shopping cart is on another domain- in GA it shows "my domain' as the top referrer - since the shopping cart is on a different domain. I have installed your code on my domain pages but am wondering what code needs to be installed on the shopping cart pages to preserve the "true source" of the traffic, not my domain... I think I am getting close. Also, I have installed GA tracking code for "multiple domains" only on the shopping cart pages and have your code on my website.

Any help would be fantastic.

December 13, 2009 5:48 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Catherine: We recommend that anyone wishing to track ecommerce for Yahoo Store in Google Analytics use Monitus. This will give you the most complete ecommerce tracking solution with Yahoo Store. Most likely, however, you'll have to choose between initial referrer and last referrer. It's unlikely you'll be able to do both with Yahoo Store.

December 14, 2009 9:07 AM

Boaz said:

Jeremy - Thanks a lot for your help. Looks like we resolved the issue, or at least very close to it!!!

Much appreciated.
Boaz

December 15, 2009 3:43 AM

Thomas G. said:

I have put the first code on all our pages as described. Just changed the "UA-XXXXXXX-X" but it does not work.
Every transactions shows our OWN domain as the source or referrer.
Any ideas why?

February 4, 2010 6:03 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Thomas: Most likely you have a third-party shopping cart. You need to modify your tracking code appropriately for multiple domains. There's an article on this in the Analytics Help Center:

http://google.com/support/analytics/bin/answer.py?hl=en&answer=55532

February 8, 2010 12:40 PM

Mark said:

Any update on this for the asynch code?

June 11, 2010 3:49 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mark: Not yet. I've made a note to write an update. I'm aiming to post it sometime this month.

June 11, 2010 7:20 AM

Robert said:

Great article and also works a charm when implemented in Magento.

However, the latest GA paradigm is to use the async code instead of _pageTracker so what would it take to adjust this code to the async tracker? Any news on that?

July 25, 2010 8:47 PM

מכונות קפה said:

Really liked to read what you wrote. I was at the Google's ambassadors program and I learned a lot. But of course it is important to practice to practice and practice again. I would like to recommend the option Analytics few know and really important. Is the segment that allows to isolate specific entrance channel and compare it to other users who enter the site. It brings a great advantage to traffic from different places.
Try it!

מכונות

November 8, 2010 5:14 AM

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would make the instructions in this post obsolete. I have not made any recent updates to my ga.js, but it should still work fine. You shouldn't be missing anything by using it.

September 8, 2009 4:08 PM

PJ said:

Hi,

Thanks alot for such a great post. I tried following your instructions but seems like have made some mistake in implementing. I am not passing "zzz", will this affect the functionality of the code? here is how the code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("");
pageTracker._setCampMediumKey("");
pageTracker._setCampSourceKey("");
pageTracker._setCampTermKey("");
pageTracker._setCampContentKey("");
pageTracker._setCampCIdKey("");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

Also, do i need to make changes on the shopping cart related pages as well? My site uses a 3rd party shopping cart.

September 30, 2009 10:24 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:

1. If you call the functions and pass nothing, that works fine:

pageTracker._setReferrerOverride();
pageTracker._setCampNameKey();
pageTracker._setCampMediumKey();
pageTracker._setCampSourceKey();
pageTracker._setCampTermKey();
pageTracker._setCampContentKey();
pageTracker._setCampCIdKey();

But I wouldn't pass the empty string as this can cause unexpected issues.

2. Most likely you won't need to make the same changes on the 3rd party shopping cart because there shouldn't be any traffic going directly to the shopping cart. As long as you have your tracking code set up correctly for multiple domains with all the proper modifications for links between domains, that should be sufficient.

October 1, 2009 8:13 AM

PJ said:

Hi Jeremy,

I tried the suggested code by passing "zzz" but it seems like every time i visit the page, the source information in the cookie is getting overwritten. Here is my entire code, can you please have a look and help me out in correcting the mistake i am making.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
"http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) { pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

October 1, 2009 2:13 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:I don't see anything wrong with the code in your comment. Do you have any additional code on your page such as Google Website Optimizer code?

October 1, 2009 2:42 PM

PJ said:

Hi Jeremy,

Just figured out that there are 2 different google analytics code on the website. Is this the reason why the source is getting owerwritten in then cookie? The second code has not been customized according to your suggestions.

Thanks & Regards

October 1, 2009 11:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ: Yes, that's why the source is getting overwritten. It's a limitation of the customization that it has to be done for Google Analytics (and Google Website Optimizer) tracking code snippet on the site, otherwise it won't work.

This is one of the advantages of using the local ga.js file I provide: since it uses a separate set of cookies, those cookies won't get overwritten unless you have another piece of tracking code that also uses the local ga.js file.

October 2, 2009 7:59 AM

Levi Page said:

What happens when their is a second purchase though? Wouldn't the visits to purchase be incorrect? Would it know show visits going back even before the first purchase?

October 10, 2009 10:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Levi: Yes, it should show all the visits, even before the first purchase. If you're using last referrer tracking, this could still happen if the referrer happens not to change. So using initial referrer instead doesn't make this more incorrect, if by correct you mean that it should only count the number of visits since the last purchase.

October 13, 2009 7:43 PM

Matthias Galica said:

In the event that you've setup adwords to pass GCLID into Analytics in a way that's intelligible as a Traffic Source (we've arbitrarily called it wtm_ coding) then can all the same functionality detailed in this post be achieved by utilizing one of the new Custom Variables through the method _setCustomVar ?

November 18, 2009 1:21 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mattias: You could get the data into Google Analytics by passing parameter values into custom variables. The downside is that you'll now have two ways to look at referral data: one that uses the standard reports (last touch) and one that uses custom variables (first touch). The fact that custom variables give you a lot of flexibility also means that they lack the structure that the standard reports will give you, so it may not be as clear what's being reported on at any given point.

The main problem with using custom variables to track initial referral data is that impressions, clicks, cost, etc., data is not associated in any way with custom variables. If you wanted to track ROI, you would have to do this manually. With the method given in this post, initial referrer data is automatically tied to AdWords cost data just as last referrer data is.

November 18, 2009 1:36 PM

Boaz said:

Hi Jeremy,
1st of all thanks a lot for your post. I've been looking for this type of solutions for one of my clients, as I want to show him a comparison of an initial referrer profile against a last referrer (default) profile.

I've implemented your solutions a couple of months ago. My original profile stayed the same, with each transaction and its value being associated with the last referrer.

In the Initial Referrer profile ALL transactions are now associated to "(direct) / (none)". I checked again and again and cannot understand what the problem is.

Here is the relevant code on my confirmation page:


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-1227832-1");
pageTracker._setDomainName("danhotels.com");
pageTracker._initData();
pageTracker._trackPageview();




try {
var firstTracker = _roit._getTracker("UA-1227832-6");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();

pageTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
pageTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
pageTracker._trackTrans();

firstTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
firstTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
firstTracker._trackTrans();

}catch(err) {}


December 10, 2009 6:46 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Boaz: I noticed that you have the following line in your Google Analytics Tracking Code:

pageTracker._setDomainName("danhotels.com");

Usually when you set the domain name, it's for a good reason, like having a site with subdomains. When you have multiple tracking objects (pageTracker and firstTracker), it's best to make the same type of changes to each. In this case, that means you'd want to add the following line to your firstTracker code:

firstTracker._setDomainName("danhotels.com");

This is almost certainly the culprit. Normally in situations with subdomains where you don't set the domain name, you would see all transactions showing up as self-referred. In this case, however, that referrer is being ignored because the code on the subdomain is able to find the __roia cookie and override the referrer with "". When it sets its own cookies, however, it's setting them to the full domain, including the subdomain, rather than the root level domain, danhotels.com.

December 10, 2009 7:33 AM

Boaz said:

Thanks Jeremy.
You are right, the reason is a subdomain.
I'll give it a go and let you know.

Boaz

December 13, 2009 3:11 AM

Catherine said:

Great post - I am trying to figure this out and have read through a couple of times. I have a yahoo store and the shopping cart is on another domain- in GA it shows "my domain' as the top referrer - since the shopping cart is on a different domain. I have installed your code on my domain pages but am wondering what code needs to be installed on the shopping cart pages to preserve the "true source" of the traffic, not my domain... I think I am getting close. Also, I have installed GA tracking code for "multiple domains" only on the shopping cart pages and have your code on my website.

Any help would be fantastic.

December 13, 2009 5:48 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Catherine: We recommend that anyone wishing to track ecommerce for Yahoo Store in Google Analytics use Monitus. This will give you the most complete ecommerce tracking solution with Yahoo Store. Most likely, however, you'll have to choose between initial referrer and last referrer. It's unlikely you'll be able to do both with Yahoo Store.

December 14, 2009 9:07 AM

Boaz said:

Jeremy - Thanks a lot for your help. Looks like we resolved the issue, or at least very close to it!!!

Much appreciated.
Boaz

December 15, 2009 3:43 AM

Thomas G. said:

I have put the first code on all our pages as described. Just changed the "UA-XXXXXXX-X" but it does not work.
Every transactions shows our OWN domain as the source or referrer.
Any ideas why?

February 4, 2010 6:03 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Thomas: Most likely you have a third-party shopping cart. You need to modify your tracking code appropriately for multiple domains. There's an article on this in the Analytics Help Center:

http://google.com/support/analytics/bin/answer.py?hl=en&answer=55532

February 8, 2010 12:40 PM

Mark said:

Any update on this for the asynch code?

June 11, 2010 3:49 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mark: Not yet. I've made a note to write an update. I'm aiming to post it sometime this month.

June 11, 2010 7:20 AM

Robert said:

Great article and also works a charm when implemented in Magento.

However, the latest GA paradigm is to use the async code instead of _pageTracker so what would it take to adjust this code to the async tracker? Any news on that?

July 25, 2010 8:47 PM

מכונות קפה said:

Really liked to read what you wrote. I was at the Google's ambassadors program and I learned a lot. But of course it is important to practice to practice and practice again. I would like to recommend the option Analytics few know and really important. Is the segment that allows to isolate specific entrance channel and compare it to other users who enter the site. It brings a great advantage to traffic from different places.
Try it!

מכונות

November 8, 2010 5:14 AM

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would make the instructions in this post obsolete. I have not made any recent updates to my ga.js, but it should still work fine. You shouldn't be missing anything by using it.

September 8, 2009 4:08 PM

PJ said:

Hi,

Thanks alot for such a great post. I tried following your instructions but seems like have made some mistake in implementing. I am not passing "zzz", will this affect the functionality of the code? here is how the code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("");
pageTracker._setCampMediumKey("");
pageTracker._setCampSourceKey("");
pageTracker._setCampTermKey("");
pageTracker._setCampContentKey("");
pageTracker._setCampCIdKey("");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

Also, do i need to make changes on the shopping cart related pages as well? My site uses a 3rd party shopping cart.

September 30, 2009 10:24 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:

1. If you call the functions and pass nothing, that works fine:

pageTracker._setReferrerOverride();
pageTracker._setCampNameKey();
pageTracker._setCampMediumKey();
pageTracker._setCampSourceKey();
pageTracker._setCampTermKey();
pageTracker._setCampContentKey();
pageTracker._setCampCIdKey();

But I wouldn't pass the empty string as this can cause unexpected issues.

2. Most likely you won't need to make the same changes on the 3rd party shopping cart because there shouldn't be any traffic going directly to the shopping cart. As long as you have your tracking code set up correctly for multiple domains with all the proper modifications for links between domains, that should be sufficient.

October 1, 2009 8:13 AM

PJ said:

Hi Jeremy,

I tried the suggested code by passing "zzz" but it seems like every time i visit the page, the source information in the cookie is getting overwritten. Here is my entire code, can you please have a look and help me out in correcting the mistake i am making.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
"http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) { pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

October 1, 2009 2:13 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:I don't see anything wrong with the code in your comment. Do you have any additional code on your page such as Google Website Optimizer code?

October 1, 2009 2:42 PM

PJ said:

Hi Jeremy,

Just figured out that there are 2 different google analytics code on the website. Is this the reason why the source is getting owerwritten in then cookie? The second code has not been customized according to your suggestions.

Thanks & Regards

October 1, 2009 11:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ: Yes, that's why the source is getting overwritten. It's a limitation of the customization that it has to be done for Google Analytics (and Google Website Optimizer) tracking code snippet on the site, otherwise it won't work.

This is one of the advantages of using the local ga.js file I provide: since it uses a separate set of cookies, those cookies won't get overwritten unless you have another piece of tracking code that also uses the local ga.js file.

October 2, 2009 7:59 AM

Levi Page said:

What happens when their is a second purchase though? Wouldn't the visits to purchase be incorrect? Would it know show visits going back even before the first purchase?

October 10, 2009 10:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Levi: Yes, it should show all the visits, even before the first purchase. If you're using last referrer tracking, this could still happen if the referrer happens not to change. So using initial referrer instead doesn't make this more incorrect, if by correct you mean that it should only count the number of visits since the last purchase.

October 13, 2009 7:43 PM

Matthias Galica said:

In the event that you've setup adwords to pass GCLID into Analytics in a way that's intelligible as a Traffic Source (we've arbitrarily called it wtm_ coding) then can all the same functionality detailed in this post be achieved by utilizing one of the new Custom Variables through the method _setCustomVar ?

November 18, 2009 1:21 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mattias: You could get the data into Google Analytics by passing parameter values into custom variables. The downside is that you'll now have two ways to look at referral data: one that uses the standard reports (last touch) and one that uses custom variables (first touch). The fact that custom variables give you a lot of flexibility also means that they lack the structure that the standard reports will give you, so it may not be as clear what's being reported on at any given point.

The main problem with using custom variables to track initial referral data is that impressions, clicks, cost, etc., data is not associated in any way with custom variables. If you wanted to track ROI, you would have to do this manually. With the method given in this post, initial referrer data is automatically tied to AdWords cost data just as last referrer data is.

November 18, 2009 1:36 PM

Boaz said:

Hi Jeremy,
1st of all thanks a lot for your post. I've been looking for this type of solutions for one of my clients, as I want to show him a comparison of an initial referrer profile against a last referrer (default) profile.

I've implemented your solutions a couple of months ago. My original profile stayed the same, with each transaction and its value being associated with the last referrer.

In the Initial Referrer profile ALL transactions are now associated to "(direct) / (none)". I checked again and again and cannot understand what the problem is.

Here is the relevant code on my confirmation page:


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-1227832-1");
pageTracker._setDomainName("danhotels.com");
pageTracker._initData();
pageTracker._trackPageview();




try {
var firstTracker = _roit._getTracker("UA-1227832-6");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();

pageTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
pageTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
pageTracker._trackTrans();

firstTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
firstTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
firstTracker._trackTrans();

}catch(err) {}


December 10, 2009 6:46 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Boaz: I noticed that you have the following line in your Google Analytics Tracking Code:

pageTracker._setDomainName("danhotels.com");

Usually when you set the domain name, it's for a good reason, like having a site with subdomains. When you have multiple tracking objects (pageTracker and firstTracker), it's best to make the same type of changes to each. In this case, that means you'd want to add the following line to your firstTracker code:

firstTracker._setDomainName("danhotels.com");

This is almost certainly the culprit. Normally in situations with subdomains where you don't set the domain name, you would see all transactions showing up as self-referred. In this case, however, that referrer is being ignored because the code on the subdomain is able to find the __roia cookie and override the referrer with "". When it sets its own cookies, however, it's setting them to the full domain, including the subdomain, rather than the root level domain, danhotels.com.

December 10, 2009 7:33 AM

Boaz said:

Thanks Jeremy.
You are right, the reason is a subdomain.
I'll give it a go and let you know.

Boaz

December 13, 2009 3:11 AM

Catherine said:

Great post - I am trying to figure this out and have read through a couple of times. I have a yahoo store and the shopping cart is on another domain- in GA it shows "my domain' as the top referrer - since the shopping cart is on a different domain. I have installed your code on my domain pages but am wondering what code needs to be installed on the shopping cart pages to preserve the "true source" of the traffic, not my domain... I think I am getting close. Also, I have installed GA tracking code for "multiple domains" only on the shopping cart pages and have your code on my website.

Any help would be fantastic.

December 13, 2009 5:48 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Catherine: We recommend that anyone wishing to track ecommerce for Yahoo Store in Google Analytics use Monitus. This will give you the most complete ecommerce tracking solution with Yahoo Store. Most likely, however, you'll have to choose between initial referrer and last referrer. It's unlikely you'll be able to do both with Yahoo Store.

December 14, 2009 9:07 AM

Boaz said:

Jeremy - Thanks a lot for your help. Looks like we resolved the issue, or at least very close to it!!!

Much appreciated.
Boaz

December 15, 2009 3:43 AM

Thomas G. said:

I have put the first code on all our pages as described. Just changed the "UA-XXXXXXX-X" but it does not work.
Every transactions shows our OWN domain as the source or referrer.
Any ideas why?

February 4, 2010 6:03 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Thomas: Most likely you have a third-party shopping cart. You need to modify your tracking code appropriately for multiple domains. There's an article on this in the Analytics Help Center:

http://google.com/support/analytics/bin/answer.py?hl=en&answer=55532

February 8, 2010 12:40 PM

Mark said:

Any update on this for the asynch code?

June 11, 2010 3:49 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mark: Not yet. I've made a note to write an update. I'm aiming to post it sometime this month.

June 11, 2010 7:20 AM

Robert said:

Great article and also works a charm when implemented in Magento.

However, the latest GA paradigm is to use the async code instead of _pageTracker so what would it take to adjust this code to the async tracker? Any news on that?

July 25, 2010 8:47 PM

מכונות קפה said:

Really liked to read what you wrote. I was at the Google's ambassadors program and I learned a lot. But of course it is important to practice to practice and practice again. I would like to recommend the option Analytics few know and really important. Is the segment that allows to isolate specific entrance channel and compare it to other users who enter the site. It brings a great advantage to traffic from different places.
Try it!

מכונות

November 8, 2010 5:14 AM

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would make the instructions in this post obsolete. I have not made any recent updates to my ga.js, but it should still work fine. You shouldn't be missing anything by using it.

September 8, 2009 4:08 PM

PJ said:

Hi,

Thanks alot for such a great post. I tried following your instructions but seems like have made some mistake in implementing. I am not passing "zzz", will this affect the functionality of the code? here is how the code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("");
pageTracker._setCampMediumKey("");
pageTracker._setCampSourceKey("");
pageTracker._setCampTermKey("");
pageTracker._setCampContentKey("");
pageTracker._setCampCIdKey("");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

Also, do i need to make changes on the shopping cart related pages as well? My site uses a 3rd party shopping cart.

September 30, 2009 10:24 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:

1. If you call the functions and pass nothing, that works fine:

pageTracker._setReferrerOverride();
pageTracker._setCampNameKey();
pageTracker._setCampMediumKey();
pageTracker._setCampSourceKey();
pageTracker._setCampTermKey();
pageTracker._setCampContentKey();
pageTracker._setCampCIdKey();

But I wouldn't pass the empty string as this can cause unexpected issues.

2. Most likely you won't need to make the same changes on the 3rd party shopping cart because there shouldn't be any traffic going directly to the shopping cart. As long as you have your tracking code set up correctly for multiple domains with all the proper modifications for links between domains, that should be sufficient.

October 1, 2009 8:13 AM

PJ said:

Hi Jeremy,

I tried the suggested code by passing "zzz" but it seems like every time i visit the page, the source information in the cookie is getting overwritten. Here is my entire code, can you please have a look and help me out in correcting the mistake i am making.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
"http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) { pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

October 1, 2009 2:13 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:I don't see anything wrong with the code in your comment. Do you have any additional code on your page such as Google Website Optimizer code?

October 1, 2009 2:42 PM

PJ said:

Hi Jeremy,

Just figured out that there are 2 different google analytics code on the website. Is this the reason why the source is getting owerwritten in then cookie? The second code has not been customized according to your suggestions.

Thanks & Regards

October 1, 2009 11:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ: Yes, that's why the source is getting overwritten. It's a limitation of the customization that it has to be done for Google Analytics (and Google Website Optimizer) tracking code snippet on the site, otherwise it won't work.

This is one of the advantages of using the local ga.js file I provide: since it uses a separate set of cookies, those cookies won't get overwritten unless you have another piece of tracking code that also uses the local ga.js file.

October 2, 2009 7:59 AM

Levi Page said:

What happens when their is a second purchase though? Wouldn't the visits to purchase be incorrect? Would it know show visits going back even before the first purchase?

October 10, 2009 10:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Levi: Yes, it should show all the visits, even before the first purchase. If you're using last referrer tracking, this could still happen if the referrer happens not to change. So using initial referrer instead doesn't make this more incorrect, if by correct you mean that it should only count the number of visits since the last purchase.

October 13, 2009 7:43 PM

Matthias Galica said:

In the event that you've setup adwords to pass GCLID into Analytics in a way that's intelligible as a Traffic Source (we've arbitrarily called it wtm_ coding) then can all the same functionality detailed in this post be achieved by utilizing one of the new Custom Variables through the method _setCustomVar ?

November 18, 2009 1:21 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mattias: You could get the data into Google Analytics by passing parameter values into custom variables. The downside is that you'll now have two ways to look at referral data: one that uses the standard reports (last touch) and one that uses custom variables (first touch). The fact that custom variables give you a lot of flexibility also means that they lack the structure that the standard reports will give you, so it may not be as clear what's being reported on at any given point.

The main problem with using custom variables to track initial referral data is that impressions, clicks, cost, etc., data is not associated in any way with custom variables. If you wanted to track ROI, you would have to do this manually. With the method given in this post, initial referrer data is automatically tied to AdWords cost data just as last referrer data is.

November 18, 2009 1:36 PM

Boaz said:

Hi Jeremy,
1st of all thanks a lot for your post. I've been looking for this type of solutions for one of my clients, as I want to show him a comparison of an initial referrer profile against a last referrer (default) profile.

I've implemented your solutions a couple of months ago. My original profile stayed the same, with each transaction and its value being associated with the last referrer.

In the Initial Referrer profile ALL transactions are now associated to "(direct) / (none)". I checked again and again and cannot understand what the problem is.

Here is the relevant code on my confirmation page:


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-1227832-1");
pageTracker._setDomainName("danhotels.com");
pageTracker._initData();
pageTracker._trackPageview();




try {
var firstTracker = _roit._getTracker("UA-1227832-6");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();

pageTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
pageTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
pageTracker._trackTrans();

firstTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
firstTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
firstTracker._trackTrans();

}catch(err) {}


December 10, 2009 6:46 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Boaz: I noticed that you have the following line in your Google Analytics Tracking Code:

pageTracker._setDomainName("danhotels.com");

Usually when you set the domain name, it's for a good reason, like having a site with subdomains. When you have multiple tracking objects (pageTracker and firstTracker), it's best to make the same type of changes to each. In this case, that means you'd want to add the following line to your firstTracker code:

firstTracker._setDomainName("danhotels.com");

This is almost certainly the culprit. Normally in situations with subdomains where you don't set the domain name, you would see all transactions showing up as self-referred. In this case, however, that referrer is being ignored because the code on the subdomain is able to find the __roia cookie and override the referrer with "". When it sets its own cookies, however, it's setting them to the full domain, including the subdomain, rather than the root level domain, danhotels.com.

December 10, 2009 7:33 AM

Boaz said:

Thanks Jeremy.
You are right, the reason is a subdomain.
I'll give it a go and let you know.

Boaz

December 13, 2009 3:11 AM

Catherine said:

Great post - I am trying to figure this out and have read through a couple of times. I have a yahoo store and the shopping cart is on another domain- in GA it shows "my domain' as the top referrer - since the shopping cart is on a different domain. I have installed your code on my domain pages but am wondering what code needs to be installed on the shopping cart pages to preserve the "true source" of the traffic, not my domain... I think I am getting close. Also, I have installed GA tracking code for "multiple domains" only on the shopping cart pages and have your code on my website.

Any help would be fantastic.

December 13, 2009 5:48 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Catherine: We recommend that anyone wishing to track ecommerce for Yahoo Store in Google Analytics use Monitus. This will give you the most complete ecommerce tracking solution with Yahoo Store. Most likely, however, you'll have to choose between initial referrer and last referrer. It's unlikely you'll be able to do both with Yahoo Store.

December 14, 2009 9:07 AM

Boaz said:

Jeremy - Thanks a lot for your help. Looks like we resolved the issue, or at least very close to it!!!

Much appreciated.
Boaz

December 15, 2009 3:43 AM

Thomas G. said:

I have put the first code on all our pages as described. Just changed the "UA-XXXXXXX-X" but it does not work.
Every transactions shows our OWN domain as the source or referrer.
Any ideas why?

February 4, 2010 6:03 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Thomas: Most likely you have a third-party shopping cart. You need to modify your tracking code appropriately for multiple domains. There's an article on this in the Analytics Help Center:

http://google.com/support/analytics/bin/answer.py?hl=en&answer=55532

February 8, 2010 12:40 PM

Mark said:

Any update on this for the asynch code?

June 11, 2010 3:49 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mark: Not yet. I've made a note to write an update. I'm aiming to post it sometime this month.

June 11, 2010 7:20 AM

Robert said:

Great article and also works a charm when implemented in Magento.

However, the latest GA paradigm is to use the async code instead of _pageTracker so what would it take to adjust this code to the async tracker? Any news on that?

July 25, 2010 8:47 PM

מכונות קפה said:

Really liked to read what you wrote. I was at the Google's ambassadors program and I learned a lot. But of course it is important to practice to practice and practice again. I would like to recommend the option Analytics few know and really important. Is the segment that allows to isolate specific entrance channel and compare it to other users who enter the site. It brings a great advantage to traffic from different places.
Try it!

מכונות

November 8, 2010 5:14 AM

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would make the instructions in this post obsolete. I have not made any recent updates to my ga.js, but it should still work fine. You shouldn't be missing anything by using it.

September 8, 2009 4:08 PM

PJ said:

Hi,

Thanks alot for such a great post. I tried following your instructions but seems like have made some mistake in implementing. I am not passing "zzz", will this affect the functionality of the code? here is how the code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("");
pageTracker._setCampMediumKey("");
pageTracker._setCampSourceKey("");
pageTracker._setCampTermKey("");
pageTracker._setCampContentKey("");
pageTracker._setCampCIdKey("");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

Also, do i need to make changes on the shopping cart related pages as well? My site uses a 3rd party shopping cart.

September 30, 2009 10:24 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:

1. If you call the functions and pass nothing, that works fine:

pageTracker._setReferrerOverride();
pageTracker._setCampNameKey();
pageTracker._setCampMediumKey();
pageTracker._setCampSourceKey();
pageTracker._setCampTermKey();
pageTracker._setCampContentKey();
pageTracker._setCampCIdKey();

But I wouldn't pass the empty string as this can cause unexpected issues.

2. Most likely you won't need to make the same changes on the 3rd party shopping cart because there shouldn't be any traffic going directly to the shopping cart. As long as you have your tracking code set up correctly for multiple domains with all the proper modifications for links between domains, that should be sufficient.

October 1, 2009 8:13 AM

PJ said:

Hi Jeremy,

I tried the suggested code by passing "zzz" but it seems like every time i visit the page, the source information in the cookie is getting overwritten. Here is my entire code, can you please have a look and help me out in correcting the mistake i am making.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
"http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) { pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

October 1, 2009 2:13 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:I don't see anything wrong with the code in your comment. Do you have any additional code on your page such as Google Website Optimizer code?

October 1, 2009 2:42 PM

PJ said:

Hi Jeremy,

Just figured out that there are 2 different google analytics code on the website. Is this the reason why the source is getting owerwritten in then cookie? The second code has not been customized according to your suggestions.

Thanks & Regards

October 1, 2009 11:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ: Yes, that's why the source is getting overwritten. It's a limitation of the customization that it has to be done for Google Analytics (and Google Website Optimizer) tracking code snippet on the site, otherwise it won't work.

This is one of the advantages of using the local ga.js file I provide: since it uses a separate set of cookies, those cookies won't get overwritten unless you have another piece of tracking code that also uses the local ga.js file.

October 2, 2009 7:59 AM

Levi Page said:

What happens when their is a second purchase though? Wouldn't the visits to purchase be incorrect? Would it know show visits going back even before the first purchase?

October 10, 2009 10:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Levi: Yes, it should show all the visits, even before the first purchase. If you're using last referrer tracking, this could still happen if the referrer happens not to change. So using initial referrer instead doesn't make this more incorrect, if by correct you mean that it should only count the number of visits since the last purchase.

October 13, 2009 7:43 PM

Matthias Galica said:

In the event that you've setup adwords to pass GCLID into Analytics in a way that's intelligible as a Traffic Source (we've arbitrarily called it wtm_ coding) then can all the same functionality detailed in this post be achieved by utilizing one of the new Custom Variables through the method _setCustomVar ?

November 18, 2009 1:21 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mattias: You could get the data into Google Analytics by passing parameter values into custom variables. The downside is that you'll now have two ways to look at referral data: one that uses the standard reports (last touch) and one that uses custom variables (first touch). The fact that custom variables give you a lot of flexibility also means that they lack the structure that the standard reports will give you, so it may not be as clear what's being reported on at any given point.

The main problem with using custom variables to track initial referral data is that impressions, clicks, cost, etc., data is not associated in any way with custom variables. If you wanted to track ROI, you would have to do this manually. With the method given in this post, initial referrer data is automatically tied to AdWords cost data just as last referrer data is.

November 18, 2009 1:36 PM

Boaz said:

Hi Jeremy,
1st of all thanks a lot for your post. I've been looking for this type of solutions for one of my clients, as I want to show him a comparison of an initial referrer profile against a last referrer (default) profile.

I've implemented your solutions a couple of months ago. My original profile stayed the same, with each transaction and its value being associated with the last referrer.

In the Initial Referrer profile ALL transactions are now associated to "(direct) / (none)". I checked again and again and cannot understand what the problem is.

Here is the relevant code on my confirmation page:


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-1227832-1");
pageTracker._setDomainName("danhotels.com");
pageTracker._initData();
pageTracker._trackPageview();




try {
var firstTracker = _roit._getTracker("UA-1227832-6");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();

pageTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
pageTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
pageTracker._trackTrans();

firstTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
firstTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
firstTracker._trackTrans();

}catch(err) {}


December 10, 2009 6:46 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Boaz: I noticed that you have the following line in your Google Analytics Tracking Code:

pageTracker._setDomainName("danhotels.com");

Usually when you set the domain name, it's for a good reason, like having a site with subdomains. When you have multiple tracking objects (pageTracker and firstTracker), it's best to make the same type of changes to each. In this case, that means you'd want to add the following line to your firstTracker code:

firstTracker._setDomainName("danhotels.com");

This is almost certainly the culprit. Normally in situations with subdomains where you don't set the domain name, you would see all transactions showing up as self-referred. In this case, however, that referrer is being ignored because the code on the subdomain is able to find the __roia cookie and override the referrer with "". When it sets its own cookies, however, it's setting them to the full domain, including the subdomain, rather than the root level domain, danhotels.com.

December 10, 2009 7:33 AM

Boaz said:

Thanks Jeremy.
You are right, the reason is a subdomain.
I'll give it a go and let you know.

Boaz

December 13, 2009 3:11 AM

Catherine said:

Great post - I am trying to figure this out and have read through a couple of times. I have a yahoo store and the shopping cart is on another domain- in GA it shows "my domain' as the top referrer - since the shopping cart is on a different domain. I have installed your code on my domain pages but am wondering what code needs to be installed on the shopping cart pages to preserve the "true source" of the traffic, not my domain... I think I am getting close. Also, I have installed GA tracking code for "multiple domains" only on the shopping cart pages and have your code on my website.

Any help would be fantastic.

December 13, 2009 5:48 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Catherine: We recommend that anyone wishing to track ecommerce for Yahoo Store in Google Analytics use Monitus. This will give you the most complete ecommerce tracking solution with Yahoo Store. Most likely, however, you'll have to choose between initial referrer and last referrer. It's unlikely you'll be able to do both with Yahoo Store.

December 14, 2009 9:07 AM

Boaz said:

Jeremy - Thanks a lot for your help. Looks like we resolved the issue, or at least very close to it!!!

Much appreciated.
Boaz

December 15, 2009 3:43 AM

Thomas G. said:

I have put the first code on all our pages as described. Just changed the "UA-XXXXXXX-X" but it does not work.
Every transactions shows our OWN domain as the source or referrer.
Any ideas why?

February 4, 2010 6:03 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Thomas: Most likely you have a third-party shopping cart. You need to modify your tracking code appropriately for multiple domains. There's an article on this in the Analytics Help Center:

http://google.com/support/analytics/bin/answer.py?hl=en&answer=55532

February 8, 2010 12:40 PM

Mark said:

Any update on this for the asynch code?

June 11, 2010 3:49 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mark: Not yet. I've made a note to write an update. I'm aiming to post it sometime this month.

June 11, 2010 7:20 AM

Robert said:

Great article and also works a charm when implemented in Magento.

However, the latest GA paradigm is to use the async code instead of _pageTracker so what would it take to adjust this code to the async tracker? Any news on that?

July 25, 2010 8:47 PM

מכונות קפה said:

Really liked to read what you wrote. I was at the Google's ambassadors program and I learned a lot. But of course it is important to practice to practice and practice again. I would like to recommend the option Analytics few know and really important. Is the segment that allows to isolate specific entrance channel and compare it to other users who enter the site. It brings a great advantage to traffic from different places.
Try it!

מכונות

November 8, 2010 5:14 AM

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would make the instructions in this post obsolete. I have not made any recent updates to my ga.js, but it should still work fine. You shouldn't be missing anything by using it.

September 8, 2009 4:08 PM

PJ said:

Hi,

Thanks alot for such a great post. I tried following your instructions but seems like have made some mistake in implementing. I am not passing "zzz", will this affect the functionality of the code? here is how the code looks like:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("");
pageTracker._setCampMediumKey("");
pageTracker._setCampSourceKey("");
pageTracker._setCampTermKey("");
pageTracker._setCampContentKey("");
pageTracker._setCampCIdKey("");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

Also, do i need to make changes on the shopping cart related pages as well? My site uses a 3rd party shopping cart.

September 30, 2009 10:24 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:

1. If you call the functions and pass nothing, that works fine:

pageTracker._setReferrerOverride();
pageTracker._setCampNameKey();
pageTracker._setCampMediumKey();
pageTracker._setCampSourceKey();
pageTracker._setCampTermKey();
pageTracker._setCampContentKey();
pageTracker._setCampCIdKey();

But I wouldn't pass the empty string as this can cause unexpected issues.

2. Most likely you won't need to make the same changes on the 3rd party shopping cart because there shouldn't be any traffic going directly to the shopping cart. As long as you have your tracking code set up correctly for multiple domains with all the proper modifications for links between domains, that should be sufficient.

October 1, 2009 8:13 AM

PJ said:

Hi Jeremy,

I tried the suggested code by passing "zzz" but it seems like every time i visit the page, the source information in the cookie is getting overwritten. Here is my entire code, can you please have a look and help me out in correcting the mistake i am making.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
"http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
if(document.cookie.match("(^|;\\s)__utma=")) { pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowAnchor(true);
pageTracker._trackPageview();
} catch(err) {}</script>

October 1, 2009 2:13 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ:I don't see anything wrong with the code in your comment. Do you have any additional code on your page such as Google Website Optimizer code?

October 1, 2009 2:42 PM

PJ said:

Hi Jeremy,

Just figured out that there are 2 different google analytics code on the website. Is this the reason why the source is getting owerwritten in then cookie? The second code has not been customized according to your suggestions.

Thanks & Regards

October 1, 2009 11:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@PJ: Yes, that's why the source is getting overwritten. It's a limitation of the customization that it has to be done for Google Analytics (and Google Website Optimizer) tracking code snippet on the site, otherwise it won't work.

This is one of the advantages of using the local ga.js file I provide: since it uses a separate set of cookies, those cookies won't get overwritten unless you have another piece of tracking code that also uses the local ga.js file.

October 2, 2009 7:59 AM

Levi Page said:

What happens when their is a second purchase though? Wouldn't the visits to purchase be incorrect? Would it know show visits going back even before the first purchase?

October 10, 2009 10:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Levi: Yes, it should show all the visits, even before the first purchase. If you're using last referrer tracking, this could still happen if the referrer happens not to change. So using initial referrer instead doesn't make this more incorrect, if by correct you mean that it should only count the number of visits since the last purchase.

October 13, 2009 7:43 PM

Matthias Galica said:

In the event that you've setup adwords to pass GCLID into Analytics in a way that's intelligible as a Traffic Source (we've arbitrarily called it wtm_ coding) then can all the same functionality detailed in this post be achieved by utilizing one of the new Custom Variables through the method _setCustomVar ?

November 18, 2009 1:21 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mattias: You could get the data into Google Analytics by passing parameter values into custom variables. The downside is that you'll now have two ways to look at referral data: one that uses the standard reports (last touch) and one that uses custom variables (first touch). The fact that custom variables give you a lot of flexibility also means that they lack the structure that the standard reports will give you, so it may not be as clear what's being reported on at any given point.

The main problem with using custom variables to track initial referral data is that impressions, clicks, cost, etc., data is not associated in any way with custom variables. If you wanted to track ROI, you would have to do this manually. With the method given in this post, initial referrer data is automatically tied to AdWords cost data just as last referrer data is.

November 18, 2009 1:36 PM

Boaz said:

Hi Jeremy,
1st of all thanks a lot for your post. I've been looking for this type of solutions for one of my clients, as I want to show him a comparison of an initial referrer profile against a last referrer (default) profile.

I've implemented your solutions a couple of months ago. My original profile stayed the same, with each transaction and its value being associated with the last referrer.

In the Initial Referrer profile ALL transactions are now associated to "(direct) / (none)". I checked again and again and cannot understand what the problem is.

Here is the relevant code on my confirmation page:


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-1227832-1");
pageTracker._setDomainName("danhotels.com");
pageTracker._initData();
pageTracker._trackPageview();




try {
var firstTracker = _roit._getTracker("UA-1227832-6");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();

pageTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
pageTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
pageTracker._trackTrans();

firstTracker._addTrans(
"51436",
"Guest",
"329",
"[tax]",
"[shipping]",
"Ashklon",
"[state]",
"Israel"
);
firstTracker._addItem(
"51436",
"[SKU/code]",
"Dan Gardens Ashkelon",
"Standard Room",
"329",
"2"
);
firstTracker._trackTrans();

}catch(err) {}


December 10, 2009 6:46 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Boaz: I noticed that you have the following line in your Google Analytics Tracking Code:

pageTracker._setDomainName("danhotels.com");

Usually when you set the domain name, it's for a good reason, like having a site with subdomains. When you have multiple tracking objects (pageTracker and firstTracker), it's best to make the same type of changes to each. In this case, that means you'd want to add the following line to your firstTracker code:

firstTracker._setDomainName("danhotels.com");

This is almost certainly the culprit. Normally in situations with subdomains where you don't set the domain name, you would see all transactions showing up as self-referred. In this case, however, that referrer is being ignored because the code on the subdomain is able to find the __roia cookie and override the referrer with "". When it sets its own cookies, however, it's setting them to the full domain, including the subdomain, rather than the root level domain, danhotels.com.

December 10, 2009 7:33 AM

Boaz said:

Thanks Jeremy.
You are right, the reason is a subdomain.
I'll give it a go and let you know.

Boaz

December 13, 2009 3:11 AM

Catherine said:

Great post - I am trying to figure this out and have read through a couple of times. I have a yahoo store and the shopping cart is on another domain- in GA it shows "my domain' as the top referrer - since the shopping cart is on a different domain. I have installed your code on my domain pages but am wondering what code needs to be installed on the shopping cart pages to preserve the "true source" of the traffic, not my domain... I think I am getting close. Also, I have installed GA tracking code for "multiple domains" only on the shopping cart pages and have your code on my website.

Any help would be fantastic.

December 13, 2009 5:48 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Catherine: We recommend that anyone wishing to track ecommerce for Yahoo Store in Google Analytics use Monitus. This will give you the most complete ecommerce tracking solution with Yahoo Store. Most likely, however, you'll have to choose between initial referrer and last referrer. It's unlikely you'll be able to do both with Yahoo Store.

December 14, 2009 9:07 AM

Boaz said:

Jeremy - Thanks a lot for your help. Looks like we resolved the issue, or at least very close to it!!!

Much appreciated.
Boaz

December 15, 2009 3:43 AM

Thomas G. said:

I have put the first code on all our pages as described. Just changed the "UA-XXXXXXX-X" but it does not work.
Every transactions shows our OWN domain as the source or referrer.
Any ideas why?

February 4, 2010 6:03 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Thomas: Most likely you have a third-party shopping cart. You need to modify your tracking code appropriately for multiple domains. There's an article on this in the Analytics Help Center:

http://google.com/support/analytics/bin/answer.py?hl=en&answer=55532

February 8, 2010 12:40 PM

Mark said:

Any update on this for the asynch code?

June 11, 2010 3:49 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Mark: Not yet. I've made a note to write an update. I'm aiming to post it sometime this month.

June 11, 2010 7:20 AM

Robert said:

Great article and also works a charm when implemented in Magento.

However, the latest GA paradigm is to use the async code instead of _pageTracker so what would it take to adjust this code to the async tracker? Any news on that?

July 25, 2010 8:47 PM

מכונות קפה said:

Really liked to read what you wrote. I was at the Google's ambassadors program and I learned a lot. But of course it is important to practice to practice and practice again. I would like to recommend the option Analytics few know and really important. Is the segment that allows to isolate specific entrance channel and compare it to other users who enter the site. It brings a great advantage to traffic from different places.
Try it!

מכונות

November 8, 2010 5:14 AM

Jam said:

Would this help to track transaction from organic traffic the same way we do with paid search?


May 22, 2009 3:31 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jam: Yes, if the initial referrer is organic, it will be preserved with this code the same way that a paid search referral would be preserved. This code keeps the initial referrer regardless of its source/medium. If you use the multiple profile approach, you can also see whether organic traffic tends to be more of the initial source that leads to a transaction or the final source before a transaction.

May 22, 2009 8:00 AM

Think.ux said:

Thank you so much Jeremy.

Would that work only for transactions or is there a way to use (or to tweak) it so it would work on goals attribution as well?

Thanks,
David.

May 22, 2009 9:33 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: This will work for goal conversions as well. A goal for a lead would tend to occur on the initial visit anyway, but for longer range goals where the referral source might change in between, this same script can be used to see the referrers initially responsible for those visitors who later converted verses the last referrer before conversion. It's a little less clear than for transactions because you can't look at a single goal conversion and see its initial and final referrer, but you can look at the difference in conversion rate for a initial referrers vs. final referrers.

May 22, 2009 9:55 AM

Ophir Prusak said:

Great Post Jeremy.

I've been thinking about something like this for a while - a solution to track both first and last campaigns touches.

This is especially important with AdWords where branded keywords are often the last touchpoint, but a non-branded keyword is what caused the initial visit.

This should work with Adwords as well - correct ??

Kudos for actually creating a solution.

- Ophir

May 22, 2009 10:39 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ophir: Yes, this works for AdWords as well. That's the benefit of this solution over similar ones that store initial referral data in the User Defined variable or elsewhere. This will tell you which AdWords campaigns, adgroups, keyword, and ad content were initially responsible for transactions, whereas other solutions can only tell you this if you are manually tagging.

May 22, 2009 10:55 AM

Greg said:

Would you post before and after screen shots, to show what a screen in Google Analytics will look like after this solution is implemented? If that's possible, thanks!

May 22, 2009 6:26 PM

David said:

Thank you Jeremy for your comment. I've followed your instructions..and a weird thing is happening:

When i load my conversion page (with e commerce code filled out) with both codes i get 0 transactions on my initial referrer profile and 2 transactions on my "regular" profile..

Here is the code..
is there something wrong with the way i used it?



try {
var firstTracker = _roit._getTracker("UA-1234-1");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview('newcode/thankyou');
} catch(err) {}


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-1234-2");
pageTracker._trackPageview('standardcode/thankyou');
} catch(err) {}

pageTracker._addTrans(
"OrderID23882084", // required
"100nis",
"tel aviv",
"Israel"
);

pageTracker._addItem(
"order-id1234", // required
"SKU",
"ROI T shirt",
"Cloth",
"99nis", // required
"1" //required
);

pageTracker._trackTrans();


May 24, 2009 8:39 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@David: You'll need to run through the same transaction functions using firstTracker in addition to pageTracker. So something like this would work:

<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

or this:

<script>
function gaEcommerce(pageTracker) {
    pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
    pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
    pageTracker._trackTrans();
}
gaEcommerce(pageTracker);
gaEcommerce(firstTracker);
</script>

or this:

<form style="display:none;" name="utmform">
    <textarea id="utmtrans">
        UTM:T|23882084||100|0|0|tel aviv|Israel|Israel
        UTM:I|23882084|DDD123|ROI T shirt|Cloth|99|1
    </textarea>
</form>

...

<script>
pageTracker._setTrans();
pageTracker._trackTrans();
firstTracker._setTrans();
firstTracker._trackTrans();
</script>

However you decide to do it, the main point is that you'll need to run ecommerce for both tracker objects. The same thing applies to virtual pageviews, event tracking, and user defined values.

May 26, 2009 8:31 AM

Amit said:

Hi Jeremy,
When would be best to use each function?

Do the come "on top" of the firstTracker "bundle" ?

May 26, 2009 11:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: Each function should be used after assigning the account number:

var firstTracker = _gat._getTracker("UA-XXXXXXX-X");

but before the _trackPageview call:

firstTracker._trackPageview();

This is true regardless of your purpose in using these functions. They can't be used before the account number because the object doesn't exist until then, and they can't be used after _trackPageview because by then Google Analytics has already used the default referrer and tags.

May 26, 2009 1:31 PM

Amit said:

Hi Jeremy,
Thanks for your patience.
So..the right way to place these would be as:



var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


try {
var pageTracker = _gat._getTracker("UA-630933-3338");
pageTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();

pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}



try {
var firstTracker = _roit._getTracker("UA-630933-333");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._addTrans("23882084", "", "100", "0", "0", "tel aviv", "Israel", "Israel");
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}

May 26, 2009 2:16 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I think I misunderstood which functions you were referring to in your previous comment. Here's how it should look:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview('GA/Newthankyou');
} catch(err) {}</script>
<script type="text/javascript" src="/path/to/ga.js"></script>
<script type="text/javascript">
try {
    var firstTracker = _roit._getTracker("UA-XXXXXXX-X");
    if(document.cookie.match("(^|;\\s)__roia=")) {
        firstTracker._setReferrerOverride("");
        firstTracker._setCampNameKey("zzz");
        firstTracker._setCampMediumKey("zzz");
        firstTracker._setCampSourceKey("zzz");
        firstTracker._setCampTermKey("zzz");
        firstTracker._setCampContentKey("zzz");
        firstTracker._setCampCIdKey("zzz");
    }
    firstTracker._trackPageview('ROI/Newthankyou');
} catch(err) {}</script>
<script>
pageTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
pageTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1"); pageTracker._trackTrans();
firstTracker._addTrans("23882084", "", "100", "0", "0", ""tel aviv", "Israel", "Israel);
firstTracker._addItem("23882084", "DDD123", "ROI T shirt", "Cloth", "99", "1");
firstTracker._trackTrans();
</script>

So as you can see, all ecommerce functions should be called after the Google Analytics Tracking Codes. You can also call just the ecommerce functions for pageTracker after the pageTracker Google Analytics Tracking Code, but before the firstTracker Google Analytics Tracking Code.

May 26, 2009 3:28 PM

Amit said:

Great.
If i understand it right,
the way you've set it up in your example will not override the initial referrer on both profiles.

Did i get it?
:)

May 26, 2009 4:47 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: You're right. That kind of defeats the purpose of having separate profiles, doesn't it :) I've fixed my comment.

May 26, 2009 5:19 PM

amit said:

U R the man.
:)

Tnx.

May 26, 2009 7:27 PM

Amit said:

Jeremy,
Im not sure if you are aware of that (or if its an implamantaion bug) but when looking at the report "Visits to purchase" i see a difference between the 2 profiles.

Lets say i had 3 visits prior to purchase / transaction.

On the profile with the initial referrer i do see 3 visits to purchase

though on the "regular" profile i see 1 visit to purchase...

Are you familiar with that?

May 31, 2009 1:05 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Amit: I'm glad you've brought this issue up, as it illustrates a particular advantage of the initial referrer profile over the last referrer profile when it comes to Google Analytics. The reason for the difference is that the Visits to Purchase report actually tracks the Visits to Purchase from the last campaign.

The campaign may change multiple times for a visitor in the last referrer profile, so your visits to purchase will not be the actual number of visits prior to purchase, only the number of visits starting with the first visit from their most recent campaign. On the other hand, campaigns never change in the initial referrer profile, so the visits to purchase will actually be true visits to purchase.

This explanation comes from Question #5 on this post from Avinash's blog. Definitely worth a look. Sometime in the future (I have no inside knowledge on this one, but I'm hopeful) this may change so that visits to purchase always reflects true visits to purchase. For now, this issue is taken care of in the initial referrer profile.

June 1, 2009 8:15 AM

Amit said:

I've been breaking my heads out...

Thanks..now i can leave this subject.

:)

June 1, 2009 11:48 AM

Jen said:

thanks so much - verrrrry useful post.

June 2, 2009 4:06 PM

Jen said:

I have a question:
all I need to change is the UA code?
What about the "zzz" stuff? Will it work like that?
(you can tell I'm out of my depth here, but hey at least I'm asking questions)

June 4, 2009 7:43 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: If you're wanting to switch from last referrer to initial referrer for all profiles in your Google Analytics account, then you can use the first script, putting your own account number in place of the UA-XXXXXXX-X.

The "zzz" stuff is literally supposed to be "zzz". Ordinarily Google Analytics will look for the standard utm parameters to determine campaign data for visitors:

http://www.roirevolution.com/landing_page/?utm_source=yahoo&utm_medium=cpc&utm_campaign=general&utm_term=tracking+transactions

Using the functions described in the article, we're telling Google Analytics to look for campaign values in the "zzz" parameter instead:

http://www.roirevolution.com/landing_page?zzz=yahoo&zzz=cpc&zzz=general&zzz=tracking+transactions

Of course we're not actually going to construct URLs like the one above. The point is that Google Analytics will ignore any campaign data passed in the normal utm parameters because it's looking for the values in the "zzz" parameter instead. Since it never finds the "zzz" parameter, it assumes that the referral data has not changed, at least from a tagging standpoint and keeps the previous referral data instead.

June 4, 2009 8:01 AM

Jen said:

Ok and it's the same with the second option after creating a new account number?

June 4, 2009 8:38 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Jen: Yes, same deal, except that you also need to upload the modified ga.js file to your server and change the path of script to point to that file.

June 4, 2009 9:16 AM

Justen said:

This script works excellent. Thank you for sharing this.

I'm wondering if there is a way to accomplish something similar with multiple domains all running on different UA codes.

For example on site one I have:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

On site two I have:

var pageTracker = _gat._getTracker("UA-XXXX-2");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
}
pageTracker._trackPageview();

Site two is our blog site. Here is a typical example of our referral model:

Vistor -> Wikipedia -> our website (UA-XXXX-1)

The referral is stored perfectly from Wikipedia, when the user keeps coming back the referral is stored as Wikipedia.

Then lets say the user comes through another site we have hooked upto GA using this approach:

Visitor -> news site -> our blog (UA-XXXX-2) -> our website (UA-XXXX-1)

The original referral is stored fine until the user clicks from 'our blog' to 'our website'. When I look at the cookie value, it ends up becoming direct traffic:

1.1245184278.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

This only happens when another GA site is involved in decision making...

I can't figure out why, any suggestions?

June 16, 2009 5:02 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: This is probably due to linking between the two domains. I would probably modify the script so that it looks something like the following:

var pageTracker = _gat._getTracker("UA-XXXX-1"); pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowLinker(true);
}
pageTracker._trackPageview();

You should make similar changes to Site Two.

If you don't call pageTracker._setAllowLinker(true), any utm parameters used for linking parameters will be ignored. By only calling pageTracker._setAllowLinker(true) when a visitor sees a domain for the first time, you'll ensure that previous referral data will not be overwritten when a visitor goes from Site Two to Site One and they've already been to Site One.

June 16, 2009 5:57 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Justen: Actually, the "solution" I gave in my last comment is incomplete. pageTracker._setAllowLinker(true) would also need to be called after the pageTracker._trackPageview() call to ensure that you could still link back to the domain.

There are still some cases that wouldn't be handled well that way, however. A better solution would be the following two step process:

1. Modify linking functions for links and forms between domains to set the optional second parameter to true. For example, instead of pageTracker._link(this.href), use pageTracker._link(this.href, true). Specifying true tells Google Analytics to add the linking parameters to the anchor instead of the query string.

2. Use the following tracking code:

var pageTracker = _gat._getTracker("UA-XXXX-1");
pageTracker._setDomainName(".xyz.com");
pageTracker._setCookieTimeout("31526000"); // 1 year
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
if(document.cookie.match("(^|;\\s)__utma=")) {
pageTracker._setReferrerOverride("");
pageTracker._setCampNameKey("zzz");
pageTracker._setCampMediumKey("zzz");
pageTracker._setCampSourceKey("zzz");
pageTracker._setCampTermKey("zzz");
pageTracker._setCampContentKey("zzz");
pageTracker._setCampCIdKey("zzz");
} else {
pageTracker._setAllowAnchor(true);
}
pageTracker._trackPageview();

Calling pageTracker._setAllowAnchor(true) tells Google Analytics to look for campaign and linking parameters in the anchor in addition to the query string. If we don't call this function, Google Analytics won't both searching the anchor, so it will never see the linking parameters.

The only time we want Google Analytics to look in the anchor in this case is when it's the very first visit, as a visitor may have come to this domain from another domain and we'd want to be able to pass that referral data. If each domain has a different initial referrer, this will prevent those initial referrers from being overwritten.

There are several advantages of this solution over the previous. First, it actually works. Second, even if you managed to get the first to work, the solution is still based on the overloaded pageTracker._setAllowLinker function. This function is overloaded because it handles both incoming linking parameters and outgoing linking parameters.

Third, while I certainly don't claim to be an SEO expert of any sort, I do believe that minimizing the use of query parameters can help increase page rank as otherwise each query parameter variation may be considered a separate page. I welcome further comment on this by anyone who does have SEO expertise.

June 17, 2009 8:22 AM

BossMa said:

The code doen't work for me , whats wrong ?

<script type="text/javascript" src="/analytics/ga.js"></script>
<script type="text/javascript">
try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>

</script>

July 1, 2009 11:36 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@BossMa: Most likely you need to move the server side include outside of the script, though I can't tell for sure without knowing what's actually in that include:

<?php
if (strpos($_SERVER['PHP_SELF'], 'checkout_success.php') !== false)
include(DIR_WS_MODULES . 'analytics/analytics.php');
?>
<script type="text/javascript" src="/analytics/ga.js"></script> <script type="text/javascript"> try {
var firstTracker = _roit._getTracker("UA-xxxxx-3");
if(document.cookie.match("(^|;\\s)__roia=")) {
firstTracker._setReferrerOverride("");
firstTracker._setCampNameKey("zzz");
firstTracker._setCampMediumKey("zzz");
firstTracker._setCampSourceKey("zzz");
firstTracker._setCampTermKey("zzz");
firstTracker._setCampContentKey("zzz");
firstTracker._setCampCIdKey("zzz");
}
firstTracker._trackPageview();
} catch(err) {}
</script>

July 1, 2009 1:58 PM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Greg: The Google Analytics reports will essentially look the same as before, except that you'll see initial referrers instead of last referrers. So if I posted screen shots, it would essentially be a screen shot with different numbers for referring sources than before.

Of course, if you implement the solution to have the data be in a different profile, your original profile will actually be untouched. So I'm reluctant to post screen shots since I'm not sure what the benefit would be. Everything looks the same; you just have numbers for initial referrers instead of last referrers.

It doesn't try to store initial referrer in the user defined report. It doesn't try to override source/mediums to show both initial and last referrer in a single report. Either you start seeing initial referrers in your existing profile going forward, or you have a new profile that shows initial referrers instead of last referrers.

If you have something in particular that you're wanting to see, let me know and I'll see if I can provide an appropriate screenshot.

August 19, 2009 10:45 AM

Ivan Canales said:

Hello,

first of all thanks for this great post.

I'm following the original Google code of ga.js and today I see that it is very different that the last week (I compare both with WinMerge). My doubts are: the changes of Google ga.js are relevant and change the goal of this post? the new Google ga.js is compatible with your actual ga.js? and are there an update of your ga.js?

Thanks in advance for response.

Best regards.

September 7, 2009 7:55 AM

Jeremy Aube, Director of Engineering Author Profile Page said:

@Ivan: I checked the ga.js code and didn't see anything that would mak