Skip to main content
The incrementMetric function allows you to track individual events as they occur, automatically updating aggregated values across all configured time units.

Function Signature

Parameters

Required Parameters

  • noun string: The entity type being tracked (e.g., “product”, “user”, “article”)
  • action string: The action being measured (e.g., “purchase”, “view”, “click”, “share”)
  • entity string: The specific entity ID (e.g., product ID, user ID)

Optional Parameters

  • count number: Number of occurrences (default: 1)
  • value number: Numerical value associated with the event (default: 1)
  • time firestore.Timestamp: When the event occurred (default: current time)

How It Works

  1. The function retrieves the metric configuration for the noun-action pair, including timezone settings
  2. For each configured time unit (hour, day, week, etc.), it:
    • Converts the event time to the configured timezone
    • Finds or creates the appropriate timeline cursor based on local time boundaries
    • Increments both the period count/value and running totals
  3. Updates the entity summary with the latest values
Timezone Behavior: Events are aggregated based on the configured timezone. For example, an event at 11 PM Pacific Time will be counted in the correct day according to Pacific Time, not UTC.

Examples

Basic Usage

Track a simple page view:

With Custom Values

Track a purchase with monetary value:

Historical Events

Track an event that occurred in the past:

Multiple Event Types

Track different actions for the same entity:

Timezone-Aware Tracking

When tracking events across timezones, the configured timezone determines aggregation boundaries:

Metric Configuration

Before using incrementMetric, ensure the metric is configured with appropriate time units and timezone:

Performance Considerations

  • Each increment triggers updates across all configured time units
  • Use batch operations via updateMetric for importing large datasets
  • Consider rate limiting for high-frequency events
  • Firestore write costs scale with the number of time units configured

Error Handling

See Also