@webority/analytics 0.1.1 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webority/analytics",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "main": "src/index.js",
6
6
  "exports": {
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) {
@@ -12,6 +12,13 @@ export function initAnalytics(config) {
12
12
  enabled: config.enabled ?? true,
13
13
  getConsent: config.getConsent ?? null,
14
14
  logger: config.logger ?? null,
15
+ // Off unless the product asks: replay records page content, so turning it on is the
16
+ // product's disclosed decision, never a package default.
17
+ sessionRecording: {
18
+ enabled: config.sessionRecording?.enabled ?? false,
19
+ maskAllInputs: config.sessionRecording?.maskAllInputs ?? true,
20
+ maskTextSelector: config.sessionRecording?.maskTextSelector ?? null,
21
+ },
15
22
  });
16
23
 
17
24
  if (isActive()) {
@@ -32,8 +39,29 @@ export function setConsent(granted) {
32
39
  return;
33
40
  }
34
41
 
35
- if (!granted && state.posthogInstance) {
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) {
36
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
+ });
37
65
  }
38
66
  }
39
67
 
@@ -43,9 +71,16 @@ export function identify(publicId, props = {}) {
43
71
  return;
44
72
  }
45
73
 
46
- ensurePosthogInitialized().then((posthog) => {
47
- posthog.identify(publicId, props);
48
- });
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
+ });
49
84
  }
50
85
 
51
86
  export function resetIdentity() {
@@ -85,9 +120,16 @@ function capture(name, props) {
85
120
  surface: state.config.surface,
86
121
  environment: state.config.environment,
87
122
  };
88
- ensurePosthogInitialized().then((posthog) => {
89
- posthog.capture(name, merged);
90
- });
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
+ });
91
133
  }
92
134
 
93
135
  function logInactive(name, props) {
@@ -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,18 +11,60 @@ export async function ensurePosthogInitialized() {
11
11
  }
12
12
 
13
13
  const config = state.config;
14
- state.posthogInitPromise = import("posthog-js").then((module) => {
15
- const posthog = module.default ?? module;
16
- posthog.init(config.clientKey, {
17
- api_host: config.host,
18
- autocapture: false,
19
- capture_pageview: true,
20
- persistence: "localStorage",
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;
21
32
  });
22
- posthog.register({ product: config.product, surface: config.surface });
23
- state.posthogInstance = posthog;
24
- return posthog;
25
- });
26
33
 
27
34
  return state.posthogInitPromise;
28
35
  }
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
+
49
+ /**
50
+ * The posthog-js init options for a config. Pure, so the smoke test can assert the mapping
51
+ * without a browser: replay stays off unless the product enabled it, and the masking choices
52
+ * ride through only when it is on.
53
+ */
54
+ export function buildInitOptions(config) {
55
+ const recording = config.sessionRecording ?? { enabled: false };
56
+ const options = {
57
+ api_host: config.host,
58
+ autocapture: false,
59
+ capture_pageview: true,
60
+ persistence: "localStorage",
61
+ disable_session_recording: !recording.enabled,
62
+ };
63
+ if (recording.enabled) {
64
+ options.session_recording = {
65
+ maskAllInputs: recording.maskAllInputs ?? true,
66
+ ...(recording.maskTextSelector ? { maskTextSelector: recording.maskTextSelector } : {}),
67
+ };
68
+ }
69
+ return options;
70
+ }