本文介绍了Google Analytics(分析)事件跟踪-不适用于下载链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了Sketch插件的工作,并创建了一个简单的登录页面,供用户下载该插件。我想使用Google Analytics(分析)事件跟踪来跟踪下载,但事件跟踪无法正常工作,我似乎无法弄清原因。

I just finished working on a plugin for Sketch and I created a simple landing page for users to download the plugin. I want to use Google Analytics event tracking to track the downloads, but the event tracking is not working and I can't seem to figure out why.

这里是链接如下:

<a href="downloads/colorspark.zip" download onClick="ga('send', 'event', 'Downloads', 'download', 'ColorSpark for Sketch');">Download</a>

有人看到我在做什么吗?除了onclick属性外,我是否还需要在其他任何地方添加其他代码?

Does anyone see what I'm doing wrong? Do I need to add any other code anywhere else besides the onclick attribute?

推荐答案

我敢打赌,您正在面对我们所面临的问题称为竞赛条件:用户单击链接后,浏览器启动页面更改,因此GA在有机会发送事件之前被中断。

My bet is that you're facing what we call a race condition: the moment the user clicks the link, the browser initiates a page change, thus GA is interrupted before it's had a chance to send the event.

2个选项


  • 打开新链接标签:将 target = _ blank 添加到您的链接中,以便它们在新标签中打开,并且不会中断当前标签中的GA。

  • 防止默认+回调::您可以对 onClick 使用自定义功能,以防止默认情况下打开链接( 返回false; ),触发GA事件,并使用GA的 hitCallback 以编程方式触发页面更改。

  • Open link in new tab: add target="_blank" to your links so they open in a new tab and don't interrupt GA in the current tab.
  • Prevent Default + Hitcallback: you can use a custom function for onClick that will prevent the link from opening by default (return false;), trigger the GA event, and use GA's hitCallback to trigger the page change programatically.

对于选项2,有不同的处理方法(因为它是自定义代码)。这是来自Google的示例:

For option 2 there are different ways of doing it (since it's custom code). Here is an example from Google:https://support.google.com/analytics/answer/1136920?hl=en

<script>
/**
* Function that tracks a click on an outbound link in Analytics.
* This function takes a valid URL string as an argument, and uses that URL string
* as the event label. Setting the transport method to 'beacon' lets the hit be sent
* using 'navigator.sendBeacon' in browser that support it.
*/
var trackOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {
     'transport': 'beacon',
     'hitCallback': function(){document.location = url;}
   });
}
</script>
You'll also need to add (or modify) the onclick attribute to your links. Use this example as a model for your own links:

<a href="http://www.example.com" onclick="trackOutboundLink('http://www.example.com'); return false;">Check out example.com</a>

这篇关于Google Analytics(分析)事件跟踪-不适用于下载链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:56