@arthurreira/analytics 0.19.0 → 0.20.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.
@@ -72,12 +72,13 @@ var AfAnalytics = (() => {
72
72
  var BC_CHANNEL = "af_analytics_session";
73
73
  var BC_ADOPT_TIMEOUT_MS = 50;
74
74
  function readConfig() {
75
- var _a, _b, _c, _d;
75
+ var _a, _b, _c, _d, _e, _f;
76
76
  const script = _currentScript != null ? _currentScript : document.querySelector("script[data-api-key]");
77
77
  const apiKey = (_b = (_a = script == null ? void 0 : script.dataset.apiKey) == null ? void 0 : _a.trim()) != null ? _b : "";
78
78
  const apiUrl = ((_d = (_c = script == null ? void 0 : script.dataset.url) == null ? void 0 : _c.trim()) != null ? _d : "").replace(/\/$/, "");
79
79
  if (!apiKey || !apiUrl) return null;
80
- return { apiKey, apiUrl };
80
+ const wsUrl = (_f = (_e = script == null ? void 0 : script.dataset.wsUrl) == null ? void 0 : _e.trim()) != null ? _f : apiUrl.replace(/^https:\/\//, "wss://").replace(/^http:\/\//, "ws://") + "/realtime";
81
+ return { apiKey, apiUrl, wsUrl };
81
82
  }
82
83
  function generateId() {
83
84
  if (typeof crypto !== "undefined" && crypto.randomUUID) {
@@ -184,7 +185,7 @@ var AfAnalytics = (() => {
184
185
  return () => channel.close();
185
186
  }
186
187
  async function createRemoteSession(apiUrl, apiKey, visitorId) {
187
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
188
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
188
189
  const nav = navigator;
189
190
  const conn = (_c = (_b = (_a = nav["connection"]) != null ? _a : nav["mozConnection"]) != null ? _b : nav["webkitConnection"]) != null ? _c : null;
190
191
  const params = new URLSearchParams(window.location.search);
@@ -209,14 +210,22 @@ var AfAnalytics = (() => {
209
210
  utm_term: params.get("utm_term"),
210
211
  utm_content: params.get("utm_content")
211
212
  };
212
- const res = await fetch(`${apiUrl}/sessions`, {
213
- method: "POST",
214
- headers: { "X-API-Key": apiKey, "Content-Type": "application/json" },
215
- body: JSON.stringify(body)
216
- });
217
- if (!res.ok) return "";
218
- const data = await res.json();
219
- return data.id;
213
+ for (let attempt = 0; attempt < 3; attempt++) {
214
+ try {
215
+ const res = await fetch(`${apiUrl}/sessions`, {
216
+ method: "POST",
217
+ headers: { "X-API-Key": apiKey, "Content-Type": "application/json" },
218
+ body: JSON.stringify(body)
219
+ });
220
+ if (res.ok) {
221
+ const data = await res.json();
222
+ return (_o = (_n = data.session_id) != null ? _n : data.id) != null ? _o : "";
223
+ }
224
+ } catch (e) {
225
+ }
226
+ if (attempt < 2) await new Promise((r) => setTimeout(r, 500 * 2 ** attempt));
227
+ }
228
+ return "";
220
229
  }
221
230
  async function getOrCreateSession(apiUrl, apiKey) {
222
231
  const stored = getStoredSession();
package/dist/client.js CHANGED
@@ -154,8 +154,7 @@ async function trackCTA(apiUrl, apiKey, sessionId, path, ctaId, ctaVariant) {
154
154
  }
155
155
 
156
156
  // src/lib/presence.ts
157
- var DEFAULT_WS_URL = "wss://edge.arthurreira.dev/realtime";
158
- function connectPresence(apiKey, sessionId, wsUrl = DEFAULT_WS_URL) {
157
+ function connectPresence(apiKey, sessionId, wsUrl) {
159
158
  if (typeof WebSocket === "undefined") {
160
159
  return { disconnect: () => {
161
160
  } };
package/dist/index.d.ts CHANGED
@@ -22,10 +22,9 @@ declare function trackException(apiUrl: string, apiKey: string, sessionId: strin
22
22
  declare function trackError(apiUrl: string, apiKey: string, sessionId: string, path: string, error: Error): Promise<void>;
23
23
  declare function trackCTA(apiUrl: string, apiKey: string, sessionId: string, path: string, ctaId: string, ctaVariant?: string): Promise<void>;
24
24
 
25
- declare const DEFAULT_WS_URL = "wss://edge.arthurreira.dev/realtime";
26
25
  interface PresenceConnection {
27
26
  disconnect: () => void;
28
27
  }
29
- declare function connectPresence(apiKey: string, sessionId: string, wsUrl?: string): PresenceConnection;
28
+ declare function connectPresence(apiKey: string, sessionId: string, wsUrl: string): PresenceConnection;
30
29
 
31
- export { DEFAULT_WS_URL, type ExceptionFields, type PresenceConnection, connectPresence, consent, createSession, isOptedOut, optIn, optOut, trackCTA, trackClick, trackCopy, trackError, trackException, trackPageview, trackScroll, trackSearch };
30
+ export { type ExceptionFields, type PresenceConnection, connectPresence, consent, createSession, isOptedOut, optIn, optOut, trackCTA, trackClick, trackCopy, trackError, trackException, trackPageview, trackScroll, trackSearch };
package/dist/index.js CHANGED
@@ -156,8 +156,7 @@ async function trackCTA(apiUrl, apiKey, sessionId, path, ctaId, ctaVariant) {
156
156
  }
157
157
 
158
158
  // src/lib/presence.ts
159
- var DEFAULT_WS_URL = "wss://edge.arthurreira.dev/realtime";
160
- function connectPresence(apiKey, sessionId, wsUrl = DEFAULT_WS_URL) {
159
+ function connectPresence(apiKey, sessionId, wsUrl) {
161
160
  if (typeof WebSocket === "undefined") {
162
161
  return { disconnect: () => {
163
162
  } };
@@ -226,7 +225,6 @@ function connectPresence(apiKey, sessionId, wsUrl = DEFAULT_WS_URL) {
226
225
  };
227
226
  }
228
227
  export {
229
- DEFAULT_WS_URL,
230
228
  connectPresence,
231
229
  consent,
232
230
  createSession,
package/package.json CHANGED
@@ -1,12 +1,7 @@
1
1
  {
2
2
  "name": "@arthurreira/analytics",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "type": "module",
5
- "scripts": {
6
- "build": "tsup",
7
- "prepare": "tsup",
8
- "dev": "tsup src/index.ts --format esm --dts --watch"
9
- },
10
5
  "exports": {
11
6
  ".": {
12
7
  "types": "./dist/index.d.ts",
@@ -37,5 +32,9 @@
37
32
  "peerDependencies": {
38
33
  "next": "^14.0.0 || ^15.0.0 || ^16.0.0",
39
34
  "react": "^18.0.0 || ^19.0.0"
35
+ },
36
+ "scripts": {
37
+ "build": "tsup",
38
+ "dev": "tsup src/index.ts --format esm --dts --watch"
40
39
  }
41
40
  }