Websites often want to send analytics or diagnostics to the server when the user has finished with the page. The most reliable way to do this is to send the data on the
visibilitychange
event:
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "hidden") {
navigator.sendBeacon("/log", analyticsData)
}
})
Posted by Dominik Schöler to makandra dev (2025-09-12 15:01)