This means that if you click on that link and purchase an item I will get a commission, at no extra cost to you!
Finally, I found time and a topic to write another Fix-It Friday post about. Today I want to implement tracking affiliate clicks with Google Analytics 4.
There are many ways to track if visitors clicked your affiliate links, and plugins like ThirstyAffiliates make it very easy to do.
I want to have these reports appear in my Google Analytics dashboard without depending on the implementation of a third-party plugin, so I can switch plugins without losing information.
Google Tag Manager
If you want to follow along, you have to have Google Tag Manager installed and use Google Analytics 4. GA4 is a new version of Google Analytics.
To set up Google Tag Manager (GTM) on your website check this tutorial: Setup and install Tag Manager.
To implement GA4, check here: Add the Google Analytics tag for Google Analytics 4 properties to your site with gtag.js.
If you still find it hard to implement, let me know. If there are enough requests I will write a tutorial post about it.
Google Analytics is a great way to keep track of your audience and performance, but it just can’t include all the specifics for every user.
You can, however, add a lot of customizations!
What I want to accomplish is to get clicks on affiliate links registered in my Google Analytics dashboard.
At the moment, it looks like this:
It shows some basic metrics like session_start, clicks, scroll, first_visits, and page_views. As for all GA reports, you can filter them per date or period.
For our goal, tracking affiliate clicks with Google Analytics 4, there isn’t a standard solution, unfortunately.
To show this data, Google first needs to receive and recognize the right events and parameters.
We can add these in Google Tag Manager (GTM).
Creating a Trigger
We start with telling Tag Manager how to identify a click on an affiliate link. There are a couple of ways to do this, and I will explain two of them, but first:
Adding a Trigger
Open your Tag Manager dashboard and find the Triggers menu link on the left-hand side.
You will find all triggers you have made in place here. If there are none, that’s ok, we will add one by clicking ‘New‘.
In the next window, click on ‘Untitled’ and give the trigger a name. Pick one that identifies it, but it really doesn’t matter how you call it. If you want to name your triggers like rock stars, it’s fine.
Now we will have to tell GTM what type of trigger it is.
Click somewhere in the ‘Trigger Configuration‘ panel. A list of trigger types to choose from will appear.
Pick ‘Just Links‘.
This will fill the Trigger Configuration panel with the parameters and conditions form for the link clicks event.
I don’t want to trigger all link clicks, but just the ones that are affiliate links.
To do this, I have to select ‘Some Link Clicks’ instead of ‘All Link Clicks.’
A row will appear where we can define the conditions. Click in the first field and pick ‘Choose Built-In Variable‘.
Selecting Trigger Conditions
The next step is important. It is important to use an identifier for the link that your visitor clicked, so GTM knows how to distinguish affiliate links from others.
One way to do this is to look at the URL. My affiliate links get a prefix from ThirstyAffiliates, and almost every link has ‘/gig/’ as part of the URL.
I could choose ‘Click URL‘ as a variable and check if it contains ‘/gig/.’ This, however, would make it more difficult to detect uncloaked links or links that, for some reason, I am bypassing the ThirstyAffiliates prefix mechanism for.
It would look like this in GTM:
Another way is to look for other identifiers.
I add a CSS class to every affiliate link. This will make it easy for me to customize how they look if I think it is necessary. I called this class ‘aflk’.
ThirstyAffiliates makes it easy for me to add a class automatically, but adding classes to links is a feature that almost every method of inserting links supports by default.
If I ever want to switch plugins or quit using ThirstyAffiliate, the tracking of affiliate clicks will still be working.
This condition would look as follows:
Adding a Tag
Now save your trigger and head over to the ‘Tags‘ section by clicking it in the menu on the left.
In it, find the ‘New‘ button and click it.
Name your tag and click in the ‘Tag Configuration‘ panel.
Select ‘Google Analytics: GA4 Event‘.
In the ‘Tag Configuration,’ you can select one of your configurations. There is probably just one or maybe two, but if there are more, you are most likely a little more advanced and know which one you need.
Select ‘Event Name‘ and click the icon.
A panel appears where you can select a variable. We are going to add on ourselves.
Name the variable and click in the ‘Variable Configuration‘ panel and select ‘Custom Event’.
Save it and open the ‘Event Parameters.’ We are going to add 2. One for the URL and one for the text that the clicked link contains.
Name them and take note of the names you are using here. You will need them later.
In the ‘Value‘ column, click the field and find the right variable from the list that appears. You will need ‘Click URL’ and ‘Click Text.’
Mine looks like this:
When you are done, scroll down to the ‘Triggering‘ panel. Click inside and find the trigger you made before.
Now save it. To make sure that everything is in place before we release this new version of our tag configuration, we will test it with the preview mode.
Find the ‘Preview‘ button in the upper right corner and click it.
You can probably leave all as-is in the ‘Start Tag Assistant‘ panel, but if you want to go straight to a certain page, you can adjust the URL. Click ‘Start‘ when you are ready.
Your website should appear in a new window. Browse to an affiliate link and click it. Maybe try a few more.
Return to the window that holds the GTM. Click the ‘Continue‘ button.
If your tag has fired, you are good to go! If not, review the previous steps, and see if you haven’t missed one or made a spelling error.
Now close this window and head back to your GTM workspace.
It is time to submit and publish our new configuration. Click the Submit button. Give your version a name and a description.
Did you write down the names of the Event Parameters?
If everything went well, we can close the GTM window and head back over to our Google Analytics window.
Configuring Google Analytics
Now we need to tell Google Analytics that we have new data to process.
Go to the ‘Events‘ tab, click on ‘Events‘ and find the button saying ‘Manage Custom Dimensions.’ Once clicked, you can add a custom dimension.
Add two—one for each event parameter (yep, the ones you wrote down). You can keep the Custom dimension name the same as the parameter name. I changed mine a little.
And now… We wait. You won’t see them in your dashboard, nor can you create reports with them until the next day (which is why I had to wait to finish this post).
Once the data is collected, you will see them in your standard reports, and you can use the parameters for your custom ones.
Tracking affiliate clicks with Google Analytics 4 and Google Tag Manager takes quite a few steps to implement, but it is not hard to follow the above steps.
Tracking your links this way makes you less dependent on the analytical dashboards and implementation of a plugin.
To do this in the ‘old’ Analytics is similar to this process, although some parameters and configurations are different.
If you haven’t started using affiliate links in your blog and wonder how affiliate marketing works, then I highly recommend The Zero To Superhero Affiliate Marketing Training Bundle.
I hope you enjoyed this post and found it helpful. If you did, then please share it among your social networks.
Don’t forget to subscribe to my email list to get updates on new posts!
Thank you for reading and as always: you are awesome!