2

I just jumped into a project that my co-worker used to work. I see the following code.

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', "my id"]);
  _gaq.push(['_trackPageview']);

  _gaq.push(['_trackEvent', 'app', 'DEACTIVATE'])

The problem is, I don't see any user events under google analytics page. I checked Content->Events.

I googled and then found out most people use

pageTracker = _gat._getTracker("id")
pageTracker._trackEvent("app","DEACTIVATE"); 

which one is correct? I like to test it myself first, but the app is already live and GA has a delay of a day. I like to confirm it first.

Update: After digging into it further, I found out that I should use the first approach. However, it still doesn't work unless you pass the value.

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', "my id"]);
  _gaq.push(['_trackPageview']);

  _gaq.push(['_trackEvent', 'app', 'DEACTIVATE',""])   // must pass value even if it's empty
Moon
  • 22,195
  • 68
  • 188
  • 269

5 Answers5

2

If you're using async version - and there's generally no reason why you shouldn't then you should always use

_gaq.push(...) - (where _gaq is just a javascript array, and push() just adds to the end)

The whole point of this array is that you can store events, whether it's a trackEvent event or a trackPageView event before the GA script may have loaded - that's why it's called async tracking. When the script has loaded it processes everything in the array.

If you have javascript on the page that pushes something to this array BEFORE the google script has finished loading then the event will still be tracked as soon as it's loaded. It also works AFTER the script has loaded.

I assume (but haven't verified this) - that once the script is loaded it converts _gaq to an observable array - or just a direct method call so that once you push anything more to it it can instantly be processed.

I'd recommend using Google Analytics Debugger which is a Chrome plugin published by Google - to see in real time what gets sent. Also this is good for general GA debugging tipe.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • +1, code snippets on Google's help page on Event Tracking use the `_gaq.push(...)` syntax as well. – jamix Mar 13 '13 at 12:24
0

I think the first part is correct. To be sure you can use http://www.fiddler2.com/fiddler2/ to see if the event is raised or install Google Analytic debugger.

ClaudiaB
  • 11
  • 2
  • You can see the docs and examples here: https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide#Anatomy – ClaudiaB Jun 13 '12 at 22:32
0

_gaq is Google Analytics asynchronous page tracking global object.
_gat is Google's traditional page tracking object.

According to Google's documentation the _gat(account) method call is deprecated.

See https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gat for more information on that.

Also see https://developers.google.com/analytics/devguides/collection/gajs/ for more information on Google Analytics in general.

Here is Google's example for basic asynchronous tracking:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_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);
})();

Do you see the above anonymous function anywhere in your page's Javascript?

The following code works for me. I did notice that in the code you posted you were missing a trailing semicolon on the event trace call. I originally copied your code which resulted in a JavaScript exception which was corrected by putting a trailing semicolon on the line.

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-######-1']);
_gaq.push(['_trackPageview']);

_gaq.push(['_trackEvent', 'app', 'DEACTIVATE']);

(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);
})();

Again, the above code works for me.

Good luck!

HeatfanJohn
  • 7,143
  • 2
  • 35
  • 41
  • I see that, my question is if " _gaq.push(['_trackEvent', 'app', 'DEACTIVATE'])" is correct or not because I don't see any events on my GA page. – Moon Jun 13 '12 at 22:23
  • Just to confirm that I understand, are you saying that you know that GA is working for your page because you are seeing the Page View being counted, but you are not seeing the event "app" "DEACTIVATE" being counted? – HeatfanJohn Jun 14 '12 at 13:32
  • Hey, I just noticed is your **_setAccount** value really **"my id"**? If yes, then that's the potential problem. **_setAccount** needs to be set to your site's Google Analytics account which is typically in the format **'UA-xxxxx-x'**, where xxxxx and x are numbers for your account. – HeatfanJohn Jun 14 '12 at 14:46
0

For the second approach, you want to use _getTrackerByName instead of _getTracker

pageTracker = _gat._getTrackerByName();
pageTracker._trackEvent("app","DEACTIVATE"); 

Either way will work, but the first is preferred.

Are you testing on a live server, or on a localhost/intranet machine? Google Analytics won't send the tracking request (__utm.gif) if your server isn't on a fully qualified domain name -- see Google Analytics GIF request not sent.

Community
  • 1
  • 1
mike
  • 7,137
  • 2
  • 23
  • 27
0

If you don't want to deploy your application all the time, and wants to be more flexible and faster, you can use this app from SiteApps, below:

http://siteapps.com/app/easy_ga_track_event-459

It's easy to setup.