当用户导航到一个域内的其他页面时,Google Analytics(分析)如何跟踪事件 [英] How does google analytics track events when user navigates to other page inside one domain

查看:103
本文介绍了当用户导航到一个域内的其他页面时,Google Analytics(分析)如何跟踪事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google的文档中,可以通过以下方式跟踪事件:

 < a onclick = _gaq.push(['_ trackEvent','category','action','opt_label',opt_value]);>点击我< / a> 

或更旧的版本:

 < a onclick =pageTracker._trackEvent('category','action','opt_label',opt_value);>点击我< / a> 

我正在用Firebug查看点击链接时发出的请求,中止请求:

http://www.google-analytics.com/__utm.gif?utmwv=4.7.2&utmn=907737223& ....



这是因为浏览器在用户导航到新页面时卸载所有javascript。如何在这种情况下执行事件跟踪?



编辑:
因为一张图片可以胜过千言万语......



当我点击链接萤火虫向我展示了这一系列的请求(这里显示的是前四个,之后请求填充页面内容)

解决方案

在用户进入下一页之前没有足够的时间让脚本完成运行。你可以做的是为你的GA代码和onclick创建一个包装函数,调用包装函数,并且在包装函数中触发GA代码之后,设置一个超时时间并用链接的url更新location.href。例如:

 < a href =somepage.htmlonclick =wrapper_function(this,'category','action' ,'opt_label','opt_value'); return false;>点击我< / a> 

< script type ='text / javascript'>
function wrapper_function(that,category,action,opt_label,opt_value){
_gaq.push(['_ trackEvent',category,action,opt_label,opt_value]);
window.setTimeout(window.location.href ='+ that.href +',1000);
}
< / script>

代码会根据您的链接而有所不同,但希望您能明白 - 基本上它会稍微等一下然后再将用户带到目标网址,为脚本提供一些时间来执行。

更新:
这个答案是几年前发布的,自那以后发生了很多事情,但我仍然得到反馈(和upvotes)偶尔,所以我想我会更新这个答案与新的信息。此答案仍然可行,但如果您使用的是Universal Analytics,则可以使用 hitCallback 功能可用。 hitCallback 函数也可用于他们的传统 _gaq (ga.js),但它没有正式记录。

In Google's documentation it is said that an event can be tracked in the following way:

<a onclick="_gaq.push(['_trackEvent', 'category', 'action', 'opt_label', opt_value]);">click me</a> 

or older version:

<a onclick="pageTracker._trackEvent('category', 'action', 'opt_label', opt_value);">click me</a>

I was looking with Firebug to the request that are made when a click on a link and I see there aborted request:

http://www.google-analytics.com/__utm.gif?utmwv=4.7.2&utmn=907737223&....

This happens because browser unload all javascript when user navigates to a new page. How in this case event tracking is performed?

Edit: Since one picture can be worth a thousand words...

When I click a link firebug shows me this sequence of requests (here are shown first four, after follows requests to fill page content)

解决方案

The problem is that there isn't enough time for the script to finish running before the user is taken to the next page. What you can do is create a wrapper function for your GA code and in the onclick, call the wrapper function and after the GA code is triggered in your wrapper function, set a time out and update location.href with the link's url. Example:

<a href="somepage.html" onclick="wrapper_function(this,'category', 'action', 'opt_label', 'opt_value');return false;">click me</a>

<script type='text/javascript'>
function wrapper_function(that,category,action,opt_label,opt_value) {
  _gaq.push(['_trackEvent', category, action, opt_label, opt_value]);
  window.setTimeout("window.location.href='" + that.href + "'", 1000);
}
</script>

code will vary a bit based on your link but hopefully you get the idea - basically it waits a little bit before taking the user to the target url to give the script some time to execute.

Update: This answer was posted several years ago and quite a lot has happened since then, yet I continue to get feedback (and upvotes) occasionally, so I thought I'd update this answer with new info. This answer is still doable but if you are using Universal Analytics then there is a hitCallback function available. The hitCallback function is also available to their traditional _gaq (ga.js) but it's not officially documented.

这篇关于当用户导航到一个域内的其他页面时,Google Analytics(分析)如何跟踪事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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