Attached information to a Link dynamically with Google Tag Manager (Part 3)

You can use a GTM script to dynamically attached information to a link. This is useful if you want to transfer information from one website to another. In this video, we are going to turn all our Amazon links into affiliate links with the help of GTM. Learn more about setting this up in today’s video.

Part 1 – Change Links with Google Tag Manager (Part 1)

Part 2 – Change Links (Search & Replace) with Google Tag Manager (Part 2)

🔗 Links:

Custom HTML Code:

Container Download:

🎓 Learn more from Measureschool:

🔀 GTM Copy Paste

🚀Looking to kick-start your data journey? Hire us:

📚 Recommended Measure Books:

📷 Gear we used to produce this video:


Welcome back. In this video, we’re gonna find out how we can attach something to a link instead of replacing a link itself. Now, some of you might have noticed that when I click on to my product links here, I go over to Amazon, but there’s no affiliate ID attached to it. So Amazon wouldn’t know who sent this traffic and wouldn’t know to send me my affiliate commission. So let’s change it. I am part of the Amazon Associates program. So I can get a link here, I can choose my tracking ID, I have several here, I’m going to go with the generic one. And I can get the link and this will give me a shortened link. I will actually take a look at the full link here and copy that. And we see that from the original URL, Amazon has attached a lot of different other information there.

Now, the most important information is actually this tag information that puts in my affiliate code onto this URL. So Amazon can identify me and knows that the traffic that I just sent him his affiliate traffic. Now, this is all wrapped up in a query string, we can see this here with the question mark, and then it starts with IE, and then some other information. That’s actually not what we need. We just want our link to look kind of like this. Let’s get rid of this here. And then just our tag and let’s check. This link works. And it shows Amazon that I sent a traffic from my measureschool affiliate account. So this is the link that we want to implement into our page. Obviously, we could go through all the links right here and change them in the HTML code itself. But what if you wanted to do this dynamically with the help of Google Tag Manager? Well, I’ve written a little script expanding on our link changing technique, which is here under the link add. And again, we are simply collecting all the links that have Amazon in them. And then I can enter something that I want to attach to this string. In our case, if you look at the URL itself, we can see that they don’t have query strings attached yet. So we’re going to start out with a question mark that we want to attach to it. And then our affiliate ID. In our case, that would be tag.

So this is something we want to attach to all the links that lead to Amazon. And we want to fire this on a trigger DOM Ready. Now DOM ready is very important because we want to make sure that the links have already been rendered, and we are now changing them and attaching our affiliate ID. So don’t make the mistake to fire them on the page view. The DOM ready needs to have happened before we can change any links. So I’ve attached this DOM ready all pages trigger and gonna unpause this so we can try it out. Let’s save this and refresh our preview and debug mode, refresh our page and there we go. If I click the buy button, now this is attached to our link and we can transfer information about us, about the affiliate over to our partner page or to the seller in this case. So we will be able to get the affiliate commission if the user buys. So pretty easy technique to tag all the links on all the pages and lead to Amazon automatically with your affiliate code. If you wanted to change that affiliate code, obviously pretty easy to change it around again in this script, and simply replace or change this around.

Now, this particular technique could be also used to attach other information that you would want your partner page to have or your other system to have if you send the user on to another domain. You could utilize it for UTM tags, for example, or add additional information to a link that you would like to have available and as third-party tracking system like a contact ID for example. So really versatile. A little caveat here that you need to take care of if you want to utilize this on many links is that this will not take care of any links that already have a query string attached. So if there’s already a question mark in the URL, you might need to replace this with a & sign and then your key and your values as double quotation mark would lead to an error or not being recognized by the tracking system. So just be aware of that as well. If there’s already a question mark in there, you might need to write some checks in order to attach the right thing to the URL. In the next lesson, we’re going to take a look at how we can change or attach a string dynamically based on where the user came from.


Leave a Comment