How to measure a time spent on a page?
I came across the above thread, which has a solution, but the user could easily modify the inital date value making the time they spent on the page any time they wanted.
I need to somehow get a rough estimate of how long the user was on the page that the user cannot modify.
Any ideas?
First, you need to determine how important seconds or minutes are.
Measure the time spent on page using javascript.
Implement a heart beat with an unique id per loaded page, which runs every n seconds or n minutes to update how long a user has stayed on your page.
Call the same heartbeat function using onbeforeunload too (which fails many times, but when it works it will improve your accuracy).
Related
I'm trying to code (in an efficient way) a log to count how many hours a user spent on the system.
The only reliable information is the login date and time. User's often just close the window and don't log off.
What I've been doing so far is to use a jQuery call every 5 or 10 seconds to a specific endpoint to just record: user's id and the current datetime. But this is, of course, very expensive. A lot of HTTP requests (imagine for 8 hours a day how many requests) and a huge (HUGE) table.
Is there a more efficient way to count this information and present a summary of worked hours by the end of the month?
I'm currently using MySQL to log this (another table inside the same database) but I can use a different database if it's a better solution.
I am currently building a page (in php/wordpress), and I need the styling to change quite drastically after exactly 30 seconds, and then back again 30 seconds after that.
I know that I can set a session in php to run a check against, but this would only happen on page load.
Alternatively I could do it with javascript, but this would restart on every page load?
How could I get the best of both worlds, so that the timer starts when the user first visit the page, while allowing me to add a class to the body element after a specific amount if time?
I'm curious to hear how this could be approached.
When the user logs out by closing their browser or by restarting their PC, how do i save the loggout time in the database?
I managed it with a kind of user-ping. Every page in my web-app uses a javascript wich makes a get-request every minute to a certain php-file. This file identifies the user and updates a last-activity timestamp in my user table. On the server I made a cronjob that triggers every 2 minutes and checks the usertable for last-activity timestamps that are older than 2 minutes. If there are timestamps older than 2 minutes you can mark the user as "logged out". This way the user can spend long time on one page and won't get logged out due to inactivity. If he closes the browser or restarts the pc the logout timestamp will be set by the cronjob and is pretty exact.
This solution works well, but uses javascript timers. It is not really nice, but makes it's job pretty fine. If your page allready uses a lot of other javascript you perhaps should use a other solution.
If they are not physically clicking the log-out button (where you are recording their log-out time), you should set the log-out time to the session expiration. In order for this method to be more accurate, decrease the lifetime of the session to a couple of minutes. Thus, after a couple of minutes of inactivity, the session will expire and your log-out time will be relatively close to when the user stopped using your software. See below on how to record this information. Cron job or javascript implementation is necessary.
Just my two cents... probably better ways to do this, but this way seems to me as the easiest.
What is the best way to calculate the total time spent by a registered user on the site? ...under these conditions
1) User logs out normally.
2) User can simply close browser.
3) User can auto-login next time he comes back.
I think the best way to do this would be to find the time spent by the user on each page and keep adding them to his total time instead of checking for the whole site. But I don't know how to implement that....please help
You can't find the exact time he leaves the system, unless he logs out. Even then, he might be browsing the site while logged out.
The approximate way to do this would be to set the start time in the session and keep incrementing the time everytime he visits a page.
So the first time the user comes to your site at time T, you will
Create a session and put the start time there
Add the total time as 0
For all subsequent requests you would
Check the start time and compare that with the time now and get the difference
Add that time to the total time
This method will not give you the time the user spent on the last page. But it will give you something to work with.
You can do this with JavaScript and a separate PHP script.
The javascript code reacts to events that mean that an user is active (such as mouse/keyboard/resize events) and invokes the php script.
The php script compares the time when it last received a request to the current time and checks if the difference is over a certain threshold (i suggest something like 10-30 minutes to prevent single-click sessions from adding up) nothing happens.
If the threshold is not reached then the difference between the two timestamps is added to the total sum in the database.
Afterwards (in both cases) the last request time is set to the current time and the script ends.
If you also want to know when the user closes your website pages you can subscribe to unload events and/or implement an heartbeat script that calls a PHP script every X seconds.
you have 2 approaches, either to create a log table in your DB to track each user (by ID) logins and logouts and then calculate the time difference between the two in each record for the specific user and then sum all of that. OR you go more complex and make 4 columns in your DB->usertable (logintime 'timestamp' - logouttime 'timestamp' - lastactive 'timestamp' - onlinetime 'int') and update each column as their names say by code according to user activities. then alter the Session.php script in the System/libraries directory at line 105 exactly after if ( ! $this->sess_read()) before the system creates a new session and write a code to check if the 'logouttime' is not the same as 'lastactive' time (to avoid session timeout expiry misunderstood in the next code) if both fields not the same, update your DB to make 'logouttime' equals 'lastactive' then at line 107 exactly after: $this->sess_update(); write a code to check if the 'logouttime' equals the 'lastactive' (and you will make that happen earlier in your logout.php script) write a code to calculate the online time by the difference between the 'logintime' and the now time 'time()' and add the result to the 'onlinetime' field. but if the 'logoutime' is not the same as 'lastactive' (that meanse the user is online and making activities in your site because you are tracking him and updating the 'lastactive' field frequently) then write a code to calculate the online time by the difference between the 'lastactive' and now time 'time()' and add the result to the 'onlinetime' field. that way you have the exactly online time logged forever in the 'onlinetime' field! I hope you got me right because the examples will be a lot long of scripts (although I don't mind to share upon request). good luck.
Use the Session ID to keep track of individual sessions.
I'm trying to manipulate my mysql tables with PHP but when I update them via PHP I have trouble with time out concerns.
Is it possible to refresh or "start a new session" while continuing on from last session? So it doesn't time out? Or is it possible to somehow go to a new page to prevent time outs?
What's the best possible way can someone help me please?
Maybe switch over to a new page after 150 successful queries or after 10 seconds?
Thanks.
10 seconds won't give you a time out usually... i was interested myself how long a browser will time out usually, but you can try sleeping for 20 seconds and then 30 and more and see if your browser times out.
I have had a Rails server running on a local Macbook with many partials and query, and 45 seconds and the page won't time out.
If you don't need immediate feedback, you can put the task in a queue to be processed by cron job every 10 minutes or every 30 minutes depending on your need.
If you need immediate feedback and the task is exceeding long, you can break them down to a safe limit, and issue a redirect to a second and third page with param task=1000, task=2000 and report so on the page. Another way that might work is to fork a process and report an ID... and when the task is all done, near the end, update a SQL table that the task with that ID is done, and have a page display all finished task and that page can refresh every 10 seconds.
Try to use set_time_limit() after successful query execution so you won't incur into timeout if everything is running ok.
http://php.net/manual/en/function.set-time-limit.php