Skip to content

feat(dashboard): share cookie choice across nhost.io and dashboard - #5352

Merged
nunopato merged 8 commits into
mainfrom
fix/cookie-consent-analytics
Nov 26, 2026
Merged

nunopato merged 8 commits into
mainfrom
fix/cookie-consent-analytics

Conversation

@nunopato

@nunopato nunopato commented Nov 9, 2026

Copy link
Copy Markdown
Member

Goal

Both Nhost web properties — the marketing site nhost.io and the dashboard at app.nhost.io — loaded Segment (and the Google Ads tag Segment pulls in), and the marketing site also loaded Plausible, in production before the visitor had made any cookie choice. The cookie banner existed but did not actually gate loading, and the two properties did not share a single decision. This change defers all Segment loading until the user explicitly accepts cookies, stores that one accept/reject decision in a single first-party cookie scoped to nhost.io so it is shared across both properties, clears persisted Segment identifiers when the user rejects or resets consent, and removes the Plausible integration from the marketing site.

Type: Enhancement

Changes

  • Deferred Segment loading (both apps) — dashboard/src/lib/segment.ts and landing/src/lib/segment.ts now export an unloaded new AnalyticsBrowser() that buffers calls in memory and sends nothing. A new idempotent loadAnalytics() calls analytics.load(...) exactly once (guarded by a module-level loaded flag) and is invoked only after consent. The disable flag still restricts real delivery to production (dashboard: isAnalyticsEnabled() = isPlatform() && !isDevOrStaging(); landing: NEXT_PUBLIC_VERCEL_ENV === 'production'), so outside production Segment loads as a no-op but the banner still works.
  • Single shared consent cookie — new dashboard/.../CookieConsent/consentCookie.ts and landing/src/lib/cookieConsent.ts (intentionally duplicated logic, each pointing at the other) read/write/delete a nhost-cookie-consent cookie holding accepted/rejected, with a 6-month Max-Age, SameSite=Lax, Secure on https, and Domain=nhost.io when the host is nhost.io or a subdomain. getCookieDomain() returns undefined on localhost/preview hosts so the cookie stays host-only there. Scoping to the apex domain is what lets the dashboard and marketing site read one choice.
  • Consent hook (both apps) — useCookieConsent reads the cookie on the client (state is undefined until read, keeping SSR/hydration consistent) and returns [consent, setConsent]. The dashboard variant adds cross-component synchronization through a custom nhost-cookie-consent-change window event and migrates a legacy cookie-consent localStorage value into the cookie; it also clears analytics storage whenever the resolved choice is false or null (i.e. rejected, or reset on the other property).
  • Clearing analytics identifiers — new clearAnalyticsStorage() synchronously removes the Segment ajs_* keys from localStorage and expires the ajs_* cookies on both the current host and nhost.io, without loading or awaiting Segment. resetAnalytics() clears storage and, only if Segment was already loaded, awaits analytics.reset() and clears again.
  • Re-open the choice — resetCookieConsent (wired into the dashboard account menu and the landing footer as a "Cookie settings" control) runs resetAnalytics(), deletes the consent cookie, and reloads so the banner reappears.
  • Banner placement and copy — the dashboard banner moved out of signup.tsx into _app.tsx, rendered globally when isPlatform() with loadAnalytics as its onAccept; signup.tsx now only initializes Google Ads reactively once consent is true and analytics is enabled. The dashboard CookieConsent.tsx derives visibility from the hook (hidden once the choice is non-null) and links to the cookies policy. The landing site gains a new CookieConsent.tsx (Accept/Reject) rendered by the Analytics component.
  • Remove Plausible (marketing site) — drops the next-plausible dependency and PlausibleProvider from landing/.../_app.tsx; Analytics is now always rendered (banner everywhere, Segment only loads in production) and the BlogListing doc comment is updated accordingly.
  • getAnonId guard — dashboard getAnonId() returns undefined when Segment has not been loaded, instead of awaiting an unloaded instance.

Flow

flowchart TD
    A[Page load] --> B{nhost-cookie-consent cookie}
    B -- accepted --> C[loadAnalytics: Segment loads, buffered calls flush]
    B -- rejected/null --> D[clearAnalyticsStorage: drop ajs_* keys]
    B -- no choice --> E[Show cookie banner]
    E -- Accept --> F[writeConsentCookie accepted + loadAnalytics]
    E -- Reject --> G[writeConsentCookie rejected + clearAnalyticsStorage]
    H[Cookie settings control] --> I[resetAnalytics + deleteConsentCookie + reload]
Loading

