未捕获的ReferenceError: ga没有定义

时间:2022-06-07 14:30:02

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.

我想知道人们点击某个按钮的次数(使用谷歌分析应该非常简单)。但是,我有一个“未捕获的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/ :

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

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.

在声明谷歌分析函数之前,它会生成第二个错误,因此我将其删除。

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

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

3 个解决方案

#1


15  

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并单击handler并在本地apache服务器上运行它,它工作得很好。不要看到任何引用错误。

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

确保你没有AdBlocker或其他阻止跟踪的软件可能会阻止google分析

 <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'});"/>

#2


3  

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网站上。我在contact form 7附加字段中有事件跟踪代码,但是在安装了插件Monster Insights之后,我不得不在主题选项中删除代码。忘记了联系方式中的密码,我也收到了这个消息。

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

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

#3


1  

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

确保你的浏览器允许网站跟踪你。如果不允许跟踪,谷歌Analytics JS文件将不会加载,您可能会得到这个错误

#1


15  

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并单击handler并在本地apache服务器上运行它,它工作得很好。不要看到任何引用错误。

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

确保你没有AdBlocker或其他阻止跟踪的软件可能会阻止google分析

 <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'});"/>

#2


3  

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网站上。我在contact form 7附加字段中有事件跟踪代码,但是在安装了插件Monster Insights之后,我不得不在主题选项中删除代码。忘记了联系方式中的密码,我也收到了这个消息。

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

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

#3


1  

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

确保你的浏览器允许网站跟踪你。如果不允许跟踪,谷歌Analytics JS文件将不会加载,您可能会得到这个错误