mixpanel / mixpanel-js

Official Mixpanel JavaScript Client Library
https://mixpanel.com/help/reference/javascript
Other
881 stars 308 forks source link

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

Open lukehatcher opened 3 months ago

lukehatcher commented 3 months ago

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 commented 3 months ago

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: https://github.com/mixpanel/mixpanel-js/blob/34b4396de534e4f5cf4b5cac80afd14a8322ce1f/src/mixpanel-core.js#L308-L334 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 commented 2 months ago

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.