Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
✨ Add cookie consent banner with cookieless analytics fallback (#3114)
Luke Vella137a92fcdf1e
diff --git a/apps/landing/public/locales/en/common.json b/apps/landing/public/locales/en/common.jsonindex ebffbc572..ac5aeb143 100644--- a/apps/landing/public/locales/en/common.json+++ b/apps/landing/public/locales/en/common.json@@ -3,6 +3,11 @@"close": "Close","committees": "Committees and boards","compare": "Compare",+ "cookieConsentAccept": "Accept",+ "cookieConsentIntro": "We use cookies to improve your experience.",+ "cookieConsentLabel": "Cookie consent",+ "cookieConsentOptOut": "Opt out",+ "cookieConsentPolicy": "Learn more in our <policyLink>cookie policy</policyLink>.","cookiePolicy": "Cookie policy","discussions": "Discussions","doodleAlternative": "Doodle alternative",diff --git a/apps/landing/src/app/[locale]/layout.tsx b/apps/landing/src/app/[locale]/layout.tsxindex b85d65b32..1ba2686dc 100644--- a/apps/landing/src/app/[locale]/layout.tsx+++ b/apps/landing/src/app/[locale]/layout.tsx@@ -1,10 +1,10 @@import "./globals.css";import languages from "@rallly/languages";-import { Analytics } from "@vercel/analytics/react";import { domAnimation, LazyMotion } from "motion/react";import type { Metadata, Viewport } from "next";import { cacheLife } from "next/cache";+import { CookieConsent } from "@/components/cookie-consent";import { sans } from "@/fonts/sans";import { I18nProvider } from "@/i18n/client/i18n-provider";import { getTranslation } from "@/i18n/server";@@ -36,9 +36,9 @@ export default async function Root(props: {<LazyMotion features={domAnimation}><I18nProvider locale={i18n.resolvedLanguage} resources={translations}>{children}+ <CookieConsent /></I18nProvider></LazyMotion>- <Analytics /></body></html>);diff --git a/apps/landing/src/components/cookie-consent.tsx b/apps/landing/src/components/cookie-consent.tsxnew file mode 100644index 000000000..01cd118de--- /dev/null+++ b/apps/landing/src/components/cookie-consent.tsx@@ -0,0 +1,50 @@+"use client";+import { useCookieConsent } from "@rallly/posthog/client";+import { Button } from "@rallly/ui/button";+import { Analytics } from "@vercel/analytics/react";+import { LinkBase } from "@/i18n/client/link";+import { Trans } from "@/i18n/client/trans";+import { useTranslation } from "@/i18n/client/use-translation";++export function CookieConsent() {+ const { t } = useTranslation();+ const { status, accept, reject } = useCookieConsent();++ return (+ <>+ {status === "granted" ? <Analytics /> : null}+ {status === "pending" ? (+ <section+ aria-label={t("cookieConsentLabel", {+ defaultValue: "Cookie consent",+ })}+ className="fixed bottom-4 left-4 z-50 flex max-w-[min(32rem,calc(100vw-2rem))] flex-wrap items-center gap-x-6 gap-y-3 rounded-xl border bg-white p-4 shadow-lg"+ >+ <p className="min-w-0 flex-1 basis-56 text-pretty text-sm">+ <Trans+ i18nKey="cookieConsentIntro"+ defaults="We use cookies to improve your experience."+ />{" "}+ <Trans+ i18nKey="cookieConsentPolicy"+ defaults="Learn more in our <policyLink>cookie policy</policyLink>."+ components={{+ policyLink: (+ <LinkBase className="underline" href="/cookie-policy" />+ ),+ }}+ />+ </p>+ <div className="flex gap-2">+ <Button onClick={reject}>+ <Trans i18nKey="cookieConsentOptOut" defaults="Opt out" />+ </Button>+ <Button variant="primary" onClick={accept}>+ <Trans i18nKey="cookieConsentAccept" defaults="Accept" />+ </Button>+ </div>+ </section>+ ) : null}+ </>+ );+}diff --git a/apps/web/instrumentation-client.ts b/apps/web/instrumentation-client.tsindex e851c2572..ab856a8c5 100644--- a/apps/web/instrumentation-client.ts+++ b/apps/web/instrumentation-client.ts@@ -10,6 +10,7 @@ const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;Sentry.init({dsn: SENTRY_DSN,+ sendDefaultPii: false,// Adjust this value in production, or use tracesSampler for greater controltracesSampleRate: 0.2,diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.jsonindex 446d90e3b..a978cf369 100644--- a/apps/web/public/locales/en/app.json+++ b/apps/web/public/locales/en/app.json@@ -171,6 +171,11 @@"continueToCheckout": "Continue to checkout","continueWithEmail": "Continue with email","controlPanel": "Control Panel",+ "cookieConsentAccept": "Accept",+ "cookieConsentIntro": "We use cookies to improve your experience.",+ "cookieConsentLabel": "Cookie consent",+ "cookieConsentOptOut": "Opt out",+ "cookieConsentPolicy": "Learn more in our <policyLink>cookie policy</policyLink>.","copied": "Copied","copiedToClipboard": "Copied to clipboard","copy": "Copy",diff --git a/apps/web/src/app/[locale]/layout.tsx b/apps/web/src/app/[locale]/layout.tsxindex 49f18e253..d07638887 100644--- a/apps/web/src/app/[locale]/layout.tsx+++ b/apps/web/src/app/[locale]/layout.tsx@@ -9,6 +9,7 @@ import { Inter } from "next/font/google";import { notFound } from "next/navigation";import type React from "react";import type { Params } from "@/app/[locale]/types";+import { CookieConsentBanner } from "@/components/cookie-consent-banner";import { SkipNavLink } from "@/components/skip-nav-link";import { BrandingProvider } from "@/features/branding/client";import { getInstanceBrandingConfig } from "@/features/branding/data";@@ -83,6 +84,7 @@ export default async function Root({<TimeZoneSync><TooltipProvider>{children}</TooltipProvider></TimeZoneSync>+ <CookieConsentBanner /></LazyMotion></TRPCProvider></I18nProvider>diff --git a/apps/web/src/components/cookie-consent-banner.tsx b/apps/web/src/components/cookie-consent-banner.tsxnew file mode 100644index 000000000..7777b7b5a--- /dev/null+++ b/apps/web/src/components/cookie-consent-banner.tsx@@ -0,0 +1,55 @@+"use client";+import { useCookieConsent } from "@rallly/posthog/client";+import { Button } from "@rallly/ui/button";+import { isSelfHosted } from "@/components/environment";+import { Trans, useTranslation } from "@/i18n/client";++export function CookieConsentBanner() {+ const { t } = useTranslation();+ const { status, accept, reject } = useCookieConsent();++ if (status !== "pending") {+ return null;+ }++ return (+ <section+ aria-label={t("cookieConsentLabel", { defaultValue: "Cookie consent" })}+ className="fixed bottom-4 left-4 z-50 flex max-w-[min(32rem,calc(100vw-2rem))] flex-wrap items-center gap-x-6 gap-y-3 rounded-xl border bg-background p-4 shadow-lg"+ >+ <p className="min-w-0 flex-1 basis-56 text-pretty text-sm">+ <Trans+ i18nKey="cookieConsentIntro"+ defaults="We use cookies to improve your experience."+ />+ {isSelfHosted ? null : (+ <>+ {" "}+ <Trans+ i18nKey="cookieConsentPolicy"+ defaults="Learn more in our <policyLink>cookie policy</policyLink>."+ components={{+ policyLink: (+ <a+ className="underline"+ href="https://rallly.co/cookie-policy"+ target="_blank"+ rel="noreferrer"+ />+ ),+ }}+ />+ </>+ )}+ </p>+ <div className="flex gap-2">+ <Button onClick={reject}>+ <Trans i18nKey="cookieConsentOptOut" defaults="Opt out" />+ </Button>+ <Button variant="primary" onClick={accept}>+ <Trans i18nKey="cookieConsentAccept" defaults="Accept" />+ </Button>+ </div>+ </section>+ );+}diff --git a/packages/posthog/src/client.ts b/packages/posthog/src/client.tsindex 0a8dbae78..e0e2c136d 100644--- a/packages/posthog/src/client.ts+++ b/packages/posthog/src/client.ts@@ -1,5 +1,6 @@"use client";import posthog from "posthog-js";+import React from "react";export { useFeatureFlagEnabled } from "posthog-js/react";@@ -21,7 +22,19 @@ if (typeof window !== "undefined" && process.env.NEXT_PUBLIC_POSTHOG_API_KEY) {enable_heatmaps: false,persistence: "cookie",autocapture: false,- opt_out_capturing_by_default: isGlobalPrivacyControlEnabled(),+ // ePrivacy consent model: until the user accepts the cookie banner —+ // and permanently if they reject it — nothing is stored on the device+ // (no ph_* cookie) and events are captured cookielessly: the client+ // sends a sentinel distinct_id and the server substitutes a+ // daily-rotating hash of IP+UA. Requires "Cookieless server hash mode"+ // in PostHog project settings; without it these events are dropped at+ // ingestion. opt_in_capturing() switches to full cookie persistence.+ cookieless_mode: "on_reject",+ opt_out_capturing_by_default: true,+ opt_out_persistence_by_default: true,+ // The consent decision rides the apex-domain cookie like the identity+ // cookie, so a choice made on the landing site carries to the app.+ opt_out_capturing_persistence_type: "cookie",cross_subdomain_cookie: true,capture_performance: {web_vitals: false,@@ -42,3 +55,39 @@ if (typeof window !== "undefined" && process.env.NEXT_PUBLIC_POSTHOG_API_KEY) {}export { posthog };++/**+ * Consent state for the cookie banner, backed by PostHog's own consent+ * store (a cross-subdomain first-party cookie — strictly necessary, so+ * exempt from consent itself).+ *+ * `status` is null until mounted (the decision is client-side state, so+ * banners must render nothing on the server), and stays null when there is+ * no PostHog key or the browser sends Global Privacy Control — GPC is an+ * opt-out signal, so showing the banner would ask the user to override a+ * choice they already expressed; capture stays cookieless instead.+ */+export function useCookieConsent() {+ const [status, setStatus] = React.useState<+ "pending" | "granted" | "denied" | null+ >(null);++ React.useEffect(() => {+ if (!posthog.__loaded || isGlobalPrivacyControlEnabled()) {+ return;+ }+ setStatus(posthog.get_explicit_consent_status());+ }, []);++ const accept = React.useCallback(() => {+ posthog.opt_in_capturing();+ setStatus("granted");+ }, []);++ const reject = React.useCallback(() => {+ posthog.opt_out_capturing();+ setStatus("denied");+ }, []);++ return { status, accept, reject };+}