Google Analytics Event Tracking - Not Working

C Stembridge picture C Stembridge · Mar 16, 2016 · Viewed 15.4k times · Source

I'm trying to track outbound link clicks using Google Analytics tracking. So far, I've been unsuccessful in getting Analytics to register anything, despite using their code from this page.

Here's a sample page I'm trying to track a link (at the bottom: "Buy Who was Ghandi?"): http://ourhomeschoolforum.com/reviews/history-geography/who-was-ghandi/

Here's the code on the page:

<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-{ID redacted}', 'auto');
  ga('send', 'pageview');

</script>

<script>
/**
* Function that tracks a click on an outbound link in Analytics
* This function takes a valid URL string as an argument, and uses that URL string
* as the event label. Setting the transport method to 'beacon' lets the hit be sent
* using 'navigator.sendBeacon' in browser that support it.
*/
var trackOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {
     'transport': 'beacon',
     'hitCallback': function(){document.location = url;}
   });
}
</script>

And here's the href I'm using:

<h2 style="text-align: center;"><a href="http://www.rainbowresource.com/product/sku/059929" target="_blank" onclick="trackOutboundLink(‘Who was Ghandi?’); return false;">Buy <em>Who Was Ghandi?</em></a></h2>

What am I doing wrong? Nothing shows in the event tracking section of Analytics Real-Time or Behavior > Events. Do I need to setup a Goal to go with it?

Answer

Klaudio Milankovic picture Klaudio Milankovic · Mar 16, 2016

I had similar problems when I was using Google Tag Manager.

I've found this tool from Google to be really helpful in debugging Analytics.

It's called Tag Assistant (Google). You can add it to Chrome

https://chrome.google.com/webstore/detail/tag-assistant-by-google/kejbdjndbnbjgmefkgdddjlbokphdefk

Also, from my experience "Events" take longer than usual to appear in Reports section, it also might be the case.

I just inspected your site and I see there are two different GA Codes there

  1. UA-4XXXX45-1
  2. UA-7XXXXX1-35

It also might be the case of not working because you have two different codes. Try removing one and then test again.