Files
Webserver/components/Analytics.tsx
Shivam Patel c0ffc01b88
All checks were successful
Deploy Website / build-and-deploy (push) Successful in 28s
shivam + claude changes
2026-02-08 23:18:21 -05:00

34 lines
1.0 KiB
TypeScript

'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 {
// Send beacon to THIS website's API, which will relay it to the admin dash
await fetch('/api/analytics', {
method: 'POST',
body: JSON.stringify({ path: pathname, timestamp: Date.now() }),
headers: {
'Content-Type': 'application/json',
'X-Analytics-Key': process.env.NEXT_PUBLIC_ANALYTICS_KEY || 'default-analytics-key',
},
keepalive: true,
});
} catch (e) {
// Fail silently
console.error('Analytics fail', e);
}
};
sendBeacon();
}, [pathname]);
return null;
}