@talosjs/analytics component tracks product events, user behavior, and metrics. It provides an IAnalytics interface and a @decorator.analytics() decorator so you can build typed, purpose-specific tracking classes, register them with the container, and send events to whatever backend you choose.
The shape of it
There is one method to learn:capture() records an event from a typed options object. Each analytics class defines its own capture options type rather than passing around an untyped bag of fields, and registration is a decorator away, so the container handles resolution and lifecycle for you. The interface is the only contract, which means you can point IAnalytics at any sink (a data warehouse, an HTTP endpoint, a queue) and the call sites never change.
How it works
You define an analytics class, implementcapture() with your tracking logic, and register it with @decorator.analytics(). The container manages its lifecycle; callers resolve the class and call capture() with a typed options object.
Decorator and usage
@decorator.analytics()
Registers an analytics class with the container. It accepts an optional scope (defaults to singleton).
capture():
Capturing events
capture() takes the typed options object your class defines, so model it on the event you track. Keep one class per concern and each one ends up with a precise shape.
Exceptions
The component shipsAnalyticsException for signaling analytics failures from your implementation. It carries a machine-readable key, a human-readable message, and a data object, so callers can branch on the key.
Keeping events clean
ReplaceRecord<string, unknown> with a precise options type per class so events stay consistent. Give each tracked concern its own class (a PageViewAnalytics, a CheckoutAnalytics, and so on); small, focused classes are easier to test and reason about. Pick a naming convention and hold to it, for example snake_case verbs like purchase_completed or button_clicked, which keeps dashboards readable.
Load any keys your implementation needs from .env and never hard-code them. The built-in PostHogAnalytics reads ANALYTICS_POSTHOG_PROJECT_TOKEN and optionally ANALYTICS_POSTHOG_HOST. Treat capture() as fire-and-forget: it returns void, so keep it lightweight and off the request’s critical path. When you throw AnalyticsException, keep the key constant per failure so callers can branch on it, and put the variable detail in data.
CLI command
Scaffold an analytics class and its test file with the generator. It writes the class undermodules/<module>/src/analytics/<Name>Analytics.ts and installs @talosjs/analytics if it is missing.
The generated class starts from this shape, ready for you to type the options and implement
capture():
Use with Claude and Codex
The generator ships a matchinganalytics:create skill. It runs the scaffold and then guides your AI agent through completing the class: defining a proper capture options type and implementing the tracking logic in capture(). Initialize the skills once for your agent:
- Claude
- Codex
Prompt
analytics:create --name=PageView, then types the capture options and implements capture().