1
votes

I am successfully tracking pages on my site to report to two Google Analytics accounts, however, I can't get the outbound link tracking to track the events in Google Analytics accounts (it's not tracking in either). Any ideas why the recordoutbound link function isn't working? Below is my code:

<script type="text/javascript">

  var _gaq = _gaq || [];

_gaq.push(['_setAccount', 'UA-1xxxxxx-x']); 
_gaq.push(['_setDomainName', 'mysite.com']); 
_gaq.push(['_setAllowLinker', true]); 
_gaq.push(['_trackPageview']); 


_gaq.push(['b._setAccount', 'UA-2xxxxxx-x']);
_gaq.push(['b._setDomainName', 'mysite.com']);
_gaq.push(['b._setAllowLinker', true]);
_gaq.push(['b._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);
  })();

function recordOutboundLink(link, category, action) { 
try { 
var onePageTracker = _gat._getTracker("UA-1xxxxxx-x"); 
var twoPageTracker = _gat._getTracker("UA-2xxxxxx-x");
onePageTracker._trackEvent(category, action); 
twoPageTracker._trackEvent(category, action);
setTimeout('document.location = "' + link.href + '"', 100) 
} catch (err) { } 
} 
</script> 
2

2 Answers

1
votes

Looks like you're using code based on Google's old outbound links tracking example, which had a few errors.

Try the following, which uses _gaq.push instead:

function recordOutboundLink(link, category, action) { 
  try{
    _gaq.push(['_trackEvent', category,  action]);
    _gaq.push(['b._trackEvent', category,  action]);
  } catch(ignore){};
  setTimeout(function(){document.location.href = link.href;}, 100);
} 
0
votes

You can condense that a bit... for one think you don't need to redefine the tracking objects, especially since you're naming them (you've only named one, b).

For the actual event tracking, you use the same _gaq.push as you do with pageview (see below).

Also, not 100% sure, but you may have a collision if you attempt to set 2 trackers to the same domainname... I seem to recall running into issues of tracking across multiple subdomains in that scenario and had to set prefix 1 with a dot.

<script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['a._setAccount', 'UA-1xxxxxx-x'],
              ['a._setDomainName', 'mysite.com'],
              ['a._setAllowLinker', true],
              ['a._trackPageview'],
              ['b._setAccount', 'UA-2xxxxxx-x'],
              ['b._setDomainName', '.mysite.com'],
              ['b._setAllowLinker', true],
              ['b._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);
      })();

    function recordOutboundLink(link, category, action) { 
        try { 
            _gaq.push(['a._trackEvent', category, action, link],
                      ['b._trackEvent', category, action, link]); 
            setTimeout('document.location = "' + link.href + '"', 100); 
        } catch (err) { } 
    } 
</script>