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