Hello There!

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

 About Riddhi Mehta

Riddhi is an experienced User Experience Designer with a demonstrated history of working in the information technology and services industry. Skilled in Prototyping, Concept Development, Design Thinking, User Experience Design, and Web Design, she has a bachelors in Product Design and a Masters in Communication Design.

Google Analytics Cross Domain OR Multiple Domain Tracking - Automatic

Google Analytics cross domain tracking

During the last several years if we have found one big problem that a lot of users face is Google Analytics Cross domain tracking OR Google Analytics Multiple Domain Tracking & how to handle pushing cookies to different domains or sub-domains. The reason for this is large corporates would have many websites for the same purpose and have several subdomains catering to specific needs. Often the solution is to get the cookie’s value passed via URL. At times such problems can be solved by structuring multiple trackers, but not always. This blog post aims to provide a solution to automate the cookie value passing via form or link using Jquery.

Why do we need cross-domain tracking?

Google Analytics cross domain tracking

Take a look at the above image, which represents a case when a visitor lands on the page xyz.com and he navigates to subdomain.xyz.com or abc.com and/or submits information on the form which is passed to abc.com. Here, Google Analytics cross-domain tracking is not enabled and all the websites are tracked under the same UA id of Google Analytics. One would not be able to visualize the data and would have duplication of data for the same visitor across all the domains.

What is required to enable cross-domain tracking?
  1. We require all domains, subdomains, and 3rd party domains tracked under 1 UA ID
  2. Use _setDomainName “none” - to enable cookie information to be passed across all domains, subdomains, and 3rd party domains.
  3. Use _setAllowLinker “true” before the call of _trackPageView
  4. We require all links and forms which are interconnected to the main domain/sub-domain or 3rd party domain appended with _link() or _linkByPost() function

Above steps would enable you to set up your Google Analytics for Multiple domains & Across different subdomains tracking under one UA id (UA-XXXXX-XX)

Now, one would need to update all links with the _link() function and all forms with _linkByPost() which are passing information to other domains. It will be a supremely time-consuming job to implement and verify through all domains. A quick easy solution is automated appending cookie values to all external domain links and forms.

Automated cross-domain/Multiple domain tracking for Google Analytics

Now, we have come up with an automated process to enable passing cookie information to all such links and forms which are external domains i.e. sub-domains or 3rd party domains.

Here are the 3 simple steps that you’ll need to do.

  1. Include Jquery javascript library reference
  2. Include default Google Analytics asynchronous tracking code with _setDomainName “none” and _setAllowLinker “true”
  3. Include the crossdomaintracking.js  - Download the crossdomaintracking.js

Your code will look like this.

<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.min.js'></script>

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']); //set your account profile id
_gaq.push(['_setDomainName', 'none']); //set domain name none
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script');     ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:'   == document.location.protocol ? 'https://ssl'   : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>

<script type="text/javascript" src="crossdomaintracking.js" ></script>
What will crossdomaintracking.js do?

It will automatically identify all external multiple domains from a given page i.e. if you are on the page xyz.com and links of subdomain.xyz.com and abc.com would be appended with _getLinkerURL function of ga.js and it will append cookie information such as

http://subdomain.xyz.com/index.html?__utmb=….&__utmv=….&_utmk=88886746&__utma=1.789654.321654987…&utmz=..

Requirement

A Jquery Framework (If you are already using a jquery library you don’t need to copy the jquery library reference)
The script needs to be pasted through all pages of your website, where you want to track external links and mail to links.

Note:

When you use it with multiple javascript frameworks necessary safeguard needs to be added for the code. If you are using javascript functions to redirect to external links it wouldn’t work.

Feel free to write your feedback if you have any about it and let us know if we can improve it for you!

 

External Link Tracking through Google Analytics

Every time when we need to track external links and want to see how many links were clicked with our website. We can track using multiple methods i.e. Google Analytic’s trackEvent or Virtual trackPageview.

We can track third party links using trackEvent function of  Google Analytics. But when it comes to appending all links in dynamically generated pages and in static pages. It becomes head-ache for appending the code and testing if everything works fine with each links.

Implement following javascript code to track external links through Google Analytics accurately. It helps in saving time by elimination of adding external link tracking code manually.

Here’s the snippet of the code:

<!-- include jquery javascript framework -->
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.min.js'></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']); //set account id

_gaq.push(['_trackPageview']);

(function() {

var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;

ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

})();

//code block to attach trackevent code for external links/mailto links

$(document).ready(function(){

// Creating custom :external selector

$.expr[':'].external = function(obj){

return !obj.href.match(/^javascript/) && (obj.hostname != location.hostname);

};

// Add 'external' CSS class to all external links

$('a:external').addClass('external');

$('a.external').click(function(){

var obcat = this.href.match(/^mailto:/);

var targeturl = this.href;

if (obcat){

targeturl = targeturl.replace('mailto:','');

_gaq.push(['_trackEvent', 'Mailto', targeturl , document.location.href,1,true]);

}else{

var targetname = this.text;
var isDoc = this.href.match(/.(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|&|?)/);
if (isDoc) {
	_gaq.push(['_trackEvent', 'DocDownlod', targetname, document.location.href,1,true]);
}else{
	_gaq.push(['_trackEvent', 'Outbound', targeturl, document.location.href,1,true]);
}

}

});

$('a').click(function(){
	var targetname = this.text;
	var isDoc = this.href.match(/.(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|&|?)/);
	if (isDoc) {
		_gaq.push(['_trackEvent', 'DocDownlod', targetname, document.location.href,1,true]);
	}
});

});

</script>

Explanation of the code:

Jquery Javascript Framework is required to work with it. Though you can also use mootools, prototype javascript framework.

<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.min.js'></script>

We can include the jquery’s library to work with DOM - Javascript

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']); //set account id
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

Above code is self explanatory, that includes Google Analytics’s Asynchronous code.

We wrap our function in $(document).ready so that it doesn’t affect page loading process.

//code block to attach trackevent code for external links/mailto links
$(document).ready(function(){
...
...
});

</script>

Then we’ll have to create a custom external selector using a function where it says whether hostname matches with current document’s hostname.

// Creating custom :external selector
$.expr[':'].external = function(obj){
return !obj.href.match(/^javascript/) && (obj.hostname != location.hostname);
};

Then we can append a custom class to all those links which are external links

// Add 'external' CSS class to all external links
$('a:external').addClass('external');

Now we can add a check if the external link is either mailto link or actual 3rd party URL and attach  a click event function for trackEvent

$('a.external').click(function(){
var obcat = this.href.match(/^mailto:/);
var targeturl = this.href;
if (obcat){
targeturl = targeturl.replace('mailto:','');
_gaq.push(['_trackEvent', 'Mailto', targeturl , document.location.href,1,true]);
}else{

var targetname = this.text;
var isDoc = this.href.match(/.(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|&|?)/);
if (isDoc) {
	_gaq.push(['_trackEvent', 'DocDownlod', targetname, document.location.href,1,true]);
}else{
	_gaq.push(['_trackEvent', 'Outbound', targeturl, document.location.href,1,true]);
}
}
});

We can check if the current link’s href matches with mailto then fire ‘Mailto’ trackevent, ‘DocDownload’ or ‘Outbound’ trackevent.

Here we’ve added 1 and “true” in the 4th and 5th argument additionally. 5th argument is a new feature which prevents the trackevent call in the calculation of bounce rate.

You can view all these events reported in your GA Interface under

Content > Event Tracking > Categories > Outbound/Mailto/DocDownload

There’s one more usage of appending cookie parameters automatically to all external links and forms. I’ll talk about it next time.

Requirement:

A Jquery Framework (If you are already using a jquery library you don’t need copy jquery library reference)

Script needs to be pasted through all pages of your website, where you want to track external links and mail to links.

Alteration:

You may use virtual trackpageview, instead of trackevent but it’s your choice.

Note:

When you use it with multiple javascript frameworks necessary safeguard needs to be added for the code. If you are using javascript functions to redirect to external links it wouldn’t work.

Bot Icon
Bot Icon

Tatvic Bot

Explore About Tatvic and Services