@webority/analytics 0.2.0 → 0.3.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 +21 -0
- package/package.json +5 -4
- package/src/analytics.js +57 -13
- package/src/posthog-bridge.js +32 -8
- package/src/state.js +3 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Webority Technologies
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webority/analytics",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"license": "MIT",
|
|
4
5
|
"type": "module",
|
|
5
6
|
"main": "src/index.js",
|
|
6
7
|
"exports": {
|
|
@@ -10,9 +11,9 @@
|
|
|
10
11
|
"src"
|
|
11
12
|
],
|
|
12
13
|
"scripts": {
|
|
13
|
-
"lint": "biome lint ./src",
|
|
14
|
-
"format": "biome format --write ./src",
|
|
15
|
-
"check": "biome check ./src",
|
|
14
|
+
"lint": "biome lint ./src ./test",
|
|
15
|
+
"format": "biome format --write ./src ./test",
|
|
16
|
+
"check": "biome check ./src ./test",
|
|
16
17
|
"test": "node test/smoke.mjs"
|
|
17
18
|
},
|
|
18
19
|
"dependencies": {
|
package/src/analytics.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ensurePosthogInitialized } from "./posthog-bridge.js";
|
|
2
|
-
import { isActive, isEligible, resetState, state } from "./state.js";
|
|
2
|
+
import { hasConsent, isActive, isEligible, resetState, state } from "./state.js";
|
|
3
3
|
import { EVENT_NAME_PATTERN } from "./vocabulary.js";
|
|
4
4
|
|
|
5
5
|
export function initAnalytics(config) {
|
|
@@ -22,25 +22,55 @@ export function initAnalytics(config) {
|
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
if (isActive()) {
|
|
25
|
-
|
|
25
|
+
warmUp();
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
// Starts the loader without waiting for it. The rejection handler is what keeps a failed
|
|
30
|
+
// load inside the package: the bridge clears its cached promise before rethrowing, so the
|
|
31
|
+
// next event retries, and the product never sees an unhandled rejection.
|
|
32
|
+
function warmUp() {
|
|
33
|
+
ensurePosthogInitialized().catch(() => {
|
|
34
|
+
// Retried on the next call. Analytics must stay invisible to the product.
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
29
38
|
export function setConsent(granted) {
|
|
30
39
|
state.consentOverride = granted;
|
|
31
40
|
|
|
32
41
|
if (granted && isEligible()) {
|
|
33
|
-
// A prior revoke leaves an already-initialized instance opted out
|
|
42
|
+
// A prior revoke leaves an already-initialized instance opted out, so re-granting must undo it.
|
|
34
43
|
if (state.posthogInstance) {
|
|
35
44
|
state.posthogInstance.opt_in_capturing();
|
|
36
45
|
} else {
|
|
37
|
-
|
|
46
|
+
warmUp();
|
|
38
47
|
}
|
|
39
48
|
return;
|
|
40
49
|
}
|
|
41
50
|
|
|
42
|
-
if (!granted
|
|
51
|
+
if (!granted) {
|
|
52
|
+
// Revoking before init finishes must still opt out: the instance does not exist yet,
|
|
53
|
+
// and init would otherwise start capturing under the consent that has just been withdrawn.
|
|
54
|
+
optOutWhenReady();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function optOutWhenReady() {
|
|
59
|
+
if (state.posthogInstance) {
|
|
43
60
|
state.posthogInstance.opt_out_capturing();
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (state.posthogInitPromise) {
|
|
65
|
+
state.posthogInitPromise
|
|
66
|
+
.then((posthog) => {
|
|
67
|
+
if (!hasConsent()) {
|
|
68
|
+
posthog?.opt_out_capturing();
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
.catch(() => {
|
|
72
|
+
// Init failed on its own; nothing was capturing.
|
|
73
|
+
});
|
|
44
74
|
}
|
|
45
75
|
}
|
|
46
76
|
|
|
@@ -50,9 +80,16 @@ export function identify(publicId, props = {}) {
|
|
|
50
80
|
return;
|
|
51
81
|
}
|
|
52
82
|
|
|
53
|
-
ensurePosthogInitialized()
|
|
54
|
-
|
|
55
|
-
|
|
83
|
+
ensurePosthogInitialized()
|
|
84
|
+
.then((posthog) => {
|
|
85
|
+
if (!posthog || !hasConsent()) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
posthog.identify(publicId, props);
|
|
89
|
+
})
|
|
90
|
+
.catch(() => {
|
|
91
|
+
// Load failure is retried on the next call. Do not surface it as an unhandled rejection.
|
|
92
|
+
});
|
|
56
93
|
}
|
|
57
94
|
|
|
58
95
|
export function resetIdentity() {
|
|
@@ -65,7 +102,7 @@ export function resetIdentity() {
|
|
|
65
102
|
export function registerEvents(names) {
|
|
66
103
|
for (const name of names) {
|
|
67
104
|
if (!EVENT_NAME_PATTERN.test(name)) {
|
|
68
|
-
throw new Error(`@webority/analytics: invalid event name "${name}"
|
|
105
|
+
throw new Error(`@webority/analytics: invalid event name "${name}". Expected snake_case.`);
|
|
69
106
|
}
|
|
70
107
|
state.registeredEvents.add(name);
|
|
71
108
|
}
|
|
@@ -73,7 +110,7 @@ export function registerEvents(names) {
|
|
|
73
110
|
|
|
74
111
|
export function trackEvent(name, props = {}) {
|
|
75
112
|
if (!state.registeredEvents.has(name)) {
|
|
76
|
-
throw new Error(`@webority/analytics: event "${name}" is not registered
|
|
113
|
+
throw new Error(`@webority/analytics: event "${name}" is not registered. Call registerEvents() first.`);
|
|
77
114
|
}
|
|
78
115
|
capture(name, props);
|
|
79
116
|
}
|
|
@@ -92,9 +129,16 @@ function capture(name, props) {
|
|
|
92
129
|
surface: state.config.surface,
|
|
93
130
|
environment: state.config.environment,
|
|
94
131
|
};
|
|
95
|
-
ensurePosthogInitialized()
|
|
96
|
-
|
|
97
|
-
|
|
132
|
+
ensurePosthogInitialized()
|
|
133
|
+
.then((posthog) => {
|
|
134
|
+
if (!posthog || !hasConsent()) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
posthog.capture(name, merged);
|
|
138
|
+
})
|
|
139
|
+
.catch(() => {
|
|
140
|
+
// Load failure is retried on the next call. Do not surface it as an unhandled rejection.
|
|
141
|
+
});
|
|
98
142
|
}
|
|
99
143
|
|
|
100
144
|
function logInactive(name, props) {
|
package/src/posthog-bridge.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { state } from "./state.js";
|
|
1
|
+
import { hasConsent, state } from "./state.js";
|
|
2
2
|
|
|
3
3
|
// Dynamic import keeps posthog-js out of the module graph entirely for callers that
|
|
4
4
|
// never go active (e.g. Local environment) — it assumes browser globals at init time.
|
|
@@ -11,17 +11,41 @@ export async function ensurePosthogInitialized() {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
const config = state.config;
|
|
14
|
-
state.posthogInitPromise = import("posthog-js")
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
state.posthogInitPromise = import("posthog-js")
|
|
15
|
+
.then((module) => {
|
|
16
|
+
const posthog = module.default ?? module;
|
|
17
|
+
// Consent can flip while posthog-js is still downloading. Opting out in the init
|
|
18
|
+
// options stops the pageview init would send before a later opt_out_capturing() call.
|
|
19
|
+
const options = applyConsentToInitOptions(buildInitOptions(config), hasConsent());
|
|
20
|
+
posthog.init(config.clientKey, options);
|
|
21
|
+
if (!hasConsent()) {
|
|
22
|
+
posthog.opt_out_capturing();
|
|
23
|
+
}
|
|
24
|
+
posthog.register({ product: config.product, surface: config.surface });
|
|
25
|
+
state.posthogInstance = posthog;
|
|
26
|
+
return posthog;
|
|
27
|
+
})
|
|
28
|
+
.catch((error) => {
|
|
29
|
+
// A rejected init left in place would fail every later event without retrying.
|
|
30
|
+
state.posthogInitPromise = null;
|
|
31
|
+
throw error;
|
|
32
|
+
});
|
|
21
33
|
|
|
22
34
|
return state.posthogInitPromise;
|
|
23
35
|
}
|
|
24
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Copies init options and, when consent is absent, sets the flag that keeps init itself
|
|
39
|
+
* from capturing. Pure so the smoke test can lock the consent decision without a browser.
|
|
40
|
+
*/
|
|
41
|
+
export function applyConsentToInitOptions(options, consentGranted) {
|
|
42
|
+
if (consentGranted) {
|
|
43
|
+
return options;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return { ...options, opt_out_capturing_by_default: true };
|
|
47
|
+
}
|
|
48
|
+
|
|
25
49
|
/**
|
|
26
50
|
* The posthog-js init options for a config. Pure, so the smoke test can assert the mapping
|
|
27
51
|
* without a browser: replay stays off unless the product enabled it, and the masking choices
|
package/src/state.js
CHANGED
|
@@ -8,12 +8,14 @@ export const state = {
|
|
|
8
8
|
registeredEvents: new Set(Object.values(events)),
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
+
// registeredEvents is deliberately not reset: an event name is part of the product's
|
|
12
|
+
// vocabulary, not of one analytics session, so a product that registers at module scope and
|
|
13
|
+
// starts analytics later in a bootstrap effect keeps its registrations.
|
|
11
14
|
export function resetState(config) {
|
|
12
15
|
state.config = config;
|
|
13
16
|
state.consentOverride = null;
|
|
14
17
|
state.posthogInstance = null;
|
|
15
18
|
state.posthogInitPromise = null;
|
|
16
|
-
state.registeredEvents = new Set(Object.values(events));
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
export function isEligible() {
|