Event Batching & Flushing
To optimize battery life and network usage, B2Metric queues events and sends them in batches.
How Event Batching Works
Automatic flush triggers:
- Batch size reached — When the queue hits
batchSizeevents (default: 20) - Time interval elapsed — Every
flushIntervalSeconds(default: 30 seconds) - App foregrounded — When user brings app from background
- App backgrounded — Before app goes to background
- Network reconnected — When internet connection is restored
sdk.logEvent("button_clicked") // Event 1 - queued
sdk.logEvent("screen_viewed") // Event 2 - queued
...
sdk.logEvent("product_viewed") // Event 20 - hits batchSize → All 20 sent immediately
sdk.logEvent("add_to_cart") // Event 21 - new queue starts
... 29 seconds pass ...
→ Event 21 sent when flushIntervalSeconds (30s) expires
Configure Batching Behavior
B2MetricAnalyticsConfig(
apiKey = "YOUR_API_KEY",
batchSize = 10, // Send after 10 events
flushIntervalSeconds = 60 // Send every 60 seconds
)
User Identification - Device ID
B2Metric automatically generates a unique Device ID (UUID v4) for each app installation.
How it works:
- Generated automatically on first SDK initialization
- Format: UUID v4 (e.g.,
a3f5b8c1-2d4e-4f6a-8b9c-0d1e2f3a4b5c) - Used as the primary identifier for tracking events
- No action required — handled automatically
Persistence:
- Stored locally on the device
- Persists across app restarts
- Removed only when the app is uninstalled
- Each installation gets a new Device ID
Offline Support
When offline:
- Events are stored locally in persistent storage
- No network requests are attempted
- Queue continues to grow with new events
When connection is restored:
- SDK detects network availability
- All queued events are sent automatically
- No events are lost
User loses internet connection
sdk.logEvent("product_viewed") // Stored locally
sdk.logEvent("add_to_cart") // Stored locally
sdk.logEvent("button_clicked") // Stored locally
... user browses offline for 10 minutes ...
User regains internet connection
→ SDK automatically detects connection
→ All stored events sent to server
Storage behavior:
- Events persist across app restarts
- Stored in app's private storage
- Events sent in chronological order
Retry Behavior
B2MetricAnalyticsConfig(
apiKey = "YOUR_API_KEY",
maxRetries = 3 // Retry failed requests up to 3 times
)
Retry logic:
- Failed requests are queued for retry
- Retries happen on next flush trigger
- After max retries, events are kept in local storage
- Will retry again on next app session or network recovery