为什么Google Analytics事件不是从onSubmit发送的? [英] Why is Google Analytics event not sent from onSubmit?

查看:153
本文介绍了为什么Google Analytics事件不是从onSubmit发送的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想跟踪用户使用Google Analytics的事件下载文件的频率,但即使事件似乎已发送,但跟踪.gif似乎无法正常到达。



要下载文件,用户需填写简要表格。当输入的信息无效时,通过函数 checkSubmit()(返回 false 或者不完整的, true 当信息是好的时候提交)。提交后,用户将被重定向到该文件。

 < script> 
函数checkSubmit(){
...
if(dataIsGood){
_gaq.push(['_ trackEvent','Download','the_filename.xxx']);
返回true; //允许表单提交
} else {
_gaq.push(['_ trackEvent','Form','info not okay']);
返回false; //保持表单不被提交
}
}
< / script>

< form action =/ form / emailcaptureformmethod =postonSubmit =return checkSubmit();>
...< / form>

在Chrome控制台中, Google Analytics Tracking Debugger 表示下载事件跟踪信标已发送,但控制台表示未能加载资源__utm.gif



这种情况只发生在 之前,事件在提交表单前被推送在我的网站上的其他活动 - 包括当表单信息不好时推送的表单事件(在上面的 else 块中)



Analytics(分析)摘要似乎正在工作,接受 _trackPageview s和 _trackEvent s。



__utm.gif的请求网址格式正确:将gadebug输出中的网址复制并粘贴到位置栏,并返回__utm.gif, p>

从其他元素推送时,事件被推送时没有问题。例如:

 < a href =#onClick =_ gaq.push(['_trackEvent','Download' ,the_filename.xxx']);>事件!< / a> 

推动事件,暂停几秒钟,然后返回 true 似乎没有任何效果 - 除了在提交表单之前添加暂停之外。



您有解决方案或建议吗?

解决方案

Javascript是单线程的。当您执行 _gaq.push 时,您正在将某些内容推送到队列中以便稍后处理。然而,你的代码在'later'之前返回 true ,并且GA代码永远不会执行(因为下一个操作是进入下一页)。



我认为这可能是使用传统或同步追踪与 ._ trackEvent


I'd like to track how often users download files using Analytics' events, but even though the events seem to have been sent, the tracking .gif doesn't seem to be arriving properly.

To download files, users are required to fill out a brief form. The information entered into this form is checked by the function checkSubmit() (which returns false when the entered info is invalid or incomplete, true when the information is good to be submitted). Once submitted, the user is redirected to the file.

<script>
function checkSubmit() {
    …
    if(dataIsGood) {
        _gaq.push(['_trackEvent', 'Download', 'the_filename.xxx']);
        return true;    //allow the form to submit
    } else {
        _gaq.push(['_trackEvent', 'Form', 'info not okay']);
        return false;   //keep the form from being submitted
    }
}
</script>

<form action="/form/emailcaptureform" method="post" onSubmit="return checkSubmit();">
… </form>

In the Chrome console, the Google Analytics Tracking Debugger says Download event tracking beacons have been sent, but the console says it failed to load resource __utm.gif

This happens only with events pushed just before the form is submitted. Events work elsewhere on my site—including the Form event pushed when the form info is no good (in the else block above)

The Analytics snippet seems to be working, accepting _trackPageviews and _trackEvents.

The request URL for __utm.gif is well-formed: copy-and-pasting the URL from the gadebug output into the location bar returns __utm.gif without a hitch.

The event is pushed without issue when pushed from another element. For example:

<a href="#" onClick="_gaq.push(['_trackEvent', 'Download', the_filename.xxx']);">Event!</a>

Pushing the event, pausing for a few seconds, then returning true does not seem to have any effect—beyond adding a pause before submitting the form.

Do you have solutions or suggestions?

解决方案

Javascript is single threaded. When you execute _gaq.push, you are pushing something on to a queue to be handled later. However your code returns true before 'later' comes, and the GA code never gets to execute (because the next action is to go to the next page).

I'd suggest that this may be an appropriate moment to use traditional or synchronous tracking with ._trackEvent

这篇关于为什么Google Analytics事件不是从onSubmit发送的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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