Tag: tracking
Google Analytics Async Action Tracking
Posted By Ryan Stout on August 04, 2010
I recently upgraded to the new _gaq async google analytics tracking code (since you don't have a choice anymore). I then wanted to upgrade a few pageTracker._trackPageView functions to the new code. Finally I figured out that you just needed to do the following:
_gaq.push(['_trackPageview', '/url/to/track']);
This really threw me at first because I saw that _gaq is originally defined as an empty array, then when the tracking code loads below it, I figured it pulls the objects out and sends them. So I felt like I would need to resend the request somehow. Shortly after I noticed that _gaq gets overwritten and push is defined to send when you call it. While I get why they did this, I'm not sure from an API design this was the best choice. Also, any googling brings up one of the two previous versions of the tracking code, and some help pages have mixed versions on them which makes it very confusing.
Worse yet, running the code above in the console would work for me, but not when I tried to track the link. I finally figured out that the issue has to do with me calling window.open after the tracking code call. Strangely enough this is enough to stop the code from tracking correctly. Placing the window.open call in a setTimeout is enough to get it working again.
_gaq.push(['_trackPageview', '/url/to/track']); setTimeout(function() { window.open(url,'_blank'); }, 0);
Tags: javascript, google analytics, tracking, _gaq
