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', '']); _gaq.push(['_setAllowLinker', true]); _gaq.push(['_trackPageview']);
// ]]>

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', '']); return false;" href="">Link to other domain</a>

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

<a onclick="'','newwindow',config='height=200, width=500, tollbar=no, menubar=no');_gaq.push(['_link', '']); 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="'','newwindow',config='height=200, width=500, tollbar=no, menubar=no');" href="javascript:void(0);">Link to other domain</a>




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.


var pageTracker = _gat._getTrackerByName(); // Gets the default tracker.
var linkerUrl = pageTracker._getLinkerUrl('');


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.

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:

Leave a reply

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

Back to Top