Skip to main content

Manual Flush

Events are delivered automatically in the background — either when the batch size is reached, or when the flush interval expires, whichever happens first. In most situations this is exactly what you want: it minimises battery and network usage by amortising the cost of a network round trip across many events.

Occasionally, however, you need to be sure that a critical event has been delivered before continuing — for example, just before completing a checkout, or just before logging the user out and tearing down the app state. In those cases call flush to send all pending events immediately:

B2Metric.logEvent('purchase_completed', {
properties: { order_id: 'ord_789', total: 4990 },
})
await B2Metric.flush()

The returned promise resolves when the in-flight send attempt completes. If the device is offline, the events are kept in the persistent queue and flush still resolves successfully — the events will be delivered as soon as connectivity is restored.

Concurrent calls to flush are safe; the SDK serialises them internally so only one delivery runs at a time.

Note

Do not call flush after every logEvent. Doing so defeats the batching logic and noticeably increases battery and network usage. Use it only at moments where event loss would be unrecoverable.