8

In Google Tag Manager, I set it up to track some data from clicks on elements that contain a certain class and record an event in Google Analytics. It seems to work just fine for text links, but I run into problems if there is another tag inside the link for an image, icon, etc. For example, the following would work fine:

<a href="link.html" class="track_this" data-tracking-info="my info">Click here</a>

But this won't work:

<a href="link.html" class="track_this"  data-tracking-info="my info">
  <span class="icon click-here"></span>
</a>

And something like this will work if you click on the text, but not if you click on the icon:

<a href="link.html" class="track_this"  data-tracking-info="my info">
  <span class="icon click-here"></span> Click Here
</a>

I know that I could add the "track_this" class into the span for the icon, but it gets REALLY messy in more complicated scenarios. Like imagine having a thumbnail image with an icon and some text below it all wrapped into one a tag. I'd have to put that class and the tracking info on the image tag, the span for the icon, the div for the text, etc.

Is there a better way to do this? Thanks!

3 Answers 3

12

I could speak more definitively on this if I could see how your GTM was setup, but my guess is that you are using an "All Elements" trigger to capture these link clicks, and filtering on "Click Classes" or "Click Element". The issue with this is that, when the link tag (<a></a>) contains another element, such as a <span>, even though that triggers your link to open, the element that GTM records as receiving the click is the span, not the link.

If you want to fix this, there are two options, either of which should work.

The first is to switch to using a "Click - Just Links" trigger type, and filter on the class "track_this". For this trigger, GTM lets click events "bubble" up until they hit a link element, and then it tests your trigger against that link, instead of the element that was clicked on. Simply using this trigger type should work for all three of your samples.

The other option is to use a more advanced filter with the "Click - All Elements" trigger. If you modify the trigger so it fires on "Some Clicks", and then make the condition that "Click Element matches CSS selector:"

.track_this, .track_this *

then it will register a click on any element that has the track_this class, as well as a click on any element inside those elements.

1
  • 1
    I have the same question but this reply is not working for me and I don't know why. In my case, I have a a <ul> that contains several <li>. I need to track when the user clicks any of these <li> elements. Each <li> has other html elements in it. The <li> has a the id of "producto-destacado". Any thoughts?
    – Paula
    Jul 19, 2020 at 23:50
1

This problem can also be solved using a little bit of javascript and a 'User-Defined Variable' in Tag Manger. This solution is for handling more complex UI components.

Explanation

Google Analytics + Tag Manger record the very specific Element or Node that is clicked by the user. That element is stored in GA as a "Click Element" variable. So in more complex UI situations it is possible the user could click on multiple elements for a single action to occur. For example. Here is a button with an icon and text.

<div
   class="button"
   id="PARENT_ID"
   onClick = () =>  ... 
>
   <span id="CHILD_ONE">
      icon
    </span>
    <span id="CHILD_TWO">
      text
     </span>
</div>

In this scenario it is possible for the user to click on any of the three id's above. All three will activate the onClick action. However, Google Analytics doesn't care about the onClick. It only cares about what specific element was clicked. IE: PARENT_ID, CHILD_ONE or CHILD_TWO.

The "User-Defined Variable" Solution.

  1. In Tag Manager go to the 'Variables'. (Left column menu.)
  2. Add a new 'User-Defined Variable'.
  3. Select the variable type as 'Custom JavaScript'.

Add:

function() {
  if ({{Click Element}}.id != "") {
    return {{Click Element}}.id;
  }
  if ({{Click Element}}.parentNode.id != "") {
    return {{Click Element}}.parentNode.id;
  }
   if ({{Click Element}}.parentNode.parentNode.id != "") {
    return {{Click Element}}.parentNode.parentNode.id;
  }
  return {{Click Element}}.parentNode.parentNode.parentNode.id;
}

This script will search the DOM up three levels up from any child Element (Node) and look for a matching Tag id. NOTE: Click Element is the variable name used by Google Analytics. It's the gtm.element the user clicked.

Setup Tag Manager Configuration to use your new 'Custom Variable'.

Customer Variable Tag Configuration

Now use the parent id for setting up your Triggers. In my example PARENT_ID will be the returned id even if a user clicks on CHILD_ONE or CHILD_TWO. So select 'contains' PARENT_ID.

------ Further Considerations -----

This solution only works within three parent levels. Also while unlikely it is possible to capture an element out of scope of what is intended.

In more complex UI components it might be preferable to add the Tag id's to every Element. If you are using a front end framework like React I would suggest making the Tag Id a dynamic prop and add it to all child components.

NOTE: Google Analytics changes often. This is a GA4 + Tag Manager solution.

1

style use pointer-events:none can't tracking by inside.

<a>
  <svg id="gtm-track">
    <rect style="pointer-events:none"> .... <rect/>
    <path style="pointer-events:none"> .... <path/>
  <svg/>
<a/>
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. Sep 19, 2022 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.