Google analytics tracking between domains

Updated: There is often a need for Google Analytics tracking between domains. Between your website, and a third party secure server that you are able to add code to, or maybe another secure server in your group of websites.

There is new Google analytics code as at January 2008. Please see the tracking code for multiple domains with the latest (asynchronous) version on the Google website, and their full Google analytics documentation.

Some Extras to Add to the tracking javascript

The standard new asynchronous tracking code is below. There are two lines that need to be added (in bold) to allow for tracking between domains.

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setDomainName', '.example.com']);
_gaq.push(['_setAllowLinker', true]);

_gaq.push(['_setAllowHash', false]);

(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>
Then on each page of the third party website, you need the following code:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', true]);

...

Ecommerce Tracking

On the thank-you page, you need to then add the following code: Per Google - tracking Ecommerce transactions

Begin with a call to _addTrans() to establish a transaction. _addTrans() takes the following arguments: Order ID,Affiliation, Total, Tax, Shipping, City, State, Country. For each item that the visitor purchases, call _addItem(). _addItem() takes the following arguments: Order ID, SKU or Code, Product Name, Category, Price, Quantity.

Here is a complete example:

<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
_gaq.push(['_addTrans',
'1234', // order ID - required
'Acme Clothing', // affiliation or store name
'11.99', // total - required
'1.29', // tax
'5', // shipping
'San Jose', // city
'California', // state or province
'USA' // country
]);

// add item might be called for every item in the shopping cart
// where your ecommerce engine loops through each item in the cart and
// prints out _addItem for each
_gaq.push(['_addItem',
'1234', // order ID - required
'DD44', // SKU/code - required
'T-Shirt', // product name
'Green Medium', // category or variation
'11.99', // unit price - required
'1' // quantity - required
]);
_gaq.push(['_trackTrans']); //submits transaction to the Analytics servers

(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>

And now for the main domain

Rather than:
<a href="http://example.com/test.html">click me</a>
change to:
<a href="http://example.com/test.html" onclick="_gaq.push(['_link', 'http://example.com/test.html']); return false;">click me</a>
If you are using a form to post to, have the following details:
- works for post and get forms
<form action="http://www.shoppingcartsite.com/myService/formProcessor.php" name="f" method="post" onsubmit="_gaq.push(['_linkByPost', this]);"> ... </form>

Get domain to show in Google Analytics reports

Once you have domain linking established, you will see only the request URI in the content reports, and not the top-level or sub-domain for a given page. So for example, for visits to the following pages:
— http://www.example.com/index.php and another page on
— http://sub.example.com/more.php

The reports will show:
— index.php
— more.php

You will not be able to distinguish which page is from which domain. To show the domain in the page reports, set a filter to include all components of the page URL, as follows:

In the Profile Settings page, click the Add Filter link.
Choose Add New Filter and provide the filter a name.
Choose Custom Filter and select Advanced on the Filter type settings.

Under Advanced settings:
FieldA should be set to Hostname
FieldB should be set to Request URI
Set the values for both Field A and Field B to (.*), which is an expression that captures all characters.

Set the Output To --> Constructor option to Request URI and provide $A1$B1 as the value for that choice.

Your reports will now show:
— www.example.com/index.php
— sub.example.com/more.php

This will capture the www.example.com portion of your URL and include that at the beginning of your page URL in the content reports section.

Make sure you track every movement

Do you have ajax or flash on your pages? If so, are you tracking those "page requests" on your stats package? Google analytics has facility to track events.

Adding Tracking onto Links

First you will need to delay outbound links by a fraction of a second to give the browser more time to load the tracking code.

<script type="text/javascript">
function recordOutboundLink(link, category, action) {
_gat._getTrackerByName()._trackEvent(category, action);
setTimeout('document.location = "' + link.href + '"', 100);
}

Next, revise outbound links to call the new function without first following the link.
For example, to log every click on a particular link to www.example.com, you would use the _trackEvent() method in the link's <a> tag:

<a href="http://www.example.com" onClick="recordOutboundLink(this, 'Outbound Links', 'example.com');return false;">
The example above uses the category label Outbound Links. This is a useful way to categorize all outbound links in the Event Tracking reports. It sets the specific name of the website as the second parameter in the call. With this structure in place, you could then see Outbound Links as one of the event categories and drill down to see which particular outbound links are the most popular.

Be sure to use return false for the onClick handler, because without that statement the browser will follow the link before the recordOutboundLink method has a chance to execute.

Air New Zealand Tickets Book Air New Zealand Tickets and save on tickets on CheapOair,book today using CheapOair's exclusive travel coupon.

Tags: 

Comments

Haiming - Search Engine Expert - Sep 23, 2008

Hi Michael,

Thank you so much for this article! It gives me the idea how to put the Google Adword tracking code in to onlineshop site. It seems like I should read all articles first before I ask you any question :)


miOOt Live chat - May 19, 2009

Thanks for your info, It is working fine with normal links . But I am having problem with form posting , Can you help on this?

Thank you

Michael Brandon - May 19, 2009

The article gives details about forms. Or look directly on the Google help for more information

Seo Services - Oct 21, 2009

Thanks for sharing your knowledge.

Samith - Mar 10, 2010

I'm having an issue with using "return false", this stops the link been processed, any ideas?

Michael Brandon - Mar 10, 2010

The onclick="pageTracker._link(this.href); takes the href in the link, and adds parameters to it then sends it to the href via javascript.

So the actual <a ref is not being processed, it is being processed via the javascript.

Then the javascript stops, since its now transferred processing to the target webpage.

Post Commentpost a comment