umami icon indicating copy to clipboard operation
umami copied to clipboard

Tracker: use sendBeacon or keepalive on fetch requests

Open Sov3rain opened this issue 8 months ago • 4 comments

Describe the feature or enhancement

Hey there,

I need to report events to my Umami instance right before navigating to a new page. Currently, the fetch request gets terminated—as expected—when the page unloads and the request hasn't completed yet.

The Navigator API offers two ways to work around this:

  • Using navigator.sendBeacon
  • Setting the keepalive option to true in the fetch request

It would be great if the tracker script could make use of one of these approaches. I'm happy to open a PR if needed!

Sov3rain avatar Apr 16 '25 07:04 Sov3rain

We investigated this a while ago and the result was:

  1. Almost every adblock blocks sendBeacon requests
  2. Firefox didn't support keepalive.

Not sure what the current state of things are.

mikecao avatar Apr 19 '25 10:04 mikecao

I had some success on Brave (shields up) using the keepalive parameter, and Safari 18.3. At least those two would work.

What could happen if the option is added anyway?

EDIT: as of now, seems supported by all major browsers: https://developer.mozilla.org/en-US/docs/Web/API/Request/keepalive

Sov3rain avatar Apr 20 '25 08:04 Sov3rain

Firefox 133 (Released 2024-11-26). I can't believe Firefox only supported keepalive recently. But I think it's fine to add keepalive at this point. Not so sure about sendBeacon.

mikecao avatar Apr 21 '25 04:04 mikecao

Keepalive should work fine for this use case, and I don’t think sendBeacon is necessary either. Would you like me to open a PR?

Sov3rain avatar Apr 21 '25 09:04 Sov3rain

This issue is stale because it has been open for 60 days with no activity.

github-actions[bot] avatar Jun 21 '25 02:06 github-actions[bot]

This issue was closed because it has been inactive for 7 days since being marked as stale.

github-actions[bot] avatar Jun 29 '25 02:06 github-actions[bot]