@webority/analytics 0.2.1 → 0.4.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.4.0",
4
+ "license": "MIT",
4
5
  "type": "module",
5
6
  "main": "src/index.js",
6
7
  "exports": {
@@ -10,15 +11,16 @@
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
- "dependencies": {
19
- "posthog-js": "1.417.1"
19
+ "peerDependencies": {
20
+ "posthog-js": "^1.417.1"
20
21
  },
21
22
  "devDependencies": {
22
- "@biomejs/biome": "2.5.8"
23
+ "@biomejs/biome": "2.5.8",
24
+ "posthog-js": "1.417.1"
23
25
  }
24
26
  }
package/src/analytics.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ensurePosthogInitialized } from "./posthog-bridge.js";
2
- import { hasConsent, isActive, isEligible, resetState, state } from "./state.js";
2
+ import { hasConsent, isActive, isEligible, resetState, state, WARNED_EVENT_LIMIT } from "./state.js";
3
3
  import { EVENT_NAME_PATTERN } from "./vocabulary.js";
4
4
 
5
5
  export function initAnalytics(config) {
@@ -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,11 +110,42 @@ 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
+ warnUnregisteredOnce(name);
114
+ return;
105
115
  }
106
116
  capture(name, props);
107
117
  }
108
118
 
119
+ // Parity with IAnalyticsClient.Track on the server half: the same developer mistake never
120
+ // throws there, it is logged and the event is dropped. A throw here runs inside the user's
121
+ // browser and can take out the handler that raised it, so the browser half must not be the
122
+ // harsher one. Once per name, because a misregistered event usually sits in a hot path.
123
+ function warnUnregisteredOnce(name) {
124
+ if (state.warnedUnregisteredEvents.has(name)) {
125
+ return;
126
+ }
127
+ if (state.warnedUnregisteredEvents.size < WARNED_EVENT_LIMIT) {
128
+ state.warnedUnregisteredEvents.add(name);
129
+ }
130
+
131
+ const logger = state.config?.logger;
132
+ const message = `@webority/analytics: event "${name}" is not registered, so it was dropped. Call registerEvents() first.`;
133
+ if (typeof logger?.warn === "function") {
134
+ logger.warn(message);
135
+ return;
136
+ }
137
+ if (typeof logger?.info === "function") {
138
+ logger.info(message);
139
+ return;
140
+ }
141
+
142
+ // The no-console rule exists to stop debug logging, and passing no logger is the default.
143
+ // Routing the one diagnostic this package emits to a logger nobody passed would hide exactly
144
+ // the mistake the warning exists to surface.
145
+ // biome-ignore lint/suspicious/noConsole: the package's one diagnostic must be visible by default
146
+ console.warn(message);
147
+ }
148
+
109
149
  function capture(name, props) {
110
150
  if (!isActive()) {
111
151
  logInactive(name, props);
@@ -1,7 +1,7 @@
1
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
- // never go active (e.g. Local environment) — it assumes browser globals at init time.
4
+ // never go active (e.g. Local environment), because it assumes browser globals at init time.
5
5
  export async function ensurePosthogInitialized() {
6
6
  if (state.posthogInstance) {
7
7
  return state.posthogInstance;
package/src/state.js CHANGED
@@ -6,14 +6,23 @@ export const state = {
6
6
  posthogInstance: null,
7
7
  posthogInitPromise: null,
8
8
  registeredEvents: new Set(Object.values(events)),
9
+ warnedUnregisteredEvents: new Set(),
9
10
  };
10
11
 
12
+ // The warning exists to reach a developer once. After this many distinct bad names the message
13
+ // has landed, and a product building names from data must not grow the set for the life of the tab.
14
+ export const WARNED_EVENT_LIMIT = 50;
15
+
16
+ // registeredEvents is deliberately not reset: an event name is part of the product's
17
+ // vocabulary, not of one analytics session, so a product that registers at module scope and
18
+ // starts analytics later in a bootstrap effect keeps its registrations. warnedUnregisteredEvents
19
+ // follows it for the same reason: the warning is about the code, not about one session, so
20
+ // re-initializing must not make the same mistake shout again.
11
21
  export function resetState(config) {
12
22
  state.config = config;
13
23
  state.consentOverride = null;
14
24
  state.posthogInstance = null;
15
25
  state.posthogInitPromise = null;
16
- state.registeredEvents = new Set(Object.values(events));
17
26
  }
18
27
 
19
28
  export function isEligible() {