@molecule/app-analytics 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +340 -0
  2. package/package.json +7 -6
package/README.md ADDED
@@ -0,0 +1,340 @@
1
+ <!--
2
+ AUTO-GENERATED — DO NOT EDIT THIS FILE.
3
+ Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
4
+ Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
5
+ To change this document, edit the module-level JSDoc in src/index.ts.
6
+ Generated: 2026-08-04T01:50:00.032Z
7
+ -->
8
+
9
+ # @molecule/app-analytics
10
+
11
+ > **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
12
+ > It is written to be read by coding agents as much as by people, and is generated from this
13
+ > package's source — edit `src/index.ts` JSDoc, not this file.
14
+
15
+ Frontend analytics interface for molecule.dev.
16
+
17
+ Provides a unified analytics API that can be backed by different
18
+ implementations (PostHog, Mixpanel, etc.).
19
+
20
+ ## Quick Start
21
+
22
+ ```typescript
23
+ import { identify, reset, setProvider, track } from '@molecule/app-analytics'
24
+ import { createProvider } from '@molecule/app-analytics-posthog'
25
+
26
+ // At startup — the key comes from your build-time env (in Vite:
27
+ // import.meta.env.VITE_POSTHOG_KEY); a missing key yields a warning +
28
+ // no-op provider, never a crash.
29
+ setProvider(createProvider({ apiKey: posthogApiKey }))
30
+
31
+ identify({ userId: user.id, email: user.email }) // on login
32
+ track({ name: 'order_placed', properties: { total } })
33
+ reset() // on logout
34
+ ```
35
+
36
+ ## Type
37
+
38
+ `core`
39
+
40
+ ## Installation
41
+
42
+ ```bash
43
+ npm install @molecule/app-analytics @molecule/app-bond
44
+ ```
45
+
46
+ ## API
47
+
48
+ ### Interfaces
49
+
50
+ #### `AnalyticsEvent`
51
+
52
+ Event properties for analytics tracking.
53
+
54
+ ```typescript
55
+ interface AnalyticsEvent {
56
+ /** Event name (e.g. `"button_clicked"`, `"purchase_completed"`). */
57
+ name: string
58
+ /** Arbitrary key-value properties for this event. */
59
+ properties?: Record<string, unknown>
60
+ /**
61
+ * Event timestamp (defaults to now). Only honored where the underlying
62
+ * browser SDK supports client-set timestamps (PostHog does); the Mixpanel
63
+ * browser SDK always stamps the time of capture. For reliable historical
64
+ * timestamps use the API-side `@molecule/api-analytics` bonds.
65
+ */
66
+ timestamp?: Date
67
+ /**
68
+ * Identified user who triggered the event. Browser analytics SDKs attribute
69
+ * events to the AMBIENT identified session — call `identify()` first;
70
+ * current browser bonds do not honor a per-event userId override. (Exists
71
+ * for interface parity with `@molecule/api-analytics`, where there is no
72
+ * ambient session and per-event IDs are required.)
73
+ */
74
+ userId?: string
75
+ /**
76
+ * Anonymous identifier for unauthenticated users. Like `userId`, browser
77
+ * bonds use the SDK's own ambient anonymous identity instead of this field.
78
+ */
79
+ anonymousId?: string
80
+ }
81
+ ```
82
+
83
+ #### `AnalyticsPageView`
84
+
85
+ Page view event.
86
+
87
+ ```typescript
88
+ interface AnalyticsPageView {
89
+ /** Page name (e.g. `"Home"`, `"Settings"`). */
90
+ name?: string
91
+ /** Page category (e.g. `"Dashboard"`, `"Marketing"`). */
92
+ category?: string
93
+ /** Full page URL. */
94
+ url?: string
95
+ /** Page path (e.g. `"/settings/profile"`). */
96
+ path?: string
97
+ /** Referrer URL. */
98
+ referrer?: string
99
+ /** Arbitrary key-value properties for this page view. */
100
+ properties?: Record<string, unknown>
101
+ }
102
+ ```
103
+
104
+ #### `AnalyticsProvider`
105
+
106
+ Analytics provider interface that all analytics bond packages must implement.
107
+
108
+ ```typescript
109
+ interface AnalyticsProvider {
110
+ /**
111
+ * Identifies a user and associates traits with them.
112
+ *
113
+ * @param user - User properties including userId and optional traits.
114
+ */
115
+ identify(user: AnalyticsUserProps): Promise<void>
116
+
117
+ /**
118
+ * Tracks a named event with optional properties.
119
+ *
120
+ * @param event - The event name and properties to track.
121
+ */
122
+ track(event: AnalyticsEvent): Promise<void>
123
+
124
+ /**
125
+ * Records a page view.
126
+ *
127
+ * @param pageView - Page view details (name, path, properties).
128
+ */
129
+ page(pageView: AnalyticsPageView): Promise<void>
130
+
131
+ /**
132
+ * Associates a user with a group (e.g. company, team).
133
+ *
134
+ * @param groupId - The group identifier.
135
+ * @param traits - Arbitrary traits to associate with the group.
136
+ */
137
+ group?(groupId: string, traits?: Record<string, unknown>): Promise<void>
138
+
139
+ /**
140
+ * Resets the current user identity and generates a new anonymous ID.
141
+ */
142
+ reset?(): Promise<void>
143
+
144
+ /**
145
+ * Flushes any queued events to the analytics service immediately.
146
+ */
147
+ flush?(): Promise<void>
148
+ }
149
+ ```
150
+
151
+ #### `AnalyticsUserProps`
152
+
153
+ User properties for analytics identification.
154
+
155
+ ```typescript
156
+ interface AnalyticsUserProps {
157
+ /** Unique user identifier. */
158
+ userId: string
159
+ /** User email address. */
160
+ email?: string
161
+ /** User display name. */
162
+ name?: string
163
+ /** Arbitrary key-value traits to associate with the user. */
164
+ traits?: Record<string, unknown>
165
+ }
166
+ ```
167
+
168
+ #### `AutoTrackingOptions`
169
+
170
+ Auto-tracking options. Pass any combination of sources — only provided
171
+ sources are tracked. Works for both web and mobile apps.
172
+
173
+ ```typescript
174
+ interface AutoTrackingOptions {
175
+ /** Auth client for login/logout/register/error events. */
176
+ authClient?: AuthClientLike
177
+ /** Router for page view tracking. */
178
+ router?: RouterLike
179
+ /** HTTP client for error tracking. */
180
+ httpClient?: HttpClientLike
181
+ /** Lifecycle client for app foreground/background and deep link tracking. */
182
+ lifecycleClient?: LifecycleClientLike
183
+ /** Push client for notification received/tapped tracking. */
184
+ pushClient?: PushClientLike
185
+ }
186
+ ```
187
+
188
+ ### Functions
189
+
190
+ #### `flush()`
191
+
192
+ Flushes any queued analytics events to the backend. Errors are silently ignored.
193
+
194
+ ```typescript
195
+ function flush(): Promise<void>
196
+ ```
197
+
198
+ **Returns:** A promise that resolves when flush completes or fails silently.
199
+
200
+ #### `getProvider()`
201
+
202
+ Retrieves the bonded analytics provider. Returns a no-op provider if
203
+ none is bonded, so analytics calls never throw.
204
+
205
+ ```typescript
206
+ function getProvider(): AnalyticsProvider
207
+ ```
208
+
209
+ **Returns:** The bonded analytics provider, or a silent no-op fallback.
210
+
211
+ #### `group(groupId, traits)`
212
+
213
+ Associates the current user with a group/organization. Errors are silently ignored.
214
+
215
+ ```typescript
216
+ function group(groupId: string, traits?: Record<string, unknown>): Promise<void>
217
+ ```
218
+
219
+ - `groupId` — The group identifier.
220
+ - `traits` — Optional traits describing the group.
221
+
222
+ **Returns:** A promise that resolves when group association completes or fails silently.
223
+
224
+ #### `hasProvider()`
225
+
226
+ Checks whether an analytics provider is currently bonded.
227
+
228
+ ```typescript
229
+ function hasProvider(): boolean
230
+ ```
231
+
232
+ **Returns:** `true` if an analytics provider is bonded.
233
+
234
+ #### `identify(user)`
235
+
236
+ Identifies the current user for analytics tracking. Errors are silently ignored.
237
+
238
+ ```typescript
239
+ function identify(user: AnalyticsUserProps): Promise<void>
240
+ ```
241
+
242
+ - `user` — The user properties (ID, email, name, traits).
243
+
244
+ **Returns:** A promise that resolves when identification completes or fails silently.
245
+
246
+ #### `page(pageView)`
247
+
248
+ Tracks a page view event. Errors are silently ignored.
249
+
250
+ ```typescript
251
+ function page(pageView: AnalyticsPageView): Promise<void>
252
+ ```
253
+
254
+ - `pageView` — The page view data (name, path, referrer, etc.).
255
+
256
+ **Returns:** A promise that resolves when page tracking completes or fails silently.
257
+
258
+ #### `reset()`
259
+
260
+ Resets the analytics state (e.g. on logout). Errors are silently ignored.
261
+
262
+ ```typescript
263
+ function reset(): Promise<void>
264
+ ```
265
+
266
+ **Returns:** A promise that resolves when reset completes or fails silently.
267
+
268
+ #### `setProvider(provider)`
269
+
270
+ Registers an analytics provider as the active singleton.
271
+
272
+ ```typescript
273
+ function setProvider(provider: AnalyticsProvider): void
274
+ ```
275
+
276
+ - `provider` — The analytics provider implementation to bond.
277
+
278
+ #### `setupAutoTracking(options)`
279
+
280
+ Sets up automatic analytics tracking for auth events, route changes,
281
+ HTTP errors, app lifecycle transitions, push notifications, and deep links.
282
+ Returns a cleanup function that removes all subscriptions.
283
+
284
+ Pass any combination of sources — only provided sources are tracked.
285
+
286
+ The analytics provider is resolved at EVENT time, not at setup time — so
287
+ `setupAutoTracking()` may safely run before the analytics bond is set
288
+ (events fired before bonding no-op; events fired after are tracked).
289
+
290
+ ```typescript
291
+ function setupAutoTracking(options: AutoTrackingOptions): () => void
292
+ ```
293
+
294
+ - `options` — Sources to auto-track.
295
+
296
+ **Returns:** A cleanup function that removes all event subscriptions.
297
+
298
+ #### `track(event)`
299
+
300
+ Tracks a named event with optional properties. Errors are silently ignored.
301
+
302
+ ```typescript
303
+ function track(event: AnalyticsEvent): Promise<void>
304
+ ```
305
+
306
+ - `event` — The event to track (name and optional properties).
307
+
308
+ **Returns:** A promise that resolves when tracking completes or fails silently.
309
+
310
+ ## Available Providers
311
+
312
+ | Provider | Package |
313
+ | -------- | ---------------------------------- |
314
+ | Mixpanel | `@molecule/app-analytics-mixpanel` |
315
+ | PostHog | `@molecule/app-analytics-posthog` |
316
+
317
+ ## Injection Notes
318
+
319
+ ### Requirements
320
+
321
+ Peer dependencies:
322
+
323
+ - `@molecule/app-bond` ^1.0.1
324
+
325
+ ### Runtime Dependencies
326
+
327
+ - `@molecule/app-bond`
328
+
329
+ - Every convenience function (`track`, `identify`, `page`, …) swallows
330
+ provider errors and no-ops when nothing is bonded — analytics can never
331
+ break the UI, so `hasProvider()` is the ONLY signal that separates
332
+ "analytics disabled/unbonded" from "events are being tracked". Check it
333
+ (and the bond's own console warning) before debugging tracking code.
334
+ - Attribution is AMBIENT in the browser: call `identify(user)` on login and
335
+ `reset()` on logout; per-event `userId`/`anonymousId` fields are not
336
+ honored by browser bonds (they exist for parity with
337
+ `@molecule/api-analytics`).
338
+ - `group(groupId)` normalizes the group TYPE to `'company'` in every bond
339
+ (Mixpanel group key, PostHog group type) — look under "company" in the
340
+ provider's UI.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/app-analytics",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Frontend analytics interface for molecule.dev",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -17,7 +17,8 @@
17
17
  }
18
18
  },
19
19
  "files": [
20
- "dist"
20
+ "dist",
21
+ "README.md"
21
22
  ],
22
23
  "keywords": [
23
24
  "molecule",
@@ -28,20 +29,20 @@
28
29
  ],
29
30
  "license": "Apache-2.0",
30
31
  "peerDependencies": {
31
- "@molecule/app-bond": "^1.0.0"
32
+ "@molecule/app-bond": "^1.0.1"
32
33
  },
33
34
  "devDependencies": {
34
- "@molecule/app-bond": "1.0.0",
35
+ "@molecule/app-bond": "1.0.2",
35
36
  "@types/node": "26.1.2",
36
37
  "typescript": "6.0.3",
37
- "vitest": "4.1.10"
38
+ "vitest": "4.1.11"
38
39
  },
39
40
  "repository": {
40
41
  "type": "git",
41
42
  "url": "https://github.com/molecule-dev/molecule.git",
42
43
  "directory": "packages/app/core/analytics"
43
44
  },
44
- "homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/app/core/analytics",
45
+ "homepage": "https://www.molecule.dev/packages/app-analytics",
45
46
  "bugs": "https://github.com/molecule-dev/molecule/issues",
46
47
  "publishConfig": {
47
48
  "access": "public"