Track Events on Analytics

In this tutorial, we will take a look at how to put a tracking code on our website for a specific action.

We will focus on the different aspects of Analytics including the different libraries Global Site Tag and Universal Analytics.

Explanations

Checking

In the first place, we will be checking which kind of library we have installed on our website. If no tag is installed, the event will not be working.

Putting the code together

Today's example allows us to track users that click on a specific link. We will use a DOM event that may send a Javascript function when someone clicks on an HTML link. This function is called OnClick and looks like this

<a href="#" onClick="#"> Subscribe to Scientia </a>

Details of the function

Universal Analytics - Universal.js

ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue]); 

gTag.js

gtag('event', , {  'event_category': ,  'event_label': ,  'value': });

This is the piece of event code that you can find on both gTag and Universal documentation. 

We have two main objects that we need to use, eventCategory and eventAction.

Let's replace the code with our own event.

<a href="#" onClick="ga('send', 'event', "Scientia", "subscribe");"> Click me </a> 

With this, we send an event with Scientia as a category and Subscribe as an action.

0
Buy Me A Coffee
searchstaruserlist-altbookcalendarfacebook-squarelinkedin-squarearrow-circle-rightmoneybellyoutube-playinstagramline-chart