15

I'm setting up Google Tag Manager on a client's site and I'm having trouble getting click event tags to fire.

I have the trigger set to fire on the button's CSS selector.

The button itself has some child elements, including an svg icon. When the svg is clicked, the click is registered in the data layer, but the tag is not fired. The tag only fires when I select the button itself.

I also tried removing event listeners in my own scripts that were attached to these buttons in case a return false; or e.stopPropagation() was blocking it, but this didn't change anything.

I had the understanding that GTM listens for click events that bubble up to the document. If this is the case my tag should fire when a child is clicked, right? Or am I misunderstanding something?

Alternatively, should I push the event to the dataLayer in my scripts rather than using a click trigger?

screenshots

10 gtm.click correctly fired the tag

9 gtm.click was the child svg that did not

The last screenshot is the firing rule for my trigger.

5
  • What is your target tag type? Is it an anchor, or is it a div or button or something else? How have you configured your click trigger? Is it firing on All Clicks or Just Links?
    – nyuen
    Apr 14, 2016 at 5:34
  • The target element is a div. The click trigger is set for all clicks, with a filter of "Click Element" to match a CSS selector.
    – alaskey
    Apr 14, 2016 at 18:41
  • 4
    Ok I think I found the answer in their docs. I need to change the target to an anchor, then change the trigger to "Just Links". "All elements" will bubble up the child info, whereas "Just Links" will give me the parent anchor info.
    – alaskey
    Apr 14, 2016 at 18:45
  • Yes, that's correct, and you could also do other things with Custom JS variables if you use the "All Links" enabler for non-anchor elements.
    – nyuen
    Apr 14, 2016 at 18:51
  • For reference, here's a wayback link for the docs @alaskey mentions above since it can no longer be found.
    – Gabrien
    Jun 24, 2021 at 15:19

3 Answers 3

22

I've encountered this type of problem a lot. It happens with <i> tags for things like glyphicons as well. Simply add CSS pointer-events:none; to that SVG (unless you require that SVG to be clickable and not just the parentElement). The pointer-events:none on the SVG will mean that when it is clicked the click event registers on the parent element.

Best way would be to have the client developers add the JS. the more hacky way would be to run something like this in a custom HTML tag via GTM

jQuery('a.link-youre-tracking svg').css('pointer-events','none)

2
  • 1
    Very nice. This is what made GTM bubble its gtm.click event to the immediate parent <a> of my <a><img></a> scenario, which is similar to wrapping svg etc'.
    – adi518
    Mar 7, 2018 at 13:09
  • I have <button><i>content</i></button>. Chrome think that I click on <i>, but FF think that I click on <button>. pointer-events:none; on <i> tad resolve this problem and all browsers now think that I clicked on <button>
    – MiF
    Feb 28, 2019 at 12:44
1

Grate solution/idea to use:

pointer-events:none

But what happens when you have complex div (20 classes and 15 elements inside) and you wrap this div with a link <a> tag (For blog postcard for example).

For now GTM lack of a normal solution for this issue :( For complex structure you should add "extra div" for pointer-events (Work fine but "not elegant").

<a class="track-this-click-by-gtm" href="url">
  <div style="pointer-events:none">
    extra unwanted div
    <i></i>
    <p>hello</p>
    <ul><li>hello2</li></ul>
    <date>2019</date>
    lorem
  </div>
</a>
3
  • I believe that if GTM is setup to track "Just links" for that trigger, it will bubble up to the <a>. Jul 31, 2019 at 18:10
  • 2
    @slothluvchunk Unfortunately, it does not. I've just tried that out and the link click was not triggered :-|
    – Oliver
    Aug 17, 2019 at 8:13
  • Interesting - I use this solution on a large number of triggers, and turning off pointer-events allows me to track whichever <a> I'm basing the triggering on, rather than the actual clicked element. Is your trigger filtering by CSS selector? An example of this is an audio player, where I track clicks to play/pause icons, but want to trigger based on the CSS selector for the actual <a> and not the svg that actually gets clicked - which has a very small/inconsistent click area. Aug 18, 2019 at 6:01
1

As said before the “Just Links” trigger will bubble to the parent <a>, so using that instead of “All Elements” should solve any issues you have with clicks registering on children of an <a>. But what if you’re trying to register clicks on a parent <button>, for example? Then you could use a Custom JavaScript variable called “Find closest” with this function:

function () {
  return function (target, selector) {
    while (!target.matches(selector) && !target.matches('body')) {
      target = target.parentElement;
    }
    return target.matches(selector) ? target : undefined;
  }
}

And then use that function in another Custom JavaScript variable like this:

var elementFound = {{Find closest}}({{Click Element}}, 'button');

Read Simo Ahava’s article on this for more info.

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.