@arthurreira/analytics 0.14.0 → 0.16.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/dist/af-analytics.umd.js +2 -8
- package/dist/client.d.ts +2 -8
- package/dist/client.js +3 -70
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -3
- package/package.json +38 -32
package/dist/af-analytics.umd.js
CHANGED
|
@@ -221,14 +221,11 @@ var AfAnalytics = (() => {
|
|
|
221
221
|
);
|
|
222
222
|
window.addEventListener("error", (e) => {
|
|
223
223
|
enqueue((id) => {
|
|
224
|
-
var _a, _b, _c, _d
|
|
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
|
-
|
|
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
|
@@ -1,22 +1,16 @@
|
|
|
1
1
|
interface AnalyticsProps {
|
|
2
2
|
apiKey: string;
|
|
3
3
|
apiUrl: string;
|
|
4
|
-
wsUrl?: string;
|
|
5
4
|
}
|
|
6
|
-
declare function Analytics({ apiKey, apiUrl
|
|
5
|
+
declare function Analytics({ apiKey, apiUrl }: AnalyticsProps): null;
|
|
7
6
|
|
|
8
7
|
interface ExceptionFields {
|
|
9
8
|
exception_type: string;
|
|
10
9
|
exception_message: string;
|
|
11
|
-
exception_filename: string | null;
|
|
12
|
-
exception_lineno: number | null;
|
|
13
|
-
exception_colno: number | null;
|
|
14
10
|
stack_trace: string | null;
|
|
15
11
|
}
|
|
16
12
|
|
|
17
|
-
declare function useAnalytics(apiUrl: string, apiKey: string
|
|
18
|
-
wsUrl?: string;
|
|
19
|
-
}): {
|
|
13
|
+
declare function useAnalytics(apiUrl: string, apiKey: string): {
|
|
20
14
|
trackPageview: (path: string) => void;
|
|
21
15
|
trackClick: (e: MouseEvent, element: HTMLElement) => void;
|
|
22
16
|
trackScroll: (depth: number) => void;
|
package/dist/client.js
CHANGED
|
@@ -119,9 +119,6 @@ async function trackError(apiUrl, apiKey, sessionId, path, error) {
|
|
|
119
119
|
await trackException(apiUrl, apiKey, sessionId, path, {
|
|
120
120
|
exception_type: error.name ?? "Error",
|
|
121
121
|
exception_message: error.message,
|
|
122
|
-
exception_filename: null,
|
|
123
|
-
exception_lineno: null,
|
|
124
|
-
exception_colno: null,
|
|
125
122
|
stack_trace: error.stack ?? null
|
|
126
123
|
});
|
|
127
124
|
}
|
|
@@ -133,60 +130,6 @@ async function trackCTA(apiUrl, apiKey, sessionId, path, ctaId, ctaVariant) {
|
|
|
133
130
|
});
|
|
134
131
|
}
|
|
135
132
|
|
|
136
|
-
// src/lib/presence.ts
|
|
137
|
-
var DEFAULT_WS_URL = "wss://edge.arthurreira.dev/realtime";
|
|
138
|
-
function connectPresence(apiKey, sessionId, wsUrl = DEFAULT_WS_URL) {
|
|
139
|
-
if (typeof WebSocket === "undefined") {
|
|
140
|
-
return { disconnect: () => {
|
|
141
|
-
} };
|
|
142
|
-
}
|
|
143
|
-
let ws = null;
|
|
144
|
-
let attempts = 0;
|
|
145
|
-
let intentionalClose = false;
|
|
146
|
-
let retryTimer = null;
|
|
147
|
-
let targetUrl;
|
|
148
|
-
try {
|
|
149
|
-
const u = new URL(wsUrl);
|
|
150
|
-
u.searchParams.set("api_key", apiKey);
|
|
151
|
-
u.searchParams.set("session_id", sessionId);
|
|
152
|
-
targetUrl = u.toString();
|
|
153
|
-
} catch {
|
|
154
|
-
return { disconnect: () => {
|
|
155
|
-
} };
|
|
156
|
-
}
|
|
157
|
-
function connect() {
|
|
158
|
-
try {
|
|
159
|
-
ws = new WebSocket(targetUrl);
|
|
160
|
-
} catch {
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
ws.onopen = () => {
|
|
164
|
-
attempts = 0;
|
|
165
|
-
ws.send(JSON.stringify({ type: "join", api_key: apiKey, session_id: sessionId }));
|
|
166
|
-
};
|
|
167
|
-
ws.onclose = () => {
|
|
168
|
-
if (intentionalClose || attempts >= 3) return;
|
|
169
|
-
const delay = Math.pow(2, attempts) * 1e3;
|
|
170
|
-
attempts++;
|
|
171
|
-
retryTimer = setTimeout(connect, delay);
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
connect();
|
|
175
|
-
return {
|
|
176
|
-
disconnect: () => {
|
|
177
|
-
intentionalClose = true;
|
|
178
|
-
if (retryTimer !== null) {
|
|
179
|
-
clearTimeout(retryTimer);
|
|
180
|
-
retryTimer = null;
|
|
181
|
-
}
|
|
182
|
-
if (ws) {
|
|
183
|
-
ws.close();
|
|
184
|
-
ws = null;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
|
|
190
133
|
// src/hooks/useAnalytics.ts
|
|
191
134
|
var SESSION_EXPIRY_MINUTES = 30;
|
|
192
135
|
var _sessionFlight = null;
|
|
@@ -213,11 +156,10 @@ async function getOrCreateSession(apiUrl, apiKey, visitorId) {
|
|
|
213
156
|
});
|
|
214
157
|
return _sessionFlight;
|
|
215
158
|
}
|
|
216
|
-
function useAnalytics(apiUrl, apiKey
|
|
159
|
+
function useAnalytics(apiUrl, apiKey) {
|
|
217
160
|
const sessionId = useRef(null);
|
|
218
161
|
const pendingEvents = useRef([]);
|
|
219
162
|
const pathname = useRef(typeof window !== "undefined" ? window?.location?.pathname ?? "" : "");
|
|
220
|
-
const presence = useRef(null);
|
|
221
163
|
const hasSentEnd = useRef(false);
|
|
222
164
|
useEffect(() => {
|
|
223
165
|
if (typeof window === "undefined") return;
|
|
@@ -227,7 +169,6 @@ function useAnalytics(apiUrl, apiKey, options) {
|
|
|
227
169
|
getOrCreateSession(apiUrl, apiKey, visitor_id).then((id) => {
|
|
228
170
|
if (cancelled) return;
|
|
229
171
|
sessionId.current = id;
|
|
230
|
-
presence.current = connectPresence(apiKey, id, options?.wsUrl);
|
|
231
172
|
pendingEvents.current.forEach((fn) => fn());
|
|
232
173
|
pendingEvents.current = [];
|
|
233
174
|
});
|
|
@@ -240,8 +181,6 @@ function useAnalytics(apiUrl, apiKey, options) {
|
|
|
240
181
|
const sendEnd = () => {
|
|
241
182
|
if (!sessionId.current || hasSentEnd.current) return;
|
|
242
183
|
hasSentEnd.current = true;
|
|
243
|
-
presence.current?.disconnect();
|
|
244
|
-
presence.current = null;
|
|
245
184
|
navigator.sendBeacon(`${apiUrl}/sessions/${sessionId.current}/end`);
|
|
246
185
|
localStorage.removeItem("af_session_id");
|
|
247
186
|
localStorage.removeItem("af_session_last_activity");
|
|
@@ -294,8 +233,8 @@ function useAnalytics(apiUrl, apiKey, options) {
|
|
|
294
233
|
}
|
|
295
234
|
|
|
296
235
|
// src/components/Analytics.tsx
|
|
297
|
-
function Analytics({ apiKey, apiUrl
|
|
298
|
-
const { trackPageview: trackPageview2, trackClick: trackClick2, trackScroll: trackScroll2, trackCopy: trackCopy2, trackException: trackException2 } = useAnalytics(apiUrl, apiKey
|
|
236
|
+
function Analytics({ apiKey, apiUrl }) {
|
|
237
|
+
const { trackPageview: trackPageview2, trackClick: trackClick2, trackScroll: trackScroll2, trackCopy: trackCopy2, trackException: trackException2 } = useAnalytics(apiUrl, apiKey);
|
|
299
238
|
const lastTracked = useRef2(null);
|
|
300
239
|
const lastScrollDepth = useRef2(0);
|
|
301
240
|
const pathname = typeof window !== "undefined" ? window.location.pathname : "";
|
|
@@ -339,9 +278,6 @@ function Analytics({ apiKey, apiUrl, wsUrl }) {
|
|
|
339
278
|
const handler = (e) => trackException2({
|
|
340
279
|
exception_type: e.error?.name ?? "Error",
|
|
341
280
|
exception_message: e.message,
|
|
342
|
-
exception_filename: e.filename ?? null,
|
|
343
|
-
exception_lineno: e.lineno ?? null,
|
|
344
|
-
exception_colno: e.colno ?? null,
|
|
345
281
|
stack_trace: e.error?.stack ?? null
|
|
346
282
|
});
|
|
347
283
|
window.addEventListener("error", handler);
|
|
@@ -354,9 +290,6 @@ function Analytics({ apiKey, apiUrl, wsUrl }) {
|
|
|
354
290
|
trackException2({
|
|
355
291
|
exception_type: reason?.name ?? "UnhandledRejection",
|
|
356
292
|
exception_message: reason?.message ?? String(reason),
|
|
357
|
-
exception_filename: null,
|
|
358
|
-
exception_lineno: null,
|
|
359
|
-
exception_colno: null,
|
|
360
293
|
stack_trace: reason?.stack ?? null
|
|
361
294
|
});
|
|
362
295
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -7,9 +7,6 @@ declare function trackSearch(apiUrl: string, apiKey: string, sessionId: string,
|
|
|
7
7
|
interface ExceptionFields {
|
|
8
8
|
exception_type: string;
|
|
9
9
|
exception_message: string;
|
|
10
|
-
exception_filename: string | null;
|
|
11
|
-
exception_lineno: number | null;
|
|
12
|
-
exception_colno: number | null;
|
|
13
10
|
stack_trace: string | null;
|
|
14
11
|
}
|
|
15
12
|
declare function trackException(apiUrl: string, apiKey: string, sessionId: string, path: string, fields: ExceptionFields): Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -110,9 +110,6 @@ async function trackError(apiUrl, apiKey, sessionId, path, error) {
|
|
|
110
110
|
await trackException(apiUrl, apiKey, sessionId, path, {
|
|
111
111
|
exception_type: error.name ?? "Error",
|
|
112
112
|
exception_message: error.message,
|
|
113
|
-
exception_filename: null,
|
|
114
|
-
exception_lineno: null,
|
|
115
|
-
exception_colno: null,
|
|
116
113
|
stack_trace: error.stack ?? null
|
|
117
114
|
});
|
|
118
115
|
}
|
package/package.json
CHANGED
|
@@ -1,35 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@arthurreira/analytics",
|
|
3
|
+
"version": "0.16.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "tsup",
|
|
7
|
+
"prepare": "tsup",
|
|
8
|
+
"dev": "tsup src/index.ts --format esm --dts --watch"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
8
14
|
},
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
"import": "./dist/index.js"
|
|
13
|
-
},
|
|
14
|
-
"./client": {
|
|
15
|
-
"types": "./dist/client.d.ts",
|
|
16
|
-
"import": "./dist/client.js"
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
"sideEffects": false,
|
|
20
|
-
"files": [
|
|
21
|
-
"dist"
|
|
22
|
-
],
|
|
23
|
-
"description": "Analytics SDK for af-analytics (browser client + helpers)",
|
|
24
|
-
"license": "MIT",
|
|
25
|
-
"dependencies": {},
|
|
26
|
-
"devDependencies": {
|
|
27
|
-
"tsup": "^8.5.1",
|
|
28
|
-
"typescript": "^5.9.2",
|
|
29
|
-
"@types/react": "^19.0.0"
|
|
30
|
-
},
|
|
31
|
-
"peerDependencies": {
|
|
32
|
-
"next": "^14.0.0 || ^15.0.0 || ^16.0.0",
|
|
33
|
-
"react": "^18.0.0 || ^19.0.0"
|
|
15
|
+
"./client": {
|
|
16
|
+
"types": "./dist/client.d.ts",
|
|
17
|
+
"import": "./dist/client.js"
|
|
34
18
|
}
|
|
19
|
+
},
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"description": "Analytics SDK for af-analytics (browser client + helpers)",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
28
|
+
"@testing-library/react": "^16.3.2",
|
|
29
|
+
"@types/react": "^19.0.0",
|
|
30
|
+
"jsdom": "^29.1.1",
|
|
31
|
+
"react": "^19.2.6",
|
|
32
|
+
"react-dom": "^19.2.6",
|
|
33
|
+
"tsup": "^8.5.1",
|
|
34
|
+
"typescript": "^5.9.2",
|
|
35
|
+
"vitest": "^4.1.7"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"next": "^14.0.0 || ^15.0.0 || ^16.0.0",
|
|
39
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
40
|
+
}
|
|
35
41
|
}
|