【问题标题】:Google Analytics event on submit提交时的 Google Analytics 事件
【发布时间】:2016-08-17 16:18:37
【问题描述】:

简单的案例,需要在提交表单时向 Google 发送事件。

我尝试了 10 种不同的方法,但要么不发送 GA 事件,要么不提交表单。

<form id="n" action="/" method="post">

发送事件但不发送表单:

<input type="button"
onclick="window._gaq.push(['._trackEvent', 'x', 'x', 'x']);document.getElementById('n').submit();">

尝试使用 setTimeout - 不发送事件但提交表单:

<input type="button"
onclick="window._gaq.push(['._trackEvent', 'x', 'x', 'x']);window.setTimeout(function(){document.getElementById('n').submit();}, 200);">

尝试在按钮上使用type="submit",没有发送事件:

<input type="submit"
onclick="window._gaq.push(['._trackEvent', 'x', 'x', 'x']);">

尝试将提交放入GA数组,它确实提交了按钮但没有注册事件:

<input type="submit"
onclick="window._gaq.push(['._trackEvent', 'x', 'x', 'x']);window._gaq.push(function(){document.getElementById('n').submit()});">

&lt;form /&gt; 中尝试使用带有onsubmit="doX();" 的jQuery,但它也不发送事件:

<script>
function doX() {
    window._gaq.push(['._trackEvent', 'test', 'test', 'test']);
    document.getElementById('n').submit();
}
</script>

window._gaq.push(['._trackEvent', 'x', 'x', 'x']); 放在控制台或代码中的某个位置是可行的。

document.getElementById('n').submit(); 放在代码或控制台中的某处也可以。

他们不只是一起工作。

我又试了几次,还是不行。

【问题讨论】:

  • 不使用最新的通用 GA 顺便说一句,使用较旧的分析。
  • 您的第一个值不应有句点。应该是这样的['_trackEvent', 'test', 'test', 'test'])
  • @colecmc 你的意思是点?我在那里有一个别名,我删除了它以缩短示例。

标签: javascript forms google-analytics dom-events


【解决方案1】:

如果您仍然使用 jQuery,那么只需使用提交操作。

在您的输入加载后,例如在页面底部或 window.loaded 区域内:

$('input#button').click( function () {
  window._gaq.push(['._trackEvent', 'x', 'x', 'x'])
  this.submit();
});

如果您切换到最新版本的 Google Analytics,他们有一个 Chrome 扩展程序,可以在控制台中显示事件详细信息,这非常好。

【讨论】:

  • 您不提交输入,而是提交表单。这不会发送事件,因此它也不起作用:-(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 1970-01-01
  • 2013-10-27
  • 2013-09-17
  • 1970-01-01
相关资源
最近更新 更多