Javascript 未捕获的 ReferenceError:ga 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31274803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:28:07  来源:igfitidea点击:

Uncaught ReferenceError: ga is not defined

javascriptgoogle-analytics

提问by meij57

I want know how many times people clicked on a particular button (should be very simple with Google analytics). However, I had an "Uncaught ReferenceError: ga is not defined" error from google console and can't find how to fix it.

我想知道人们点击特定按钮的次数(使用 Google 分析应该很简单)。但是,我从 google 控制台收到了“未捕获的 ReferenceError:ga 未定义”错误,并且找不到解决方法。

I added this in the head:

我在头中添加了这个:

     <script>

      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)
      [0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-XXXXXXXX-X', 'auto') ; 

      ga('send', 'pageview');

    </script>

And add an onclick event on the button by generating the code with this tool http://gaconfig.com/google-analytics-event-tracking/contact-form/:

并通过使用此工具http://gaconfig.com/google-analytics-event-tracking/contact-form/生成代码在按钮上添加一个 onclick 事件:

onclick="ga('send', 'event', { eventCategory: 'Book button', eventAction: 'Click', eventLabel: 'enquiry home page'});"

Then I set up the goals in google analytics, but still have this error in the console.

然后我在谷歌分析中设置了目标,但在控制台中仍然有这个错误。

So I tried to add:

所以我试着补充:

var gaq = gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_trackPageview']);

Before declaring the google analytics function, but it generates a second error so I just dropped it.

在声明 google 分析功能之前,但它产生了第二个错误,所以我只是放弃了它。

Does anyone know if the problem comes from the script I'm using? Or if it is from the onclick event code?

有谁知道问题是否来自我正在使用的脚本?或者如果它来自 onclick 事件代码?

回答by kiran

I created a simple HTML with the same JS and click handler and ran it in my local apache server, it just works fine. Don't see any reference errors.

我使用相同的 JS 和单击处理程序创建了一个简单的 HTML,并在我的本地 apache 服务器中运行它,它运行良好。看不到任何参考错误。

Make sure you don't have AdBlocker or other software preventing tracking which might be blocking the GoogleAnalytics `

确保您没有 AdBlocker 或其他阻止跟踪的软件,这些软件可能会阻止 GoogleAnalytics `

 <script>

    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)
    [0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-XXXXXXXX-X', 'auto') ; 

    ga('send', 'pageview');

  </script>

<input type= "button" value ="Click Me" onclick="ga('send', 'event', { eventCategory: 'Book button', eventAction: 'Click', eventLabel: 'enquiry home page'});"/>

回答by user7305940

The same problem occured in my Wordpress website. I had event tracking code in my contact form 7 additional field, but after I installed the plugin Monster Insights I had to delete the code in the Theme Options. Forgetting the code in my contact forms, I got this message too.

我的 Wordpress 网站也出现了同样的问题。我的联系表单 7 附加字段中有事件跟踪代码,但在安装插件 Monster Insights 后,我不得不删除主题选项中的代码。忘记了联系表格中的代码,我也收到了这条消息。

So delete all tracking code if you start using this plugin. Hope someone finds this information usefull..

因此,如果您开始使用此插件,请删除所有跟踪代码。希望有人发现这些信息有用..

回答by jordy

You settled this question already but I just wanted to add:

你已经解决了这个问题,但我只想补充:

Make sure your browser is allowing sites to track you. If you disallow tracking, the Google Analytics JS file will not load and you may get this error

确保您的浏览器允许网站跟踪您。如果您禁止跟踪,Google Analytics JS 文件将不会加载,您可能会收到此错误

回答by Mav2287

Open a new incognito window. If you are logged in then the analytics code won't be ran. Also instead of "ga" you need to use "__gaTracker" for some reason Monster Insights doesn't use "ga" they change it to "__gaTracker".

打开一个新的隐身窗口。如果您已登录,则不会运行分析代码。此外,由于某种原因,您需要使用“__gaTracker”代替“ga”,Monster Insights 不使用“ga”,而是将其更改为“__gaTracker”。