views:

480

answers:

3

I am calling the Google Analytics _trackEvent() function on a web page, and get back an error from the obfuscated Google code. In Firebug, it comes back "q is undefined". In Safari developer console: "TypeError: Result of expression 'q' [undefined] is not an object."

As a test, I have reduced the page to only this call, and still get the error back. Besides the necessary elements and the standard Google tracking code, my page is:

<script>
  pageTracker._trackEvent('Survey', 'Checkout - Survey', 'Rating', 3);
</script>

Results is that error.

What's going on here?

+2  A: 

This problem seems to occure when the page is not fully loaded yet: http://www.google.com/support/forum/p/Google+Analytics/thread?tid=4596554b1e9a1545&amp;hl=en

The provided solution is to wait for pageTracker.cb

function trackEvent(target, action, opt_label, opt_value) {
  if(pageTracker && !pageTracker.cb) {
    setTimeout(function() {
      trackEvent(target, action, opt_label, opt_value);
    }, 200);
    return;
  }
  pageTracker._trackEvent(target, action, opt_label, opt_value);
}
Mef
Looks good. Error is gone. Waiting to see if the results show up in Analytics. If you don't hear back from me, everything worked.
Laizer
+3  A: 

Actually the answer no. 1 is not correct. That's because pageTracker.cb never gets set (it's an obfuscated property name) with other versions of GA.

You should call upon initialization: pageTracker._initData()

GiantRabbit
Thanks man, that worked for me.
Eran Betzalel
A: 

This looks like a bug in ga.js introduced when they added _initData() functionality to _trackPageview(). Unfortunately _initData() isn't actually called after the conditional. Hope they fix it before they deprecate _initData() for good.

e.g. This page suggests the above should work without calling _initData(): http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&amp;answer=55527

Jason Ang