我刚刚完成了Sketch插件的工作,我创建了一个简单的登陆页面,供用户下载插件。我想使用事件跟踪来跟踪下载,但是事件跟踪不起作用,我似乎不知道为什么。
下面是链接的样子:
<a href="downloads/colorspark.zip" download onClick="ga('send', 'event', 'Downloads', 'download', 'ColorSpark for Sketch');">Download</a>有人看到我做错什么了吗?除了onclick属性之外,我还需要在任何其他地方添加其他代码吗?
发布于 2018-07-13 08:14:38
我敢打赌,您面临的是我们所称的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>https://stackoverflow.com/questions/51294112
复制相似问题