使用jQuery进行表单提交的Google Analytics事件跟踪 [英] Using jQuery for Google Analytics event tracking of Form Submits

查看:91
本文介绍了使用jQuery进行表单提交的Google Analytics事件跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩谷歌分析事件跟踪,可以看到一些插件漂流,但我试图看看我是否可以把东西放在一起,这将是非常通用和简单易用。

 < script> 
$(form)。submit(function(){
_gaq.push(['_trackEvent',$(this).attr('name'),'Form Submission','The- form-name',false]);
});
< / script>

我想,如果我有这些简单的代码片段在我的页面上徘徊,可以跟踪何时提交表单以及通过利用表单名称和表单标识符我可以看到它们何时提交。



我想我可以使用这种简单的方法来跟踪链接点击次数等,但只是想知道是否有人做过这样的事情,并且会对这是否是用Google Analytics跟踪事件的可靠方法有任何建议。



之所以我这样做是因为我会拥有在网站上动态生成的所有内容,而且大多数标签都会将id关联到它们,所以这种方式我不需要通过使用onclick事件将它们直接添加到html中处理程序和类似的东西。



感谢您的任何建议

解决方案

你可以这样做,但你必须延迟表单提交在奥德留下时间让trackEvent继续。否则表单将立即提交,阻止trackEvent完成并被发送出去。这同样适用于跟踪点击次数。



这应该是有效的:

  <脚本> 
$(form)。submit(function(e){
e.preventDefault(); //防止提交表单
_gaq.push(['_ trackEvent',$ (this).attr('name'),'Form Submission','The-form-name',false]);
$(this).off('submit'); //防止'this 'jquery提交事件递归
setTimeout(function(){//延迟表单提交
e.currentTarget.submit(); //在跟踪事件完成后提交表单
} ,350); // 350 ms
});
< / script>

注意:尽管这会保证您的表单将被提交,但并不保证提交事件将会跟踪100%的时间。但200ms 350ms应该足够好,可以正确跟踪90%以上。 (编辑:我碰到延迟,考虑高延迟蜂窝网络)。

在这个特殊的例子中, onsubmit 在低延迟连接上。完整的Google广告跟踪器脚本最多可以创建4个请求,每个请求的范围为30-40毫秒(低于延迟350毫秒)。从技术上讲,您只需考虑发送到Google的第一个请求的延迟,以便将事件保存到Google Analytics中。



保证100%事件得到跟踪的唯一方法是使用如下所述的命中回调: https://stackoverflow.com/a/12461558/1647538
但是, Hit Callback的注意事项是它可以阻止表单被提交,因此Hit Callback方法在这里不是很适合。


Im currently playing around with Google Analytics event tracking and can see a few plugins floating around, but im trying to see if i can put something together that would be very generic and simple to use.

<script>
$("form").submit(function(){
    _gaq.push(['_trackEvent', $(this).attr('name'), 'Form Submission', 'The-form-name',, false]);
});
</script>

I figure if i have a few of these simple snippets of code lingering around on my page this way i can track when a form is submitted and by leveraging form names and form id's i can see when they are submitted.

I figure i can probably use this simple method to track link clicks etc but was just wondering whether anyone has done anything like this before and would have any suggestions as to whether this is a reliable method for tracking events with Google Analytics.

The reason why im looking at doing it like this is because I will have everything generated dynamically on the site and the majority of tags will have id's associated to them, so this way i wont need to add them directly into the html by using the onclick event handlers and stuff like that.

thanks for any advice

解决方案

You can do it this way but you will have to delay the form submit in order to leave time for the trackEvent to proceed. Or else the form will be submitted right away, preventing the trackEvent from completing and being sent out. The same applies for tracking clicks.

This should work:

<script>
$("form").submit(function(e){
    e.preventDefault();//prevent the form from being submitted
    _gaq.push(['_trackEvent', $(this).attr('name'), 'Form Submission', 'The-form-name',, false]);
    $(this).off('submit');//prevent 'this' jquery submit event recursion
    setTimeout(function(){//delay the form submit
        e.currentTarget.submit();//submit form after the track event had time to complete
    }, 350);//350 ms
});
</script>

NB: While this guarantees that your form will be submitted, it does not guarantee that the submit event will be tracked 100% of the time. But the 200ms 350ms should be good enough to get over 90% properly tracked. (Edit: I bumped the delay with considerations for high latency cellular networks).

In this particular example of a form onsubmit on a low-latency connection. The most a full Google Ad tracker script can make is 4 requests, each in the 30-40 ms range (below the 350ms delay indicated). And technically, you only have to account for the latency of the first request being sent to reach Google, for the event to be saved in Google Analytics.

The only other way to insure 100% that the event got tracked, is to use the Hit Callback as described at: https://stackoverflow.com/a/12461558/1647538 However, the caveat of the Hit Callback is that it could prevent the form from being submitted, thus the Hit Callback method is not really advisable here.

这篇关于使用jQuery进行表单提交的Google Analytics事件跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