Learn how to track clicks on links that lead to other websites in GoodMetrics, either for specific links or for every outbound link automatically.
Outbound links are links on your site that point to another website. Tracking them tells you which external destinations your visitors click through to, such as partner sites, affiliate links, or social profiles.
GoodMetrics tracks outbound clicks using a single event with the destination URL passed as a property. This keeps your reports tidy: one event holds the total outbound click count, and you can expand it to see which URLs were clicked.
Track specific outbound links
Section titled “Track specific outbound links”If you only care about a handful of external links, add a shared class to the ones you want to track and pass each link’s URL as the property.
Add a class to the links:
<a href="https://partner.com" class="gm-outbound">Visit our partner</a>Then track clicks on those links:
window.addEventListener('load', function() { document.querySelectorAll('.gm-outbound').forEach(function(link) { link.addEventListener('click', function() { gmTrackEvent('Outbound Link Click', { property: link.href }); }); });});Every link with the gm-outbound class now rolls up under a single Outbound Link Click event, broken down by destination URL.
Track all outbound links automatically
Section titled “Track all outbound links automatically”To track every link that points to another website, use the script below. It detects outbound links by comparing each link’s hostname to your own, and passes the destination URL as the property:
window.addEventListener('load', function() { document.querySelectorAll('a[href]').forEach(function(link) { if (link.hostname && link.hostname !== window.location.hostname) { link.addEventListener('click', function() { gmTrackEvent('Outbound Link Click', { property: link.href }); }); } });});Place this after your GoodMetrics tracking script. Internal links are skipped automatically, so only clicks to external sites are tracked.
Viewing your results
Section titled “Viewing your results”In the Top Events report, find the Outbound Link Click event for the total click count. Expand it to see the breakdown by destination URL.
- These scripts attach to links that exist when the page loads. If your site adds links after load (for example, in a single-page app), those links will not be tracked until the script runs again.
- The destination URL is sent as the property, so links with the same URL are grouped together.
- You can use any event name you like.
Outbound Link Clickis just a convention.