Uncaught ReferenceError: ga is not defined

后端 未结 5 2025
野趣味
野趣味 2021-02-18 15:24

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\"

相关标签:
5条回答
  • 2021-02-18 15:51

    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.

    Make sure you don't have AdBlocker or other software preventing tracking which might be blocking the 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'});"/>
    
    0 讨论(0)
  • 2021-02-18 15:56

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

    0 讨论(0)
  • 2021-02-18 16:01

    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.

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

    0 讨论(0)
  • 2021-02-18 16:09

    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

    0 讨论(0)
  • 2021-02-18 16:10

    This is a follow on from all the others who had an issue with the Monster Insights Wordpress plugin which renames ga to __gaTracker.

    I didn't want to hardcode __gaTracker in my JS (to send an event to GA) - in case someone later removed the MonsterInsights plugin and my script just stopped working.

    Here is my code:

    <script type="text/javascript">
        var ga = typeof ga === "undefined" && typeof __gaTracker !== "undefined" ? __gaTracker : ga;
        ga('send', 'event', 'Order', 'Received', 'My cool product name');
    </script>
    

    The first line creates a var called ga which is either the original ga object or the __gaTracker object if ga does not exist. Note: if ga and __gaTracker both don't exist you will end up with the same error ga is not defined

    0 讨论(0)
提交回复
热议问题