Track how long a user stays on a page?

How can I track how long a user stays on a page before requesting another or just leaving the site?

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

I believe this will require php and javascript, but I'm not quite sure how to do this.

Perhaps using this $_SERVER in php to get the runtime and then get the timestamp when the user clicks elsewhere and just compares the two?

+4
source share
2 answers

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

 window.setTimeout(function(){ // Do stuff after 20 minutes of loading the page // Then using jQuery you can call a PHP  to do stuff like so: $.ajax({ url: '/myScript.php', method: 'POST', success: function(result){ //The request was successful and the output is stored in a variable: result }, complete: function(){ //Do stuff when the request is completed (Ignores if success or not) }, beforeSend: function(){ //Do something before the request is sent } }); }, 20 * 60 * 1000); //This is in milliseconds that why I use the equation 


For multiple pages:
I suggest you set a cookie with how the user clicks on the page, and on each page checks to see if the cookie exists. If it exists, run a query every x minutes per second to see if 20 minutes have passed since the cookie was created.


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

+6
source

I put some work into a small JavaScript library and a service that shows how long a user has been on a web page. It has the added advantage of more accurately (not quite, though) tracking how long the user actually interacts with the page. It ignores the time when the user switches to different tabs, goes into standby mode, minimizes the browser, etc. The Google Analytics method has the drawback (as I understand it) that it only checks when a new request is processed by your domain, and this is not always accurate. It does not take into account that someone is no longer viewing your page, minimized the browser, switched tabs to 3 different web pages since the last load of your page, etc.

For reference, the solution is not ideal. But, I hope this also gives value. You can implement the Javaacript API and collect statistics yourself, or you can use a service that does all this for you.

http://timemejs.com

An example of its use:

Include on your page:

 <script src="https://timemejs.com/timeme.min.js"></script> <script type="text/javascript"> TimeMe.initialize({ currentPageName: "home-page", // page name idleTimeoutInSeconds: 15 // time before user considered idle }); </script> 

If you want to report time about yourself to your server:

 xmlhttp=new XMLHttpRequest(); xmlhttp.open("POST","ENTER_URL_HERE",true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); var timeSpentOnPage = TimeMe.getTimeOnCurrentPageInSeconds(); xmlhttp.send(timeSpentOnPage); 

TimeMe.js also supports the transfer of synchronization data through websockets, so you do not need to try to force a full HTTP request in the document.onbeforeunload event.

0
source

All Articles