Streaming live at 10am (PST)

Track Events (clicks) using Google Analytics Script

This is a simple script to track events in your Google Analytics account.

If you want to track a buttons clicks, give the button a class ga-event and add the following attributes:

  • data-event-category = “Website Leads”
  • data-event-label = “Book A Demo”

NOTE: Change Website Leads and Book A Demo to whatever you want to call these. Create different names for however you want to segment the events.

Paste the below piece of code in the footer code of your site settings.

<script type="text/javascript">
  //GA Event Tracker Script. Licensed under MIT. Free for any use by all. Written by Paul Seal from codeshare.co.uk

  // Get the category, action and label from the element and send it to GA. The action is optional, because mostly it will be a click event.
  var trackClickEvent = function () {
    var eventCategory = this.getAttribute("data-event-category");
    var eventAction = this.getAttribute("data-event-action");
    var eventLabel = this.getAttribute("data-event-label");
    var eventValue = this.getAttribute("data-event-value");
    ga('send', 'event', eventCategory, (eventAction != undefined && eventAction != '' ? eventAction : 'click'), eventLabel, eventValue);
  };

  // Find all of the elements on the page which have the class 'ga-event'
  var elementsToTrack = document.getElementsByClassName("ga-event");

  // Add an event listener to each of the elements you found
  var elementsToTrackLength = elementsToTrack.length;
  for (var i = 0; i < elementsToTrackLength; i++) {
    elementsToTrack[i].addEventListener('click', trackClickEvent, false);
  }
  
</script>

You can track as many buttons as you like. The results will appear in your Google Analytics dashboard under Behaviour > Events and also under Real Time > Events

For example

1 Like

Thanks @Diarmuid_Sexton . This is just perfect for clients who don’t want to use Google Tag Manager and still want to track in GA some events.