Skip to main content
The @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, implement capture() 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.
Since the interface is the only contract, you can swap or stub the backend without touching any call site.

Decorator and usage

@decorator.analytics()

Registers an analytics class with the container. It accepts an optional scope (defaults to singleton).
Resolve it from the container and call capture():
Choose a non-default scope when you need a fresh instance per request:

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 ships AnalyticsException 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.
Catch it at the call site to handle analytics errors without breaking the request:

Keeping events clean

Replace Record<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 under modules/<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():
See analytics:create for the full command reference.

Use with Claude and Codex

The generator ships a matching analytics: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:
Then ask Claude in natural language. It maps the request to the generator, runs it, and fills in the implementation:
Prompt
For example, the prompt above maps to analytics:create --name=PageView, then types the capture options and implements capture().