简体   繁体   English

Google Analytics(分析)发送事件有时会失败

[英]Google Analytics send event fails sometimes

I am trying to log an event each time a user downloads a specific file, but the request to analytics servers gets cancelled every now and then (the download starts though). 每当用户下载特定文件时,我都尝试记录一个事件,但是对分析服务器的请求有时会被取消(尽管下载开始)。

Here is the code: 这是代码:

ga('send', 'event', {
    'eventCategory': 'Addon',
    'eventAction': 'Download',
    'hitCallback': function() {
        document.location = $scope.file.file_url;
    }
});

(The file resides on a different host in case that info matters) (如果信息重要,则文件位于不同的主机上)

I suppose the request is cancelled by the browser, but why is it happening and what can I do to prevent it? 我想该请求已被浏览器取消,但是为什么会发生,我该怎么做才能阻止它?

Does the user stays on the same page during the download? 用户在下载过程中是否停留在同一页面上? If the user is redirected to another page (such as a PDF preview of the file that she downloaded, or a thank you page) then the code might not have time to execute. 如果用户被重定向到另一个页面(例如,她下载的文件的PDF预览或“谢谢”页面),则代码可能没有时间执行。 Use JS setTimeout to delay the redirection. 使用JS setTimeout延迟重定向。

Beyond that, more info and examples are needed in order to troubleshoot your issue. 除此之外,还需要更多信息和示例以解决您的问题。

I solved the problem by adding the download attribute to my a tag: 我通过将download属性添加到我的标签中来解决了这个问题:

<a href="mydownloadurl" download>Download</a>

I noticed that I was getting this message when starting the download: 我注意到开始下载时收到此消息:

Resource interpreted as Document but transferred with MIME type application/octet-stream 资源被解释为文档,但以MIME类型application / octet-stream传输

After googling around I found the solution, which was to add the download attribute. 谷歌搜索后,我找到了解决方案,即添加了download属性。 Turns out that the ga request is no longer cancelled with that in place. 事实证明,使用该请求后,ga请求不再被取消。 The browser must have been confused by that error and cancelled all ongoing requests (or something). 浏览器一定被该错误所迷惑,并取消了所有正在进行的请求(或其他请求)。 I did some testing now and out of about 20 ga calls all of them were successful. 我现在进行了一些测试,在大约20 ga的呼叫中,所有呼叫均成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM