Search code examples
asp.netjavascriptanalytics

How long has a user been on a asp.net page (custom analytics)?


I am trying to come up with a way to measure how long a user has been on a page in my ASP.NET application. I am storing the userid, pagename, pageenteredtime and pagelefttime in a database. Each record has its own unique id as well, called featureuselogid.

At the moment, I can track when a user comes into the page with the page_load function on the server side. I store the userid, pagename and pageenteredtime.

After that im stuck, and need some guidance in the right direction. I need to record the time the user leaves the page. I know in javascript there is a window.onbeforeunload function, which will cover most cases (browser shutdown, links etc).

But how do I pass the featureuselogid to the javascript? If i can do that, I think I can make a webservice call from the javascript and update the record with the pagelefttime.

Am I going down the wrong path?

Cheers in advance.


Solution

  • Just put the featureuselogid in a hidden field in the page, with a unique ID that will make it accessible to Javascript, or set a javascript variable.

    Having said that, I believe that you are going to be able to most reliably detect Page_Load. You can determine the time between pages by measuring the time between Page_Load events. You won't get the browser closure, but IMO knowing when the user closes the browser is not all that meaningful.