I am using the Universal Analytics tag in Google Tag Manager to track user interaction. I want to set up click listeners from within GTM that will fire when certain buttons are clicked on the page. The buttons are Angular Material components.
The issue is that Angular Material puts a wrapper element on top of my button, changing the HTML from this:
<button mat-raised-button type="button" class="blue l" (click)="onContinue()">CONTINUE</button>
to this:
<button _ngcontent-c20="" class="blue l mat-raised-button" mat-raised-button="" type="button">
<span class="mat-button-wrapper">CONTINUE</span>
<div class="mat-button-ripple mat-ripple" matripple="" ng-reflect-centered="false" ng-reflect-disabled="false" ng-reflect-trigger="[object HTMLButtonElement]"></div><div class="mat-button-focus-overlay"></div>
</button>
In order to pick up the click events in GTM I have to set the trigger to listen for clicks on the <span> element rather than the <button> element. Thus I loose all identifying information about the button; Any HTML id or class that I use for the <button> is not reflected in the <span>. GTM is unable to distinguish which button was clicked. How can I uniquely identify my button in a way that is visible to GTM?
Note: Identifying the button using the button text (the inner HTML of the <span>) is not an option since I have multiple buttons on the page with the same text.
I ran into this problem and finally found the solution:
The solution in Google Tag Manager:
Enable Variable-type Click Element.
Create two triggers:
1) Trigger Type: Click-All Elements, Some Clicks, Click ID - equals - "your-id"
2) Trigger Type: Click-All Elements, Some Clicks, Click Element - Matches CSS Selector - "button#your-id span" (see image)
Then in your Tag set two triggers, one to trigger 1, the second to trigger 2.

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With