5

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?

C Stembridge
  • 53
  • 1
  • 1
  • 4

2 Answers2

5

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.

Jakub Kriz
  • 1,501
  • 2
  • 21
  • 29
  • Good thoughts. May I ask how you found the second GA code (UA-76641-35)? I'm not able to find it when inspecting the page source and searching for it. – C Stembridge Mar 16 '16 at 14:20
  • I think you're probably right about events taking a while to report — a couple clicks have now shown up in Analytics. – C Stembridge Mar 16 '16 at 14:25
  • I've used Tag Assistant and it picks up two shortcodes. – Klaudio Milankovic Mar 16 '16 at 19:51
  • Using this tag assistant helped me figure out why my events weren't being registered, I could use the correct GA code. Nice answer, really helpful. – Matheus Aug 28 '17 at 14:07
4

You need to change the 'smart' (or angled) quotes to 'straight' quotes around the "Who was Ghandi?" text, in the onclick handler:

onclick="trackOutboundLink('Who was Ghandi?'); return false;"

Javascript doesn't really like the smart quotes.

nyuen
  • 8,829
  • 2
  • 21
  • 28