Skip to content
Advertisement

Track how long a user stays on a page?

How can I track how long a user stays on a page before they request another or simply leave the site?

Basically, I want to do a check, if a user stays on the page for 20 minutes or longer, then do something.

I believe this would require php and javascript, but I am not exactly sure how to accomplish it.

Maybe using this $_SERVER in php to get the time of execution, and then get a timestamp when the user clicks somewhere else and simply compare the two?

Advertisement

Answer

You can do all this with simple javascript.
For a single page:

JavaScript


For multiple pages:
I suggest you set a cookie with the time a user hits a page and on each page check if the cookie exists. If it exists run a query every x ammount of seconds to see if the 20 minutes have passed since the cookie has been created.


For full Ajax documentation head to: http://api.jquery.com/jQuery.ajax/

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement