English 中文(简体)
Google Analytics _trackEvent troubles
原标题:

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.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <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"></script>
    <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-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()); 
          }
      });
      </script> 
  </head>
  <body>
  </body>
</html>
问题回答

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">
<html xmlns="http://www.w3.org/1999/xhtml">
  <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"></script>
    <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>

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">
<html xmlns="http://www.w3.org/1999/xhtml">
  <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"></script>
    <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>




相关问题
Web Analytics for Platform with Custom Events

I m building a platform that produces websites. Think wordpress.com as a similar example. Each site is going to be a subdomain of my domain like abc.mydomain.com or xyz.mydomain.com. I have a few ...

Removal of homepage login users from analytics

The problem I am having is filtering out the users that come to our homepage just to login, since we have the client button on the homepage (and yes I ve tried to get them to put it somewhere else). ...

Finding runs of a particular value

I have a table in Oracle 10 that is defined like this: LOCATION HOUR STATUS -------------------------------------- 10 12/10/09 5:00PM 1 10 12/10/09 6:00PM 1 ...

tracking download completions from a website/cdn

I have a Drupal website where users are clicking on a link that initiates a file download from a content delivery network (CDN). A script is tracking the number of users who click the link to begin ...

Update based on subquery fails

I am trying to do the following update in Oracle 10gR2: update (select voyage_port_id, voyage_id, arrival_date, port_seq, row_number() over (partition by voyage_id order by arrival_date) as ...

Google Analytics _trackEvent troubles

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 ...

热门标签