The only way to find out if someone has left your site is if ... they stop loading pages.
Itโs best to keep track of the โlast impressionโ time in a session in your server application. Update it to now with every page request. If their last impression is more than, say, four hours, you know that they left for a while, so cancel their session and start a new one for them.
source share