Welcome to Freshview thoughts, a place for our ideas and ramblings about building and supporting a popular web application from Sydney.

Stay in the loop

Subscribe to our RSS feed

Dig into a category

Search all posts

Our products

Campaign MonitorThe easiest way for designers to send email newsletters for themselves and their clients.

Setting up Google Analytics and Website Optimizer across multiple domains and sub-domains

Posted by Dave Martin on January 23, 2009 11:09 AM

Setting up Google Analytics and Website Optimizer correctly across multiple domains & subdomains can get pretty tricky. With our latest release of Campaign Monitor, we wanted to make sure that we could utilize both Google Analytics and Website Optimizer to help improve our site. As a result, we had to piece together a solution from about half a dozen different articles. If you ever find yourself in the same boat, here's the step by step tutorial:

Note: This article applies only for the latest version of the Google Analytics tracking code (ga.js).

Step 1) Tag all cross-domain links and forms

The first thing you will need to do is identify ALL links & forms on your site (even links on pages that do not include website optimizer tests) that go from one domain (or sub-domain) to another and tag thcode with the following code:

For links, add: onclick="javascript:firstTracker._link(this.href); return false;"

Example: <a href="https://www.domain2.com" onclick="javascript:firstTracker._link(this.href); return false;">Domain 2</a>

For forms, add: onSubmit="javascript:firstTracker._linkByPost(this);"

Example: <form action="http://www.domain2.com" method="post" onSubmit="javascript:firstTracker._linkByPost(this);">

Notes:

  • firstTracker._link and firstTracker._linkByPost are shared by both Google Analytics and Website Optimizer to track visitors cross-domain

References:

Step 2) Load JS on Domain1.com and all of it's sub-domains

Add the following tracking code to the HEAD section of all pages on domain1.com (including sub-domains, www.domain1.com & sub.domain1.com)

<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 firstTracker = _gat._getTracker("UA-xxxxx-x");
firstTracker._setDomainName(".domain1.com");
firstTracker._setAllowLinker(true);
firstTracker._setAllowHash(false);
firstTracker._trackPageview();

} catch (err) { }

</script>

Notes:

  • You MUST include this code above the links in step 1
  • Be sure to include a "." before your domain for _setDomainName
  • Replace UA-xxxxx-x with your Google Analytics profile ID
  • _setAllowLinker set to true enables linking to another domain for Google Analytics
  • _setAllowHash set to false prevents a unique domain hash (or numerical representation) of the domain name to be used in the cookie

References:

Step 3) Load JS on Domain2.com and all of it's sub-domains

<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 firstTracker = _gat._getTracker("UA-xxxxx-x");
firstTracker._setDomainName(".domain2.com");
firstTracker._setAllowLinker(true);
firstTracker._setAllowHash(false);
firstTracker._trackPageview();
</script>

} catch (err) { }

</script>

Notes:

  • Your tracking ID (UA-xxxxx-x) should be the same as in step 2
  • Be sure to include a "." before your domain for _setDomainName
  • See step 2 for notes and references

Step 4) Add the Website Optimizer Control Script to the top of your test page

Add the following to the very top of your test page (Before the <HTML> tag):

<script type="text/javascript">
_uhash = "off";
_ulink ="1";
_udn ="domain1.com";
</script>

<script type="text/javascript">
function utmx_section(){}function utmx(){}
(function(){var k='XXXXXXXXXX',d=document,l=d.location,c=d.cookie;function f(n){
if(c){var i=c.indexOf(n+'=');if(i>-1){var j=c.indexOf(';',i);return c.substring(i+n.
length+1,j<0?c.length:j)}}}var x=f('__utmx'),xx=f('__utmxx'),h=l.hash;
d.write('<sc'+'ript src="'+
'http'+(l.protocol=='https:'?'s://ssl':'://www')+'.google-analytics.com'
+'/siteopt.js?v=1&utmxkey='+k+'&utmx='+(x?x:'')+'&utmxx='+(xx?xx:'')+'&utmxtime='
+new Date().valueOf()+(h?'&utmxhash='+escape(h.substr(1)):'')+
'" type="text/javascript" charset="utf-8"></sc'+'ript>')})();
</script>

Notes:

  • You must add a reference to _udn above the normal utmx_section function in order for your test to work
  • Be sure to include a "." before your domain for _udn
  • Replace XXXXXXXXXX above with the value found in the control script of your Website Optimizer test

Reference:

Step 5) Add the Website Optimizer tracking Script under the GA JS code on your test page

Your GA JS code on the test page will now look 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 firstTracker = _gat._getTracker("UA-xxxxx-x");
firstTracker._setDomainName(".domain1.com");
firstTracker._setAllowLinker(true);
firstTracker._setAllowHash(false);
firstTracker._trackPageview();

