tags:

views:

93

answers:

5

I have this requirement:

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.

Edited: By John Hartsock

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.

  • User enters your site. -- I have a start time for the home page
  • User goes to page 2 of your site -- I have a stop time for the home page and a start time for page 2
  • User exits your site. -- How do you get the final stop time for page 2

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?

+1  A: 

In a web based system, there's no way to reliably do this. Sure, you can record each page that a user displays and record the length of time between each view but what happens when they close the browser on the last page they're displaying on? That's just one of dozens of problems with this requirement.

KevinDTimm
Considering its just metrics, I think reliability isn't important.
Jonathan Allen
@Jonathan - Yeh, who bases business decisions on metrics or accurate facts? It's not like they are useful or anything like that. </sarcasm>
Coronatus
There are plenty of existing solutions that record this kind of data. As long as users dont deactivate javascript, you can get pretty good reliability. And for most sites, this is a pretty small percentage.
Guillaume
why the downvote? my exact issue is referenced in the edit to the OP - the key word is reliably (and it cannot be done with 100% accuracy, only approximations)
KevinDTimm
+4  A: 

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.

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.

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:

  • You can count the number of visits to each page by counting start times.
  • You can calculate the length of time the user displayed each page for the records that have both start and end time.
  • If you have other information about users, such as roles or locations, you can do more analysis of page viewing. For example, if you know roles, you can see which roles use which pages the most.

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.


Edit: Either of these approaches might meet your needs.

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

DOK
Is the "navigation event handler" something avaialble in JavaScript? (Sorry, I'm a Windows programmer. I don't do web-work very often.)
Jonathan Allen
@Jonathan Allen: I used "navigation event handler" as a general term for a number of events including such things as a button click or hyperlink click that takes the user to another page. This might occur on a menu. Whenever the user clicks on something that navigates to another page, they are leaving the current page. At that point, you can capture the time they left the page and record it. If you have links consisting of anchor tags, you might have to change those to LinkButtons so you can capture the event on the server-side.
DOK
the "navigation event handler" can be javascript. A good candidate is window.unload.
Guillaume
+1  A: 

What about an AJAX based approach? It would only work when Javascript is on the client side, but sending a POST to some script every 15 seconds will get you a reasonable amount of granularity.

There are also more complicated "reverse-AJAX" things you might be able to do... but I don't know much about them.

Dragontamer5788
I don't know about that. 15 seconds isn't very granular, and anything less will cause too much volatility in the database unless I build some sort of lazy writer...
Jonathan Allen
+1  A: 

This has to be done with some javascript. As the other said, it is not completely reliable. But you should be able to get more than enough accurate data.

This will need to call your server from javascript code when the page is unloaded. The javascript event to hook is window.unload. Or you can use a nicer API, like jQuery. Or you could use a ready made solution, like WebTrends, or Google Analytics. I think that both record the length of time that the page was displayed.

Good web analytics is pretty hard. And it becomes harder if you have to manage a lot of traffic. You should try to find an existing solution and not reinvent your own ...

Guillaume
though it's not specified here, maybe this is an edge case - one where the usage of the website will be a text only browser - or an assistive device. how does the javascript only solution work in that case? since the OP stated a rich client world was not his domain this does not seem to be too far from his world.
KevinDTimm
A: 

You can use onunload to do what you need. Have it send a AJAX request to your server to update a database. You may want to return false then do document.close once the AJAX request has completed such that it won't quit prematurely and the ajax won't get discarded.

In the database you'll just want to store the page, the ip address, the time of the event, and whether it was a onload or onunload event.

That is all there is too it.

balupton