Events not being tracked in new Google Analytics (analytics.js) setup

52,262

Solution 1

The only way I solved the problem was rolling back to the previous version of Analytics (non-beta):

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-39570713-2']);
  _gaq.push(['_setDomainName', 'optimino.com']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

Solution 2

If you are using Google Tag Manager and also want to trigger some events via code, ga('send'...) does not appear to be enough. You need to first fetch the appropriate analytics object:

if ("ga" in window) {
    tracker = ga.getAll()[0];
    if (tracker)
        tracker.send("event", "Test", "Test GA");
}

Note that this assumes you're only using a single Google Analytics Tracking code on your site. If you happen to be using multiple, you may need to fetch the appropriate one by name or index.

Solution 3

New version of analytics has a new syntax. Replace the line below;

ga('send', 'event', 'button', 'click', 'contact form');

with this;

gtag('event', 'click', {'event_category' : 'button',
  'event_label' : 'contact form'});

Reference; https://developers.google.com/analytics/devguides/collection/gtagjs/events

Solution 4

For testing purposes you could also use the hitCallback method:

ga('send', {
  'hitType': 'event',         
  'eventCategory': 'button', 
  'eventAction': 'click',     
  'eventLabel': 'contact form',
  'hitCallback' : function () {
      alert("Event received");
   }
});

Update: comma was missing.

Solution 5

For GA for this moment... Sending a new page in SPA looks finally like this for me:

if (window.ga){
    window.ga.getAll()[0].set('page', location);
    window.ga.getAll()[0].send('pageview')
}

This shows exactly what wanted on GA reports like a new page is hit and the title and all are correct.

Share:
52,262
squeezemylime
Author by

squeezemylime

Startup guy. Design aficionado. Mobile innovator. I work as a UX Consultant via Optimino (optimino.com). My current focus is on bringing innovative products to market as quickly as possible and with few obstacles, working with agile teams across the globe to develop solutions for clients in the Internet and Mobile industry. I am results-driven and imaginative, always seeking new and better ways of getting things done.

Updated on June 16, 2021

Comments

  • squeezemylime
    squeezemylime almost 3 years

    I have a website that I am using the new Universal Analytics (analytics.js) to track. Everything is setup and working (pageviews, referrals, etc.) using the following code snippet:

    <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-39570713-1', 'site.com');
      ga('send', 'pageview');
    
    </script>
    

    That is located before the </head> tag.

    I am using JQuery to fire off an event. I tested the JQuery with an alert message and it is getting called, so that isn't the problem. Here is the snippet that fires when a button is clicked:

    $('#submitButton').on('click', function() {
          ga('send', 'event', 'button', 'click', 'contact form');
        });
    

    Nothing is appearing in the Events section of Analytics. I keep clicking the button, even from different computers just to make sure it isn't excluding my IP address. Because the Analytics doc that Google provides does not provide a whole lot of explanation I'm at a loss here.