From 41e0b5d04588793d638b28530220200e9f4e21e2 Mon Sep 17 00:00:00 2001 From: Marc Hess Date: Fri, 8 May 2026 08:18:20 -0400 Subject: [PATCH] Revert consent-gated PostHog capture --- apps/blog/src/instrumentation-client.ts | 68 +++++-------------------- apps/docs/src/instrumentation-client.ts | 45 +--------------- apps/site/src/instrumentation-client.ts | 68 +++++-------------------- 3 files changed, 26 insertions(+), 155 deletions(-) diff --git a/apps/blog/src/instrumentation-client.ts b/apps/blog/src/instrumentation-client.ts index 61342594c8..be223868f3 100644 --- a/apps/blog/src/instrumentation-client.ts +++ b/apps/blog/src/instrumentation-client.ts @@ -1,57 +1,13 @@ -/** - * Returns true if the visitor has already accepted analytics cookies via - * CookieYes (i.e. the consent cookie is present and includes "analytics:yes"). - * Used to immediately opt returning visitors in without waiting for the banner. - */ -function hasAnalyticsConsent(): boolean { - try { - const match = document.cookie - .split("; ") - .find((c) => c.startsWith("cookieyes-consent=")); - if (!match) return false; - return decodeURIComponent(match.split("=")[1]).includes("analytics:yes"); - } catch { - return false; - } -} - -const initPostHog = () => { - import("posthog-js").then(({ default: posthog }) => { - posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, { - api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST, - capture_pageview: "history_change", - defaults: "2025-11-30", - opt_out_capturing_by_default: true, - loaded: (ph) => { - ph.register({ - site_name: "mono-blog", - environment: "production", - }); - }, +import posthog from "posthog-js"; + +posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, { + api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST, + capture_pageview: "history_change", + defaults: "2025-11-30", + loaded: (posthog) => { + posthog.register({ + site_name: "mono-blog", + environment: "production", }); - - if (hasAnalyticsConsent()) { - posthog.opt_in_capturing(); - } - - document.addEventListener("cookieyes-consent-update", (event: Event) => { - const detail = (event as CustomEvent<{ accepted: string[] }>).detail; - if ( - Array.isArray(detail?.accepted) && - detail.accepted.includes("analytics") - ) { - posthog.opt_in_capturing(); - } else { - posthog.opt_out_capturing(); - } - }); - }); -}; - -if (typeof window !== "undefined") { - if (document.readyState === "complete") { - initPostHog(); - } else { - window.addEventListener("load", initPostHog, { once: true }); - } -} + }, +}); diff --git a/apps/docs/src/instrumentation-client.ts b/apps/docs/src/instrumentation-client.ts index 88e7958c50..63958b191b 100644 --- a/apps/docs/src/instrumentation-client.ts +++ b/apps/docs/src/instrumentation-client.ts @@ -1,59 +1,18 @@ import posthog from "posthog-js"; import * as Sentry from "@sentry/nextjs"; -/** - * Returns true when the visitor has already accepted analytics cookies via - * the CookieYes consent banner. Checked on every page load so that returning - * visitors who previously consented are opted-in immediately, without waiting - * for the banner to re-appear. - */ -function hasAnalyticsConsent(): boolean { - try { - const match = document.cookie - .split("; ") - .find((c) => c.startsWith("cookieyes-consent=")); - if (!match) return false; - return decodeURIComponent(match.split("=")[1]).includes("analytics:yes"); - } catch { - return false; - } -} - posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, { api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST, capture_pageview: "history_change", defaults: "2025-11-30", - // Do not capture anything until the visitor explicitly grants analytics - // consent via CookieYes. Opt-in logic is handled below. - opt_out_capturing_by_default: true, - loaded: (ph) => { - ph.register({ + loaded: (posthog) => { + posthog.register({ site_name: "mono-docs", environment: "production", }); }, }); -// Returning visitor: consent cookie already present — opt in immediately. -if (hasAnalyticsConsent()) { - posthog.opt_in_capturing(); -} - -// New / updating visitor: react to live banner interactions. -// CookieYes fires "cookieyes-consent-update" on the document whenever the -// visitor accepts or rejects categories from the consent banner. -document.addEventListener("cookieyes-consent-update", (event: Event) => { - const detail = (event as CustomEvent<{ accepted: string[] }>).detail; - if ( - Array.isArray(detail?.accepted) && - detail.accepted.includes("analytics") - ) { - posthog.opt_in_capturing(); - } else { - posthog.opt_out_capturing(); - } -}); - Sentry.init({ dsn: "https://e83ce4699e59051fdeaa330bf4a0dfb9@o4510879743737856.ingest.us.sentry.io/4510879744000000", diff --git a/apps/site/src/instrumentation-client.ts b/apps/site/src/instrumentation-client.ts index b802cfb121..4f59745388 100644 --- a/apps/site/src/instrumentation-client.ts +++ b/apps/site/src/instrumentation-client.ts @@ -1,57 +1,13 @@ -/** - * Returns true if the visitor has already accepted the "analytics" category - * via the CookieYes consent banner (i.e. on a return visit where the consent - * cookie is already present). - */ -function hasAnalyticsConsent(): boolean { - try { - const match = document.cookie - .split("; ") - .find((c) => c.startsWith("cookieyes-consent=")); - if (!match) return false; - return decodeURIComponent(match.split("=")[1]).includes("analytics:yes"); - } catch { - return false; - } -} - -const initPostHog = () => { - import("posthog-js").then(({ default: posthog }) => { - posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, { - api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST, - capture_pageview: "history_change", - defaults: "2025-11-30", - opt_out_capturing_by_default: true, - loaded: (ph) => { - ph.register({ - site_name: "mono-site", - environment: "production", - }); - }, +import posthog from "posthog-js"; + +posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, { + api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST, + capture_pageview: "history_change", + defaults: "2025-11-30", + loaded: (posthog) => { + posthog.register({ + site_name: "mono-site", + environment: "production", }); - - if (hasAnalyticsConsent()) { - posthog.opt_in_capturing(); - } - - document.addEventListener("cookieyes-consent-update", (event: Event) => { - const detail = (event as CustomEvent<{ accepted: string[] }>).detail; - if ( - Array.isArray(detail?.accepted) && - detail.accepted.includes("analytics") - ) { - posthog.opt_in_capturing(); - } else { - posthog.opt_out_capturing(); - } - }); - }); -}; - -if (typeof window !== "undefined") { - if (document.readyState === "complete") { - initPostHog(); - } else { - window.addEventListener("load", initPostHog, { once: true }); - } -} + }, +});