@molecule/app-analytics-mixpanel 1.0.0 → 1.0.1

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 +266 -0
  2. package/package.json +5 -4
package/README.md ADDED
@@ -0,0 +1,266 @@
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.751Z
7
+ -->
8
+
9
+ # @molecule/app-analytics-mixpanel
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
+ Mixpanel analytics provider for molecule.dev frontend.
16
+
17
+ ## Quick Start
18
+
19
+ ```typescript
20
+ import { setProvider } from '@molecule/app-analytics'
21
+ import { createProvider } from '@molecule/app-analytics-mixpanel'
22
+
23
+ // Canonical wiring: read the browser-side token from Vite env and pass it
24
+ // through options. Without a token, skip bonding — analytics calls no-op.
25
+ // VITE_MIXPANEL_TOKEN below stands for `import.meta.env.VITE_MIXPANEL_TOKEN`
26
+ // (write the `import.meta.env` read in your app's bond-setup file).
27
+ const token = VITE_MIXPANEL_TOKEN as string | undefined
28
+ if (token) {
29
+ setProvider(createProvider({ token }))
30
+ }
31
+ ```
32
+
33
+ ## Type
34
+
35
+ `provider`
36
+
37
+ ## Installation
38
+
39
+ ```bash
40
+ npm install @molecule/app-analytics-mixpanel @molecule/app-analytics mixpanel-browser
41
+ ```
42
+
43
+ ## API
44
+
45
+ ### Interfaces
46
+
47
+ #### `AnalyticsEvent`
48
+
49
+ Event properties for analytics tracking.
50
+
51
+ ```typescript
52
+ interface AnalyticsEvent {
53
+ /** Event name (e.g. `"button_clicked"`, `"purchase_completed"`). */
54
+ name: string
55
+ /** Arbitrary key-value properties for this event. */
56
+ properties?: Record<string, unknown>
57
+ /**
58
+ * Event timestamp (defaults to now). Only honored where the underlying
59
+ * browser SDK supports client-set timestamps (PostHog does); the Mixpanel
60
+ * browser SDK always stamps the time of capture. For reliable historical
61
+ * timestamps use the API-side `@molecule/api-analytics` bonds.
62
+ */
63
+ timestamp?: Date
64
+ /**
65
+ * Identified user who triggered the event. Browser analytics SDKs attribute
66
+ * events to the AMBIENT identified session — call `identify()` first;
67
+ * current browser bonds do not honor a per-event userId override. (Exists
68
+ * for interface parity with `@molecule/api-analytics`, where there is no
69
+ * ambient session and per-event IDs are required.)
70
+ */
71
+ userId?: string
72
+ /**
73
+ * Anonymous identifier for unauthenticated users. Like `userId`, browser
74
+ * bonds use the SDK's own ambient anonymous identity instead of this field.
75
+ */
76
+ anonymousId?: string
77
+ }
78
+ ```
79
+
80
+ #### `AnalyticsPageView`
81
+
82
+ Page view event.
83
+
84
+ ```typescript
85
+ interface AnalyticsPageView {
86
+ /** Page name (e.g. `"Home"`, `"Settings"`). */
87
+ name?: string
88
+ /** Page category (e.g. `"Dashboard"`, `"Marketing"`). */
89
+ category?: string
90
+ /** Full page URL. */
91
+ url?: string
92
+ /** Page path (e.g. `"/settings/profile"`). */
93
+ path?: string
94
+ /** Referrer URL. */
95
+ referrer?: string
96
+ /** Arbitrary key-value properties for this page view. */
97
+ properties?: Record<string, unknown>
98
+ }
99
+ ```
100
+
101
+ #### `AnalyticsProvider`
102
+
103
+ Analytics provider interface that all analytics bond packages must implement.
104
+
105
+ ```typescript
106
+ interface AnalyticsProvider {
107
+ /**
108
+ * Identifies a user and associates traits with them.
109
+ *
110
+ * @param user - User properties including userId and optional traits.
111
+ */
112
+ identify(user: AnalyticsUserProps): Promise<void>
113
+ /**
114
+ * Tracks a named event with optional properties.
115
+ *
116
+ * @param event - The event name and properties to track.
117
+ */
118
+ track(event: AnalyticsEvent): Promise<void>
119
+ /**
120
+ * Records a page view.
121
+ *
122
+ * @param pageView - Page view details (name, path, properties).
123
+ */
124
+ page(pageView: AnalyticsPageView): Promise<void>
125
+ /**
126
+ * Associates a user with a group (e.g. company, team).
127
+ *
128
+ * @param groupId - The group identifier.
129
+ * @param traits - Arbitrary traits to associate with the group.
130
+ */
131
+ group?(groupId: string, traits?: Record<string, unknown>): Promise<void>
132
+ /**
133
+ * Resets the current user identity and generates a new anonymous ID.
134
+ */
135
+ reset?(): Promise<void>
136
+ /**
137
+ * Flushes any queued events to the analytics service immediately.
138
+ */
139
+ flush?(): Promise<void>
140
+ }
141
+ ```
142
+
143
+ #### `AnalyticsUserProps`
144
+
145
+ User properties for analytics identification.
146
+
147
+ ```typescript
148
+ interface AnalyticsUserProps {
149
+ /** Unique user identifier. */
150
+ userId: string
151
+ /** User email address. */
152
+ email?: string
153
+ /** User display name. */
154
+ name?: string
155
+ /** Arbitrary key-value traits to associate with the user. */
156
+ traits?: Record<string, unknown>
157
+ }
158
+ ```
159
+
160
+ #### `MixpanelOptions`
161
+
162
+ Options for creating a Mixpanel provider.
163
+
164
+ ```typescript
165
+ interface MixpanelOptions {
166
+ token?: string
167
+ debug?: boolean
168
+ }
169
+ ```
170
+
171
+ ### Functions
172
+
173
+ #### `createProvider(options)`
174
+
175
+ Creates a Mixpanel analytics provider using `mixpanel-browser`. Initializes the SDK
176
+ with the provided token and returns an `AnalyticsProvider` that maps molecule events to
177
+ Mixpanel's `track`, `identify`, `people.set`, and `set_group` APIs.
178
+
179
+ When no token is provided, this does NOT initialize the SDK (mixpanel-browser
180
+ accepts `init('')` in TOTAL silence and then sends every event to
181
+ api.mixpanel.com with an empty token, where it is dropped server-side with no
182
+ client-side breadcrumb): it logs one actionable warning naming
183
+ VITE_MIXPANEL_TOKEN and returns a no-op provider, so bonding the lazy
184
+ `provider` export without configuration degrades gracefully instead of
185
+ becoming a silent event black hole.
186
+
187
+ ```typescript
188
+ function createProvider(options?: MixpanelOptions): AnalyticsProvider
189
+ ```
190
+
191
+ - `options` — Mixpanel configuration including `token` and optional `debug` flag.
192
+
193
+ **Returns:** An `AnalyticsProvider` backed by the Mixpanel browser SDK.
194
+
195
+ ### Constants
196
+
197
+ #### `provider`
198
+
199
+ Lazy-initialized Mixpanel analytics provider singleton.
200
+
201
+ ```typescript
202
+ const provider: AnalyticsProvider
203
+ ```
204
+
205
+ ## Core Interface
206
+
207
+ Implements `@molecule/app-analytics` interface.
208
+
209
+ ## Bond Wiring
210
+
211
+ This provider cannot self-configure: app-side bonds never read env internally (Vite only embeds `import.meta.env`, not `process.env`), so the bare `provider` export can never receive the required VITE_MIXPANEL_TOKEN secret. Bonding it directly (`setProvider(provider)`) silently ships a no-op even when the secret IS set correctly in your app's env — call `createProvider(options)` with the secret read from your app's env, then pass the result to `setProvider()`.
212
+
213
+ See Quick Start above for the exact wiring:
214
+
215
+ ```typescript
216
+ import { setProvider } from '@molecule/app-analytics'
217
+ import { createProvider } from '@molecule/app-analytics-mixpanel'
218
+
219
+ // Canonical wiring: read the browser-side token from Vite env and pass it
220
+ // through options. Without a token, skip bonding — analytics calls no-op.
221
+ // VITE_MIXPANEL_TOKEN below stands for `import.meta.env.VITE_MIXPANEL_TOKEN`
222
+ // (write the `import.meta.env` read in your app's bond-setup file).
223
+ const token = VITE_MIXPANEL_TOKEN as string | undefined
224
+ if (token) {
225
+ setProvider(createProvider({ token }))
226
+ }
227
+ ```
228
+
229
+ ## Injection Notes
230
+
231
+ ### Requirements
232
+
233
+ Peer dependencies:
234
+
235
+ - `@molecule/app-analytics` ^1.0.1
236
+
237
+ ### Environment Variables
238
+
239
+ - `VITE_MIXPANEL_TOKEN` _(required)_ — Mixpanel project token
240
+ - Setup: Copy the Project Token from Mixpanel → Project Settings → Access Keys. This is a public browser-side token — Vite embeds it into the client bundle (VITE_ prefix required).
241
+ - Get it here: [https://mixpanel.com/settings/project](https://mixpanel.com/settings/project)
242
+
243
+ ### Runtime Dependencies
244
+
245
+ - `@molecule/app-analytics`
246
+ - `mixpanel-browser`
247
+
248
+ The provider does NOT read env itself — configuration flows in through
249
+ `createProvider(options)`. The canonical env name is `VITE_MIXPANEL_TOKEN`
250
+ (the `VITE_` prefix is required: Vite only embeds `VITE_`-prefixed vars
251
+ into the client bundle, and molecule's scaffolded app `.env` only includes
252
+ `VITE_*` secrets). Do NOT use the API-side twin name (`MIXPANEL_TOKEN`) in
253
+ frontend code — that belongs to `@molecule/api-analytics-mixpanel` and
254
+ never reaches the browser. The Mixpanel project token is a public
255
+ browser-side credential, safe to embed client-side.
256
+
257
+ Bonding without a token is failure-safe: `createProvider()` (and the lazy
258
+ `provider` export, which cannot receive options) logs ONE console warning
259
+ naming VITE_MIXPANEL_TOKEN and returns a no-op provider — the raw SDK would
260
+ otherwise accept an empty token in total silence and every event would
261
+ vanish with no breadcrumb. If events never appear in Mixpanel, check the
262
+ browser console for that warning FIRST.
263
+
264
+ `AnalyticsEvent.timestamp` is NOT honored: the Mixpanel browser SDK has no
265
+ supported client-set timestamp and always stamps the time of capture. For
266
+ historical timestamps use `@molecule/api-analytics-mixpanel` server-side.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/app-analytics-mixpanel",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Mixpanel analytics provider for molecule.dev frontend",
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",
@@ -29,10 +30,10 @@
29
30
  "mixpanel-browser": "2.81.0"
30
31
  },
31
32
  "peerDependencies": {
32
- "@molecule/app-analytics": "^1.0.0"
33
+ "@molecule/app-analytics": "^1.0.1"
33
34
  },
34
35
  "devDependencies": {
35
- "@molecule/app-analytics": "1.0.0",
36
+ "@molecule/app-analytics": "1.0.1",
36
37
  "@types/node": "26.1.2",
37
38
  "typescript": "6.0.3",
38
39
  "vitest": "4.1.10"