made analytics work

This commit is contained in:
anonymousratwastaken 2025-12-21 09:39:58 +00:00
parent 43a650a9b6
commit 06bf69c167
2 changed files with 28 additions and 14 deletions

View file

@ -1,24 +1,39 @@
"use client"; "use client";
import { useEffect, useState } from "react";
import { Analytics } from "@vercel/analytics/next"; import { Analytics } from "@vercel/analytics/next";
import { SpeedInsights } from "@vercel/speed-insights/next"; import { SpeedInsights } from "@vercel/speed-insights/next";
export function ConditionalAnalytics() { export function ConditionalAnalytics() {
const [analyticsEnabled, setAnalyticsEnabled] = useState(false);
useEffect(() => {
// check if analytics are enabled
const enabled = localStorage.getItem("analytics-enabled") === "true";
setAnalyticsEnabled(enabled);
}, []);
if (!analyticsEnabled) return null;
return ( return (
<> <>
<Analytics /> <Analytics
<SpeedInsights /> beforeSend={(event) => {
// check if user has consented
const analyticsEnabled = localStorage.getItem("analytics-enabled") === "true";
// if not consented, return null to block the event
if (!analyticsEnabled) {
return null;
}
// if consented, return the event to send it
return event;
}}
/>
<SpeedInsights
beforeSend={(event) => {
// check if user has consented
const analyticsEnabled = localStorage.getItem("analytics-enabled") === "true";
// if not consented, return null to block the event
if (!analyticsEnabled) {
return null;
}
// if consented, return the event to send it
return event;
}}
/>
</> </>
); );
} }

View file

@ -23,7 +23,6 @@ export function CookieConsent() {
onClick: () => { onClick: () => {
localStorage.setItem("cookie-consent", "accepted"); localStorage.setItem("cookie-consent", "accepted");
localStorage.setItem("analytics-enabled", "true"); localStorage.setItem("analytics-enabled", "true");
window.location.reload();
}, },
}, },
cancel: { cancel: {