The Tracking Code by Google Analytics works by default for tracking a single domain or subdomain. Howewer, there are several scenarios where we can need a more complex implementation. Below we explain to users some possible cases and how to solve them.
You can choose program with an asynchronous or traditional syntax. The asynchronous syntax provides the same way to customize the tracking of your domine but it is more flexible than the traditional syntax so it is your decision to choose one option or the other.
Tracking a unique domain and its subdomains
If you want to follow the behaviour of your domain and its subdomains in the same account (or profile), you must to follow the next considerations.
We take as example:
domain: www.carmenworld.com
subdomains: blog.carmenworld.com and shop.carmenworld.com

You must modify your tracking code in the lines shown below.
Asynchronous syntax:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxx-1']);
_gaq.push(['_setDomainName', '.carmenworld.com']);
_gaq.push(['_trackPageview']);- Traditional (ga.js) syntax:
var pageTracker = _gat._getTracker("UA-xxxxx-1");
pageTracker._setDomainName("carmenworld.com");
pageTracker._trackPageview();
Due to the fact the domain name is set to the top-level domain you don’t have to adjust the links or forms of these three sites (all of them share the same cookies).
Tracking a multiple domains and its subdomains
Now, our example has two domains: www.dogs.com and www.cat.com, and many domains like smalls.dogs.com.

We show below the lines you must modify in your Code Tracking.
- Domain 1: www.dogs.com
- Asynchronous syntax
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxx-1']);
_gaq.push(['_setDomainName', 'dogs.com']);
_gaq.push(['_setAllowLinker', true]);
...
<a href="http://www.dogs-com.com/intro.html"
onclick="_gaq.push(['_link', 'http://www.dogs-blog.com/intro.html']); return false;">See my blog</a>
... - Traditional (ga.js) syntax
var pageTracker = _gat._getTracker("UA-xxxxx-1");
pageTracker._setDomainName('dogs.com');
pageTracker._setAllowLinker(true);
...
<a href="http://www.dogs-blog.com/intro.html"
onclick="pageTracker._link('http://www.dogs-blog.com/intro.html'); return false;">See my blog</a>
...
You have to modify only the links that point to www.dog-blog.com. It is not necessary to do with links of the sub-domain due to the fact the domain www.dogs.com let cookie acces for its domains.
- Asynchronous syntax
- Subdomain of Domain 1: small.dogs.com
- Asynchronous syntax
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxx-1']);
_gaq.push(['_setDomainName', 'small.dogs.com']);
_gaq.push(['_setAllowLinker', true]);
...
<a href="http://www.dogs-blog.com/intro.html"
onclick="_gaq.push(['_link', 'http://www.dogs-blog.com/intro.html']); return false;">See my blog</a> - Traditional (ga.js) syntax
var pageTracker = _gat._getTracker("UA-xxxxx-1");
pageTracker._setDomainName('small.dogs.com');
pageTracker._setAllowLinker(true);
...
<a href="http://www.dogs-blog.com/intro.html"
onclick="pageTracker._link('http://www.dogs-blog.com/intro.html'); return false;">See my blog</a>
...
Links belongs to primary domain do not require any change because of they share the same cookies with their primary domain.
- Asynchronous syntax
- Domain 2: www.dogs-blog.com
- Asynchronous syntax
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxx-1']);
_gaq.push(['_setDomainName', 'dogs-blog.com']);
_gaq.push(['_setAllowLinker', true]);
...
<a href="http://small.dogs.com/intro.html"
onclick="_gaq.push(['_link', 'http://small.dogs.com/intro.html']); return false;">See my pet store</a>
... - Traditional (ga.js) syntax
var pageTracker = _gat._getTracker("UA-xxxxx-1");
pageTracker._setDomainName('dogs-blog.com');
pageTracker._setAllowLinker(true);
...
<a href="http://dogs-small.dogs.com/intro.html"
onclick="pageTracker._link('http://small.dogs.com/intro.html'); return false;">See my pet store</a>
...
With these instructions you set the domain name to the top-level so if your want to add neb sub-domains you will let share the cookies in order to not change anything else.
Source: Google Analytics Help.
You can find more information in the next video.
Really like this post! Good job on outlining the why and how in ways newbies can understand and seasoned bloggers can refer to. One to bookmark and share.