Skip to main content

Event Batching & Queuing

MentiQ Analytics uses an intelligent event queuing and batching system to optimize network requests, handle failures gracefully, and ensure reliable event delivery even in poor network conditions.

Overview

Instead of sending every event immediately, MentiQ queues events locally and sends them in batches. This approach:
  • Reduces network overhead - Fewer HTTP requests
  • Improves performance - Non-blocking event tracking
  • Handles offline scenarios - Events queued until connection restored
  • Provides retry logic - Automatic retry with exponential backoff
  • Prevents data loss - Events stored until successfully sent

How It Works

1

Event Creation

When you call track(), page(), or other tracking methods, events are created and added to an in-memory queue.
2

Queue Storage

Events are stored as QueuedEvent objects with metadata:
3

Automatic Flushing

Events are automatically sent when either:
  • Queue reaches batchSize (default: 20 events)
  • flushInterval timer expires (default: 10 seconds)
4

Batch Processing

Events are split into batches and sent to the backend endpoint:
5

Retry on Failure

If batch fails, events are re-queued with exponential backoff:
  • 1st retry: Wait 1 second (retryDelay)
  • 2nd retry: Wait 2 seconds (retryDelay × 2)
  • 3rd retry: Wait 4 seconds (retryDelay × 4)
  • After 3 attempts: Events discarded (configurable)

Configuration Options

Batch Size

Number of events to accumulate before sending:
Increase batch size (30-50) for:
  • High-traffic applications
  • Reducing network requests
  • Cost optimization
Decrease batch size (5-10) for:
  • Real-time analytics requirements
  • Low-traffic applications
  • Testing and development

Flush Interval

Maximum time to wait before sending queued events:
Events are sent when either batch size is reached or flush interval expires, whichever comes first.

Max Queue Size

Maximum number of events to keep in queue:
When queue is full, oldest events are removed to make room for new ones. Increase this value for offline-first applications.

Retry Configuration

Control retry behavior for failed requests:
Retry delays use exponential backoff:
  • Attempt 1: retryDelay × 2^0 = 1000ms
  • Attempt 2: retryDelay × 2^1 = 2000ms
  • Attempt 3: retryDelay × 2^2 = 4000ms

Manual Flushing

You can manually trigger event sending:

Immediate Flush

Always flush before:
  • Page navigation that might unload the analytics instance
  • Critical events that must be sent immediately
  • Application shutdown or cleanup

Flush on Unload

Automatically flush when user leaves the page:

Queue Management

Check Queue Size

Monitor how many events are queued:

Clear Queue

Remove all queued events (use with caution):
clearQueue() permanently discards all queued events. Only use this for:
  • User logout/reset scenarios
  • Testing and development
  • Error recovery after configuration changes

Backend Batch Endpoint

Events are sent to the batch endpoint in this format:

Request Format

Response Format

Success (200 OK):
Error (4xx/5xx):

Event Transformation

Before sending, events are transformed from the internal format to backend-compatible format:

Internal Event Format

Backend Event Format

Event Type Mapping

The SDK automatically maps event types:

Performance Optimization

Flush on Critical Events

For important events that must be sent immediately:

Debugging

Enable debug mode to see batching behavior:
You’ll see logs like:

Error Handling

The SDK handles errors gracefully:

Network Failures

API Errors

Best Practices

Configure appropriate batch sizes based on your traffic volume
Flush before navigation to prevent event loss
Monitor queue size in high-traffic applications
Use manual flush sparingly to avoid performance impact
Enable debug mode during development to understand behavior
Handle flush promises for critical events
Don’t set batchSize: 1 unless you need real-time analytics. This creates one HTTP request per event and can impact performance.

Next Steps

TypeScript Types

Learn about QueuedEvent and batching types

Privacy Compliance

Implement privacy-safe event tracking