问题描述
当有人填写表单并点击提交时,我需要在谷歌分析中跟踪一个事件.出现的结果页面是一个标准的仪表板类型页面,因此为了跟踪该页面上的事件,我必须在 url 中传递事件,然后读取 url 并输出谷歌分析事件跟踪 javascript 代码基于它.不过,这是一个经常添加书签的页面,并且页面会重新加载、点击返回等.所以我真的不想在 URL 中传递跟踪事件并搞砸分析.
I need to track an event in google analytics when someone fills out a form and clicks submit. The resulting page that comes up is a standard dashboard-type page, so in order to track the event on that page I'd have to pass in the event in the url and then read the url and output the google analytics event tracking javascript code based on it. This is a frequently bookmarked page though and page that is reloaded, clicked back to, etc. So I'd really rather not pass tracking events in the URL and screw up the analytics.
相反,我更愿意使用表单在页面上执行类似以下 jQuery 代码的操作:
Instead, I'd much rather do something like the following jQuery code on the page with the form:
$('#form_id').submit(function() {
_gaq.push('_trackEvent', 'my category', 'my action');
});
我担心上面的问题是我会错过一些正在跟踪的事件,因为在调用该 javascript 之后,浏览器将立即提交表单并转到另一个网页.如果 utm.gif 跟踪图像没有及时加载,我会错过这个事件 :(.
The problem I fear with the above is that I'm going to miss some events being tracked because immediately after calling that javascript the browser is going to submit the form and go to another webpage. If the utm.gif tracking image isn't loaded in time, I miss the event :(.
我的恐惧有道理吗?我如何确保不会错过正在跟踪的事件?
Is my fear justified? How do I ensure I don't miss events being tracked?
推荐答案
只有 2 种方法可以 100% 确保所有表单提交(在启用 JS 且未阻止 GA 的用户中)如下:
There are only 2 ways to ensure, 100%, that all form submissions (amongst users who have JS enabled and who don't block GA) is as follows:
- 您可以执行 AJAX 提交,然后不必担心页面会发生变化,从而有时间让 GA 处理并加载新页面以代替旧页面.
- 您可以强制表单在新窗口中打开它的操作,从而让主页上的所有后台进程保持工作,并防止出现您担心的竞争情况.
这样做的原因是 Google Analytics 没有回调函数,因此您永远无法确定您正在捕获所有的提交,即使您设置了 10 秒的延迟.
The reason for this is that Google Analytics does not have a callback function, so you can't ever be certain you're capturing all of the submits, even if you put a 10 second lag.
或者,您可以将 GET 值传递给提交的页面并在该页面上设置该值的检查.如果设置,您可以发送 trackEvent 调用.
Alternately, you can just pass a GET value to the submitted page and setup a check on that page for the value. If its set, you can send a trackEvent call.
这篇关于单击表单提交后在谷歌分析中跟踪事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!