Track Link Clicks with Google Tag Manager and Analytics


Tracking basic Google Analytics information is a great start to understanding your users, and how they access and use your website, but doesn't always provide in-depth information. In today's guide, we're going to look at how to track specific link clicks by your users using Google Tag Manager combined with Google Analytics.


Why to Track Link Clicks with Google Tag Manager

While Google Analytics on it's own is great for tracking pageviews, time on each page, and how your users move throughout your website, on it's own, it won't track specific link clicks that are external to your own website.

Where a lot of blog and website creators might need this information is when you have affiliate links on your website. You want to know how many of your users click affiliate links, but Google Analytics, on it's own, won't tell you this information. That's where Google Tag Manager and Google Analytics events come into play.

The 4 Steps to Tracking Link Clicks on your WordPress Website

There are 4 overall steps you need to complete if you want to track clicks to your affiliate links (or other external links) within your WordPress website.

  1. Setup a uniform 301 redirect for each of your affiliate links in WordPress
  2. Create Events for each of your affiliate links in Google Analytics
  3. Create a Google Tag Manager account and connect it to your WordPress website.
  4. Create Tags and Triggers for each of your affiliate link clicks.

See below for in-depth how-to's for each of the steps.

Steps to Setup 301 Forwards for External Links in WordPress with the SEO Framework

For any affiliate links that you plan to use on your website, the best thing to do is create a page within your website that uses a 301 forward to your affiliate link, instead of linking directly to the affiliate website. This is good for multiple reasons:

  1. Instead of a link that looks spammy with tracking code and affiliate ID, your users will just see a link within your own website, which is much more trustworthy.
  2. If you ever need to update your affiliate link in the future, you will only have to update the 301 redirect page, instead of finding the link on every page of your website and updating each of them manually.

Here's how you can setup 301 redirects for your WordPress website, using the SEO Framework Plug-in.

Step 1 : Install the SEO Framework WordPress Plugin

When you use the free SEO Framework plugin for your SEO needs (which I highly recommend), it includes the functionality to add 301 redirects for any page. You can follow my SEO Framework guide for a step-by-step guide on installing and setting up this plugin.

2 - install and activate The SEO framework plugin

Step 2 : Create a New Page

In your WordPress dashboard, access the Pages menu, and click Add New.

1 - Redirect - add new page in wordpress

Step 3: Create the page title and URL slug

Create a page title that represents the URL redirect, and make sure that it creates a good URL slug, as this is the link you will use on all of your pages to link to your affiliate.

You can also create a bit of a description for the page content, in case the redirect ever errors for someone visiting the link.

2 - Redirect - create page name and slug

Step 4 : Update Meta Settings and 301 Redirect URL

In the Visibility section of the Page SEO Settings, set Indexing to noindex, and link following to nofollow. Also check the Exclude box. This tells Google and other crawlers that this page is just a placeholder and doesn't have any content.

Now input the 301 Redirect URL to your affiliate link that you want your users to go to.

3 - Redirect - update Page SEO settings

You can now use the link for this new WordPress page anywhere on your website where you want to link to your affiliate, and it will forward your users to your affiliate link!

Steps to Create Events for Links in Google Analytics

Now that you've created the 301 redirect for your affiliate link, it's time to create an event in Google Analytics that will be associated with a click on that WordPress link.

Step 5: Access the Goals menu in Google Analytics

Login to your Google Analytics account (see the guide on how to install Google Analytics on your WordPress site), click on the Admin setting cog on the bottom left, and access the Goals menu. This is where you can create custom events for tracking link clicks.

1 - Access goals menu in Google Analytics

Step 6 : Add a New Goal

To setup a new event, click the New Goal button from the goals menu.

2 - Create a New Goal

Step 7: Select Custom in the Goal setup

In the first part of the Goal setup, select Custom, then click Continue.

3 - Select a Custom Goal

Step 8: Fill in the Goal description

Create a name for your goal that accurately represents what you're trying to track, then select Event as your goal type. Then click Continue.

4 - Name your Goal and Select Event

Step 9: Fill in the Goal details

Now fill in the goal details. The better descriptions you put, the easier it is to analyze your results if you start to accumulate a lot of events to track. You can see that in my example, I've put Referral as the category, LinkClick as the action, and the Affiliate type as the Label. You can leave the Value field blank. Then click Save.

5 - Fill out Event details and click save

Steps to create a Google Tag Manager account and connect to your WordPress site built with GeneratePress

Now that you've setup the event that will be associated with users clicking a certain link on your website, you'll now need to use Google Tag Manager to connect the actual actions from your users to your Google Analytics tracking. In this section we'll look at creating a Google Tag Manager account, and how to integrate it into a WordPress website that is built with GeneratePress.

Step 10: Create a new account with Google Tag Manager

