@arthurreira/analytics 0.13.0 → 0.15.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.
@@ -221,14 +221,11 @@ var AfAnalytics = (() => {
221
221
  );
222
222
  window.addEventListener("error", (e) => {
223
223
  enqueue((id) => {
224
- var _a, _b, _c, _d, _e, _f, _g;
224
+ var _a, _b, _c, _d;
225
225
  return trackException(apiUrl, apiKey, id, {
226
226
  exception_type: (_b = (_a = e.error) == null ? void 0 : _a.name) != null ? _b : "Error",
227
227
  exception_message: e.message,
228
- exception_filename: (_c = e.filename) != null ? _c : null,
229
- exception_lineno: (_d = e.lineno) != null ? _d : null,
230
- exception_colno: (_e = e.colno) != null ? _e : null,
231
- stack_trace: (_g = (_f = e.error) == null ? void 0 : _f.stack) != null ? _g : null
228
+ stack_trace: (_d = (_c = e.error) == null ? void 0 : _c.stack) != null ? _d : null
232
229
  });
233
230
  });
234
231
  });
@@ -239,9 +236,6 @@ var AfAnalytics = (() => {
239
236
  return trackException(apiUrl, apiKey, id, {
240
237
  exception_type: (_a = reason == null ? void 0 : reason.name) != null ? _a : "UnhandledRejection",
241
238
  exception_message: (_b = reason == null ? void 0 : reason.message) != null ? _b : String(reason),
242
- exception_filename: null,
243
- exception_lineno: null,
244
- exception_colno: null,
245
239
  stack_trace: (_c = reason == null ? void 0 : reason.stack) != null ? _c : null
246
240
  });
247
241
  });
package/dist/client.d.ts CHANGED
@@ -5,6 +5,12 @@ interface AnalyticsProps {
5
5
  }
6
6
  declare function Analytics({ apiKey, apiUrl, wsUrl }: AnalyticsProps): null;
7
7
 
8
+ interface ExceptionFields {
9
+ exception_type: string;
10
+ exception_message: string;
11
+ stack_trace: string | null;
12
+ }
13
+
8
14
  declare function useAnalytics(apiUrl: string, apiKey: string, options?: {
9
15
  wsUrl?: string;
10
16
  }): {
@@ -12,6 +18,7 @@ declare function useAnalytics(apiUrl: string, apiKey: string, options?: {
12
18
  trackClick: (e: MouseEvent, element: HTMLElement) => void;
13
19
  trackScroll: (depth: number) => void;
14
20
  trackCopy: () => void;
21
+ trackException: (fields: ExceptionFields) => void;
15
22
  trackError: (error: Error) => void;
16
23
  trackCTA: (ctaId: string, ctaVariant?: string) => void;
17
24
  trackSearch: (query: string) => void;
package/dist/client.js CHANGED
@@ -109,11 +109,17 @@ async function trackSearch(apiUrl, apiKey, sessionId, path, query) {
109
109
  search_query: query
110
110
  });
111
111
  }
112
- async function trackError(apiUrl, apiKey, sessionId, path, error) {
112
+ async function trackException(apiUrl, apiKey, sessionId, path, fields) {
113
113
  await sendEvent(apiUrl, apiKey, {
114
- ...BASE_FIELDS(sessionId, "error", path),
115
- error_message: error.message,
116
- stack_trace: error.stack || null
114
+ ...BASE_FIELDS(sessionId, "js_exception", path),
115
+ ...fields
116
+ });
117
+ }
118
+ async function trackError(apiUrl, apiKey, sessionId, path, error) {
119
+ await trackException(apiUrl, apiKey, sessionId, path, {
120
+ exception_type: error.name ?? "Error",
121
+ exception_message: error.message,
122
+ stack_trace: error.stack ?? null
117
123
  });
118
124
  }
119
125
  async function trackCTA(apiUrl, apiKey, sessionId, path, ctaId, ctaVariant) {
@@ -180,6 +186,7 @@ function connectPresence(apiKey, sessionId, wsUrl = DEFAULT_WS_URL) {
180
186
 
181
187
  // src/hooks/useAnalytics.ts
182
188
  var SESSION_EXPIRY_MINUTES = 30;
189
+ var _sessionFlight = null;
183
190
  async function getOrCreateSession(apiUrl, apiKey, visitorId) {
184
191
  const storedSessionId = localStorage.getItem("af_session_id");
185
192
  const lastActivity = localStorage.getItem("af_session_last_activity");
@@ -191,10 +198,17 @@ async function getOrCreateSession(apiUrl, apiKey, visitorId) {
191
198
  return storedSessionId;
192
199
  }
193
200
  }
194
- const newSessionId = await createSession(apiUrl, apiKey, visitorId);
195
- localStorage.setItem("af_session_id", newSessionId);
196
- localStorage.setItem("af_session_last_activity", String(now));
197
- return newSessionId;
201
+ if (_sessionFlight) return _sessionFlight;
202
+ _sessionFlight = createSession(apiUrl, apiKey, visitorId).then((id) => {
203
+ localStorage.setItem("af_session_id", id);
204
+ localStorage.setItem("af_session_last_activity", String(Date.now()));
205
+ _sessionFlight = null;
206
+ return id;
207
+ }).catch((err) => {
208
+ _sessionFlight = null;
209
+ throw err;
210
+ });
211
+ return _sessionFlight;
198
212
  }
199
213
  function useAnalytics(apiUrl, apiKey, options) {
200
214
  const sessionId = useRef(null);
@@ -261,6 +275,9 @@ function useAnalytics(apiUrl, apiKey, options) {
261
275
  trackCopy: () => {
262
276
  enqueueOrRun(() => trackCopy(apiUrl, apiKey, sessionId.current, pathname.current));
263
277
  },
278
+ trackException: (fields) => {
279
+ enqueueOrRun(() => trackException(apiUrl, apiKey, sessionId.current, pathname.current, fields));
280
+ },
264
281
  trackError: (error) => {
265
282
  enqueueOrRun(() => trackError(apiUrl, apiKey, sessionId.current, pathname.current, error));
266
283
  },
@@ -275,7 +292,7 @@ function useAnalytics(apiUrl, apiKey, options) {
275
292
 
276
293
  // src/components/Analytics.tsx
277
294
  function Analytics({ apiKey, apiUrl, wsUrl }) {
278
- const { trackPageview: trackPageview2, trackClick: trackClick2, trackScroll: trackScroll2, trackCopy: trackCopy2, trackError: trackError2 } = useAnalytics(apiUrl, apiKey, { wsUrl });
295
+ const { trackPageview: trackPageview2, trackClick: trackClick2, trackScroll: trackScroll2, trackCopy: trackCopy2, trackException: trackException2 } = useAnalytics(apiUrl, apiKey, { wsUrl });
279
296
  const lastTracked = useRef2(null);
280
297
  const lastScrollDepth = useRef2(0);
281
298
  const pathname = typeof window !== "undefined" ? window.location.pathname : "";
@@ -316,10 +333,27 @@ function Analytics({ apiKey, apiUrl, wsUrl }) {
316
333
  }, [trackCopy2]);
317
334
  useEffect2(() => {
318
335
  if (typeof window === "undefined") return;
319
- const handler = (e) => trackError2(new Error(e.message));
336
+ const handler = (e) => trackException2({
337
+ exception_type: e.error?.name ?? "Error",
338
+ exception_message: e.message,
339
+ stack_trace: e.error?.stack ?? null
340
+ });
320
341
  window.addEventListener("error", handler);
321
342
  return () => window.removeEventListener("error", handler);
322
- }, [trackError2]);
343
+ }, [trackException2]);
344
+ useEffect2(() => {
345
+ if (typeof window === "undefined") return;
346
+ const handler = (e) => {
347
+ const reason = e.reason;
348
+ trackException2({
349
+ exception_type: reason?.name ?? "UnhandledRejection",
350
+ exception_message: reason?.message ?? String(reason),
351
+ stack_trace: reason?.stack ?? null
352
+ });
353
+ };
354
+ window.addEventListener("unhandledrejection", handler);
355
+ return () => window.removeEventListener("unhandledrejection", handler);
356
+ }, [trackException2]);
323
357
  return null;
324
358
  }
325
359
  export {
package/dist/index.d.ts CHANGED
@@ -4,6 +4,12 @@ declare function trackClick(apiUrl: string, apiKey: string, sessionId: string, p
4
4
  declare function trackScroll(apiUrl: string, apiKey: string, sessionId: string, path: string, depth: number): Promise<void>;
5
5
  declare function trackCopy(apiUrl: string, apiKey: string, sessionId: string, path: string): Promise<void>;
6
6
  declare function trackSearch(apiUrl: string, apiKey: string, sessionId: string, path: string, query: string): Promise<void>;
7
+ interface ExceptionFields {
8
+ exception_type: string;
9
+ exception_message: string;
10
+ stack_trace: string | null;
11
+ }
12
+ declare function trackException(apiUrl: string, apiKey: string, sessionId: string, path: string, fields: ExceptionFields): Promise<void>;
7
13
  declare function trackError(apiUrl: string, apiKey: string, sessionId: string, path: string, error: Error): Promise<void>;
8
14
  declare function trackCTA(apiUrl: string, apiKey: string, sessionId: string, path: string, ctaId: string, ctaVariant?: string): Promise<void>;
9
15
 
@@ -13,4 +19,4 @@ interface PresenceConnection {
13
19
  }
14
20
  declare function connectPresence(apiKey: string, sessionId: string, wsUrl?: string): PresenceConnection;
15
21
 
16
- export { DEFAULT_WS_URL, type PresenceConnection, connectPresence, createSession, trackCTA, trackClick, trackCopy, trackError, trackPageview, trackScroll, trackSearch };
22
+ export { DEFAULT_WS_URL, type ExceptionFields, type PresenceConnection, connectPresence, createSession, trackCTA, trackClick, trackCopy, trackError, trackException, trackPageview, trackScroll, trackSearch };
package/dist/index.js CHANGED
@@ -100,11 +100,17 @@ async function trackSearch(apiUrl, apiKey, sessionId, path, query) {
100
100
  search_query: query
101
101
  });
102
102
  }
103
- async function trackError(apiUrl, apiKey, sessionId, path, error) {
103
+ async function trackException(apiUrl, apiKey, sessionId, path, fields) {
104
104
  await sendEvent(apiUrl, apiKey, {
105
- ...BASE_FIELDS(sessionId, "error", path),
106
- error_message: error.message,
107
- stack_trace: error.stack || null
105
+ ...BASE_FIELDS(sessionId, "js_exception", path),
106
+ ...fields
107
+ });
108
+ }
109
+ async function trackError(apiUrl, apiKey, sessionId, path, error) {
110
+ await trackException(apiUrl, apiKey, sessionId, path, {
111
+ exception_type: error.name ?? "Error",
112
+ exception_message: error.message,
113
+ stack_trace: error.stack ?? null
108
114
  });
109
115
  }
110
116
  async function trackCTA(apiUrl, apiKey, sessionId, path, ctaId, ctaVariant) {
@@ -176,6 +182,7 @@ export {
176
182
  trackClick,
177
183
  trackCopy,
178
184
  trackError,
185
+ trackException,
179
186
  trackPageview,
180
187
  trackScroll,
181
188
  trackSearch
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  { "name": "@arthurreira/analytics",
2
- "version": "0.13.0",
2
+ "version": "0.15.0",
3
3
  "type": "module",
4
4
  "scripts": {
5
5
  "build": "tsup",