Files
Webserver/components/Analytics.tsx

37 lines
1.1 KiB
TypeScript
Raw Normal View History

2026-02-08 03:01:52 -05:00
'use client';
import { useEffect } from 'react';
2026-05-25 09:49:40 -04:00
import { usePathname, useSearchParams } from 'next/navigation';
2026-02-08 03:01:52 -05:00
export function Analytics() {
const pathname = usePathname();
2026-05-25 09:49:40 -04:00
const searchParams = useSearchParams();
const queryString = searchParams.toString();
const visitPath = queryString ? `${pathname}?${queryString}` : pathname;
2026-02-08 03:01:52 -05:00
useEffect(() => {
// Send beacon on mount and path change
const sendBeacon = async () => {
try {
2026-02-08 14:05:15 -05:00
// Send beacon to THIS website's API, which will relay it to the admin dash
await fetch('/api/analytics', {
2026-02-08 03:01:52 -05:00
method: 'POST',
2026-05-25 09:49:40 -04:00
body: JSON.stringify({ path: visitPath }),
2026-02-08 23:18:21 -05:00
headers: {
'Content-Type': 'application/json',
},
2026-02-08 03:01:52 -05:00
keepalive: true,
});
} catch (e) {
2026-05-25 09:49:40 -04:00
if (process.env.NODE_ENV !== 'production') {
console.error('Analytics fail', e);
}
2026-02-08 03:01:52 -05:00
}
};
sendBeacon();
2026-05-25 09:49:40 -04:00
}, [visitPath]);
2026-02-08 03:01:52 -05:00
return null;
}