I have been checking google analytics. they track user exit very efficiently.
I had written a code to process some server side code on window’s onbeforeunload event to keep a track of when has the user closed the browser, but that was very unreliable, the script wasn’t fired everytime.
What method does google use to efficiently track this?
In my understanding it doesn’t.
It only tracks new pageviews.
Therefore the time a user is on a page A is measured by the time in between the time he started viewing page A and the time he started viewing page B.
Therefore Google can’t measure the time you spent on page B (if that’s the last page you visited).
More info on the GA help page about Time on Page
UPDATE A real time visit ends when a user hasn’t been active in the last 5 minutes. Source