Skip to main content

Tracking link clicks

You can track link and button clicks as custom events in GoodMetrics to measure engagement with CTAs, outbound links, download buttons, or any other clickable elements on your site.

Step 1: Add the click tracking script to your site

Add the following script to your website, placed before the closing </body> tag and after your GoodMetrics tracking script. This script listens for clicks on any element with the data-gm-event attribute and triggers the corresponding event.

<script>
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('[data-gm-event]').forEach(function(el) {
el.addEventListener('click', function() {
var eventName = el.getAttribute('data-gm-event');
if (eventName && typeof gmTrackEvent === 'function') {
gmTrackEvent(eventName);
}
});
});
});
</script>

Step 2: Add the tracking attribute to your elements

To track a specific link, add the data-gm-event attribute with your event name:

<a href="https://example.com" data-gm-event="Click Example Link">Example</a>

Tracking button clicks

The same approach works for buttons:

<button data-gm-event="Start Free Trial">Start Free Trial</button>

You can add the data-gm-event attribute to as many links and buttons as you want. Each element can trigger a different event by using a unique data-gm-event value.