Skip to content
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

is it possible to flush all batched events at once? #426

Open
lukehatcher opened this issue May 23, 2024 · 2 comments
Open

is it possible to flush all batched events at once? #426

lukehatcher opened this issue May 23, 2024 · 2 comments

Comments

@lukehatcher
Copy link

lukehatcher commented May 23, 2024

I know there is

mixpanel.track('my event', {foo: 'bar'}, { 'send_immediately': true });

Similarly, I know I can edit the config - but that only effects following events, plus I still want batching.

mixpanel.set_config({ batch_requests: false });
// or
mixpanel.set_config({ batch_flush_interval_ms: 0 });

Use Case: I have a large batch_flush_interval_ms interval and want to flush everything before navigation to a new external URL, but I don't want to give up my batching and use send_immediately every time I call mixpanel.track

I have tried editing the flushAfter property of the events queued inside localStorage with no luck.

@tdumitrescu
Copy link
Member

I don't think we currently have a publicly-exposed method to flush everything. The SDK does already attempt to flush every time the page/tab loses visibility:

if (sendBeacon && window.addEventListener) {
// Before page closes or hides (user tabs away etc), attempt to flush any events
// queued up via navigator.sendBeacon. Since sendBeacon doesn't report success/failure,
// events will not be removed from the persistent store; if the site is loaded again,
// the events will be flushed again on startup and deduplicated on the Mixpanel server
// side.
// There is no reliable way to capture only page close events, so we lean on the
// visibilitychange and pagehide events as recommended at
// https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event#usage_notes.
// These events fire when the user clicks away from the current page/tab, so will occur
// more frequently than page unload, but are the only mechanism currently for capturing
// this scenario somewhat reliably.
var flush_on_unload = _.bind(function() {
if (!this.request_batchers.events.stopped) {
this.request_batchers.events.flush({unloading: true});
}
}, this);
window.addEventListener('pagehide', function(ev) {
if (ev['persisted']) {
flush_on_unload();
}
});
window.addEventListener('visibilitychange', function() {
if (document['visibilityState'] === 'hidden') {
flush_on_unload();
}
});
but this can never be totally reliable.

If you just want something that works quickly, calling mixpanel.start_batch_senders() will cause them to flush immediately even if it's already running (but this is an implementation detail).

@rahulbansal16
Copy link

I am using it in an electron app and I want to flush out everything before all windows are closed.
The implementation etc is happening in the main process so there is no window over there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants