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

And Google universal analytics with the required setup.

The following is the now "old" Google asynchronous analytics - not updated for the Google universal analytics.

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.

Tags: