Tally Clicks by Name with Google Analytics

It is important to know where people are clicking on your web pages. By default, the GA tracker only records which page was viewed. You can add names to all your links to keep a tally.

The GA tracker will look at the title of the document and its URL. You can optionally change what is recorded as the URL. That will produce a page view. On top of that, you can record an event. Like a click or a video play. You can assign a name to each event. Something like ‘opening-img’, ‘nav-link-1’, ‘author-name’, etc. The GA tracker will then record the page view and the name of the click.

Tracking Click Events

To track click events, you have to add a ‘tally’ function that uses the GA send event command.

<script>
    function tally(linkName) { ga("send", "event", "campaignName", "click", linkName); }
</script>

Do read the documentation. Notice the default is to just ‘send’ a ‘pageview’. Here we are creating a function that will ‘send’ an ‘event’. The third parameter is the ‘category’. This is the bucket your tallies will be put in. It is important to name the category something meaningful. Typically I use a string like ‘siteName-region-translation’, maybe the country name too. So something like ‘brand01-EU-FR’ vs ‘brand01-EU-DE’. The foruth parameter is the kind of event you want to record. In the example, we are recording a ‘click’ event. The last parameter is the name of the link.

To actually use the tally function, you have one more step. Somehow the web page needs to know which links it applies to. You can do this the old way or the new way. The old way is to use the onclick attribute of the anchor element. The new way is to register a click event handler.

You should use the new way. I’ll let you look that up and decide how you want to do that. I would use a class name. If that’s not your thing then add onclick=”tally(‘theNameOfTheLink’)” to every link you want to be tracked.

Tracking Documentation

Google Analytics Docs: https://developers.google.com/analytics/devguides/collection/analyticsjs/

Page Tracking: https://developers.google.com/analytics/devguides/collection/analyticsjs/pages

Event Tracking: https://developers.google.com/analytics/devguides/collection/analyticsjs/events