JavaScript (web) metrics installation
Note: Metrics is in alpha. Setup details may change before general availability.
If posthog-js is already running on your site, you can record metrics directly with the posthog.metrics API. No new packages, no extra authentication.
- 1
Install posthog-js
RequiredIf you haven't already, install posthog-js via the snippet or npm and initialize it with your project token. Metrics requires an up-to-date SDK version, so upgrade if you're on an older release.
There is no metrics-specific setup required: the metrics API authenticates with the same project token the SDK already uses. Optionally, set a service name so your metrics are easy to find and filter:
JavaScript - 2
Record metrics
RequiredUse the metric type that matches what you're measuring:
JavaScriptAdd attributes to slice a metric by dimension, keeping the set of values small and bounded:
JavaScriptGood attributes:
route,status,plan. Bad attributes: user IDs, session IDs, request IDs. Every unique combination of attribute values creates a new series, so high-cardinality dimensions belong in logs or traces, not metrics.Samples aggregate in memory and flush as one data point per series every few seconds, so recording in hot paths is cheap.
- 3
Instrument alongside existing metrics
OptionalIf your app already records metrics with another system, don't rip it out. Add the PostHog call next to the existing one, reusing the same metric name and attributes, so both systems chart the same series while you evaluate.
- 4
Next steps
CheckpointWhat you can do with your metricsAction Description Why you need metrics What metrics show you that events and logs don't Getting started guide Pick the right metric type, add attributes carefully, and chart what matters Group and filter Group by an attribute for one line per value, or filter with key=valuechipsHow metrics works How metrics are ingested, stored, and queried Query with SQL Every metric lands in the posthog.metricstable, queryable from the SQL tab