Q:

javascript event only on leaving site

I am creating my own simple stats to record which pages were read and for how long etc.
I then use an ajax call to record the info in a database, it's working using the window.ONBEFOREUNLOAD event, however this creates a database record for each page visited and instead I want to save the page stats to js variables and then only do 1 ajax call when the visitor finally leaves the site.
Is there a way of creating an event listener using pure javascript to detect when the user leaves the site, maybe by evaluation the body's click event ???

A:

No.
It is not possible for a browser to provide such an event by default: the browser itself has no awareness of what encompasses an entire site, it's thus impossible for a browser to know when a user leaves a site.
It is easy implement a solution yourself in JavaScript. The implementation is easy, the solution is hard.
You need to consider how you can tell when a user leaves your site. How do you define exit points? Can you define exit points? This is a non-trivial problem. I am not certain a solution exists.

