Track Outbound Links in WordPress with Google Analytics (no plugin, no redirect, no markup changes)

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.

Ready to get more leads and sales?

Use Hollerbox to design and deploy popups faster.

4 thoughts on “Track Outbound Links in WordPress with Google Analytics (no plugin, no redirect, no markup changes)”

  1. Thank you,
    I have just started to track my first outbound links.
    The plugin works flawlessly, I can track all the external links and see specifically where they where targeted.
    You saved me from a lot of work. I have add holler box in my premium plugin’s list.
    Cheers!

  2. Dear Scott

    This plugin works beautifully! Is there any way to adapt it to trigger when a linked image is clicked instead of just a text link? It won’t trigger when the link is an image, I have tested both an image link and a text link, and only the text link click goes through to GA.

    I am happy to compensate you if you could adapt this for an image link.

    The website in the website field is the site I am trying to get it to work, if I use the images as links, it doesn’t send the event to GA, so I have implemented text links under each image, and this works.

    Thank you
    Ian

Leave a Comment

Scroll to Top