Event Tracking with Href Onclick

Have you ever wondered how many users have clicked your contact button, dowloaded your white paper, attempted to sign up for your newsletter or clicked to call you?

It can be very frustrating sometimes not knowing how users are engaging with your website or even knowing how many leads you are missing out on.

Well there is a solution! It’s called “href onclick” and it’s here to help you track how users engage with these areas.

Href onclicks are one of the most widely used methods for tracking key performance indicators (KPIs) on your website. You know…the money making areas of your website!

What if I told you that the only thing you need to know is how to structure the events you are tracking so that all you have to do is monitor and report on them from a marketing perspective. Its true!

There are tools out on the world wide web today that will automatically generate these trackable links. I am going to walk you through how to structure your events to be more effective, and then how to set up href onclick urls on your website.

What is an href onclick event?

An href onclick link is a link that attaches itself to en existing element or acts as a unique link that can be tracked by Google Analytics. The most common use of this is to set up goals in analytics so that marketing professionals and business analysts can monitor and measure the results of their marketing efforts through these trackable links or elements.

If you are interested in tracking link campaigns I would suggest you consume this very well thought out article on understanding your users and tracking them via a UTM and leverage Data Driven Labs UTM builder to help you create your UTMs.

First Step to tracking an event using an href onclick event?

First rule to setting up an onclick event; identify what type of Google Analytics code you have set up on your website. There are two options:

  • Classis Analytics
  • Universal Analytics

[themify_box style=”blue highlight”]For more Information on how to set up Google Analytics click here[/themify_box]

Below is what a classic analytics script would look like on your website:

Classic Google Analytics

Below is what a universal analytics script would look like on your website:

Universal Analytics Code

What’s Next?……

Structure your Events to be Successful

There are three areas that we will focus on in tracking our marketing efforts

  • Category
  • Action
  • Label

There is an art to structuring your events so that you can track them in Google Analytics. Here is an example of one method in which you would structure an href onlclick link or element. This example is on a newsletter subscription form; both a popup and an on-page form on a homepage.

Newsletter subscription popup

Category: “Newsletter“, Action: “Subscribed“, Label: “Popup


Newsletter Signup Homepage

Category: “Newsletter“, Action: “Subscribed“, Label: “Homepage


In the example above I able to track both events under the newsletter category and determine where the subscription occurred. This is very helpful because now I have the ability to see what marketing channels are influencing the users decision to subscribe to my newsletter.

Structuring your href onlclick link or element can get extremely detailed and precise. Here is an excellent resource for an event tracking naming strategy.

Build Your Href Onclick Event with an Event Tracking Builder

Now that you understand the intent of structuring your events lets set one up using the Google Analytics Event Tracking URL Builder by SageRock. In this example it will be to track the above newsletter subscription performed on the home page. Our categories will be structured as follows:

  • URL = https://www.megamikejr.com/
  • Category = Newsletter
  • Action = Subscribed
  • Label = Homepage

Event Tracking Builder

Since we are tracking a button and not an actual link we want to attach the event parameters to the button on our subscription form. There are many was to go about identifying the area in which you need to access this code. So I am going to assume you know exactly where to go and how to access this area.

So what you want to do is find the code on the homepage that has the “Subscribe” button in it. You then want to strip the event tracking parameters from the url that was generated from SageRock.

Below is an example of the Asynchronous (Classic) Google Analytics href onclick Link:

  • <a href=”https://www.megamikejr.com/” onclick=”_gaq.push([‘_trackEvent’, ‘Newsletter’, ‘Subscribe’, ‘Homepage’]);”>Homepage</a>
  • Event tracking parameter: onclick=”_gaq.push([‘_trackEvent’, ‘Newsletter’, ‘Subscribe’, ‘Homepage’]);”

Below is an example of the Universal Google Analytics href onclick link:

  • <a href=”https://www.megamikejr.com/” onclick=”ga(‘send’, ‘event’, ‘Newsletter’, ‘Subscribe’, ‘Homepage’);”>Homepage</a>
  • Event tracking parameter: onclick=”ga(‘send’, ‘event’, ‘Newsletter’, ‘Subscribe’, ‘Homepage’);”

Once you have the parameter that matches your Google Analytics code and have found the button area of the subscription form, you then want to insert the parameter just after “<button ” (see below).

Event tracking code

Testing Your href onclick Event in Google Analytics

Excellent! You have now installed the code on your site and are ready to test it. Follow these steps:

  1. Go to your the Google Analytics account that contains the proper UA code
  2. Navigate to the report Real-Time > EventsEvent tracking - Testing
  3. Open a new tab in your browser
  4. Go to the area in which you installed the code
  5. Fill out the newsletter text area
  6. Click the Subscribe button
  7. Go back to Google Analytics
  8. And check to see if the event fired (see below)

Real Time Event Tracking

Note: the event will not fire if you are filtering out your own IP address

If everything is done correctly you have successfully built a trackable event and can now measure the success of your marketing efforts. Congratulations! If you have found this information to be helpful and/or useful, or you still need help figuring out how to build a trackable event in Google Analytics, feel free to comment below.

p.s. don’t forget to subscribe 😉