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
- The function retrieves the metric configuration for the noun-action pair, including timezone settings
- 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
- Updates the entity summary with the latest values
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 usingincrementMetric
, 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
- updateMetric - Batch update metrics
- firebridgeMetric - Access metric utilities
- Types Reference - Detailed type definitions