@webority/analytics 0.3.0 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webority/analytics",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -16,10 +16,11 @@
16
16
  "check": "biome check ./src ./test",
17
17
  "test": "node test/smoke.mjs"
18
18
  },
19
- "dependencies": {
20
- "posthog-js": "1.417.1"
19
+ "peerDependencies": {
20
+ "posthog-js": "^1.417.1"
21
21
  },
22
22
  "devDependencies": {
23
- "@biomejs/biome": "2.5.8"
23
+ "@biomejs/biome": "2.5.8",
24
+ "posthog-js": "1.417.1"
24
25
  }
25
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) {
@@ -110,11 +110,42 @@ export function registerEvents(names) {
110
110
 
111
111
  export function trackEvent(name, props = {}) {
112
112
  if (!state.registeredEvents.has(name)) {
113
- throw new Error(`@webority/analytics: event "${name}" is not registered. Call registerEvents() first.`);
113
+ warnUnregisteredOnce(name);
114
+ return;
114
115
  }
115
116
  capture(name, props);
116
117
  }
117
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
+
118
149
  function capture(name, props) {
119
150
  if (!isActive()) {
120
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,11 +6,18 @@ 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
+
11
16
  // registeredEvents is deliberately not reset: an event name is part of the product's
12
17
  // 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.
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.
14
21
  export function resetState(config) {
15
22
  state.config = config;
16
23
  state.consentOverride = null;