Cross-domain user tracking with google analytics and _link redirect problem

I was working on a cross-domain website which needs to track our users with Google analytics code. The code to do that is easy and standard and can be found on the Google analytics dev guide.

<script type="text/javascript">// <![CDATA[
var _gaq = _gaq || []; _gaq.push(['_setAccount', '00-00000-0']); _gaq.push(['_setDomainName', 'example.com']); _gaq.push(['_setAllowLinker', true]); _gaq.push(['_trackPageview']);
// ]]>
</script>

Now on the link that drive visitors to the other domain you need to add this little piece of code.

<a onclick="_gaq.push(['_link', 'http://www.my-new-example.com/home.html']); return false;" href="http://www.my-new-example.com/home.html">Link to other domain</a>

That’s fine so far, what about if you need to have a new window?

<a onclick="window.open('http://www.my-new-example.com/home.html','newwindow',config='height=200, width=500, tollbar=no, menubar=no');_gaq.push(['_link', 'http://www.my-new-example.com/home.html']); return false;" href="javascript:void(0);">Link to other domain</a>

I am sorry but in that case you will come with a problem as the _link actually fires a redirection to the new paging so you will end up with a both a pop up window and a parent window change.

I either tried jQuery solutions like

<a class="mylink" onclick="window.open('http://www.my-new-example.com/home.html','newwindow',config='height=200, width=500, tollbar=no, menubar=no');" href="javascript:void(0);">Link to other domain</a>

 

$('a.mylink').click(function(event){
event.preventDefault();
_ga.push(['_link','http://www.my-new-example.com/home.html'])
})

 

That didn’t work either. At the end I found a different Google analytics method called _getLinkerUrl which actually does the job perfect. So you keep the first part of the above code and you play a little bit on the jQuery part.

 

$('a.mylink').click(function(event){
event.preventDefault();
var pageTracker = _gat._getTrackerByName(); // Gets the default tracker.
var linkerUrl = pageTracker._getLinkerUrl('http://www.my-new-example.com/home.html');
})

 

This function actually pass the GATC cookie to the new domain. Just bear in mind that the _getLinkerUrl needs also _setDomainName() to the current domain and _setAllowLinker() to true.

I hope you find this easy and save you time, any suggestions or comments are welcome.

Share it!Share on Google+Share on FacebookTweet about this on TwitterShare on LinkedInShare on StumbleUpon

Comment (1)

  1. Roman
    June 24, 2013 at 1:10 pm · Reply

    I solved the problem with javascript help. Wrote guide how to do it: http://pilotaz.blogspot.com/2013/06/tracking-redirects-with-google-analytics.html

Leave a reply

Your email address will not be published. Required fields are marked *

Back to Top