@molecule/app-analytics-posthog 1.0.3 → 1.0.5
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 +4 -2
- package/dist/provider.d.ts +2 -0
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +44 -1
- package/dist/provider.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-
|
|
6
|
+
Generated: 2026-11-01T03:27:30.420Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/app-analytics-posthog
|
|
@@ -194,6 +194,8 @@ a bonds.ts setup function) does NOT re-configure the SDK: `posthog-js`'s
|
|
|
194
194
|
is a no-op that keeps the FIRST configuration. This logs one actionable
|
|
195
195
|
warning naming the ignored call before the SDK's own generic warning fires.
|
|
196
196
|
|
|
197
|
+
SDK-added location/referrer properties are scrubbed by the installed `before_send` hook.
|
|
198
|
+
|
|
197
199
|
```typescript
|
|
198
200
|
function createProvider(options?: PostHogOptions): AnalyticsProvider
|
|
199
201
|
```
|
|
@@ -243,7 +245,7 @@ if (apiKey) {
|
|
|
243
245
|
|
|
244
246
|
Peer dependencies:
|
|
245
247
|
|
|
246
|
-
- `@molecule/app-analytics` ^1.
|
|
248
|
+
- `@molecule/app-analytics` ^1.1.1
|
|
247
249
|
|
|
248
250
|
### Environment Variables
|
|
249
251
|
|
package/dist/provider.d.ts
CHANGED
|
@@ -28,6 +28,8 @@ import type { PostHogOptions } from './types.js';
|
|
|
28
28
|
* is a no-op that keeps the FIRST configuration. This logs one actionable
|
|
29
29
|
* warning naming the ignored call before the SDK's own generic warning fires.
|
|
30
30
|
*
|
|
31
|
+
* SDK-added location/referrer properties are scrubbed by the installed `before_send` hook.
|
|
32
|
+
*
|
|
31
33
|
* @param options - PostHog configuration including `apiKey`, optional `host`, and `autocapture`.
|
|
32
34
|
* @returns An `AnalyticsProvider` backed by the PostHog browser SDK.
|
|
33
35
|
*/
|
package/dist/provider.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,KAAK,EAGV,iBAAiB,EAElB,MAAM,yBAAyB,CAAA;
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,KAAK,EAGV,iBAAiB,EAElB,MAAM,yBAAyB,CAAA;AAGhC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAsChD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,cAAc,GAAI,UAAU,cAAc,KAAG,iBA+GzD,CAAA;AAID,6DAA6D;AAC7D,eAAO,MAAM,QAAQ,EAAE,iBAUrB,CAAA"}
|
package/dist/provider.js
CHANGED
|
@@ -6,6 +6,31 @@
|
|
|
6
6
|
* @module
|
|
7
7
|
*/
|
|
8
8
|
import { posthog } from 'posthog-js';
|
|
9
|
+
import { snapshotAnalyticsSdkUrls } from '@molecule/app-analytics';
|
|
10
|
+
/** Runs after posthog-js has added location, referrer, session and person properties. */
|
|
11
|
+
const scrubSdkUrls = (event) => snapshotAnalyticsSdkUrls(event);
|
|
12
|
+
const DETACHED_OBSERVERS = Symbol.for('@molecule/app-analytics-posthog/detached-observers');
|
|
13
|
+
/** The production ESM build minifies the emitter's field name, but not its public shape. */
|
|
14
|
+
const captureObserverEmitter = () => Object.values(Object.getOwnPropertyDescriptors(posthog))
|
|
15
|
+
.map((descriptor) => descriptor.value)
|
|
16
|
+
.find((value) => value !== null &&
|
|
17
|
+
typeof value === 'object' &&
|
|
18
|
+
typeof Reflect.get(value, 'emit') === 'function' &&
|
|
19
|
+
typeof Reflect.get(value, 'on') === 'function');
|
|
20
|
+
/** eventCaptured is an observer API: mutations must not reach the already-captured request. */
|
|
21
|
+
const detachCaptureObservers = () => {
|
|
22
|
+
const emitter = captureObserverEmitter();
|
|
23
|
+
if (!emitter || emitter[DETACHED_OBSERVERS])
|
|
24
|
+
return;
|
|
25
|
+
const emit = emitter.emit;
|
|
26
|
+
Object.defineProperty(emitter, DETACHED_OBSERVERS, { value: true });
|
|
27
|
+
emitter.emit = function (event, ...args) {
|
|
28
|
+
const captured = event === 'eventCaptured' && args.length > 0
|
|
29
|
+
? [snapshotAnalyticsSdkUrls(args[0]), ...args.slice(1)]
|
|
30
|
+
: args;
|
|
31
|
+
return Reflect.apply(emit, this, [event, ...captured]);
|
|
32
|
+
};
|
|
33
|
+
};
|
|
9
34
|
/**
|
|
10
35
|
* Creates a PostHog analytics provider using `posthog-js`. Initializes the SDK with the
|
|
11
36
|
* provided API key and host, and returns an `AnalyticsProvider` that maps molecule events to
|
|
@@ -27,6 +52,8 @@ import { posthog } from 'posthog-js';
|
|
|
27
52
|
* is a no-op that keeps the FIRST configuration. This logs one actionable
|
|
28
53
|
* warning naming the ignored call before the SDK's own generic warning fires.
|
|
29
54
|
*
|
|
55
|
+
* SDK-added location/referrer properties are scrubbed by the installed `before_send` hook.
|
|
56
|
+
*
|
|
30
57
|
* @param options - PostHog configuration including `apiKey`, optional `host`, and `autocapture`.
|
|
31
58
|
* @returns An `AnalyticsProvider` backed by the PostHog browser SDK.
|
|
32
59
|
*/
|
|
@@ -75,7 +102,22 @@ export const createProvider = (options) => {
|
|
|
75
102
|
...(options?.host ? { api_host: options.host } : {}),
|
|
76
103
|
autocapture: options?.autocapture ?? false,
|
|
77
104
|
capture_pageview: false, // We handle page views manually via auto-tracking
|
|
105
|
+
before_send: scrubSdkUrls,
|
|
78
106
|
});
|
|
107
|
+
// A pre-initialized singleton keeps its first config. Compose in that case and put this hook
|
|
108
|
+
// LAST so another hook cannot re-add a location/referrer credential after the scrub.
|
|
109
|
+
const configuredBeforeSend = posthog.config?.before_send;
|
|
110
|
+
if (configuredBeforeSend !== scrubSdkUrls && typeof posthog.set_config === 'function') {
|
|
111
|
+
const previous = Array.isArray(configuredBeforeSend)
|
|
112
|
+
? configuredBeforeSend
|
|
113
|
+
: configuredBeforeSend
|
|
114
|
+
? [configuredBeforeSend]
|
|
115
|
+
: [];
|
|
116
|
+
posthog.set_config({
|
|
117
|
+
before_send: [...previous.filter((hook) => hook !== scrubSdkUrls), scrubSdkUrls],
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
detachCaptureObservers();
|
|
79
121
|
return {
|
|
80
122
|
async identify(user) {
|
|
81
123
|
posthog.identify(user.userId, {
|
|
@@ -88,7 +130,8 @@ export const createProvider = (options) => {
|
|
|
88
130
|
// posthog-js supports a client-set timestamp via CaptureOptions — honor
|
|
89
131
|
// the interface's `timestamp` field instead of silently dropping it.
|
|
90
132
|
if (event.timestamp) {
|
|
91
|
-
|
|
133
|
+
const timestamp = event.timestamp instanceof Date ? event.timestamp : new Date(String(event.timestamp));
|
|
134
|
+
posthog.capture(event.name, event.properties, { timestamp });
|
|
92
135
|
}
|
|
93
136
|
else {
|
|
94
137
|
posthog.capture(event.name, event.properties);
|
package/dist/provider.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAsB,OAAO,EAAE,MAAM,YAAY,CAAA;AAQxD,OAAO,EAAE,w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AAI,EAAE,QAAQ,CAAC,CAAA;IAC/C,CAAC;IACD,uKAAuK;IACvK,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK;QAChB,IAAI,CAAC,SAAS;YAAE,SAAS,GAAG,cAAc,EAAE,CAAA;QAC5C,OAAO,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,IAAI,EAAE,KAAK,CAAC,CAAA;IAC5C,CAAC;CACF,CAAC,CAAA"}
|
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.5",
|
|
4
4
|
"description": "PostHog analytics provider for molecule.dev frontend",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -30,10 +30,10 @@
|
|
|
30
30
|
"posthog-js": "1.438.21"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@molecule/app-analytics": "^1.
|
|
33
|
+
"@molecule/app-analytics": "^1.1.1"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@molecule/app-analytics": "1.
|
|
36
|
+
"@molecule/app-analytics": "1.1.2",
|
|
37
37
|
"@types/node": "26.6.5",
|
|
38
38
|
"typescript": "6.0.3",
|
|
39
39
|
"vitest": "4.1.12"
|