@ooneex/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.
Why this component
- One method to learn.
capture()records an event from a typed options object. - Type-safe events. Each analytics class defines its own capture options type instead of an untyped bag of fields.
- Container-managed. Register with a decorator and resolve from the container — no manual wiring.
- Backend agnostic. Implement
IAnalyticsto target any sink (a data warehouse, an HTTP endpoint, a queue) while keeping the same call sites.
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 — model it on the event you track. Keep one class per concern so each has 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.
Best practices
- Type your capture options. Replace
Record<string, unknown>with a precise options type per class so events stay consistent. - One class per tracked concern. A
PageViewAnalytics, aCheckoutAnalytics, etc. — small, focused classes are easier to test and reason about. - Name events consistently. Use a stable convention like
snake_caseverbs (purchase_completed,button_clicked) so dashboards stay clean. - Keep backend credentials in the environment. Load any keys your implementation needs from
.env; never hard-code them. capture()is fire-and-forget. It returnsvoid— keep it lightweight and off the request’s critical path.- Throw
AnalyticsExceptionwith a stablekey. Callers branch on the key; keep keys constant and put variable detail indata.
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 @ooneex/analytics if it is missing.
| Option | Description | Default |
|---|---|---|
--name | Analytics class name. The Analytics suffix is appended automatically. | Prompted if omitted |
--module | Target module the class is generated into. | shared |
--override | Overwrite an existing class without prompting. | false |
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().