Search code examples
javascriptgoogle-analyticsevent-logoutbound

Google Analytics, Outbound Event Tracking


Having an Issue With recording outbound link events in my google analytics. This is such a simple task I cannot understand where it can go wrong. Can any one notice any issues with my code. The google functions are being executed upon my onclick events, The issue is that no event is being logged on my google analytics dashboard In Behaviour > Events > Overview

google_analytics.php

<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','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-86854644-1', 'auto');
  ga('send', 'pageview');


 var trackOutboundLink = function(url) {
  alert(url);
     ga('send', 'event', 'outbound', 'click', url, {
       'transport': 'beacon',
       'hitCallback': function(){document.location = url;}
     });
}



function handleOutboundLinkClicks(event) {

  ga('send', 'event', {
    eventCategory: 'Outbound Link',
    eventAction: 'click',
    eventLabel: event,
    transport: 'beacon'
  });
}

</script>

Click Event

onclick=\"handleOutboundLinkClicks(this);\"

header.php

include_once("/includes/google_analytics.php");

Solution

  • Thanks For the help, I agree that Event Listeners are a better approach especially with multiple event but my initial function and click event worked, Turns out it just took some time for google analytics to present it on my dashboard. Thanks