简体   繁体   English

通过 Google Tag Manager 使用 Google Analytics 进行 Angular Material 按钮点击跟踪

[英]Angular Material button click tracking using Google Analytics via Google Tag Manager

I am using the Universal Analytics tag in Google Tag Manager to track user interaction.我在 Google 标签管理器中使用 Universal Analytics 标签来跟踪用户互动。 I want to set up click listeners from within GTM that will fire when certain buttons are clicked on the page.我想从 GTM 中设置点击侦听器,当点击页面上的某些按钮时,它会触发。 The buttons are Angular Material components.按钮是 Angular Material 组件。

The issue is that Angular Material puts a wrapper element on top of my button, changing the HTML from this:问题是 Angular Material 在我的按钮顶部放置了一个包装元素,从而改变了 HTML:

<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.为了在 GTM 中获取点击事件,我必须设置触发器来侦听<span>元素而不是<button>元素上的点击。 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> .我用于<button>任何 HTML id 或类都不会反映在<span> GTM is unable to distinguish which button was clicked. GTM 无法区分点击了哪个按钮。 How can I uniquely identify my button in a way that is visible to GTM?如何以 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.注意:使用按钮文本( <span>的内部 HTML)识别按钮不是一种选择,因为页面上有多个按钮具有相同的文本。

I ran into this problem and finally found the solution:我遇到了这个问题,终于找到了解决方案:

The solution in Google Tag Manager: Google Tag Manager 中的解决方案:

Enable Variable-type Click Element.启用变量类型点击元素。

Create two triggers:创建两个触发器:

1) Trigger Type: Click-All Elements, Some Clicks, Click ID - equals - "your-id" 1) 触发器类型:点击所有元素,一些点击,点击 ID - 等于 - “你的 ID”

2) Trigger Type: Click-All Elements, Some Clicks, Click Element - Matches CSS Selector - "button#your-id span" (see image) 2) 触发器类型:点击所有元素、一些点击、点击元素 - 匹配 CSS 选择器 - “button#your-id span”(见图)

Then in your Tag set two triggers, one to trigger 1, the second to trigger 2.然后在你的标签中设置两个触发器,一个触发 1,第二个触发 2。

CSS 选择器配置示例

I would reccommend to use a data attribute to seperate the tracking from other code like this:我建议使用数据属性将跟踪与其他代码分开,如下所示:

<button data-gtm-continue-button>...</buttton>

and use a GTM-Trigger like this: Click Element -> matches CSS selector -> [data-gtm-continue-button], [data-gtm-continue-button] *并使用这样的 GTM-触发器:单击元素 -> 匹配 CSS 选择器 -> [data-gtm-continue-button], [data-gtm-continue-button] *

In your case if you do not want to use data attibutes you could also just adjust your CSS selector to the following:在您的情况下,如果您不想使用数据属性,您也可以将 CSS 选择器调整为以下内容:

button#my-button, button#my-button *

this makes use of the css selector * which means any element within the selector.这利用了 css 选择器 *,这意味着选择器中的任何元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM