[英]Google Analytics does not track link click events
I am currently using analytics.js (the newer version of GA) and I am trying to track all types of events from my website, including when a user clicks on an anchor tag pointing to an external URL. 我目前正在使用analytics.js (较新版本的GA),我正在尝试从我的网站跟踪所有类型的事件,包括当用户点击指向外部URL的锚标记时。 I am currently using this setup:
我目前正在使用此设置:
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-KEY-HERE', { 'alwaysSendReferrer': true, 'allowAnchor': true });
and I send the events when hitting a link like this: 当我点击这样的链接时发送事件:
$(document).on("click", ".anchor-class", function (event) {
label = //get data from anchor element here ...
ga('send', 'event', 'Link Clicked', 'Click Details', label);;
return;
}
});
and this does not send anything to GA (even though the event handler is calling the ga(send...) method). 并且这不会向GA发送任何内容(即使事件处理程序正在调用ga(发送...)方法)。 How ever, if I use this exact technique for but with event.preventDefault();
但是,如果我使用这个确切的技术但是使用event.preventDefault(); at the beginning of the function, the event is sent and it appears in GA dashboard.
在功能开始时,事件将被发送并显示在GA仪表板中。
Is there some setting that I missed in order to make this work properly? 为了使这项工作正常,我错过了一些设置吗?
Use the hitCallback function: 使用hitCallback函数:
$(document).on('click','a', function(event){
event.preventDefault();
var label = $(this).attr('href');
ga('send', 'event', 'Link Clicked', 'Click Details', label, {
'hitCallback': function(){
window.location.href = label;
}
});
});
As pointed out by Blexy, the correct way to do this is to use a hit callback. 正如Blexy所指出的,正确的方法是使用命中回调。 However, you also need to take into account that users may block Google Analytics using some privacy protection tool such as Ghostery, in which case the hit callback will never be executed.
但是,您还需要考虑用户可能会使用某些隐私保护工具(例如Ghostery)阻止Google Analytics,在这种情况下,点击回调将永远不会被执行。 The following article explains how to implement this correctly:
以下文章解释了如何正确实现此功能:
http://veithen.github.io/2015/01/24/outbound-link-tracking.html http://veithen.github.io/2015/01/24/outbound-link-tracking.html
We currently had this issue and ported our analytics code off of the website and into GTM. 我们目前遇到了这个问题,并将我们的分析代码从网站移植到GTM中。 Another issue is that we have hundreds of sites that cannot have the new code released to them to deprecate the on-page analytics, but we already had GTM on them.
另一个问题是,我们有数百个网站无法向他们发布新代码以弃用页面分析,但我们已经有了GTM。
We were able to find the jQuery events that were bound to the click event and write code in GTM that removes the jQuery event on those clicked buttons via the exact event handler. 我们能够找到绑定到click事件的jQuery事件,并在GTM中编写代码,通过精确的事件处理程序删除这些单击按钮上的jQuery事件。 Then we were able to apply standard GTM click triggers and tags so we didn't get double eventing.
然后我们能够应用标准GTM点击触发器和标签,因此我们没有得到双重事件。
Assuming you can easily remove the code from the page the following should work great using GTM. 假设您可以轻松地从页面中删除代码,以下内容应该可以使用GTM。
This will fire an event off to analytics when a user clicks a given element and it will make any navigation wait until the corresponding tags have finished firing first before letting the page navigate away. 当用户单击给定元素时,这将触发事件到分析,并且它将使任何导航等待,直到相应的标记在让页面离开之前首先完成触发。
This is the quick and standard way now with the newer GTM. 这是现在使用更新的GTM的快速和标准方式。
Click Element
Built-In variable. Click Element
Built-In变量。 You can enable this under Variables --> Configure --> Click Element
. Variables --> Configure --> Click Element
下启用此功能。 Page Path
or Page URL
Built-In variable. Page Path
或Page URL
Built-In变量。 This is later used to help you determine which pages to run the trigger on. function() { return {{Click Element}}.innerText; }
function() { return {{Click Element}}.innerText; }
function() { return {{Click Element}}.innerText; }
Page Path
matches RegEx
.*
Page Path
matches RegEx
.*
Page Path
matches RegEx
^/path/my-page$
specific page Page Path
matches RegEx
^/path/my-page$
特定页面matches RegEx
Page Path
matches RegEx
^/path/my-page/.*
Page Path
matches RegEx
^/path/my-page/.*
/
or if you need anchors - normally it's best to be explicit on anchors so you don't get any funny business. /
或者如果你需要锚点 - 通常最好明确锚定所以你不要没有任何有趣的事情。 Click Element
matches CSS selector
a
Click Element
matches CSS selector
a
Click Element
matches CSS selector
.container .calls-to-action a
Click Element
matches CSS selector
.container .calls-to-action a
Click Element
matches CSS selector
a[href^="http"]
Click Element
matches CSS selector
a[href^="http"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.