@webority/analytics 0.2.0 → 0.2.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/package.json +1 -1
- package/src/analytics.js +43 -8
- package/src/posthog-bridge.js +32 -8
package/package.json
CHANGED
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) {
|
|
@@ -39,8 +39,29 @@ export function setConsent(granted) {
|
|
|
39
39
|
return;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
if (!granted
|
|
42
|
+
if (!granted) {
|
|
43
|
+
// Revoking before init finishes must still opt out: the instance does not exist yet,
|
|
44
|
+
// and init would otherwise start capturing under the consent that has just been withdrawn.
|
|
45
|
+
optOutWhenReady();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function optOutWhenReady() {
|
|
50
|
+
if (state.posthogInstance) {
|
|
43
51
|
state.posthogInstance.opt_out_capturing();
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (state.posthogInitPromise) {
|
|
56
|
+
state.posthogInitPromise
|
|
57
|
+
.then((posthog) => {
|
|
58
|
+
if (!hasConsent()) {
|
|
59
|
+
posthog?.opt_out_capturing();
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
.catch(() => {
|
|
63
|
+
// Init failed on its own; nothing was capturing.
|
|
64
|
+
});
|
|
44
65
|
}
|
|
45
66
|
}
|
|
46
67
|
|
|
@@ -50,9 +71,16 @@ export function identify(publicId, props = {}) {
|
|
|
50
71
|
return;
|
|
51
72
|
}
|
|
52
73
|
|
|
53
|
-
ensurePosthogInitialized()
|
|
54
|
-
|
|
55
|
-
|
|
74
|
+
ensurePosthogInitialized()
|
|
75
|
+
.then((posthog) => {
|
|
76
|
+
if (!posthog || !hasConsent()) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
posthog.identify(publicId, props);
|
|
80
|
+
})
|
|
81
|
+
.catch(() => {
|
|
82
|
+
// Load failure is retried on the next call. Do not surface it as an unhandled rejection.
|
|
83
|
+
});
|
|
56
84
|
}
|
|
57
85
|
|
|
58
86
|
export function resetIdentity() {
|
|
@@ -92,9 +120,16 @@ function capture(name, props) {
|
|
|
92
120
|
surface: state.config.surface,
|
|
93
121
|
environment: state.config.environment,
|
|
94
122
|
};
|
|
95
|
-
ensurePosthogInitialized()
|
|
96
|
-
|
|
97
|
-
|
|
123
|
+
ensurePosthogInitialized()
|
|
124
|
+
.then((posthog) => {
|
|
125
|
+
if (!posthog || !hasConsent()) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
posthog.capture(name, merged);
|
|
129
|
+
})
|
|
130
|
+
.catch(() => {
|
|
131
|
+
// Load failure is retried on the next call. Do not surface it as an unhandled rejection.
|
|
132
|
+
});
|
|
98
133
|
}
|
|
99
134
|
|
|
100
135
|
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
|