var secondTracker = _gat._getTracker("UA-yyyyy-y");
secondTracker._setDomainName("domain1.com");
secondTracker._setAllowLinker(true);
secondTracker._setAllowHash(false);
secondTracker._trackPageview("/zzzzzzzzzz/test");

} catch (err) { }

</script>

Notes:

  • Again, be sure to include this code above the links in step 1
  • Replace UA-yyyyy-y with your account ID from Website Optimizer
  • Replace /zzzzzzzzzz/test with the ID of your current Website Optimizer test (Found in tracking script code when creating a new multivariate test)
  • Be sure to include a "." before your domain for _setDomainName

Reference:

Step 6) Add the website optimizer page section script to your test page around each test elcodeent

It's pretty simple. You can read more about this in step two of any new multivariate experiment, or by visiting this help topic.

Step 7) Add your website optimizer conversion script to your goal page (the page that users reach after a successful conversion)

Add the following code to the end of your conversion pages source code:

<script>
if(typeof(urchinTracker)!='function')document.write('<sc'+'ript src="'+
'http'+(document.location.protocol=='https:'?'s://ssl':'://www')+
'.google-analytics.com/urchin.js'+'"></sc'+'ript>')
</script>

<script>

try {

_uacct = 'UA-yyyyy-y';
_udn =".domain1.com";
_ulink ="1";
_uhash = "0";
urchinTracker("/zzzzzzzzzz/goal");

} catch (err) { }

</script>

Notes:

  • Again, replace UA-yyyyy-y with your account ID from Website Optimizer
  • And, replace /zzzzzzzzzz/goal with the ID of your current Website Optimizer conversion script (It should have goal at the end instead of test)
  • Be sure to include a "." before your domain for _udn
  • _udn manually sets the domain for Website Optimizer
  • _ulink set to 1 enables linking to another domain for Website Optimizer
  • _uhash set to 0 prevents a unique domain hash (or numerical representation) of the domain name to be used in the cookie

Step 8) Add advanced filter to show domains

Add the following filter to your Google Analytics profile so that you see domains in your content reports.

Filter Type: Custom filter > Advanced
Field A: Hostname
Extract A: (.*)
Field B: Request URI
Extract B: (.*)
Output To: Request URI
Constructor: $A1$B1

That's it - 8 easy (well actually slightly difficult) steps... I hope you find it useful.

A quick thanks to Eric Vasilik and ShoreTel for their help in troubleshooting this code.

11 comments so far

Luke Stevens

wrote on January 23, 2009 3:03 PM

Wow, fantastic write up Dave! Thanks for sharing the tutorial & glad you've solved quite a tricky problem :)

Judd Lyon

wrote on February 11, 2009 12:55 PM

Thanks for taking the time share this. Is it me or does this seem unnecessarily painful?

Travis Bell

wrote on February 20, 2009 12:32 PM

Unnecessarily painful sums it to me as well. Pretty sure this could *a lot* easier. *sigh*

Gavin Doolan

wrote on March 12, 2009 3:04 AM

I agree this needs to be simplified thanks for the excellent write up all the same.

Tarandeep

wrote on April 10, 2009 2:26 AM

Hi,

Thanks for sharing. Am working on it and update you it it work for me. Have a quick question for you guys. In your code snippet at all the places you have used ga.js except Goal page. For conversion code you have used urchin.js, Any specific reason for it???

Tarandeep

wrote on April 11, 2009 1:00 AM

Hey Dave,

It did not work for me :(

alejo jm

wrote on September 11, 2009 1:59 PM

the other way is using PHP :-)


try {
var pageTracker = _gat._getTracker("");
pageTracker._trackPageview();
} catch(err) {}

In the google analytics you must create different key for each domain

alejo jm

wrote on September 11, 2009 2:00 PM

the other way is using PHP :-)
--php
$domains = array('domain1.com'=>'UA-XXXXX-XX1',
'www.domain1.com'=>'UA-XXXXX-XX1',
'www.domain2.com'=>'UA-XXXXX-XX2',
'domain2.com'=>'UA-XXXXX-XX2');
--php

try {
var pageTracker = _gat._getTracker("--php echo $domains[$_SERVER['SERVER_NAME']--php");
pageTracker._trackPageview();
} catch(err) {}

In the google analytics you must create different key for each domain

Mathieu Beausoleil

wrote on December 11, 2009 4:13 AM

Why are you using urchin for the step 7 instead of using the standard GWO conversion code ?

Chris Peters

wrote on January 29, 2010 12:38 AM

This will very likely save my life. Consider yourself a hero, Dave. :D

Nirav gandhi

wrote on February 25, 2010 7:00 PM

I m little confuse for two-main domain analytics,
U have mention extract->A (.*) mens ,we have to put first main domain name and put the . ahead of this
like .xyz.com and for extract->b like .abc.com is it?
$A1$B1 mens u have to add both domain name like $xyz.com$abc.com
am i true sir?if not pls expalin this term $A1$B1

Got anything to add?

Name

Web site

Your comments (basic HTML is fine)