Google Analytics(分析)事件跟踪-不适用于下载链接

时间:2018-07-11 21:01:29

标签: google-analytics download event-tracking

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

链接如下所示:

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

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

1 个答案:

答案 0 :(得分:1)

我敢打赌,您面对的是race condition:用户单击链接时,浏览器启动页面更改,因此GA在有机会发送事件之前被中断。

2个选项

  • 在新标签页中打开链接:将target="_blank"添加到您的链接中,以便它们在新标签页中打开,并且不会中断当前标签页中的GA。
  • 防止默认+回调调用:您可以为onClick使用自定义功能,该功能将阻止默认情况下打开链接(return false;),触发GA事件以及使用GA的hitCallback以编程方式触发页面更改。

对于选项2,有不同的实现方式(因为它是自定义代码)。这是来自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>