Learn business growth with Google Analytics 4 Forums Google Analytics 4 Tracking External Site Link Clicks with Google Tag Manager and G4A: A Comprehensive Guide Reply To: Tracking External Site Link Clicks with Google Tag Manager and G4A: A Comprehensive Guide

  • Wyatt

    Member
    25 June 2023 at 1:25 am

    Sure, let’s break it down into a discussion:

    Let’s say you want to create a sneak peak link from your website that secretly tracks clicks before whisking users to their intended destination. To do that, you’ll need Google Tag Manager (GTM).

    Here’s a simple step-by-step guide:

    1. First, you need to establish a new URL variable in GTM that checks for the presence of a “redirect” query in your URL. Let’s call this url-redirect-parameter.

    2. Next, you’ll need to craft a JavaScript variable that checks for url-redirect-parameter and redirects users to its URL after a short delay, giving GTM enough time to register the visit. Not tech-savvy? No worries, here’s the code for it:

    `javascript
    function() {
    try {
    return {{url-redirect-parameter}}?setTimeout(function(){window.location='{{url-redirect-parameter}}’},500):false;
    }
    catch(e) {
    return e;
    }
    }
    `

    3. Now, link this new JavaScript variable to a new GTM tag and have it trigger your Google Analytics (GA) tag on your website.

    What you have now is a clever little system that springs into action the moment it detects “redirect=” in your website URL. It logs the visit and then redirects the user to their destination.

    There is another way to do this – by simply adding UTM parameters to a page on your site and have a code on the page auto-redirect to a parameter. It’s a similar principle: the catch here is the presence of a query string (QS) in the URL.

    At the end of the day, both methods tell you who is clicking on your links and where they’re going. Now you can see that traffic in your Universal Analytics results.

    This seamlessly integrates with GTM and GA and doesn’t involve adding additional code to another website. So yeah, it’s pretty cool how you can layer your own tracking onto someone else’s link, right?