Track custom events on your website using Google Analytics

Google Analytics is a very robust solution when it comes to tracking users. But, the tiny script everyone includes isn’t enough.

It might be critical for you to know how many users click a particular button (call-to-action), or visit pages using cross-selling strategies rather than direct navigation. Actually, you need custom events.

Hands-on practical example

Let’s say you’ve just built an incredible landing page for your product and you’d like to track how effective your call-to-actions are.

Something like that:

We will focus on the big purple button: Is it clicked frequently or not?

Three things cannot be long hidden: the sun, the moon, and the truth. — Buddha

There’s only one thing you need to know:

Short explanation:

'send': It is a command (known as hitType in the manual). It will ask Google Analytics to trigger “something”.

'event': The “something” is an event. So you will trigger an event.

'Category': What has been interacted with. Quoting the manual: video, button, picture, etc.

'Action': The kind of interaction. For instance: a video is played ( 'play' action), an article is read ( 'read' action), and so on.

Further details:

'Label': Lets you categorize events. For instance, when an article is read you could provide the name of the article as a label. See later.

'Value': A numeric value. For instance, when a download has ended you could track how long it has taken for the browser to download it. See later.

How it’s done

Arching back to our call-to-action, we’d like to know how many times it’s been clicked.

Snippet:

It means:

Trigger a custom event in Google Analytics.

A button has been clicked.

This button is a Call-To-Action.

Now, let’s imagine I have a second call-to-action further on my website.

I would like to know how it performs compared to the first one.

Snippet:

This is how labels can prove to be useful.

Another example

You can leave or get straight to my article’s ending if everything is already clear for you .

I’ll be using Promises this time (requires a bit more knowledge of Javascript).

Let’s say you want to know how long it has taken for a client to download a file.

We’ll assume that the download function is an asynchronous request that returns a Promise.

Snippet:

Thanks for reading

I hope this tutorial will let you improve your tracking experience using Google Analytics. It’s been very useful for me, and online tutorials seemed to be much harder for just a single line of Javascript.