updateMetric
function allows you to update metrics with multiple events at once, perfect for importing historical data, rebuilding metrics, or processing batched events.
Function Signature
Parameters
Required Parameters
- noun
string
: The entity type being tracked - action
string
: The action being measured - entity
string
: The specific entity ID - events
TrackableEvent[]
: Array of events to process
TrackableEvent Structure
Optional Parameters
- startingCount
number
: Initial count before events (default: 0) - startingValue
number
: Initial value before events (default: 0) - clean
boolean
: Delete existing data before update (default: true)
How It Works
- Retrieves metric configuration including time units and timezone settings
- If
clean
is true, deletes all existing data for the entity - Sorts events chronologically
- Builds timeline aggregations for each configured unit using the configured timezone
- Executes batch update for all timeline sections
- Updates entity summary with final totals
Examples
Import Historical Data
Rebuild Metrics from Existing Data
Append to Existing Metrics
Process Batch Events
Performance Benefits
Batch Operations
updateMetric
uses Firestore batch operations for efficiency:
Timeline Building
The function efficiently builds aggregated timelines:- Groups events by time period
- Calculates running totals
- Creates only necessary timeline sections
Advanced Usage
Custom Time Units and Timezone
Configure metrics with specific time units and timezone before updating:Migration Example
Migrate from an old analytics system with timezone awareness:Cross-Timezone Event Processing
Handle events from multiple timezones correctly:Error Handling
See Also
- incrementMetric - Track individual events
- buildTimeline - Timeline building utility
- Types Reference - Detailed type definitions