Added visitor analytics

This commit is contained in:
Akshay Kolli
2026-02-08 03:01:52 -05:00
parent e8e44c6162
commit 35f7254734
2 changed files with 37 additions and 0 deletions

View File

@@ -15,6 +15,8 @@ export const metadata: Metadata = {
description: "My personal website", description: "My personal website",
}; };
import { Analytics } from "../components/Analytics";
export default function RootLayout({ export default function RootLayout({
children, children,
}: Readonly<{ }: Readonly<{
@@ -30,6 +32,7 @@ export default function RootLayout({
{children} {children}
</main> </main>
<Footer /> <Footer />
<Analytics />
</body> </body>
</html> </html>
); );

34
components/Analytics.tsx Normal file
View File

@@ -0,0 +1,34 @@
'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 {
// Point to the Admin Dashboard API
// In prod, this URL needs to be the absolute URL of admin_dash
// For local docker, standard localhost access might work if CORS allows,
// or we route through a proxy.
// For this personal setup, let's assume they are on same domain or localhost.
await fetch('http://localhost:3000/api/track', { // TODO: Make this configurable
method: 'POST',
body: JSON.stringify({ path: pathname, timestamp: Date.now() }),
headers: { 'Content-Type': 'application/json' },
keepalive: true,
});
} catch (e) {
// Fail silently
console.error('Analytics fail', e);
}
};
sendBeacon();
}, [pathname]);
return null;
}