This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect } from 'react';
|
||||
import { usePathname } from 'next/navigation';
|
||||
import { usePathname, useSearchParams } from 'next/navigation';
|
||||
|
||||
export function Analytics() {
|
||||
const pathname = usePathname();
|
||||
const searchParams = useSearchParams();
|
||||
const queryString = searchParams.toString();
|
||||
const visitPath = queryString ? `${pathname}?${queryString}` : pathname;
|
||||
|
||||
useEffect(() => {
|
||||
// Send beacon on mount and path change
|
||||
@@ -13,21 +16,21 @@ export function Analytics() {
|
||||
// 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() }),
|
||||
body: JSON.stringify({ path: visitPath }),
|
||||
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);
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
console.error('Analytics fail', e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
sendBeacon();
|
||||
}, [pathname]);
|
||||
}, [visitPath]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user