@arthurreira/analytics 0.1.0 → 0.2.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/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/client.d.ts +6 -6
- package/dist/client.js +145 -9
- package/dist/index.d.ts +0 -2
- package/dist/index.js +82 -24
- package/package.json +7 -8
- package/dist/chunk-6KBNYQEQ.js +0 -156
- package/dist/chunk-JWEVI74E.js +0 -158
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 arthurreira
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
af-analytics SDK
|
|
2
|
+
|
|
3
|
+
Lightweight analytics SDK (browser client + server helpers) extracted from the af-analytics monorepo.
|
|
4
|
+
|
|
5
|
+
Quick start
|
|
6
|
+
|
|
7
|
+
Install from npm:
|
|
8
|
+
|
|
9
|
+
pnpm add @arthurreira/analytics
|
|
10
|
+
|
|
11
|
+
Client usage (browser-only entry):
|
|
12
|
+
|
|
13
|
+
import { initClient } from '@arthurreira/analytics/client'
|
|
14
|
+
|
|
15
|
+
Server / build entry (exports server-safe helpers):
|
|
16
|
+
|
|
17
|
+
import analytics from '@arthurreira/analytics'
|
|
18
|
+
|
|
19
|
+
Building locally
|
|
20
|
+
|
|
21
|
+
pnpm install
|
|
22
|
+
pnpm build
|
|
23
|
+
|
|
24
|
+
Publishing
|
|
25
|
+
|
|
26
|
+
- Add an `NPM_TOKEN` secret to the repository and tag a release (e.g. `v0.1.0`) to trigger CI publish.
|
|
27
|
+
|
|
28
|
+
License: MIT
|
package/dist/client.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
interface AnalyticsProps {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
apiUrl: string;
|
|
4
|
+
}
|
|
5
|
+
declare function Analytics({ apiKey, apiUrl }: AnalyticsProps): null;
|
|
6
|
+
|
|
1
7
|
declare function useAnalytics(apiUrl: string, apiKey: string): {
|
|
2
8
|
trackPageview: (path: string) => void;
|
|
3
9
|
trackClick: (e: MouseEvent, element: HTMLElement) => void;
|
|
@@ -8,10 +14,4 @@ declare function useAnalytics(apiUrl: string, apiKey: string): {
|
|
|
8
14
|
trackSearch: (query: string) => void;
|
|
9
15
|
};
|
|
10
16
|
|
|
11
|
-
interface AnalyticsProps {
|
|
12
|
-
apiKey: string;
|
|
13
|
-
apiUrl: string;
|
|
14
|
-
}
|
|
15
|
-
declare function Analytics({ apiKey, apiUrl }: AnalyticsProps): null;
|
|
16
|
-
|
|
17
17
|
export { Analytics, useAnalytics };
|
package/dist/client.js
CHANGED
|
@@ -1,9 +1,145 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { useRef, useEffect } from 'react';
|
|
2
|
+
import { usePathname } from 'next/navigation';
|
|
3
|
+
|
|
4
|
+
// src/lib/api.ts
|
|
5
|
+
var BASE_FIELDS = (sessionId, eventType, path) => ({
|
|
6
|
+
session_id: sessionId,
|
|
7
|
+
event_type: eventType,
|
|
8
|
+
path,
|
|
9
|
+
page_url: window.location.href
|
|
10
|
+
});
|
|
11
|
+
async function createSession(apiUrl, apiKey, visitorId) {
|
|
12
|
+
const response = await fetch(`${apiUrl}/sessions`, {
|
|
13
|
+
method: "POST",
|
|
14
|
+
headers: {
|
|
15
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
16
|
+
"Content-Type": "application/json"
|
|
17
|
+
},
|
|
18
|
+
body: JSON.stringify({ visitor_id: visitorId })
|
|
19
|
+
});
|
|
20
|
+
const data = await response.json();
|
|
21
|
+
return data.id;
|
|
22
|
+
}
|
|
23
|
+
async function sendEvent(apiUrl, apiKey, payload) {
|
|
24
|
+
await fetch(`${apiUrl}/events`, {
|
|
25
|
+
method: "POST",
|
|
26
|
+
headers: {
|
|
27
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
28
|
+
"Content-Type": "application/json"
|
|
29
|
+
},
|
|
30
|
+
body: JSON.stringify(payload)
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async function trackPageview(apiUrl, apiKey, sessionId, path) {
|
|
34
|
+
await sendEvent(apiUrl, apiKey, {
|
|
35
|
+
...BASE_FIELDS(sessionId, "pageview", path),
|
|
36
|
+
page_url: `${window.location.origin}${path}`,
|
|
37
|
+
// build from path, not window.location.href
|
|
38
|
+
referrer: document.referrer || null,
|
|
39
|
+
scroll_depth: 0
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
async function trackClick(apiUrl, apiKey, sessionId, path, e, element) {
|
|
43
|
+
await sendEvent(apiUrl, apiKey, {
|
|
44
|
+
...BASE_FIELDS(sessionId, "click", path),
|
|
45
|
+
x_position: e.clientX,
|
|
46
|
+
y_position: e.clientY,
|
|
47
|
+
element_id: element.id || null,
|
|
48
|
+
element_class: element.className || null,
|
|
49
|
+
element_text: element.innerText?.slice(0, 100) || null
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
async function trackScroll(apiUrl, apiKey, sessionId, path, depth) {
|
|
53
|
+
await sendEvent(apiUrl, apiKey, {
|
|
54
|
+
...BASE_FIELDS(sessionId, "scroll", path),
|
|
55
|
+
scroll_depth: depth
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
async function trackCopy(apiUrl, apiKey, sessionId, path) {
|
|
59
|
+
await sendEvent(apiUrl, apiKey, {
|
|
60
|
+
...BASE_FIELDS(sessionId, "copy", path),
|
|
61
|
+
copied_text: window.getSelection()?.toString().slice(0, 200) || null
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
async function trackSearch(apiUrl, apiKey, sessionId, path, query) {
|
|
65
|
+
await sendEvent(apiUrl, apiKey, {
|
|
66
|
+
...BASE_FIELDS(sessionId, "search", path),
|
|
67
|
+
search_query: query
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
async function trackError(apiUrl, apiKey, sessionId, path, error) {
|
|
71
|
+
await sendEvent(apiUrl, apiKey, {
|
|
72
|
+
...BASE_FIELDS(sessionId, "error", path),
|
|
73
|
+
error_message: error.message,
|
|
74
|
+
stack_trace: error.stack || null
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
async function trackCTA(apiUrl, apiKey, sessionId, path, ctaId, ctaVariant) {
|
|
78
|
+
await sendEvent(apiUrl, apiKey, {
|
|
79
|
+
...BASE_FIELDS(sessionId, "cta_click", path),
|
|
80
|
+
cta_id: ctaId,
|
|
81
|
+
cta_variant: ctaVariant || null
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/hooks/useAnalytics.ts
|
|
86
|
+
function useAnalytics(apiUrl, apiKey) {
|
|
87
|
+
const sessionId = useRef(null);
|
|
88
|
+
const pendingEvents = useRef([]);
|
|
89
|
+
const pathname = useRef(typeof window !== "undefined" ? window?.location?.pathname ?? "" : "");
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
if (typeof window === "undefined") return;
|
|
92
|
+
const visitor_id = localStorage.getItem("af_analytics_visitor_id") || crypto.randomUUID();
|
|
93
|
+
localStorage.setItem("af_analytics_visitor_id", visitor_id);
|
|
94
|
+
createSession(apiUrl, apiKey, visitor_id).then((id) => {
|
|
95
|
+
sessionId.current = id;
|
|
96
|
+
pendingEvents.current.forEach((fn) => fn());
|
|
97
|
+
pendingEvents.current = [];
|
|
98
|
+
});
|
|
99
|
+
}, []);
|
|
100
|
+
function enqueueOrRun(fn) {
|
|
101
|
+
if (sessionId.current) {
|
|
102
|
+
fn();
|
|
103
|
+
} else {
|
|
104
|
+
pendingEvents.current.push(fn);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
trackPageview: (path) => {
|
|
109
|
+
enqueueOrRun(() => trackPageview(apiUrl, apiKey, sessionId.current, path));
|
|
110
|
+
},
|
|
111
|
+
trackClick: (e, element) => {
|
|
112
|
+
enqueueOrRun(() => trackClick(apiUrl, apiKey, sessionId.current, pathname.current, e, element));
|
|
113
|
+
},
|
|
114
|
+
trackScroll: (depth) => {
|
|
115
|
+
enqueueOrRun(() => trackScroll(apiUrl, apiKey, sessionId.current, pathname.current, depth));
|
|
116
|
+
},
|
|
117
|
+
trackCopy: () => {
|
|
118
|
+
enqueueOrRun(() => trackCopy(apiUrl, apiKey, sessionId.current, pathname.current));
|
|
119
|
+
},
|
|
120
|
+
trackError: (error) => {
|
|
121
|
+
enqueueOrRun(() => trackError(apiUrl, apiKey, sessionId.current, pathname.current, error));
|
|
122
|
+
},
|
|
123
|
+
trackCTA: (ctaId, ctaVariant) => {
|
|
124
|
+
enqueueOrRun(() => trackCTA(apiUrl, apiKey, sessionId.current, pathname.current, ctaId, ctaVariant));
|
|
125
|
+
},
|
|
126
|
+
trackSearch: (query) => {
|
|
127
|
+
enqueueOrRun(() => trackSearch(apiUrl, apiKey, sessionId.current, pathname.current, query));
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// src/components/Analytics.tsx
|
|
133
|
+
function Analytics({ apiKey, apiUrl }) {
|
|
134
|
+
const pathname = usePathname();
|
|
135
|
+
const { trackPageview: trackPageview2 } = useAnalytics(apiUrl, apiKey);
|
|
136
|
+
const lastTracked = useRef(null);
|
|
137
|
+
useEffect(() => {
|
|
138
|
+
if (lastTracked.current === pathname) return;
|
|
139
|
+
lastTracked.current = pathname;
|
|
140
|
+
trackPageview2(pathname);
|
|
141
|
+
}, [pathname]);
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export { Analytics, useAnalytics };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
export { Analytics, useAnalytics } from './client.js';
|
|
2
|
-
|
|
3
1
|
declare function createSession(apiUrl: string, apiKey: string, visitorId?: string): Promise<any>;
|
|
4
2
|
declare function trackPageview(apiUrl: string, apiKey: string, sessionId: string, path: string): Promise<void>;
|
|
5
3
|
declare function trackClick(apiUrl: string, apiKey: string, sessionId: string, path: string, e: MouseEvent, element: HTMLElement): Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -1,24 +1,82 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
1
|
+
// src/lib/api.ts
|
|
2
|
+
var BASE_FIELDS = (sessionId, eventType, path) => ({
|
|
3
|
+
session_id: sessionId,
|
|
4
|
+
event_type: eventType,
|
|
5
|
+
path,
|
|
6
|
+
page_url: window.location.href
|
|
7
|
+
});
|
|
8
|
+
async function createSession(apiUrl, apiKey, visitorId) {
|
|
9
|
+
const response = await fetch(`${apiUrl}/sessions`, {
|
|
10
|
+
method: "POST",
|
|
11
|
+
headers: {
|
|
12
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
13
|
+
"Content-Type": "application/json"
|
|
14
|
+
},
|
|
15
|
+
body: JSON.stringify({ visitor_id: visitorId })
|
|
16
|
+
});
|
|
17
|
+
const data = await response.json();
|
|
18
|
+
return data.id;
|
|
19
|
+
}
|
|
20
|
+
async function sendEvent(apiUrl, apiKey, payload) {
|
|
21
|
+
await fetch(`${apiUrl}/events`, {
|
|
22
|
+
method: "POST",
|
|
23
|
+
headers: {
|
|
24
|
+
"Authorization": `Bearer ${apiKey}`,
|
|
25
|
+
"Content-Type": "application/json"
|
|
26
|
+
},
|
|
27
|
+
body: JSON.stringify(payload)
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
async function trackPageview(apiUrl, apiKey, sessionId, path) {
|
|
31
|
+
await sendEvent(apiUrl, apiKey, {
|
|
32
|
+
...BASE_FIELDS(sessionId, "pageview", path),
|
|
33
|
+
page_url: `${window.location.origin}${path}`,
|
|
34
|
+
// build from path, not window.location.href
|
|
35
|
+
referrer: document.referrer || null,
|
|
36
|
+
scroll_depth: 0
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
async function trackClick(apiUrl, apiKey, sessionId, path, e, element) {
|
|
40
|
+
await sendEvent(apiUrl, apiKey, {
|
|
41
|
+
...BASE_FIELDS(sessionId, "click", path),
|
|
42
|
+
x_position: e.clientX,
|
|
43
|
+
y_position: e.clientY,
|
|
44
|
+
element_id: element.id || null,
|
|
45
|
+
element_class: element.className || null,
|
|
46
|
+
element_text: element.innerText?.slice(0, 100) || null
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
async function trackScroll(apiUrl, apiKey, sessionId, path, depth) {
|
|
50
|
+
await sendEvent(apiUrl, apiKey, {
|
|
51
|
+
...BASE_FIELDS(sessionId, "scroll", path),
|
|
52
|
+
scroll_depth: depth
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
async function trackCopy(apiUrl, apiKey, sessionId, path) {
|
|
56
|
+
await sendEvent(apiUrl, apiKey, {
|
|
57
|
+
...BASE_FIELDS(sessionId, "copy", path),
|
|
58
|
+
copied_text: window.getSelection()?.toString().slice(0, 200) || null
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
async function trackSearch(apiUrl, apiKey, sessionId, path, query) {
|
|
62
|
+
await sendEvent(apiUrl, apiKey, {
|
|
63
|
+
...BASE_FIELDS(sessionId, "search", path),
|
|
64
|
+
search_query: query
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
async function trackError(apiUrl, apiKey, sessionId, path, error) {
|
|
68
|
+
await sendEvent(apiUrl, apiKey, {
|
|
69
|
+
...BASE_FIELDS(sessionId, "error", path),
|
|
70
|
+
error_message: error.message,
|
|
71
|
+
stack_trace: error.stack || null
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
async function trackCTA(apiUrl, apiKey, sessionId, path, ctaId, ctaVariant) {
|
|
75
|
+
await sendEvent(apiUrl, apiKey, {
|
|
76
|
+
...BASE_FIELDS(sessionId, "cta_click", path),
|
|
77
|
+
cta_id: ctaId,
|
|
78
|
+
cta_variant: ctaVariant || null
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { createSession, trackCTA, trackClick, trackCopy, trackError, trackPageview, trackScroll, trackSearch };
|
package/package.json
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arthurreira/analytics",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.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
|
+
".": "./dist/index.js",
|
|
12
7
|
"./client": "./dist/client.js"
|
|
13
8
|
},
|
|
14
9
|
"types": "./dist/index.d.ts",
|
|
@@ -26,5 +21,9 @@
|
|
|
26
21
|
"peerDependencies": {
|
|
27
22
|
"next": "^14.0.0 || ^15.0.0 || ^16.0.0",
|
|
28
23
|
"react": "^18.0.0 || ^19.0.0"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsup",
|
|
27
|
+
"dev": "tsup src/index.ts --format esm --dts --watch"
|
|
29
28
|
}
|
|
30
|
-
}
|
|
29
|
+
}
|
package/dist/chunk-6KBNYQEQ.js
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
// src/lib/api.ts
|
|
2
|
-
var BASE_FIELDS = (sessionId, eventType, path) => ({
|
|
3
|
-
session_id: sessionId,
|
|
4
|
-
event_type: eventType,
|
|
5
|
-
path,
|
|
6
|
-
page_url: window.location.href
|
|
7
|
-
});
|
|
8
|
-
async function createSession(apiUrl, apiKey, visitorId) {
|
|
9
|
-
const response = await fetch(`${apiUrl}/sessions`, {
|
|
10
|
-
method: "POST",
|
|
11
|
-
headers: {
|
|
12
|
-
"Authorization": `Bearer ${apiKey}`,
|
|
13
|
-
"Content-Type": "application/json"
|
|
14
|
-
},
|
|
15
|
-
body: JSON.stringify({ visitor_id: visitorId })
|
|
16
|
-
});
|
|
17
|
-
const data = await response.json();
|
|
18
|
-
return data.id;
|
|
19
|
-
}
|
|
20
|
-
async function sendEvent(apiUrl, apiKey, payload) {
|
|
21
|
-
await fetch(`${apiUrl}/events`, {
|
|
22
|
-
method: "POST",
|
|
23
|
-
headers: {
|
|
24
|
-
"Authorization": `Bearer ${apiKey}`,
|
|
25
|
-
"Content-Type": "application/json"
|
|
26
|
-
},
|
|
27
|
-
body: JSON.stringify(payload)
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
async function trackPageview(apiUrl, apiKey, sessionId, path) {
|
|
31
|
-
await sendEvent(apiUrl, apiKey, {
|
|
32
|
-
...BASE_FIELDS(sessionId, "pageview", path),
|
|
33
|
-
page_url: `${window.location.origin}${path}`,
|
|
34
|
-
// build from path, not window.location.href
|
|
35
|
-
referrer: document.referrer || null,
|
|
36
|
-
scroll_depth: 0
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
async function trackClick(apiUrl, apiKey, sessionId, path, e, element) {
|
|
40
|
-
await sendEvent(apiUrl, apiKey, {
|
|
41
|
-
...BASE_FIELDS(sessionId, "click", path),
|
|
42
|
-
x_position: e.clientX,
|
|
43
|
-
y_position: e.clientY,
|
|
44
|
-
element_id: element.id || null,
|
|
45
|
-
element_class: element.className || null,
|
|
46
|
-
element_text: element.innerText?.slice(0, 100) || null
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
async function trackScroll(apiUrl, apiKey, sessionId, path, depth) {
|
|
50
|
-
await sendEvent(apiUrl, apiKey, {
|
|
51
|
-
...BASE_FIELDS(sessionId, "scroll", path),
|
|
52
|
-
scroll_depth: depth
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
async function trackCopy(apiUrl, apiKey, sessionId, path) {
|
|
56
|
-
await sendEvent(apiUrl, apiKey, {
|
|
57
|
-
...BASE_FIELDS(sessionId, "copy", path),
|
|
58
|
-
copied_text: window.getSelection()?.toString().slice(0, 200) || null
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
async function trackSearch(apiUrl, apiKey, sessionId, path, query) {
|
|
62
|
-
await sendEvent(apiUrl, apiKey, {
|
|
63
|
-
...BASE_FIELDS(sessionId, "search", path),
|
|
64
|
-
search_query: query
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
async function trackError(apiUrl, apiKey, sessionId, path, error) {
|
|
68
|
-
await sendEvent(apiUrl, apiKey, {
|
|
69
|
-
...BASE_FIELDS(sessionId, "error", path),
|
|
70
|
-
error_message: error.message,
|
|
71
|
-
stack_trace: error.stack || null
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
async function trackCTA(apiUrl, apiKey, sessionId, path, ctaId, ctaVariant) {
|
|
75
|
-
await sendEvent(apiUrl, apiKey, {
|
|
76
|
-
...BASE_FIELDS(sessionId, "cta_click", path),
|
|
77
|
-
cta_id: ctaId,
|
|
78
|
-
cta_variant: ctaVariant || null
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// src/hooks/useAnalytics.ts
|
|
83
|
-
import { useEffect, useRef } from "react";
|
|
84
|
-
function useAnalytics(apiUrl, apiKey) {
|
|
85
|
-
const sessionId = useRef(null);
|
|
86
|
-
const pendingEvents = useRef([]);
|
|
87
|
-
const pathname = useRef(typeof window !== "undefined" ? window?.location?.pathname ?? "" : "");
|
|
88
|
-
useEffect(() => {
|
|
89
|
-
if (typeof window === "undefined") return;
|
|
90
|
-
const visitor_id = localStorage.getItem("af_analytics_visitor_id") || crypto.randomUUID();
|
|
91
|
-
localStorage.setItem("af_analytics_visitor_id", visitor_id);
|
|
92
|
-
createSession(apiUrl, apiKey, visitor_id).then((id) => {
|
|
93
|
-
sessionId.current = id;
|
|
94
|
-
pendingEvents.current.forEach((fn) => fn());
|
|
95
|
-
pendingEvents.current = [];
|
|
96
|
-
});
|
|
97
|
-
}, []);
|
|
98
|
-
function enqueueOrRun(fn) {
|
|
99
|
-
if (sessionId.current) {
|
|
100
|
-
fn();
|
|
101
|
-
} else {
|
|
102
|
-
pendingEvents.current.push(fn);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return {
|
|
106
|
-
trackPageview: (path) => {
|
|
107
|
-
enqueueOrRun(() => trackPageview(apiUrl, apiKey, sessionId.current, path));
|
|
108
|
-
},
|
|
109
|
-
trackClick: (e, element) => {
|
|
110
|
-
enqueueOrRun(() => trackClick(apiUrl, apiKey, sessionId.current, pathname.current, e, element));
|
|
111
|
-
},
|
|
112
|
-
trackScroll: (depth) => {
|
|
113
|
-
enqueueOrRun(() => trackScroll(apiUrl, apiKey, sessionId.current, pathname.current, depth));
|
|
114
|
-
},
|
|
115
|
-
trackCopy: () => {
|
|
116
|
-
enqueueOrRun(() => trackCopy(apiUrl, apiKey, sessionId.current, pathname.current));
|
|
117
|
-
},
|
|
118
|
-
trackError: (error) => {
|
|
119
|
-
enqueueOrRun(() => trackError(apiUrl, apiKey, sessionId.current, pathname.current, error));
|
|
120
|
-
},
|
|
121
|
-
trackCTA: (ctaId, ctaVariant) => {
|
|
122
|
-
enqueueOrRun(() => trackCTA(apiUrl, apiKey, sessionId.current, pathname.current, ctaId, ctaVariant));
|
|
123
|
-
},
|
|
124
|
-
trackSearch: (query) => {
|
|
125
|
-
enqueueOrRun(() => trackSearch(apiUrl, apiKey, sessionId.current, pathname.current, query));
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// src/components/Analytics.tsx
|
|
131
|
-
import { useEffect as useEffect2, useRef as useRef2 } from "react";
|
|
132
|
-
import { usePathname } from "next/navigation";
|
|
133
|
-
function Analytics({ apiKey, apiUrl }) {
|
|
134
|
-
const pathname = usePathname();
|
|
135
|
-
const { trackPageview: trackPageview2 } = useAnalytics(apiUrl, apiKey);
|
|
136
|
-
const lastTracked = useRef2(null);
|
|
137
|
-
useEffect2(() => {
|
|
138
|
-
if (lastTracked.current === pathname) return;
|
|
139
|
-
lastTracked.current = pathname;
|
|
140
|
-
trackPageview2(pathname);
|
|
141
|
-
}, [pathname]);
|
|
142
|
-
return null;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export {
|
|
146
|
-
createSession,
|
|
147
|
-
trackPageview,
|
|
148
|
-
trackClick,
|
|
149
|
-
trackScroll,
|
|
150
|
-
trackCopy,
|
|
151
|
-
trackSearch,
|
|
152
|
-
trackError,
|
|
153
|
-
trackCTA,
|
|
154
|
-
useAnalytics,
|
|
155
|
-
Analytics
|
|
156
|
-
};
|
package/dist/chunk-JWEVI74E.js
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
|
|
3
|
-
// src/lib/api.ts
|
|
4
|
-
var BASE_FIELDS = (sessionId, eventType, path) => ({
|
|
5
|
-
session_id: sessionId,
|
|
6
|
-
event_type: eventType,
|
|
7
|
-
path,
|
|
8
|
-
page_url: window.location.href
|
|
9
|
-
});
|
|
10
|
-
async function createSession(apiUrl, apiKey, visitorId) {
|
|
11
|
-
const response = await fetch(`${apiUrl}/sessions`, {
|
|
12
|
-
method: "POST",
|
|
13
|
-
headers: {
|
|
14
|
-
"Authorization": `Bearer ${apiKey}`,
|
|
15
|
-
"Content-Type": "application/json"
|
|
16
|
-
},
|
|
17
|
-
body: JSON.stringify({ visitor_id: visitorId })
|
|
18
|
-
});
|
|
19
|
-
const data = await response.json();
|
|
20
|
-
return data.id;
|
|
21
|
-
}
|
|
22
|
-
async function sendEvent(apiUrl, apiKey, payload) {
|
|
23
|
-
await fetch(`${apiUrl}/events`, {
|
|
24
|
-
method: "POST",
|
|
25
|
-
headers: {
|
|
26
|
-
"Authorization": `Bearer ${apiKey}`,
|
|
27
|
-
"Content-Type": "application/json"
|
|
28
|
-
},
|
|
29
|
-
body: JSON.stringify(payload)
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
async function trackPageview(apiUrl, apiKey, sessionId, path) {
|
|
33
|
-
await sendEvent(apiUrl, apiKey, {
|
|
34
|
-
...BASE_FIELDS(sessionId, "pageview", path),
|
|
35
|
-
page_url: `${window.location.origin}${path}`,
|
|
36
|
-
// build from path, not window.location.href
|
|
37
|
-
referrer: document.referrer || null,
|
|
38
|
-
scroll_depth: 0
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
async function trackClick(apiUrl, apiKey, sessionId, path, e, element) {
|
|
42
|
-
await sendEvent(apiUrl, apiKey, {
|
|
43
|
-
...BASE_FIELDS(sessionId, "click", path),
|
|
44
|
-
x_position: e.clientX,
|
|
45
|
-
y_position: e.clientY,
|
|
46
|
-
element_id: element.id || null,
|
|
47
|
-
element_class: element.className || null,
|
|
48
|
-
element_text: element.innerText?.slice(0, 100) || null
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
async function trackScroll(apiUrl, apiKey, sessionId, path, depth) {
|
|
52
|
-
await sendEvent(apiUrl, apiKey, {
|
|
53
|
-
...BASE_FIELDS(sessionId, "scroll", path),
|
|
54
|
-
scroll_depth: depth
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
async function trackCopy(apiUrl, apiKey, sessionId, path) {
|
|
58
|
-
await sendEvent(apiUrl, apiKey, {
|
|
59
|
-
...BASE_FIELDS(sessionId, "copy", path),
|
|
60
|
-
copied_text: window.getSelection()?.toString().slice(0, 200) || null
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
async function trackSearch(apiUrl, apiKey, sessionId, path, query) {
|
|
64
|
-
await sendEvent(apiUrl, apiKey, {
|
|
65
|
-
...BASE_FIELDS(sessionId, "search", path),
|
|
66
|
-
search_query: query
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
async function trackError(apiUrl, apiKey, sessionId, path, error) {
|
|
70
|
-
await sendEvent(apiUrl, apiKey, {
|
|
71
|
-
...BASE_FIELDS(sessionId, "error", path),
|
|
72
|
-
error_message: error.message,
|
|
73
|
-
stack_trace: error.stack || null
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
async function trackCTA(apiUrl, apiKey, sessionId, path, ctaId, ctaVariant) {
|
|
77
|
-
await sendEvent(apiUrl, apiKey, {
|
|
78
|
-
...BASE_FIELDS(sessionId, "cta_click", path),
|
|
79
|
-
cta_id: ctaId,
|
|
80
|
-
cta_variant: ctaVariant || null
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// src/hooks/useAnalytics.ts
|
|
85
|
-
import { useEffect, useRef } from "react";
|
|
86
|
-
function useAnalytics(apiUrl, apiKey) {
|
|
87
|
-
const sessionId = useRef(null);
|
|
88
|
-
const pendingEvents = useRef([]);
|
|
89
|
-
const pathname = useRef(typeof window !== "undefined" ? window?.location?.pathname ?? "" : "");
|
|
90
|
-
useEffect(() => {
|
|
91
|
-
if (typeof window === "undefined") return;
|
|
92
|
-
const visitor_id = localStorage.getItem("af_analytics_visitor_id") || crypto.randomUUID();
|
|
93
|
-
localStorage.setItem("af_analytics_visitor_id", visitor_id);
|
|
94
|
-
createSession(apiUrl, apiKey, visitor_id).then((id) => {
|
|
95
|
-
sessionId.current = id;
|
|
96
|
-
pendingEvents.current.forEach((fn) => fn());
|
|
97
|
-
pendingEvents.current = [];
|
|
98
|
-
});
|
|
99
|
-
}, []);
|
|
100
|
-
function enqueueOrRun(fn) {
|
|
101
|
-
if (sessionId.current) {
|
|
102
|
-
fn();
|
|
103
|
-
} else {
|
|
104
|
-
pendingEvents.current.push(fn);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return {
|
|
108
|
-
trackPageview: (path) => {
|
|
109
|
-
enqueueOrRun(() => trackPageview(apiUrl, apiKey, sessionId.current, path));
|
|
110
|
-
},
|
|
111
|
-
trackClick: (e, element) => {
|
|
112
|
-
enqueueOrRun(() => trackClick(apiUrl, apiKey, sessionId.current, pathname.current, e, element));
|
|
113
|
-
},
|
|
114
|
-
trackScroll: (depth) => {
|
|
115
|
-
enqueueOrRun(() => trackScroll(apiUrl, apiKey, sessionId.current, pathname.current, depth));
|
|
116
|
-
},
|
|
117
|
-
trackCopy: () => {
|
|
118
|
-
enqueueOrRun(() => trackCopy(apiUrl, apiKey, sessionId.current, pathname.current));
|
|
119
|
-
},
|
|
120
|
-
trackError: (error) => {
|
|
121
|
-
enqueueOrRun(() => trackError(apiUrl, apiKey, sessionId.current, pathname.current, error));
|
|
122
|
-
},
|
|
123
|
-
trackCTA: (ctaId, ctaVariant) => {
|
|
124
|
-
enqueueOrRun(() => trackCTA(apiUrl, apiKey, sessionId.current, pathname.current, ctaId, ctaVariant));
|
|
125
|
-
},
|
|
126
|
-
trackSearch: (query) => {
|
|
127
|
-
enqueueOrRun(() => trackSearch(apiUrl, apiKey, sessionId.current, pathname.current, query));
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// src/components/Analytics.tsx
|
|
133
|
-
import { useEffect as useEffect2, useRef as useRef2 } from "react";
|
|
134
|
-
import { usePathname } from "next/navigation";
|
|
135
|
-
function Analytics({ apiKey, apiUrl }) {
|
|
136
|
-
const pathname = usePathname();
|
|
137
|
-
const { trackPageview: trackPageview2 } = useAnalytics(apiUrl, apiKey);
|
|
138
|
-
const lastTracked = useRef2(null);
|
|
139
|
-
useEffect2(() => {
|
|
140
|
-
if (lastTracked.current === pathname) return;
|
|
141
|
-
lastTracked.current = pathname;
|
|
142
|
-
trackPageview2(pathname);
|
|
143
|
-
}, [pathname]);
|
|
144
|
-
return null;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export {
|
|
148
|
-
createSession,
|
|
149
|
-
trackPageview,
|
|
150
|
-
trackClick,
|
|
151
|
-
trackScroll,
|
|
152
|
-
trackCopy,
|
|
153
|
-
trackSearch,
|
|
154
|
-
trackError,
|
|
155
|
-
trackCTA,
|
|
156
|
-
useAnalytics,
|
|
157
|
-
Analytics
|
|
158
|
-
};
|