Key files

  • dashboard/src/lib/segment.ts (+80/-8), landing/src/lib/segment.ts (+69/-5) — deferred load, storage clearing, reset.
  • dashboard/src/components/common/CookieConsent/consentCookie.ts (+77), landing/src/lib/cookieConsent.ts (+52) — shared cookie contract (duplicated).
  • dashboard/.../CookieConsent/useCookieConsent.ts (+54), landing/src/components/analytics/useCookieConsent.ts (+45) — consent hook and reset helper.
  • dashboard/src/pages/_app.tsx, dashboard/src/pages/signup.tsx, landing/src/pages/_app.tsx, landing/src/components/analytics/analytics.tsx — banner placement and consent-gated loading.
  • landing/src/components/analytics/CookieConsent.tsx (+34), dashboard CookieConsent.tsx / AccountMenuActions.tsx, landing Footer.tsx — banner UI and "Cookie settings" entry points.
  • landing/package.json, landing/.../BlogListing.tsx — Plausible removal and comment update.
  • Tests (grouped, +918): */segment.test.ts, */analyticsStorage.test.ts, */cookieConsent.test.ts, */useCookieConsent.test.ts.

Review issue: Review finding 01 — .nhost-code/review/PR_fix-cookie-consent-analytics_COMMENT_001.md — ADDRESSED, ACCEPT
Review issue: Review finding 02 — .nhost-code/review/PR_fix-cookie-consent-analytics_COMMENT_002.md — ADDRESSED, ACCEPT
Resolution:
- resetAnalytics() in both the dashboard and landing now calls only the loaded
  instance's synchronous reset() (analytics.instance?.reset()) and never waits on
  the buffered AnalyticsBrowser.reset(), so the "Cookie settings" reset completes
  even when Segment failed to load.
- Both resetCookieConsent() helpers catch and log SDK reset errors and always
  delete the consent cookie and reload in a finally block, so a hanging or
  throwing SDK can no longer strand consent withdrawal.
- Added matching regression tests in both apps for pending/rejecting buffered
  resets and a throwing instance reset.
Why:
- A buffered AnalyticsBrowser call only settles after a successful load; when the
  CDN settings fetch is blocked the SDK swallows the error and the call never
  settles, hanging the reset. Analytics#reset() is synchronous in the installed
  SDK, so calling it on analytics.instance avoids the hang without timers.
Decisions:
- Made resetAnalytics() and both resetCookieConsent() helpers synchronous; their
  only callers are onClick handlers. Preserved loaded, getAnonId, and consent
  cookie read behavior. Switched affected suites to vi.resetAllMocks() so unused
  mockImplementationOnce queues do not leak between tests.
Checks:
- `biome check` (dashboard, 4 files) — exit 0
- `tsc --noEmit` (dashboard) — exit 0
- `vitest run` (dashboard targeted) — exit 0: 32/32 passed
- `vitest run` (landing targeted) — exit 0: 33/33 passed
- `prettier --check` (landing, 4 files) — exit 0
- `tsc --noEmit` (landing) — exit 0
- `eslint .` (landing) — exit 2: cannot start; pre-existing ajv/env crash that reproduces on untouched files, WU-001 changes no package/lock (operator approved committing)
Review issue: Review finding 03 — .nhost-code/review/PR_fix-cookie-consent-analytics_COMMENT_003.md — ADDRESSED, ACCEPT
Resolution:
- migrateLegacyConsent() now writes the shared nhost-cookie-consent cookie only
  for a legacy rejection (localStorage cookie-consent='false'); a legacy
  acceptance is dropped while the legacy key is still removed, so the new
  wider-disclosure banner is shown again. Documented the decision in the function
  comment and updated the hook test to assert null consent, no shared cookie, and
  a removed legacy key.
Why:
- A legacy acceptance came from the old signup-only banner with narrower wording.
  Migrating it would write a shared Domain=nhost.io accepted cookie that loads
  Segment and Google Ads on nhost.io and across the dashboard without the user
  seeing the new banner. Carrying over only rejections never widens consent.
Decisions:
- Applied the operator's rejection-only migration decision (operator-approved)
  without changing cookie attributes or landing behavior.
Checks:
- `biome check` (dashboard, 2 files) — exit 0
- `tsc --noEmit` (dashboard) — exit 0
- `vitest run` (dashboard targeted) — exit 0: 32/32 passed
@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Vercel Deployment Info - landing

@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Vercel Deployment Info - dashboard

@nunopato nunopato changed the title Fix/cookie consent analytics feat(consent): share cookie choice across nhost.io and dashboard Nov 10, 2026
@nunopato nunopato changed the title feat(consent): share cookie choice across nhost.io and dashboard feat(dashboard): share cookie choice across nhost.io and dashboard Nov 10, 2026
@nunopato
nunopato merged commit d50e76b into main Nov 26, 2026
14 checks passed
@nunopato
nunopato deleted the fix/cookie-consent-analytics branch November 26, 2026 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants