Question: 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?

Answer: 
You can do all this with simple javascript.   

**For a single page:**

```
window.setTimeout(function(){
// Do stuff after 20 minutes of loading the page
// Then using jQuery you can call a PHP script 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's why I use the equation 

```

**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/>
I've put some work into a small JavaScript library and service that times how long a user is on a web page. It has the added benefit of more accurately (not perfectly, though) tracking how long a user is actually interacting with the page. It ignores time that a user switches to different tabs, goes idle, minimizes the browser, etc. The Google Analytics method has the shortcoming (as I understand it) that it only checks when a new request is handled by your domain, which is not always accurate. It doesn't consider if someone is no longer viewing your page, has minimized the browser, has switched tabs to 3 different web pages since last loading your page, etc.

For reference - no solution is perfect. But hopefully this one provides value, too. You can implement the Javaacript API and gather the statistics yourself or you can use the service which does it all for you.

<http://timemejs.com>

An example of its usage:

**Include in 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 the times yourself to your backend:**

```
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 sending timing data via websockets, so you don't have to try to force a full http request into the `document.onbeforeunload` event.