Google Analytics _trackEvent 问题

发布于 2024-08-10 20:00:30 字数 1683 浏览 2 评论 0原文

我在使用 Google Analytics 和 _trackEvent 时遇到了一些菜鸟麻烦。

在文档中使用它似乎很简单,但我不能这么简单 工作的例子。对 _trackEvent 的调用失败,并显示“TypeError: o is undefined”

对 _trackPageview 的调用成功,我可以在分析仪表板中看到它已更新。

我尝试查看 ga.js 以了解发生了什么 - 只是有一个头痛的表现!

这是我第一次涉足 GA——尤其是自定义事件。该帐户是新的。 一切似乎都已正确设置 - 但我可能不知道是否正确!

看起来很简单 - 但显然我错过了一些东西。 非常感谢任何帮助我摘除眼罩的帮助!

-vs

示例 HTML - 只需要一个跟踪代码。

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独患者 2024-08-17 20:00:30

代码块出现菜鸟错误!这是完整版本。

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

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>
月依秋水 2024-08-17 20:00:30

结果是在调用 _trackEvent 之前需要调用 _initData()。不知道为什么会这样,但现在似乎有效。希望它对其他人有帮助。
修改后的样本..

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文