visibilitychange
EVENT is the easiest and reliable way to identify if you are going to see your website visitor for the last time for that page. This is an excellent article from Ilya Grigorik that talks about it.
// The event fires when
// 1. A user switches tab
// 2. Minimizes the browser window
// 3. Closes the tab
// 4. ......
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
navigator.sendBeacon('/this-is-probably-the-last-you-will-see-me')
}
})