I wrote a simple script to track outbound links from your WordPress site using Google Analytics.

No plugin, no redirect, and no onclick events in your link tags. No complex setup with Google Analytics, just a simple list of outbound urls in your events.

Click here to skip to the script.

Why is this so hard?

This sounds simple, but it took a while to figure this out.

I wanted a simple way to see what links people were clicking the most, and to list those out in Google Analytics. All of the articles I found on Google were using a paid plugin, or a complex script with redirects. Even Google’s own documentation says to change every link on your site using an onclick event:

You'll also need to add (or modify) the onclick attribute to your links. Use this example as a model for your own links:

<a href="http://www.example.com" onclick="captureOutboundLink('http://www.example.com'); return false;">Check out example.com</a>

Change every single link tag on my whole site? No thank you.

Other articles said you have to use Google Tag Manager, but it requires a bunch of steps to setup. I’m too lazy for that, just give me something I can copy/paste!

The Solution: A Simple Script

I wrote a script that is copy/paste and will track outbound links in Google Analytics events with no other setup or changes.

This script can be inserted between script tags in your site footer. It tracks all clicks on anything, then if it’s a link with an href that does not match your domain, it sends the event to GA.

You could make this script a bit better by only tracking clicks on a certain container, or only links with target=”_blank”.

Make it a plugin!

I created a very simple plugin that loads the script above in your site footer if you want something that doesn’t require coding.

Download the plugin here. Add it to your site under Plugins => Add New => Upload.

You must already be adding your GA tracking script somewhere else.

After adding the tracking code, wait 24 hours, then you can see all your links under Behavior => Events => Event Label in Google Analytics.

Published by Scott

Leave a Reply

Your email address will not be published. Required fields are marked *

Get 50% off the Holler Box Pro Bundle! Click here to redeem.

Holler Box