@arthurreira/analytics 0.1.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/chunk-6KBNYQEQ.js +156 -0
- package/dist/chunk-JWEVI74E.js +158 -0
- package/dist/client.d.ts +17 -0
- package/dist/client.js +9 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +24 -0
- package/package.json +30 -0
|
@@ -0,0 +1,156 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,158 @@
|
|
|
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
|
+
};
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
declare function useAnalytics(apiUrl: string, apiKey: string): {
|
|
2
|
+
trackPageview: (path: string) => void;
|
|
3
|
+
trackClick: (e: MouseEvent, element: HTMLElement) => void;
|
|
4
|
+
trackScroll: (depth: number) => void;
|
|
5
|
+
trackCopy: () => void;
|
|
6
|
+
trackError: (error: Error) => void;
|
|
7
|
+
trackCTA: (ctaId: string, ctaVariant?: string) => void;
|
|
8
|
+
trackSearch: (query: string) => void;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
interface AnalyticsProps {
|
|
12
|
+
apiKey: string;
|
|
13
|
+
apiUrl: string;
|
|
14
|
+
}
|
|
15
|
+
declare function Analytics({ apiKey, apiUrl }: AnalyticsProps): null;
|
|
16
|
+
|
|
17
|
+
export { Analytics, useAnalytics };
|
package/dist/client.js
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { Analytics, useAnalytics } from './client.js';
|
|
2
|
+
|
|
3
|
+
declare function createSession(apiUrl: string, apiKey: string, visitorId?: string): Promise<any>;
|
|
4
|
+
declare function trackPageview(apiUrl: string, apiKey: string, sessionId: string, path: string): Promise<void>;
|
|
5
|
+
declare function trackClick(apiUrl: string, apiKey: string, sessionId: string, path: string, e: MouseEvent, element: HTMLElement): Promise<void>;
|
|
6
|
+
declare function trackScroll(apiUrl: string, apiKey: string, sessionId: string, path: string, depth: number): Promise<void>;
|
|
7
|
+
declare function trackCopy(apiUrl: string, apiKey: string, sessionId: string, path: string): Promise<void>;
|
|
8
|
+
declare function trackSearch(apiUrl: string, apiKey: string, sessionId: string, path: string, query: string): Promise<void>;
|
|
9
|
+
declare function trackError(apiUrl: string, apiKey: string, sessionId: string, path: string, error: Error): Promise<void>;
|
|
10
|
+
declare function trackCTA(apiUrl: string, apiKey: string, sessionId: string, path: string, ctaId: string, ctaVariant?: string): Promise<void>;
|
|
11
|
+
|
|
12
|
+
export { createSession, trackCTA, trackClick, trackCopy, trackError, trackPageview, trackScroll, trackSearch };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Analytics,
|
|
3
|
+
createSession,
|
|
4
|
+
trackCTA,
|
|
5
|
+
trackClick,
|
|
6
|
+
trackCopy,
|
|
7
|
+
trackError,
|
|
8
|
+
trackPageview,
|
|
9
|
+
trackScroll,
|
|
10
|
+
trackSearch,
|
|
11
|
+
useAnalytics
|
|
12
|
+
} from "./chunk-6KBNYQEQ.js";
|
|
13
|
+
export {
|
|
14
|
+
Analytics,
|
|
15
|
+
createSession,
|
|
16
|
+
trackCTA,
|
|
17
|
+
trackClick,
|
|
18
|
+
trackCopy,
|
|
19
|
+
trackError,
|
|
20
|
+
trackPageview,
|
|
21
|
+
trackScroll,
|
|
22
|
+
trackSearch,
|
|
23
|
+
useAnalytics
|
|
24
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@arthurreira/analytics",
|
|
3
|
+
"version": "0.1.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
|
+
".": "./dist/index.js",
|
|
12
|
+
"./client": "./dist/client.js"
|
|
13
|
+
},
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"description": "Analytics SDK for af-analytics (browser client + helpers)",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"dependencies": {},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"tsup": "^8.5.1",
|
|
23
|
+
"typescript": "^5.9.2",
|
|
24
|
+
"@types/react": "^19.0.0"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"next": "^14.0.0 || ^15.0.0 || ^16.0.0",
|
|
28
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
29
|
+
}
|
|
30
|
+
}
|