@molecule/app-analytics-posthog 1.0.0
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/LICENSE +115 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +52 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +37 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +134 -0
- package/dist/provider.js.map +1 -0
- package/dist/types.d.ts +15 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PostHog analytics provider for molecule.dev frontend.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { setProvider } from '@molecule/app-analytics'
|
|
7
|
+
* import { createProvider } from '@molecule/app-analytics-posthog'
|
|
8
|
+
*
|
|
9
|
+
* // Canonical wiring: read the browser-side key from Vite client env and pass
|
|
10
|
+
* // it through options. `importMetaEnv` stands for Vite's `import.meta.env` —
|
|
11
|
+
* // in your app write `import.meta.env?.VITE_POSTHOG_KEY` directly. Without a
|
|
12
|
+
* // key, skip bonding — analytics calls no-op.
|
|
13
|
+
* const apiKey = importMetaEnv?.VITE_POSTHOG_KEY as string | undefined
|
|
14
|
+
* const host = importMetaEnv?.VITE_POSTHOG_HOST as string | undefined
|
|
15
|
+
* if (apiKey) {
|
|
16
|
+
* setProvider(createProvider({ apiKey, ...(host ? { host } : {}) }))
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* The provider does NOT read env itself — configuration flows in through
|
|
22
|
+
* `createProvider(options)`. The canonical env names are `VITE_POSTHOG_KEY`
|
|
23
|
+
* and `VITE_POSTHOG_HOST` (the `VITE_` prefix is required: Vite only embeds
|
|
24
|
+
* `VITE_`-prefixed vars into the client bundle, and molecule's scaffolded app
|
|
25
|
+
* `.env` only includes `VITE_*` secrets). Do NOT use the API-side twin names
|
|
26
|
+
* (`POSTHOG_API_KEY`/`POSTHOG_HOST`) in frontend code — those belong to
|
|
27
|
+
* `@molecule/api-analytics-posthog` and never reach the browser. The PostHog
|
|
28
|
+
* project API key (`phc_...`) is a public browser-side credential, safe to
|
|
29
|
+
* embed client-side.
|
|
30
|
+
*
|
|
31
|
+
* Bonding without a key is failure-safe: `createProvider()` (and the lazy
|
|
32
|
+
* `provider` export, which cannot receive options) logs ONE console warning
|
|
33
|
+
* naming VITE_POSTHOG_KEY and returns a no-op provider instead of initializing
|
|
34
|
+
* the SDK with an empty key. If events never appear in PostHog, check the
|
|
35
|
+
* browser console for that warning FIRST. When no `host` is passed, the SDK's
|
|
36
|
+
* own default (PostHog Cloud US, `https://us.i.posthog.com`) applies — EU
|
|
37
|
+
* projects MUST set `VITE_POSTHOG_HOST=https://eu.i.posthog.com` or events are
|
|
38
|
+
* sent to the US region and silently never appear in the EU project.
|
|
39
|
+
*
|
|
40
|
+
* `createProvider()` called a second time (e.g. Vite HMR re-running a
|
|
41
|
+
* bonds.ts setup function) does NOT re-configure PostHog: `posthog-js`'s
|
|
42
|
+
* underlying client is a module-level singleton, so the second call's
|
|
43
|
+
* `host`/`autocapture`/etc. are silently ignored by the SDK — the FIRST
|
|
44
|
+
* configuration always wins. This logs one actionable console warning naming
|
|
45
|
+
* the ignored call before the SDK's own generic "already initialized"
|
|
46
|
+
* warning fires.
|
|
47
|
+
*
|
|
48
|
+
* @module
|
|
49
|
+
*/
|
|
50
|
+
export * from './provider.js';
|
|
51
|
+
export * from './types.js';
|
|
52
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AAEH,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PostHog analytics provider for molecule.dev frontend.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { setProvider } from '@molecule/app-analytics'
|
|
7
|
+
* import { createProvider } from '@molecule/app-analytics-posthog'
|
|
8
|
+
*
|
|
9
|
+
* // Canonical wiring: read the browser-side key from Vite client env and pass
|
|
10
|
+
* // it through options. `importMetaEnv` stands for Vite's `import.meta.env` —
|
|
11
|
+
* // in your app write `import.meta.env?.VITE_POSTHOG_KEY` directly. Without a
|
|
12
|
+
* // key, skip bonding — analytics calls no-op.
|
|
13
|
+
* const apiKey = importMetaEnv?.VITE_POSTHOG_KEY as string | undefined
|
|
14
|
+
* const host = importMetaEnv?.VITE_POSTHOG_HOST as string | undefined
|
|
15
|
+
* if (apiKey) {
|
|
16
|
+
* setProvider(createProvider({ apiKey, ...(host ? { host } : {}) }))
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* The provider does NOT read env itself — configuration flows in through
|
|
22
|
+
* `createProvider(options)`. The canonical env names are `VITE_POSTHOG_KEY`
|
|
23
|
+
* and `VITE_POSTHOG_HOST` (the `VITE_` prefix is required: Vite only embeds
|
|
24
|
+
* `VITE_`-prefixed vars into the client bundle, and molecule's scaffolded app
|
|
25
|
+
* `.env` only includes `VITE_*` secrets). Do NOT use the API-side twin names
|
|
26
|
+
* (`POSTHOG_API_KEY`/`POSTHOG_HOST`) in frontend code — those belong to
|
|
27
|
+
* `@molecule/api-analytics-posthog` and never reach the browser. The PostHog
|
|
28
|
+
* project API key (`phc_...`) is a public browser-side credential, safe to
|
|
29
|
+
* embed client-side.
|
|
30
|
+
*
|
|
31
|
+
* Bonding without a key is failure-safe: `createProvider()` (and the lazy
|
|
32
|
+
* `provider` export, which cannot receive options) logs ONE console warning
|
|
33
|
+
* naming VITE_POSTHOG_KEY and returns a no-op provider instead of initializing
|
|
34
|
+
* the SDK with an empty key. If events never appear in PostHog, check the
|
|
35
|
+
* browser console for that warning FIRST. When no `host` is passed, the SDK's
|
|
36
|
+
* own default (PostHog Cloud US, `https://us.i.posthog.com`) applies — EU
|
|
37
|
+
* projects MUST set `VITE_POSTHOG_HOST=https://eu.i.posthog.com` or events are
|
|
38
|
+
* sent to the US region and silently never appear in the EU project.
|
|
39
|
+
*
|
|
40
|
+
* `createProvider()` called a second time (e.g. Vite HMR re-running a
|
|
41
|
+
* bonds.ts setup function) does NOT re-configure PostHog: `posthog-js`'s
|
|
42
|
+
* underlying client is a module-level singleton, so the second call's
|
|
43
|
+
* `host`/`autocapture`/etc. are silently ignored by the SDK — the FIRST
|
|
44
|
+
* configuration always wins. This logs one actionable console warning naming
|
|
45
|
+
* the ignored call before the SDK's own generic "already initialized"
|
|
46
|
+
* warning fires.
|
|
47
|
+
*
|
|
48
|
+
* @module
|
|
49
|
+
*/
|
|
50
|
+
export * from './provider.js';
|
|
51
|
+
export * from './types.js';
|
|
52
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AAEH,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PostHog analytics provider for molecule.dev frontend.
|
|
3
|
+
*
|
|
4
|
+
* Uses the `posthog-js` browser SDK.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { AnalyticsProvider } from '@molecule/app-analytics';
|
|
9
|
+
import type { PostHogOptions } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Creates a PostHog analytics provider using `posthog-js`. Initializes the SDK with the
|
|
12
|
+
* provided API key and host, and returns an `AnalyticsProvider` that maps molecule events to
|
|
13
|
+
* PostHog's `capture`, `identify`, and `group` APIs.
|
|
14
|
+
*
|
|
15
|
+
* When no API key is provided, this does NOT initialize the SDK (`posthog.init('')`
|
|
16
|
+
* leaves the singleton uninitialized — the SDK logs its own generic error, but
|
|
17
|
+
* every subsequent call is silently dropped): it logs one actionable warning
|
|
18
|
+
* naming VITE_POSTHOG_KEY and returns a no-op provider, so bonding the lazy
|
|
19
|
+
* `provider` export without configuration degrades gracefully.
|
|
20
|
+
*
|
|
21
|
+
* When no `host` is provided, the SDK's own default (PostHog Cloud US,
|
|
22
|
+
* `https://us.i.posthog.com`) applies — EU projects must pass their region host
|
|
23
|
+
* (`https://eu.i.posthog.com`).
|
|
24
|
+
*
|
|
25
|
+
* Calling this a second time with a DIFFERENT config (e.g. Vite HMR re-running
|
|
26
|
+
* a bonds.ts setup function) does NOT re-configure the SDK: `posthog-js`'s
|
|
27
|
+
* `posthog` export is a module-level singleton, so the second `.init()` call
|
|
28
|
+
* is a no-op that keeps the FIRST configuration. This logs one actionable
|
|
29
|
+
* warning naming the ignored call before the SDK's own generic warning fires.
|
|
30
|
+
*
|
|
31
|
+
* @param options - PostHog configuration including `apiKey`, optional `host`, and `autocapture`.
|
|
32
|
+
* @returns An `AnalyticsProvider` backed by the PostHog browser SDK.
|
|
33
|
+
*/
|
|
34
|
+
export declare const createProvider: (options?: PostHogOptions) => AnalyticsProvider;
|
|
35
|
+
/** Lazy-initialized PostHog analytics provider singleton. */
|
|
36
|
+
export declare const provider: AnalyticsProvider;
|
|
37
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +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;AAEhC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAEhD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,cAAc,GAAI,UAAU,cAAc,KAAG,iBA8FzD,CAAA;AAID,6DAA6D;AAC7D,eAAO,MAAM,QAAQ,EAAE,iBAUrB,CAAA"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PostHog analytics provider for molecule.dev frontend.
|
|
3
|
+
*
|
|
4
|
+
* Uses the `posthog-js` browser SDK.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { posthog } from 'posthog-js';
|
|
9
|
+
/**
|
|
10
|
+
* Creates a PostHog analytics provider using `posthog-js`. Initializes the SDK with the
|
|
11
|
+
* provided API key and host, and returns an `AnalyticsProvider` that maps molecule events to
|
|
12
|
+
* PostHog's `capture`, `identify`, and `group` APIs.
|
|
13
|
+
*
|
|
14
|
+
* When no API key is provided, this does NOT initialize the SDK (`posthog.init('')`
|
|
15
|
+
* leaves the singleton uninitialized — the SDK logs its own generic error, but
|
|
16
|
+
* every subsequent call is silently dropped): it logs one actionable warning
|
|
17
|
+
* naming VITE_POSTHOG_KEY and returns a no-op provider, so bonding the lazy
|
|
18
|
+
* `provider` export without configuration degrades gracefully.
|
|
19
|
+
*
|
|
20
|
+
* When no `host` is provided, the SDK's own default (PostHog Cloud US,
|
|
21
|
+
* `https://us.i.posthog.com`) applies — EU projects must pass their region host
|
|
22
|
+
* (`https://eu.i.posthog.com`).
|
|
23
|
+
*
|
|
24
|
+
* Calling this a second time with a DIFFERENT config (e.g. Vite HMR re-running
|
|
25
|
+
* a bonds.ts setup function) does NOT re-configure the SDK: `posthog-js`'s
|
|
26
|
+
* `posthog` export is a module-level singleton, so the second `.init()` call
|
|
27
|
+
* is a no-op that keeps the FIRST configuration. This logs one actionable
|
|
28
|
+
* warning naming the ignored call before the SDK's own generic warning fires.
|
|
29
|
+
*
|
|
30
|
+
* @param options - PostHog configuration including `apiKey`, optional `host`, and `autocapture`.
|
|
31
|
+
* @returns An `AnalyticsProvider` backed by the PostHog browser SDK.
|
|
32
|
+
*/
|
|
33
|
+
export const createProvider = (options) => {
|
|
34
|
+
const apiKey = options?.apiKey ?? '';
|
|
35
|
+
if (!apiKey) {
|
|
36
|
+
// Graceful degradation with a molecule-canonical breadcrumb: the SDK's own
|
|
37
|
+
// "initialized without a token" error never names the env var this stack
|
|
38
|
+
// actually wires (VITE_POSTHOG_KEY), so debugging dead-ends there.
|
|
39
|
+
console.warn('[analytics] PostHog API key not configured — analytics events will NOT be delivered. ' +
|
|
40
|
+
'Pass createProvider({ apiKey }) (canonical source: import.meta.env.VITE_POSTHOG_KEY) ' +
|
|
41
|
+
'or skip bonding the provider entirely.');
|
|
42
|
+
const noop = async () => { };
|
|
43
|
+
return {
|
|
44
|
+
identify: noop,
|
|
45
|
+
track: noop,
|
|
46
|
+
page: noop,
|
|
47
|
+
group: noop,
|
|
48
|
+
reset: noop,
|
|
49
|
+
flush: noop,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
// posthog-js's `posthog` export is a MODULE-LEVEL singleton: calling
|
|
53
|
+
// `.init()` a second time (e.g. Vite HMR re-running a bonds.ts setup
|
|
54
|
+
// function, or a duplicate createProvider() call) does NOT create a second
|
|
55
|
+
// client — the SDK checks its own `__loaded` flag, logs a generic
|
|
56
|
+
// '[PostHog.js] You have already initialized PostHog! Re-initializing is
|
|
57
|
+
// a no-op' warning, and returns immediately WITHOUT applying the new
|
|
58
|
+
// config. A changed host/autocapture/etc. on the second call is therefore
|
|
59
|
+
// silently discarded — recoverable (the SDK does warn) but the warning
|
|
60
|
+
// never says WHICH options were ignored, so this molecule-canonical
|
|
61
|
+
// warning names the actual failure mode before the SDK's own generic one
|
|
62
|
+
// fires. (`posthog.__loaded` is undefined — falsy — on any stub that
|
|
63
|
+
// doesn't model it, e.g. the mocked SDK in provider.test.ts, so this never
|
|
64
|
+
// fires for the standard single-call test/usage path.)
|
|
65
|
+
if (posthog.__loaded) {
|
|
66
|
+
console.warn("[analytics] PostHog is already initialized — this createProvider() call's " +
|
|
67
|
+
'options (host/autocapture/etc.) will be IGNORED; the FIRST configuration wins. ' +
|
|
68
|
+
'This is expected if Vite HMR re-ran your bond setup; if not, createProvider() ' +
|
|
69
|
+
'is being called more than once with different options.');
|
|
70
|
+
}
|
|
71
|
+
posthog.init(apiKey, {
|
|
72
|
+
// Only override api_host when the caller supplies one — the SDK's own
|
|
73
|
+
// default is the CURRENT PostHog Cloud US endpoint (us.i.posthog.com);
|
|
74
|
+
// hardcoding the legacy app.posthog.com here would re-introduce drift.
|
|
75
|
+
...(options?.host ? { api_host: options.host } : {}),
|
|
76
|
+
autocapture: options?.autocapture ?? false,
|
|
77
|
+
capture_pageview: false, // We handle page views manually via auto-tracking
|
|
78
|
+
});
|
|
79
|
+
return {
|
|
80
|
+
async identify(user) {
|
|
81
|
+
posthog.identify(user.userId, {
|
|
82
|
+
email: user.email,
|
|
83
|
+
name: user.name,
|
|
84
|
+
...user.traits,
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
async track(event) {
|
|
88
|
+
// posthog-js supports a client-set timestamp via CaptureOptions — honor
|
|
89
|
+
// the interface's `timestamp` field instead of silently dropping it.
|
|
90
|
+
if (event.timestamp) {
|
|
91
|
+
posthog.capture(event.name, event.properties, { timestamp: event.timestamp });
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
posthog.capture(event.name, event.properties);
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
async page(pageView) {
|
|
98
|
+
posthog.capture('$pageview', {
|
|
99
|
+
$current_url: pageView.url,
|
|
100
|
+
$pathname: pageView.path,
|
|
101
|
+
$referrer: pageView.referrer,
|
|
102
|
+
page_name: pageView.name,
|
|
103
|
+
page_category: pageView.category,
|
|
104
|
+
...pageView.properties,
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
async group(groupId, traits) {
|
|
108
|
+
posthog.group('company', groupId, traits);
|
|
109
|
+
},
|
|
110
|
+
async reset() {
|
|
111
|
+
posthog.reset();
|
|
112
|
+
},
|
|
113
|
+
async flush() {
|
|
114
|
+
// posthog-js doesn't have an explicit flush — events are sent automatically
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
/** Default PostHog provider instance (lazy-initialized via Proxy on first method call). */
|
|
119
|
+
let _provider = null;
|
|
120
|
+
/** Lazy-initialized PostHog analytics provider singleton. */
|
|
121
|
+
export const provider = new Proxy({}, {
|
|
122
|
+
get(_, prop, receiver) {
|
|
123
|
+
if (!_provider)
|
|
124
|
+
_provider = createProvider();
|
|
125
|
+
return Reflect.get(_provider, prop, receiver);
|
|
126
|
+
},
|
|
127
|
+
// set trap: methods run with `this` bound to the proxy — without it, instance-state writes land on the dummy target and are lost (see api-push-notifications-web-push)
|
|
128
|
+
set(_, prop, value) {
|
|
129
|
+
if (!_provider)
|
|
130
|
+
_provider = createProvider();
|
|
131
|
+
return Reflect.set(_provider, prop, value);
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAWpC;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,OAAwB,EAAqB,EAAE;IAC5E,MAAM,MAAM,GAAG,OAAO,EAAE,MAAM,IAAI,EAAE,CAAA;IACpC,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,2EAA2E;QAC3E,yEAAyE;QACzE,mEAAmE;QACnE,OAAO,CAAC,IAAI,CACV,uFAAuF;YACrF,uFAAuF;YACvF,wCAAwC,CAC3C,CAAA;QACD,MAAM,IAAI,GAAG,KAAK,IAAmB,EAAE,GAAE,CAAC,CAAA;QAC1C,OAAO;YACL,QAAQ,EAAE,IAAI;YACd,KAAK,EAAE,IAAI;YACX,IAAI,EAAE,IAAI;YACV,KAAK,EAAE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AAG,CAAC,SAAS,EAAE,IAAI,EAAE,KAAK,CAAC,CAAA;IAC5C,CAAC;CACF,CAAC,CAAA"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PostHog app analytics types for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
export type { AnalyticsEvent, AnalyticsPageView, AnalyticsProvider, AnalyticsUserProps, } from '@molecule/app-analytics';
|
|
7
|
+
/**
|
|
8
|
+
* Options for creating a PostHog provider.
|
|
9
|
+
*/
|
|
10
|
+
export interface PostHogOptions {
|
|
11
|
+
apiKey?: string;
|
|
12
|
+
host?: string;
|
|
13
|
+
autocapture?: boolean;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,EACV,cAAc,EACd,iBAAiB,EACjB,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,yBAAyB,CAAA;AAEhC;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/app-analytics-posthog",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "PostHog analytics provider for molecule.dev frontend",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"molecule",
|
|
24
|
+
"analytics",
|
|
25
|
+
"posthog"
|
|
26
|
+
],
|
|
27
|
+
"license": "Apache-2.0",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"posthog-js": "1.408.1"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@molecule/app-analytics": "^1.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@molecule/app-analytics": "1.0.0",
|
|
36
|
+
"@types/node": "26.1.2",
|
|
37
|
+
"typescript": "6.0.3",
|
|
38
|
+
"vitest": "4.1.10"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
43
|
+
"directory": "packages/app/bonds/analytics/posthog"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/app/bonds/analytics/posthog",
|
|
46
|
+
"bugs": "https://github.com/molecule-dev/molecule/issues",
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
}
|
|
50
|
+
}
|