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
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.
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;
}
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.
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.
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.
Here is how you can get the element ID of any element on your landing page by clicking on CSS style editor:
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: