Link click tracking in Google Tag Manager for GA4

In this guide you will learn how to track specific links on your page inside of GTM.

1. The first step is to ensure that your GTM container ID is added on your landing page: Implementation guide available here.

Alternatively, you can place the GTM Google tag in the Settings > HTML > Head section of the landing page.

2. Create a new custom variable and use the type Custom JavaScript

Screen_Capture_on_2022-06-22_at_13-27-42.gif

3. Name the variable something descriptive, such as Instapage element-ID so that you can easily identify it. We will use this variable inside triggers in GTM.

Screen_Capture_on_2022-06-22_at_13-30-26.gif

4. Paste the following code inside the variable. Make sure that you Save after.

function () {
  if ({{Click Element}}.parentElement.id !== '') {
    return {{Click Element}}.parentElement.id;
  }
  else if ({{Click Element}}.parentElement.parentElement.id !== '') {
    return {{Click Element}}.parentElement.parentElement.id;
  }
  else if ({{Click Element}}.parentElement.parentElement.parentElement.id !== '') {
    return {{Click Element}}.parentElement.parentElement.parentElement.id;
  }
  else if ({{Click Element}}.parentElement.parentElement.parentElement.parentElement.id !== '')
    return {{Click Element}}.parentElement.parentElement.parentElement.parentElement.id;
}

Screen_Capture_on_2022-06-22_at_13-43-04.gif

5. Now you need to create a trigger for link clicks. Click on Triggers > New and pick the type Just links as shown in the GIF below.

Screen_Capture_on_2022-06-22_at_14-33-29.gif

The trigger can be set to All Links if one needs to track any type of link or Some Links to customize which specific link on the page needs to be tracked.

Using 'Some Links'

1. Using the built-in Click URL variable from GTM, then the 'contains' operator, you can activate the trigger when a page visitor clicks on any instance of the specific URL that you include.

The event will fire regardless of what type of element or how many elements this URL is placed on in your landing page.

Screen_Capture_on_2022-06-22_at_16-14-07.gif

2. Combining the Click URL variable and the custom one we created, you can set the event to fire only when a specific instance of that URL is clicked; the one attached to the specific element ID. 

trigger.png

Here is how you can get the element ID of any element on your landing page by clicking on CSS style editor:

find_element_id.gif

By using the second option, the event will fire only when both conditions are met: the element ID matches and the URL on that element is the one you specified.

NOTE: The instructions above show you how to create a trigger for link clicks. Inside GTM you will still need to set up the tags that you need, meaning what will actually happen when the action is triggered - what is going to be tracked and in what platform. Make sure to add your GA4 Measurement ID. Here is an example tag:

ga4_tag.png