Visit the Google Tag Manager website, login using a Google account, and select Add a New Account. From here, fill out the information associated with your website. If you're setting up the Google Tag Manager account with a WordPress website, you can select Web as your target platform.

6 - Create a Google Tag Manager account

Step 11: Copy the code snippets

Google Tag Manager will now provide you the code for you to insert into your website so that GTM can track user actions. There are two separate code snippets you'll need to insert into your GeneratePress WordPress website.

7 - Google Tag Manager Code

Step 12: Add a new Element in your WordPress site

If you've built your website using the GeneratePress theme, it's very easy to insert the code snippets where they need to go. Access the Elements menu in your WordPress dasboard, and select Add New Element. Elements allow you to insert code across your website in certain spots.

8 - Add new hooks

Step 13: Create a Hook for the head

Create a Hook and label the hook with something along the lines of Google Tag Manager Head. Copy and paste the first snippet of code that Google Tag Manager provided you in Step 2. Select wp_head as the hook type, as you want this code snippet inserted in the head tag of every page of your website.

10 - Google Tag Manager Head

Step 14: Add head and body hooks site wide in GeneratePress

In the Display Rules menu, select the Entire Site as the Location. This means you will be able to track clicks on every page of your site. You can exclude pages as you like if you won't be tracking clicks on certain pages.

Save this element. Repeat those steps for the Body code snippet. Create another new element, but this time select wp_body_open for the hook.

11 - GeneratePress Location for Hook

Google Tag Manager is now setup and integrated into your WordPress website. If you've built your website with a different theme, there are plugins available that allow you to insert code snippets in the head and body of the website.

Steps to create Tag and Triggers for your link clicks

Now it's time to create the Tags and Triggers to track the link clicks on your website. Triggers wait for a certain action from a user on your website (e.g. clicking a certain link), and when it happens, activate the associated Tag (e.g. the Google Analytics event created for action).

Step 15: Create a new tag

In the workspace for your new account, click the New Tag button.

12 - Select new tag in Google Tag Manager

Step 16: Setup a new tag

Click the Tag Configuration box to create a new Tag.

13 - Select new Tag Configuration

Step 17: Choose Universal Analytics as your Tag Type

Select the Google Analytics: Universal Analytics tag type.

14 - Universal Analytics for Google Analytics

Step 18: Copy information from the Google Analytics events you created previously

After you've selected the tag type, select Event as your Track Type, and provide the event details that you created in the prior Google Analytics steps.

Then create a new Google Analytics Setting.

15 - Update event settings

Step 19 : Create Google Analytics Variable with your Tracking ID

Now go back to your Google Analytics account. Select the Admin cog icon on the bottom left, click the Tracking Code menu and copy the Tracking ID.

16 - Get your Tracking ID

Step 20: Fill in your GA Tracking ID for the Variable Configuration

Paste the Tracking ID into your Google Tag Manager Google Analytics Setting.

17 - Input your tracking ID

Step 21: Configure a New Trigger

Now select the Triggering box to add a trigger to be associated with the Tag you just created.

19 - Create a trigger

Step 22: Click the Plus Sign to Add a New Trigger

To create a new trigger based on a user clicking a specific link on your website, select the Plus icon.

20 - Click the plus button

Step 23: Select Just Links as your Trigger Type

Choose Just Links as your trigger type. This is the trigger type that will monitor the links that are clicked on a page on your site.

21 - Select just links

Step 24: Fire Trigger only for Certain URL Clicks

Select Some Link Clicks for when the trigger fires. Select the trigger to fire with Click URL that contains the slug of the affiliate link URL page that you created earlier. This means the event trigger will fire whenever a link is clicked on your website where the URL contains the text string that you input.

22 - Trigger Configuration

Step 25: Save your new Tag

Review your tag and trigger settings, and click Save.

23 - Save tag

Step 26: Submit your Workspace Changes

Now that you've created your tag and trigger, you have to publish them to your website. Click Submit at the top right.

24 - Submit tag changes

Step 27: Publish Submission

Name the changes that your making to your website, and then click the Publish button. This will publish that tag and triggers live to your website.

25 - Publish changes

You can now use the link for this new WordPress page anywhere on your website where you want to link to your affiliate, and it will forward your users to your affiliate link!

Google Tag Manager is now setup and integrated into your WordPress website. If you've built your website with a different theme, there are plugins available that allow you to insert code snippets in the head and body of the website.

Finished and Testing

And there you have it. Your website is now tracking particular link clicks that happen on your website, and show you the results in your Google Analytics page. You may have to clear any page cache that you have for all of the code you added to go live.

To preview, open your Google Analytics account, and select Events under the Realtime reports section.

Now open your WordPress website in a different browser, and click the link that you've just setup tracking for. In your Google Analytics account, the Event link show now show up!

27 - Testing Google Tag Manager link

Leave a Comment