I have a site that uses google analytics and is that uses a number of domains. I was asked to add cross domain tracking by doing this:
1) Add an async tracker
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-21602333-1']);
_gaq.push(['_setDomainName', 'domain.com']);
_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);
})();
2) Add the following code to all links that go to one of the other domains.
<a href="http://www.domain-aanvraag.nl[rest van de link]" onclick="_gaq.push(['_link', 'http://www.domain.com']); return false;">Go!</a>)
The _link option does the following according to google:
This method works in conjunction with the _setDomainName() and
_setAllowLinker() methods to enable cross-domain user tracking. The _link() method passes the GATC cookies from this site to another via URL parameters (HTTP GET). It also changes the document.location and
redirects the user to the new URL.
Ok. This all works fine for normal outgoing links.
Here’s the problem. I have a form in which the user can select one of two radio buttons and after selecting one he hits a submit button. If the first radio button is selected a fancybox popup is opened with an iframe in it pointing to the a page on another domain. If the second radio button is opened the current window is redirected to another page on this domain.
I only want to do the _gaq_push(['_link', ...]) thingy when the user selects the first option and clicks the button.
So I have two issues with this:
- Where should I place the onclick.
- How do I use this code using fancybox? It seems to always set the document.location of the main screen and not of the iframe in my fancybox, resulting in a complete refresh of my screen.
Here’s a stripped down working example of my code so far: https://www.domain.com/test/analytics.html
I digged a little deeper and the underlying problem here is that I am trying to create a cross domain tracking link to another domain in another iframe instead of a whole new window (the iframe is created by fancybox). So instead of using _link I should use _getLinkerUrl as described here:
https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSite#trackingIFrames