@arthurreira/analytics 0.2.0 → 0.2.2
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/client.js +20 -13
- package/dist/index.js +14 -7
- package/package.json +34 -28
package/dist/client.js
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use client";
|
|
2
|
+
"use client";
|
|
3
|
+
|
|
4
|
+
// src/components/Analytics.tsx
|
|
5
|
+
import { useEffect as useEffect2, useRef as useRef2 } from "react";
|
|
6
|
+
|
|
7
|
+
// src/hooks/useAnalytics.ts
|
|
8
|
+
import { useEffect, useRef } from "react";
|
|
3
9
|
|
|
4
10
|
// src/lib/api.ts
|
|
5
11
|
var BASE_FIELDS = (sessionId, eventType, path) => ({
|
|
6
12
|
session_id: sessionId,
|
|
7
13
|
event_type: eventType,
|
|
8
14
|
path,
|
|
9
|
-
page_url: window.location.href
|
|
15
|
+
page_url: typeof window !== "undefined" ? window.location.href : ""
|
|
10
16
|
});
|
|
11
17
|
async function createSession(apiUrl, apiKey, visitorId) {
|
|
12
18
|
const response = await fetch(`${apiUrl}/sessions`, {
|
|
@@ -33,9 +39,8 @@ async function sendEvent(apiUrl, apiKey, payload) {
|
|
|
33
39
|
async function trackPageview(apiUrl, apiKey, sessionId, path) {
|
|
34
40
|
await sendEvent(apiUrl, apiKey, {
|
|
35
41
|
...BASE_FIELDS(sessionId, "pageview", path),
|
|
36
|
-
page_url: `${window.location.origin}${path}`,
|
|
37
|
-
|
|
38
|
-
referrer: document.referrer || null,
|
|
42
|
+
page_url: `${typeof window !== "undefined" ? window.location.origin : ""}${path}`,
|
|
43
|
+
referrer: typeof document !== "undefined" ? document.referrer || null : null,
|
|
39
44
|
scroll_depth: 0
|
|
40
45
|
});
|
|
41
46
|
}
|
|
@@ -58,7 +63,7 @@ async function trackScroll(apiUrl, apiKey, sessionId, path, depth) {
|
|
|
58
63
|
async function trackCopy(apiUrl, apiKey, sessionId, path) {
|
|
59
64
|
await sendEvent(apiUrl, apiKey, {
|
|
60
65
|
...BASE_FIELDS(sessionId, "copy", path),
|
|
61
|
-
copied_text: window.getSelection()?.toString().slice(0, 200) || null
|
|
66
|
+
copied_text: typeof window !== "undefined" ? window.getSelection()?.toString().slice(0, 200) || null : null
|
|
62
67
|
});
|
|
63
68
|
}
|
|
64
69
|
async function trackSearch(apiUrl, apiKey, sessionId, path, query) {
|
|
@@ -131,15 +136,17 @@ function useAnalytics(apiUrl, apiKey) {
|
|
|
131
136
|
|
|
132
137
|
// src/components/Analytics.tsx
|
|
133
138
|
function Analytics({ apiKey, apiUrl }) {
|
|
134
|
-
const pathname = usePathname();
|
|
135
139
|
const { trackPageview: trackPageview2 } = useAnalytics(apiUrl, apiKey);
|
|
136
|
-
const lastTracked =
|
|
137
|
-
|
|
140
|
+
const lastTracked = useRef2(null);
|
|
141
|
+
const pathname = typeof window !== "undefined" ? window.location.pathname : "";
|
|
142
|
+
useEffect2(() => {
|
|
138
143
|
if (lastTracked.current === pathname) return;
|
|
139
144
|
lastTracked.current = pathname;
|
|
140
145
|
trackPageview2(pathname);
|
|
141
|
-
}, [pathname]);
|
|
146
|
+
}, [pathname, trackPageview2]);
|
|
142
147
|
return null;
|
|
143
148
|
}
|
|
144
|
-
|
|
145
|
-
|
|
149
|
+
export {
|
|
150
|
+
Analytics,
|
|
151
|
+
useAnalytics
|
|
152
|
+
};
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ var BASE_FIELDS = (sessionId, eventType, path) => ({
|
|
|
3
3
|
session_id: sessionId,
|
|
4
4
|
event_type: eventType,
|
|
5
5
|
path,
|
|
6
|
-
page_url: window.location.href
|
|
6
|
+
page_url: typeof window !== "undefined" ? window.location.href : ""
|
|
7
7
|
});
|
|
8
8
|
async function createSession(apiUrl, apiKey, visitorId) {
|
|
9
9
|
const response = await fetch(`${apiUrl}/sessions`, {
|
|
@@ -30,9 +30,8 @@ async function sendEvent(apiUrl, apiKey, payload) {
|
|
|
30
30
|
async function trackPageview(apiUrl, apiKey, sessionId, path) {
|
|
31
31
|
await sendEvent(apiUrl, apiKey, {
|
|
32
32
|
...BASE_FIELDS(sessionId, "pageview", path),
|
|
33
|
-
page_url: `${window.location.origin}${path}`,
|
|
34
|
-
|
|
35
|
-
referrer: document.referrer || null,
|
|
33
|
+
page_url: `${typeof window !== "undefined" ? window.location.origin : ""}${path}`,
|
|
34
|
+
referrer: typeof document !== "undefined" ? document.referrer || null : null,
|
|
36
35
|
scroll_depth: 0
|
|
37
36
|
});
|
|
38
37
|
}
|
|
@@ -55,7 +54,7 @@ async function trackScroll(apiUrl, apiKey, sessionId, path, depth) {
|
|
|
55
54
|
async function trackCopy(apiUrl, apiKey, sessionId, path) {
|
|
56
55
|
await sendEvent(apiUrl, apiKey, {
|
|
57
56
|
...BASE_FIELDS(sessionId, "copy", path),
|
|
58
|
-
copied_text: window.getSelection()?.toString().slice(0, 200) || null
|
|
57
|
+
copied_text: typeof window !== "undefined" ? window.getSelection()?.toString().slice(0, 200) || null : null
|
|
59
58
|
});
|
|
60
59
|
}
|
|
61
60
|
async function trackSearch(apiUrl, apiKey, sessionId, path, query) {
|
|
@@ -78,5 +77,13 @@ async function trackCTA(apiUrl, apiKey, sessionId, path, ctaId, ctaVariant) {
|
|
|
78
77
|
cta_variant: ctaVariant || null
|
|
79
78
|
});
|
|
80
79
|
}
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
export {
|
|
81
|
+
createSession,
|
|
82
|
+
trackCTA,
|
|
83
|
+
trackClick,
|
|
84
|
+
trackCopy,
|
|
85
|
+
trackError,
|
|
86
|
+
trackPageview,
|
|
87
|
+
trackScroll,
|
|
88
|
+
trackSearch
|
|
89
|
+
};
|
package/package.json
CHANGED
|
@@ -1,29 +1,35 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
1
|
+
{ "name": "@arthurreira/analytics",
|
|
2
|
+
"version": "0.2.2",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"scripts": {
|
|
5
|
+
"build": "tsup",
|
|
6
|
+
"prepare": "tsup",
|
|
7
|
+
"dev": "tsup src/index.ts --format esm --dts --watch"
|
|
8
|
+
},
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
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"
|
|
34
|
+
}
|
|
29
35
|
}
|