You can use pagehide event. According to chrome, pageHide is more reliable than beforeunload. But again, like other events, this event is not reliably fired by browsers, especially on mobile.
pagehide
pageHide
beforeunload
For more information: