Event Tracking

Capture custom events, structured log messages, and session attributes. Events are queued locally and flushed to the backend in batches.

Quick Start

import WhatzBug from '@whatzbug/react-native';

// Track a product event
WhatzBug.track('purchase_completed', {
  productId: 'sku_001',
  amount: 29.99,
  currency: 'USD',
});

// Log a structured message
WhatzBug.log('info', 'User completed onboarding', {
  step: 'profile_setup',
  duration: 4500,
});

// Set a session attribute
WhatzBug.setAttribute('subscription_tier', 'pro');

API Reference

track(name, payload?)

Track a named event with optional structured payload.

ParameterTypeRequiredDescription
namestringYesEvent name (e.g. 'purchase_completed')
payloadRecord<string, any>NoStructured event data
Note: Event names starting with $ are reserved for internal SDK events. Do not use the $ prefix for custom events.

log(level, message, data?)

Log a structured message. This is a convenience wrapper that emits a $log event.

ParameterTypeRequiredDescription
level'debug' | 'info' | 'warn' | 'error'YesLog severity level
messagestringYesHuman-readable log message
dataRecord<string, unknown>NoAdditional structured context

setAttribute(key, value)

Set a custom attribute on the current session. Attributes persist until the session ends or is cleared.

ParameterTypeRequiredDescription
keystringYesAttribute name
valuestring | number | booleanYesAttribute value

Event Pipeline

Events follow this path through the SDK:

  1. track() creates an envelope with a timestamp, device ID, and session context.
  2. The envelope is added to a persistent queue backed by AsyncStorage.
  3. The queue flushes in batches (default: up to 50 events per flush, every 5 seconds).
  4. On flush failure, events are returned to the queue for retry.
Debug mode: When debug is enabled, events are also tapped into the real-time WebSocket stream for instant visibility in the Desktop App.

Best Practices

  • Use descriptive, snake_case event names: checkout_started, item_added_to_cart.
  • Keep payload data flat and serializable. Avoid nested objects beyond one level.
  • Use setAttribute() for session-level context (user tier, app version, experiment group).
  • Use log() for diagnostic messages; use track() for business/product events.

Next Steps