Google Analytics(分析):如何跟踪在新标签中打开的出站链接? [英] Google Analytics: how to track outbound links that open in a new tab?

查看:160
本文介绍了Google Analytics(分析):如何跟踪在新标签中打开的出站链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上有很多出站链接,我希望通过Google Analytics(analytics.js)跟踪。



Google的文档关于出站链接跟踪是清楚的,他们建议的实施对我有效。 问题在于在新标签页/窗口中打开的链接。谷歌建议通过回调函数打开链接,该函数在跟踪事件发送给GA后更新document.location。但是这显然不会在新标签中打开链接。关键的是,使用window.open似乎成为弹出窗口拦截器的受害者。



这是Google建议的实现:

 <脚本> 
var trackOutboundLink = function(url){
ga('send','event','outbound','click',url,{'hitCallback':
function(){
document.location = url;
}
});
}
< / script>

我可以简单地省略回调函数并让浏览器打开新的选项卡,但如果我这样做,谷歌表示,这次活动有可能不会被注册 - 我的跟踪将不准确。

如果您正在跟踪出站点击链接和链接将在新窗口或标签中打开,您不必担心回调。该页面将很好地跟踪事件。问题来自同一帧中的链接,因为点击事件正在发送的同时,点击跟踪页面被拆除。因此,对于新窗口/标签页中的目标链接,不要担心,因为您的正常点击事件可以正常工作。



如果您需要跟踪出站链接,但是担心关于在当前选项卡/窗口中打开的链接,一种解决方案是使用服务器端重定向脚本来执行Google Analytics(分析)跟踪。它的工作原理有点像这样:




  • mousedown 上,链接通过JavaScript替换。从 http://example.com / yourTrackingScript?gaCategory = Something& gaEvent = Click& gaLabel = SomeLink& url = http%3A%2F %2Fexample.com 。在 mousedown 上发生这种情况很重要,这样如果有人右键单击链接以在新选项卡/窗口中打开,则仍会插入服务器端跟踪脚本。 Google会在其搜索结果页面上使用此方法。
  • / yourTrackingScript 衡量协议
  • / yourTrackingScript 会响应302重定向到查询字符串中传递的任何网址。

  • 用户被重定向到最终目的地,这一切通常发生得很快,甚至没有注意到。


I have a number of outbound links on a website which I'm hoping to track with Google Analytics (analytics.js).

Google's docs on outbound link tracking are clear and the implementation they suggest works for me. The problem is with links that open in a new tab / window. Google suggests that the links be opened via a callback function which updates the document.location once the tracking event has been sent to GA. But this obviously won't open links in a new tab. And, crucially, using window.open instead seems to fall victim to popup blockers.

This is Google's suggested implementation:

<script>
var trackOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
     function () {
     document.location = url;
     }
   });
}
</script>

I can simply omit the callback function and let the browser open the new tab but if I do that, Google says there's a chance the event won't get registered - and my tracking will be inaccurate.

解决方案

If you are tracking outbound links on click and your links are to open up in a new window or tab, you don't have to worry about a callback. The page will track the event just fine. The issue comes from links in the same frame, since the page with the click tracking is being torn down at the same time the event for the click is being sent. So, for links with targets in new windows/tabs, don't worry as your normal click event will work fine.

If you need to track outbound links but are concerned about links opening in the current tab/window, one solution is to have a server-side redirect script that does the Google Analytics tracking. It works a bit like this:

  • On mousedown, the href attribute of the link is replaced via JavaScript. From http://example.com to /yourTrackingScript?gaCategory=Something&gaEvent=Click&gaLabel=SomeLink&url=http%3A%2F%2Fexample.com. It's important that this happens on mousedown so that if someone right-clicks your link to open in a new tab/window, the server-side tracking script is still inserted. Google uses this method on their search results page.
  • /yourTrackingScript fires off your event from the server-side to Google Analytics using the measurement protocol.
  • /yourTrackingScript responds with a 302 redirect to whatever URL was passed in the query string.
  • The user is redirected to the final destination, and this all typically happens fast enought that they don't even notice.

这篇关于Google Analytics(分析):如何跟踪在新标签中打开的出站链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