Search code examples
javascripthtmlgoogle-analyticsevent-tracking

How to record trackevent in google analytics?


This is my analytics code so far:

(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', 'XXXX', {cookieDomain: 'none', legacyCookieDomain: 'none'});ga('send', 'pageview');

I am trying to record an event like this:

<a style="
    color: #00b258; font-size: 15px;" onclick="ga('_trackEvent', 'Submit an inquiry', 'Click Submit Inquiry', 'Opened Inquiry form on ' + document.url);return true;" href="http://test.com">Submit An Inquiry</a>

But when I go in firebug net panel no request is sent. What am I missing? Is my syntax wrong?


Solution

  • Your mixing up the tracking code a little I think. It should look more like this. The first item should be Send not _trackevent.

    <a href='#' onclick="ga('send', 'event', 'button', 'click', 'test', 4);"> Test Event</a> 
    

    Trying to fix yours: Im not sure its going to let you add the document.url but let me know if it does :)

    <a style="color: #00b258; font-size: 15px;" onclick="ga('send', 'Submit an inquiry', 'Click Submit Inquiry', 'Opened Inquiry form on ' + document.url);" href="http://test.com">Submit An Inquiry</a>
    

    Link for reference: Event Tracking - Web Tracking (analytics.js)