Log in to your account
Sign up

How to find and dynamically replace links with GTM

05 December 2018 | 0 comments | Posted by Che Kohler in nichemarket Advice

Dynamicallly replace links using GTM

If you've been following my blogs for a while and don't know that GTM is one of my favourite tools, then I obviously am not preaching the gospel enough. GTM continues to amaze me not just with its user interface updates and features but in the way the community has rallied around the tool and continue to come up with exciting use cases.

Did you hear the good news?

You no longer need a developer for every little thing your mind, team, boss cooks up when trying to improve the performance of your site. GTM is excellent for those times where you're low on resources, and you want to make a chance and prove a point. Perhaps you run a test via GTM, and if it has a positive effect on your website, you can provide the numbers and make a case for a ticket to deploy it as a feature.

Why would I need to migrate my features?

Yes, I realise you can do it in GTM, but it doesn't mean everything that can be done via GTM, should be done via GTM. It's a javascript based tool and has its limitations. You don't want to sacrifice site performance and leave everything to "hacks" if you can help it. Migrating your newly tested features to server-side rendered code is always the end goal.

Why would I want to replace links?

Websites as continue to get bigger and bigger by the day. What was once a simple single domain site has now turned into multiple subdomains, multiple domains, multiple subfolders for various devices or user needs. Managing a website that big or with so many elements can often cause users to end up in places not meant for them. This could be via a referral link, an old social media post or even poor tracking in ads or remarketing. 

Whatever the case may be, there is always a way to salvage the situation, and link finding and replacing does just the trick.

Why am I excited about this feature? Well for its endless applications of course. It could be used to redirect users to preferred versions of content or products; it could be used to improve your tracking or automated and much more. 

How do I replace links with GTM?

The link find and replace feature is not a standard feature of GTM and you will need to make use of some custom javascript. In the example below, we will re-write nichemarket.co.za users to nichemarket.co.za/af/ should a user who would like to see Afrikaans content

Creating your trigger

In this case, a trigger would need to be configured on a custom variable triggered by the users selected preference.

Creating your tag

  • Head over to your GTM account
  • Create a new tag
  • Provide a name - IE - *Use case* Redirect
  • Under tag configuration select custom HTML template
  • Paste in the following code


    (function () {

    var links = document.querySelectorAll('a[href*="nichemarketbooks.co.za"]')

    //change nichemarket.co.za to any domain you want to target

    var searchString = ".co.za/" //the string to be searched forEach

    var replacementString = ".co.za/af/" //the replacement for the searched string


    var original = link.getAttribute("href");

    var replace = original.replace(searchString,replacementString)

    link.setAttribute("href",replace) }) })();


    • Add the trigger we created earlier
    • Click save
    • Click Publish
    • Test your tag

    Use cases for replacing links with GTM

    Now that you know how to find and replace links here's how you can apply them to your site. These use cases may help you get your head around why this is such a helpful feature and how it could possibly be applied to your website.

    Affiliate Linking

    We all know that affiliate linking is a heavily flawed system and creating custom affiliate links is a pain for everyone involved. Getting your links wrong only benefits the company running the program as they don't have to pay out commissions on links that are not tagged correctly. Using GTM to find and replace links can help affiliate programs be more effective in their tagging by removing the chance of human error.

    Geolocation linking

    If you have domains or subfolders geared to serving users from various countries you can easily pick up where these users are coming from. Then redirect the user to the correct version of the site should they end up on the wrong version.

    Personalisation linking

    If you have preferred versions of product pages based on the region within your country, the type of traffic channel they enter from, or even custom variables you created for your site. You can fire the tag on any of these and send users to a preferred link for a more optimised experience.

    Language linking

    If you have domains or subfolders geared to serving users with various languages you can trigger the tag based on users language preference. Then redirect that user to the correct version of the site should they end up on the wrong version.

    Mobi site linking

    If you're using a mobile site or AMP site you may want to fire a tag based on device or screen size and send the user to the preferred version of the site for a more optimised experience.

    Road blocking users

    You could create a custom event that picks up if a user is not logged in or signed in to your site and instead of giving them access to certain content you can redirect them into creating an account.

    The SEO impact of rewriting links

    If you're planning on doing link rewriting you may also want to consider the impact it may have on SEO, depending on what you're trying to achieve you may want to block certain subfolders or specific pages, use canonicalisation, a ref lang and region tags. This will help Google understand why you're redirecting users and not see these pages as duplicate content.

    Calling in the right replacement

    No website is perfect, and users can always find themselves in places not meant for them. It's up to you to try and navigate them the ideal part of the site, think of yourself as a digital chauffeur or sherpa. New users may need more help than ones familiar with your site and its particular quirks so being able to guide them effectively can come in handy. 

    We all know how important it is to have an optimised experience and how it is key to retention and conversion. 

    If you have any interesting ways with which you would replace links on your site, then let me know in the comments! I'd love to hear about it.

    Contact us

    If you want to know more about digital marketing for your site, don’t be shy we’re happy to assist. Simply contact us

    Tags: google tag manager , tracking , analytics

    Previous: {{ previousBlog.sTitle }}

    Posted {{ previousBlog.dtDatePosting }}

    Next: {{ nextBlog.sTitle }}

    Posted {{ nextBlog.dtDatePosting }}

    You might also like

    Amazon Streaming Ads

    Amazon Expands Sponsored Streaming TV Ads

    11 July 2024

    Posted by Che Kohler in Shopaholics

    Amazon launches sponsored TV ads, offering brands a new way to reach streaming audiences. The ads leverage the e-commerce giant's vast data for targe...

    Read more
    Comp shopping tips

    How To Do Fashion Comp Shopping in South Africa

    17 July 2024

    Posted by Inna Makovska in Fashionista

    All items are on your device: now you can easily create a look with a few clicks. Learn how to maximize the benefits of online shopping and save time...

    Read more

    Leave us a comment


    {{comment.iDayLastEdit}} day ago

    {{comment.iDayLastEdit}} days ago


    Sign up for our newsletter