Karen
Karen

Reputation: 23

GA-Using trackEvent and multiple accounts

I have a site that requires feeding data to two analytics account and I'm not sure how to implement the trackEvent since there are 2 different account numbers. Which one will it go to or both? This is my code:

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-1']);
_gaq.push(['_setDomainName', 'example.com']);
_gaq.push(['_trackPageview']);

// Second tracker 
_gaq.push(['secondTracker._setAccount','UA-YYYYYYY-2']);
_gaq.push(['_setDomainName', 'example.com']); 
_gaq.push(['secondTracker._trackPageview']);

function recordOutboundLink(link, category, action) {
try {
var myTracker=_gat._getTrackerByName();
_gaq.push(['myTracker._trackEvent', category , action ]);
setTimeout('document.location = "' + link.href + '"', 100)
}catch(err){}
}
</script>
</head> listed here to indicate placement

<script type="text/javascript">  (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);
})();
</script>
</body> listed here to indicate placement

The site is a primary domain with a mobile subdomain (which is why there is the setDomainName).

Questions: 1) Will the trackEvent record for both GA accounts? 2) Have I split the asynch code properly and can the recordOutboundLink be in the same script with the gaq_push?

Thanks, Karen

Upvotes: 2

Views: 297

Answers (1)

mike
mike

Reputation: 7177

  1. A single _trackEvent call will only send data to a single GA profile. If you want to send data to both profiles, you need to make multiple calls.
  2. There's no problem putting recordOutboundLink in the same script where _gaq is defined.
  3. Assuming you're using the onclick attribute on a link -- don't forget to return false so the default link behavior is ignored.
  4. FYI: The example code Google has for tracking outbound links is mucked up, almost like they were updating old style code to async and got lost part way through :/

Try something like the following:

function recordOutboundLink(link, category, action) {
  _gaq.push(['_trackEvent', category , action ]);
  _gaq.push(['secondTracker._trackEvent', category , action ]);
  setTimeout(function(){document.location = link.href}, 100);
  return false;
}

<a href="linkToTrack" onclick="return recordOutboundLink(this, 'category', 'action')"

Upvotes: 1

Related Questions