easy way to do it is at the top of every script include a small php inc file that just monitors the ref, dt, ip etc inserting to your db... you can also append what they clicked on in the uri so you can track it that way as well... no need for javascript.
obviously if there is no more activity, they've closed the browser, because up until that point you've monitored where they are going in the site
