网页:系统会记录用户显示每个页面的时长 [英] Web: The system will record the length of time the user displayed each page

查看:25
本文介绍了网页:系统会记录用户显示每个页面的时长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个要求:

系统会记录用户显示每一页的时长.

The system will record the length of time the user displayed each page.

虽然在富客户端应用程序中微不足道,但我不知道人们通常如何跟踪它.

While trivial in a rich-client app, I have no idea how people usually go about tracking this.

John Hartsock

我一直对此很好奇,在我看来,这可以通过使用 document.onunload 事件来准确捕获所有页面的星号和停止时间.基本上只要用户停留在您的网站上,您将始终能够获得除最后一个页面之外的每个页面的开始和停止时间.这是场景.

I have always been curious about this and It seems to me that this could be possible with the use of document.onunload events, to accurately caputure star and stop times for all pages. Basically as long as a user stays on your site you will always be able to get the start and stop time for each page except the last one. Here is the scenario.

  • 用户进入您的网站.- 我有一个首页的开始时间
  • 用户转到您网站的第 2 页 -- 我有主页的停止时间和第 2 页的开始时间
  • 用户退出您的网站.-- 如何获得第 2 页的最终停止时间

问题变成了是否可以跟踪用户何时关闭窗口或离开您的网站?是否可以使用 onunload 事件?如果不是,那么还有哪些其他可能性?显然,AJAX 是一种途径,但还有哪些其他途径?

The question becomes is it possible to track when a user closes the window or navigates away from your site? Would it be possible to use the onunload events? If not, then what are some other possibilities? Clearly AJAX would be one route, but what are some other routes?

推荐答案

我不认为您可以捕获每一个页面查看,但我认为您可能能够捕获足够的信息以供分析网站使用情况.

I don't think you can capture every single page viewing, but I think you might be able to capture enough information to be worthwhile for analysis of website usage.

创建一个包含以下列的数据库表:网页名称、用户名、开始时间和结束时间.

Create a database table with columns for: web page name, user name, start time, and end time.

在页面加载时,将一条记录插入包含前三个字段数据的表中.返回该记录的 ID 以备将来使用.

On page load, INSERT a record into the table containing data for the first three fields. Return the ID of that record for future use.

在任何导航中,使用之前返回的 ID 更新导航事件处理程序中的记录.

On any navigation, UPDATE the record in the navigation event handler, using the ID returned earlier.

与同时具有开始时间和结束时间的记录相比,您最终会得到更多带有开始时间的记录.但是,您可以从这个简单的数据中进行这些分析:

You will end up with a lot more records with start times than records with both start and end time. But, you can do these analyses from this simple data:

  • 您可以通过计算开始时间来计算每个页面的访问次数.
  • 对于同时具有开始时间和结束时间的记录,您可以计算用户显示每个页面的时间长度.
  • 如果您有关于用户的其他信息,例如角色或位置,您可以对页面查看进行更多分析.例如,如果您了解角色,则可以查看哪些角色使用的页面最多.

您的数据可能会因某些页面比其他页面更频繁地被放弃这一事实而被扭曲.

It is possible that your data will be distorted by the fact that some pages are abandoned more often than others.

但是,您当然可以尝试捕获这些数据并查看它的合理性.有时,在现实世界中,我们不得不使用不完美的信息来应对.但这可能就足够了.

However, you certainly can try to capture this data and see how reasonable it appears. Sometimes in the real world, we have to make due with less than perfect information. But that may be enough.

这些方法中的任何一种都可能满足您的需求.

Either of these approaches might meet your needs.

1) 这是 Ajax 解决方案的 HTML 部分.它来自 这个页面,其中包含用于在文本文件中记录信息的 PHP 代码——如果您愿意,可以轻松更改为写入数据库.

1) Here's the HTML portion of an Ajax solution. It's from this page, which has PHP code for recording the information in a text file -- easy enough to change to writing to a database if you wish.

 <html>
<head>
<title>Duration Logging Demo</title>
<script type="text/javascript">
var oRequest;
var tstart = new Date();

// ooooo, ajax. ooooooo …
if(window.XMLHttpRequest)
oRequest = new XMLHttpRequest();
else if(window.ActiveXObject)
oRequest = new ActiveXObject("Microsoft.XMLHTTP");

function sendAReq(sendStr)
// a generic function to send away any data to the server
// specifically ‘logtimefile.php’ in this case
{
oRequest.open("POST", "logtimefile.php", true); //this is where the stuff is going
oRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
oRequest.send(sendStr);
}

function calcTime()
{
var tend = new Date();
var totTime = (tend.getTime() – tstart.getTime())/1000;
msg = "[URL:" location.href "] Time Spent: " totTime " seconds";
sendAReq(‘tmsg=’ msg);
}
</script>
</head>

<body onbeforeunload="javascript:calcTime();">
Hi, navigate away from this page or Refresh this page to find the time you spent seeing
this page in a log file in the server.
</body>
</html>

2) 另一个人提议在Page_Load.在该点写入初始数据库记录.然后,在计时器的 Elapsed 事件上,更新该记录.在 onbeforeunload 中做最后的更新.然后,如果由于某种原因您错过了最后一个 onbeforeunload 事件,至少您将记录用户在页面上花费的大部分时间(取决于计时器 Interval).当然,如果你每秒更新一次,并且有成百上千的并发用户,这种解决方案将是相对资源密集型的.因此,您可以配置为应用程序打开和关闭此功能.

2) Another fellow proposes creating a timer in Page_Load. Write the initial database record at that point. Then, on the timer's Elapsed event, do an update of that record. Do a final update in onbeforeunload. Then, if for some reason you miss the very last onbeforeunload event, at least you will have recorded most of the time the user spent on the page (depending upon the timer Interval). Of course, this solution will be relatively resource-intensive if you update every second and have hundreds or thousands of concurrent users. So, you could make it configurable that this feature be turned on and off for the application.

这篇关于网页:系统会记录用户显示每个页面的时长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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