Generic-blog-header.jpg

How To Set Up Google Analytics Cross Domain Iframe Tracking

Keep up with the very latest developments in the digital marketing world

By Dave Ashworth
on 18/1/16

We recently set up cross domain tracking within an iframe on a client site, but had a few technical SEO issues getting it to work correctly, in particular relating to maintaining the original site referral source/medium.

Whilst this article on Google’s Developer site has a suggested solution, we found we had to tweak the code slightly to ensure it worked every time.

The code we set up is as follows:

Domain A

ga('create', 'UA-XXXXXX-X', 'auto', {'allowLinker': true });
ga('require', 'linker');
ga('linker:autoLink', ['domain-b.com']);
ga('send', 'pageview');
function iframe_init(){
if(typeof ga!=='undefined') {
ga(function(tracker) {
var clientId = tracker.get('clientId');
var frameWindow = document.getElementById('iframe-name').contentWindow;
var frameURL = 'http://www.domain-b.com/URL';
frameWindow.postMessage(clientId, frameURL);
});
}
}
<iframe id="iframe-name" src="http://www.domain-b.com/URL" onload="iframe_init();></iframe>

Domain B

window.addEventListener('message', function(event) {
ga('create', 'UA-290198-1', 'auto', {'allowLinker': true, 'clientId': event.data });
ga('require', 'linker');
ga('linker:autoLink', ['domain-a.co.uk']);
ga('send', 'pageview');
});


Referral Exclusion List

In addition to this, in order to maintain the session’s source across the 2 domains, you will need to add your primary domain to the Referral Exclusion List within its Google Analytics account.

Comments