I've not tried it, but it sounds like the Leave Trigger in the Marketplace may do what you need.
https://marketplace.mendix.com/link/component/230595
I hope this helps.
The only DOM method of capturing the closure is beforeunload and most people will use this internally. However, its never guaranted and never recommended and never gives enough time to even make async call.
The recommended solution for you is use heartbeat + timeoout solution.
The timeout solution menas small periodic “I’m alive” pings + server-side expiry covers tab close, crashes, sleep, and network loss.