A technique for detecting when a user leaves a page.
const captureLeavingPage = () => {
window.onbeforeunload = function (evt) {
var message = "Are you sure you want to leave?";
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
}
</script>
I am now looking to track when a user clicks "leave this page" and save that action to a database. How can I achieve this?