So you want to know how much time users spend on any specific page of your web application? This article explains how to log what pages your users visit and how much time they spend on each page. This kind of data can provide valuable insight into what forms of your application are too complicated and need to be split into several smaller forms. Or if they keep coming back to the welcome page this may mean your navigation inside the app can be improved.
1. Create log table
The following SQL script creates the log table in the database. The syntax is for MySQL, you can create a similar table in any other database manually, just make sure that field names and datatypes stay the same.
A few notes here:
Note 1: Login is not required. If your project doesn’t use login, ‘userID’ field will be empty.
Note 2: if ‘timeoff’ field is empty that means user spent less than five seconds on the page.
Note 3: it makes sense to add ‘timetracker’ table to the project as well so you can test this functionality. If you do so, set ‘View as’ time of timeon/timeoff fields to ‘Datetime’.
3. Server-side code (PHP and C#)
The following code goes to the AfterAppInit event.