-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to wait for requests on navigation #205
Comments
There's a small hack you can do for this - you can block the event loop for (with a while (true) loop) inside the beforeunload handler. Obviously the page will 'freeze' but ideally you'd do this for a really short while. You can see this approach from He@p does when you beautify their tracking script - they do it for 100ms by default. |
Haha, that's nifty. Hopefully no browsers will start punishing scripts that do this any time soon. Worth trying for sure. |
Usecase which we have:
In cases like these it is highly unlikely that Ideally
or even on Posthog's API side with a config option or something. Or even moving failure into the callback itself, so there isn't a need to IMO the timeout blocking the event loop wouldn't be a solution, more like a band-aid in this case, it still doesn't provide guarantee that an event went through while freezing the page, which is bad for end user UX. |
I think the right call is to expose something like As a temporary workaround, the undocumented internal posthog.submit('Form submitted', { properties: { formParams } })
await submit(formParams)
posthog._handle_unload() // flush the buffer
await new Promise(resolve => window.setTimeout(resolve, 500)) // wait 500ms
window.reload() Unfortunately it doesn't return a promise nor take a callback, but a 500ms delay could be enough to make sure the events get captured. |
With the current version of posthog-js there is also the option to use
|
Following. We would like to see this implemented also. We are concerned that we are losing events (and thus that Posthog data is inaccurate and misleading our customer intelligence efforts) because there is no way to ensure the events are actually delivered. |
Me too. Capturing when a user leaves your website must be a common use case. In our case, when the user clicks a button, if the form data is valid, we want to capture an event then redirect. Ideally postHost.capture would take a callback or return a promise, but a method to flush the queue (and get a promise/callback for when it is flushed) would also work. |
This could be partially solved if we supported offline applications because we'd always keep a copy of our events in an IndexedDB-based database, which could then be flushed once we came back to that page. See #1583 |
When user navigates away from the page, we do try to send queued events in a best-effort way with sendBeacon. However this has a low reliability rate, resulting in lost events and more. This can be especially annoying for form submissions, which immediately reload the page.
Proposal:
Add configuration option that pauses page navigation until posthog-js has finished sending out requests or until some timeframe has passed (e.g. 1s). Default this to false, but suggest it if users if they're experiencing trouble.
The text was updated successfully, but these errors were encountered: