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

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

问题描述

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

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> 

或更旧的版本:

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

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

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&....

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

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

既然一张图值一千字……

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)

推荐答案

问题是没有足够的时间让脚本在用户被带到下一页之前完成运行.您可以做的是为您的 GA 代码创建一个包装函数,并在 onclick 中调用包装函数,在您的包装函数中触发 GA 代码后,设置超时并使用链接的 url 更新 location.href.示例:

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>

代码会根据您的链接而有所不同,但希望您能理解 - 基本上它会等待一段时间,然后将用户带到目标 url 以给脚本一些时间来执行.

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.

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

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.

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

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