Google跟踪代码管理器中的自定义Javascript宏:确定单击了哪个按钮

时间:2014-08-14 14:10:25

标签: javascript jquery google-tag-manager

我有一个访问者获得两个页面,其中有两个按钮:是和否。

这是html:

<footer>
<button id="authCreateAcctConfirmNoBtn" class="button2">No</button>
<button id="authCreateAcctConfirmYesBtn" type="submit" class="red-button">Yes</button></footer>

我对js有点了解,但我知道从控制台看我是否这样做:

document.querySelectorAll('footer > button')

然后返回两个按钮。

如果我这样做:

document.querySelectorAll('footer > button')[0].textContent

textContent&#34; No&#34;返回第一个按钮。类似的,如果我将0更改为1然后&#34;是&#34;归还。

时,触发规则会触发在Google-Tag-Manager中称为宏的脚本

&#34;发生了点击&#34;和 &#34;点击元素的ID包含&#34; nim_authCreateAcctConfirm&#34;&#34; (因此覆盖两个按钮) 然后运行脚本(在Google-Tag-Manager&#34中称为宏)

我的目标是跟踪访问者是否点击了是或否。因此,根据点击的按钮,执行

document.querySelectorAll('footer > button')[0].textContent // for No
document.querySelectorAll('footer > button')[1].textContent // for Yes

我会在触发/触发规则上使用什么脚本来说明返回上述两个选项中的任何一个。我试图避免简单地使用if语句,而是使用一种方式来说&#34;这个&#34;为了

return thisButton.textContent

1 个答案:

答案 0 :(得分:2)

由于您已为两个按钮分配了唯一ID,因此您可以使用它。我认为这将是一种更有效的方法。您可以使用默认的{{element ID}}宏来返回单击按钮的ID。所以在GTM中你会这样做:

  1. 设置点击监听器gtm.click,以触发所有页面
  2. 对于&#34; No&#34;按钮,设置在{{event}}等于gtm.click时触发的标记,以及{{element ID}}等于&#34; authCreateAcctConfirmNoBtn&#34;
  3. 时触发的标记
  4. 对于&#34;是&#34;按钮,设置在{{event}}等于gtm.click时触发的标记,以及当{{element ID}}等于&#34; authCreateAcctConfirmYesBtn&#34;
  5. 希望这有帮助。