@webority/analytics 0.2.1 → 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 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.2.1",
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
@@ -22,19 +22,28 @@ export function initAnalytics(config) {
22
22
  });
23
23
 
24
24
  if (isActive()) {
25
- ensurePosthogInitialized();
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 — re-grant must undo it.
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
- ensurePosthogInitialized();
46
+ warmUp();
38
47
  }
39
48
  return;
40
49
  }
@@ -93,7 +102,7 @@ export function resetIdentity() {
93
102
  export function registerEvents(names) {
94
103
  for (const name of names) {
95
104
  if (!EVENT_NAME_PATTERN.test(name)) {
96
- throw new Error(`@webority/analytics: invalid event name "${name}" — expected snake_case`);
105
+ throw new Error(`@webority/analytics: invalid event name "${name}". Expected snake_case.`);
97
106
  }
98
107
  state.registeredEvents.add(name);
99
108
  }
@@ -101,7 +110,7 @@ export function registerEvents(names) {
101
110
 
102
111
  export function trackEvent(name, props = {}) {
103
112
  if (!state.registeredEvents.has(name)) {
104
- throw new Error(`@webority/analytics: event "${name}" is not registered — call registerEvents() first`);
113
+ throw new Error(`@webority/analytics: event "${name}" is not registered. Call registerEvents() first.`);
105
114
  }
106
115
  capture(name, props);
107
116
  }
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() {