-
Notifications
You must be signed in to change notification settings - Fork 0
/
analytics.ts
47 lines (42 loc) · 1.21 KB
/
analytics.ts
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Add this in your layout.tsx:
// <ArroyoPageview endpoint="https://8f29-136-25-102-57.ngrok-free.app" />
// src/app/analytics.tsx
export function ArroyoPageview({
endpoint,
}: {
endpoint: string,
}): JSX.Element {
const pathname = usePathname();
const searchParams = useSearchParams();
useEffect(() => {
if (pathname) {
let url = window.origin + pathname;
if (searchParams && searchParams.toString()) {
url = url + `?${searchParams.toString()}`;
}
let event = {
path: pathname,
url: url,
host: window.location.host,
browser: window.navigator.userAgent,
screen_height: window.screen.height,
screen_width: window.screen.width,
referer: document.referrer,
title: document.title,
browserLanguage: window.navigator.language,
browserVersion: window.navigator.appVersion,
time: Date.now(),
};
// asynchronously send the event to the server
fetch(endpoint, {
method: "POST",
body: JSON.stringify(event),
mode: "no-cors",
headers: {
"Content-Type": "application/json",
},
});
}
}, [pathname, searchParams]);
return <></>;
}