Files
Webserver/components/Analytics.tsx

34 lines
1.0 KiB
TypeScript
Raw Permalink Normal View History

2026-02-08 03:01:52 -05:00
'use client';
import { useEffect } from 'react';
import { usePathname } from 'next/navigation';
export function Analytics() {
const pathname = usePathname();
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',
body: JSON.stringify({ path: pathname, timestamp: Date.now() }),
2026-02-08 23:18:21 -05:00
headers: {
'Content-Type': 'application/json',
'X-Analytics-Key': process.env.NEXT_PUBLIC_ANALYTICS_KEY || 'default-analytics-key',
},
2026-02-08 03:01:52 -05:00
keepalive: true,
});
} catch (e) {
// Fail silently
console.error('Analytics fail', e);
}
};
sendBeacon();
}, [pathname]);
return null;
}