Skip to main content
The 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

  1. Retrieves metric configuration including time units and timezone settings
  2. If clean is true, deletes all existing data for the entity
  3. Sorts events chronologically
  4. Builds timeline aggregations for each configured unit using the configured timezone
  5. Executes batch update for all timeline sections
  6. Updates entity summary with final totals
Timezone Handling: All timeline aggregations respect the configured timezone, ensuring events are grouped into the correct time periods based on local time boundaries rather than UTC.

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