Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 12 additions & 56 deletions apps/blog/src/instrumentation-client.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}
},
});
Comment thread
carlagn marked this conversation as resolved.
45 changes: 2 additions & 43 deletions apps/docs/src/instrumentation-client.ts
Original file line number Diff line number Diff line change
@@ -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",

Expand Down
68 changes: 12 additions & 56 deletions apps/site/src/instrumentation-client.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}
},
});
Loading