You can send a request when a page is closed, either using the Fetch API with the keepalive
option:
const data = {a: 1, b: 2}; // some data to be logged, such as usage statistics
const handler = () => fetch(url, {method: "POST", keepalive: true});
window.addEventListener("unload", handler);
Or the Beacon API:
const handler = () => navigator.sendBeacon(url, blob);