views:

326

answers:

2

I'm having some noob troubles with Google Analytics and _trackEvent.

Using it seems straight forward in the documentation, but I can't get this simple example to work. The call to _trackEvent fails with 'TypeError: o is undefined'

The call to _trackPageview is succeeding and I can see it updated in the analytics dashboard.

I tried peeking at ga.js to understand what's up - just have a headache to show for it!

This is my first foray into GA - especially with custom events. The account is new. Everything seemed to be correctly setup - but I probably wouldn't know if it wasn't!

It seems so simple - but obviously I'm missing something. Any help with removing my blind-folds is much appreciated!

-vs

Example HTML - only need a tracking code.

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); var pageTracker; try { pageTracker = _gat._getTracker("UA-XXXXXX-1"); pageTracker._trackPageview();
} catch(err) { console.log(err.toString()); } $(document).ready(function() { try { pageTracker._trackEvent('my_category', 'my_action', 'my_optional_label', 42); } catch(err) { console.log('trackEvent ' + err.toString()); } });

A: 

noob error with the code-block! here's the full version.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"&gt;&lt;/script&gt;
    <script type="text/javascript">
      var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
      document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
      var pageTracker;
      try {
        pageTracker = _gat._getTracker("UA-xxxxxxx-1");
        pageTracker._trackPageview();        
      } catch(err) { 
        console.log(err.toString()); 
      }
      $(document).ready(function() {
          try {
            pageTracker._trackEvent('my_category', 'my_action', 'my_optional_label', 42); 
          } catch(err) { 
            console.log('trackEvent ' + err.toString()); 
          }
      });
      </script> 
  </head>
  <body>
  </body>
</html>
Vivek
A: 

Turns out one needs to call _initData() before the call to _trackEvent. Not sure why this is the case, but it seems to work now. Hopefully it helps someone else. The modified sample..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"&gt;&lt;/script&gt;
    <script type="text/javascript">
      var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
      document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
      var pageTracker;
      try {
        pageTracker = _gat._getTracker("UA-xxxxx-1");
        pageTracker._trackPageview();        
      } catch(err) { 
        alert(err.toString()); 
      }
      $(document).ready(function() {
          try {
            pageTracker._initData(); // <<--- The addition. 
            pageTracker._trackEvent('my_category', 'my_action', 'my_optional_label', 42); 
          } catch(err) { 
            alert('trackEvent ' + err.toString()); 
          }
      });
      </script> 
  </head>
  <body>
  </body>
</html>
Vivek