AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend): set every Google Consent Mode default before the banner answers (#15936)
Krzysztof Czerwinski2ec5d4eeaa05
diff --git a/autogpt_platform/frontend/src/app/__tests__/layout.test.tsx b/autogpt_platform/frontend/src/app/__tests__/layout.test.tsxindex 3dc482320d..8098f9af76 100644--- a/autogpt_platform/frontend/src/app/__tests__/layout.test.tsx+++ b/autogpt_platform/frontend/src/app/__tests__/layout.test.tsx@@ -77,9 +77,11 @@ describe("RootLayout consent scripts", () => {strategy: "beforeInteractive","data-cookieconsent": "ignore",});- expect(defaults.dangerouslySetInnerHTML?.__html).toContain(- "gtag('consent','default'",- );+ const defaultsScript = defaults.dangerouslySetInnerHTML?.__html;+ expect(defaultsScript).toContain("gtag('consent','default'");+ expect(defaultsScript).toContain('"functionality_storage":"denied"');+ expect(defaultsScript).toContain('"personalization_storage":"denied"');+ expect(defaultsScript).toContain('"security_storage":"granted"');expect(cookiebot).toEqual({id: "Cookiebot",src: "https://consent.cookiebot.com/uc.js",diff --git a/autogpt_platform/frontend/src/services/analytics/__tests__/consent-consumers.test.tsx b/autogpt_platform/frontend/src/services/analytics/__tests__/consent-consumers.test.tsxindex 9cfb6b8dc9..4140a556b0 100644--- a/autogpt_platform/frontend/src/services/analytics/__tests__/consent-consumers.test.tsx+++ b/autogpt_platform/frontend/src/services/analytics/__tests__/consent-consumers.test.tsx@@ -112,6 +112,8 @@ describe("with NEXT_PUBLIC_COOKIEBOT_CBID", () => {ad_storage: "denied",ad_user_data: "denied",ad_personalization: "denied",+ functionality_storage: "denied",+ personalization_storage: "denied",},]);delete (window as unknown as { dataLayer?: unknown }).dataLayer;@@ -234,6 +236,15 @@ describe("ConsentWithdrawalReload", () => {expect(reload).toHaveBeenCalledOnce();});+ it("does not reload when only preferences are taken back", () => {+ installCookiebot({ statistics: true, preferences: true });+ render(<ConsentWithdrawalReload />);++ act(() => answerCookiebot({ statistics: true }));++ expect(reload).not.toHaveBeenCalled();+ });+it("does not reload when the visitor grants more, or repeats the same answer", () => {installCookiebot();render(<ConsentWithdrawalReload />);diff --git a/autogpt_platform/frontend/src/services/analytics/consent-mode.test.ts b/autogpt_platform/frontend/src/services/analytics/consent-mode.test.tsindex dc79ece533..34de156e85 100644--- a/autogpt_platform/frontend/src/services/analytics/consent-mode.test.ts+++ b/autogpt_platform/frontend/src/services/analytics/consent-mode.test.ts@@ -24,8 +24,9 @@ function consentUpdates() {);}-function update(analytics: boolean, advertising: boolean) {+function update(analytics: boolean, advertising: boolean, preferences = false) {const ads = advertising ? "granted" : "denied";+ const prefs = preferences ? "granted" : "denied";return ["consent","update",@@ -34,6 +35,8 @@ function update(analytics: boolean, advertising: boolean) {ad_storage: ads,ad_user_data: ads,ad_personalization: ads,+ functionality_storage: prefs,+ personalization_storage: prefs,},];}@@ -45,27 +48,56 @@ function runDefaultsScript(): unknown[][] {);}-const ALL_DENIED = {+const DEFAULTS = {ad_storage: "denied",ad_user_data: "denied",ad_personalization: "denied",analytics_storage: "denied",+ functionality_storage: "denied",+ personalization_storage: "denied",+ security_storage: "granted",wait_for_update: 500,};+// Every Consent Mode v2 signal; Google treats one without a default as not+// denied.+const CONSENT_SIGNALS = [+ "ad_storage",+ "ad_user_data",+ "ad_personalization",+ "analytics_storage",+ "functionality_storage",+ "personalization_storage",+ "security_storage",+];+describe("buildConsentDefaultsScript", () => {afterEach(() => {delete (window as DataLayerWindow).dataLayer;delete window.gtag;});- it("denies every signal for every visitor and passes click IDs through URLs", () => {+ it("denies every signal that needs consent for every visitor and passes click IDs through URLs", () => {expect(runDefaultsScript()).toEqual([- ["consent", "default", ALL_DENIED],+ ["consent", "default", DEFAULTS],["set", "url_passthrough", true],]);});+ it("sets a default for every Consent Mode signal, granting only security storage", () => {+ const [[, , defaults]] = runDefaultsScript() as [+ [string, string, Record<string, unknown>],+ ];++ expect(Object.keys(defaults)).toEqual(+ expect.arrayContaining(CONSENT_SIGNALS),+ );+ const granted = CONSENT_SIGNALS.filter(+ (name) => defaults[name] === "granted",+ );+ expect(granted).toEqual(["security_storage"]);+ });+it("sets no region-specific default", () => {// The vendored gtag.js resolves every visitor to the location baked into// it, so a region-specific default would apply to everyone.@@ -124,7 +156,7 @@ describe("followConsentForGoogleTag", () => {answerCookiebot({ statistics: true, marketing: true });expect(dataLayerEntries()).toEqual([- ["consent", "default", ALL_DENIED],+ ["consent", "default", DEFAULTS],["set", "url_passthrough", true],["consent",@@ -134,12 +166,43 @@ describe("followConsentForGoogleTag", () => {ad_storage: "granted",ad_user_data: "granted",ad_personalization: "granted",+ functionality_storage: "denied",+ personalization_storage: "denied",},],]);unfollow();});+ it("sends preferences consent as the functionality and personalization signals", () => {+ configureCookiebot();+ installCookiebot();+ const unfollow = followConsentForGoogleTag();++ answerCookiebot({ preferences: true });+ answerCookiebot({ statistics: true });++ expect(consentUpdates()).toEqual([+ update(false, false, true),+ update(true, false, false),+ ]);+ unfollow();+ });++ it("never updates security storage, which stays granted", () => {+ configureCookiebot();+ installCookiebot({ statistics: true, marketing: true, preferences: true });+ const unfollow = followConsentForGoogleTag();++ answerCookiebot({});++ expect(consentUpdates()).toHaveLength(2);+ consentUpdates().forEach(([, , signals]) =>+ expect(signals).not.toHaveProperty("security_storage"),+ );+ unfollow();+ });+it("keeps every signal denied when the visitor declines", () => {runDefaultsScript();configureCookiebot();diff --git a/autogpt_platform/frontend/src/services/analytics/consent-mode.ts b/autogpt_platform/frontend/src/services/analytics/consent-mode.tsindex 0bed4d64d9..b12e062a76 100644--- a/autogpt_platform/frontend/src/services/analytics/consent-mode.ts+++ b/autogpt_platform/frontend/src/services/analytics/consent-mode.ts@@ -12,9 +12,12 @@ import { DATA_LAYER_NAME } from "./gtag";const WAIT_FOR_UPDATE_MS = 500;// Consent Mode v2 defaults, rendered as a beforeInteractive script ahead of-// Cookiebot and the Google tag. Every signal starts denied for every visitor-// until Cookiebot has their answer; the updates come from-// followConsentForGoogleTag below. There is deliberately no `region` split:+// Cookiebot and the Google tag. Google treats a signal without a default as+// not denied, so all seven get one. Every signal that needs consent starts+// denied for every visitor until Cookiebot has their answer; the updates come+// from followConsentForGoogleTag below. security_storage covers storage the+// service needs (Cookiebot's "necessary" category), so it is always granted+// and never updated. There is deliberately no `region` split:// the vendored public/gtag.js has the location of the machine that downloaded// it baked in and uses that instead of looking the visitor up, so every// visitor would match the same region. The shim stays local so it doesn't@@ -30,6 +33,9 @@ export function buildConsentDefaultsScript(): string {ad_user_data: "denied",ad_personalization: "denied",analytics_storage: "denied",+ functionality_storage: "denied",+ personalization_storage: "denied",+ security_storage: "granted",wait_for_update: WAIT_FOR_UPDATE_MS,})});`,// Carries the ad click ID across pages in the URL while cookies are denied.@@ -46,19 +52,23 @@ function signal(granted: boolean): Signal {export function buildConsentUpdate(consent: ConsentState) {const ads = signal(consent.advertising);+ const preferences = signal(consent.preferences);return {analytics_storage: signal(consent.analytics),ad_storage: ads,ad_user_data: ads,ad_personalization: ads,+ functionality_storage: preferences,+ personalization_storage: preferences,};}/*** Sends the visitor's answer to the Google tag as a Consent Mode update, now* and whenever it changes. Cookiebot's own Consent Mode integration sends the- * same signals; this keeps a denial reaching the tag even if that integration- * is switched off in the Cookiebot admin. Until there is an answer the denied+ * same signals from the same categories (preferences maps to functionality and+ * personalization); this keeps a denial reaching the tag even if that+ * integration is switched off in the Cookiebot admin. Until there is an answer the denied* defaults stand; an answer Cookiebot later withdraws is sent as a denial.* Returns the unsubscribe.*/diff --git a/autogpt_platform/frontend/src/services/consent/ConsentWithdrawalReload.tsx b/autogpt_platform/frontend/src/services/consent/ConsentWithdrawalReload.tsxindex d591ba664b..9201e21cbf 100644--- a/autogpt_platform/frontend/src/services/consent/ConsentWithdrawalReload.tsx+++ b/autogpt_platform/frontend/src/services/consent/ConsentWithdrawalReload.tsx@@ -31,8 +31,16 @@ export function ConsentWithdrawalReload() {return null;}+// No script runs on preferences consent, so taking it back leaves nothing to+// shed; the Google tag gets that change as a Consent Mode update.+const SCRIPT_CATEGORIES: ConsentCategory[] = [+ "analytics",+ "monitoring",+ "advertising",+];+function wasWithdrawn(previous: ConsentState, next: ConsentState): boolean {- return (Object.keys(previous) as ConsentCategory[]).some(+ return SCRIPT_CATEGORIES.some((category) => previous[category] && !next[category],);}diff --git a/autogpt_platform/frontend/src/services/consent/consent.test.ts b/autogpt_platform/frontend/src/services/consent/consent.test.tsindex f2af62c5a8..d26ccad481 100644--- a/autogpt_platform/frontend/src/services/consent/consent.test.ts+++ b/autogpt_platform/frontend/src/services/consent/consent.test.ts@@ -38,7 +38,7 @@ afterEach(() => {});describe("category mapping", () => {- it("maps analytics and monitoring to statistics, advertising to marketing", () => {+ it("maps analytics and monitoring to statistics, advertising to marketing, preferences to preferences", () => {expect(toConsentState({necessary: true,@@ -46,15 +46,25 @@ describe("category mapping", () => {statistics: true,marketing: false,}),- ).toEqual({ analytics: true, monitoring: true, advertising: false });+ ).toEqual({+ analytics: true,+ monitoring: true,+ advertising: false,+ preferences: true,+ });expect(toConsentState({necessary: true,- preferences: true,+ preferences: false,statistics: false,marketing: true,}),- ).toEqual({ analytics: false, monitoring: false, advertising: true });+ ).toEqual({+ analytics: false,+ monitoring: false,+ advertising: true,+ preferences: false,+ });});it("denies everything without an answer", () => {@@ -86,6 +96,7 @@ describe("with a Cookiebot domain group", () => {analytics: true,monitoring: true,advertising: false,+ preferences: false,});});@@ -98,6 +109,7 @@ describe("with a Cookiebot domain group", () => {analytics: false,monitoring: false,advertising: true,+ preferences: false,});});@@ -148,6 +160,7 @@ describe("with a Cookiebot domain group", () => {analytics: false,monitoring: false,advertising: true,+ preferences: false,});});@@ -193,6 +206,7 @@ describe("with a Cookiebot domain group", () => {analytics: true,monitoring: true,advertising: false,+ preferences: false,});expect(readConsentFromCookieHeader("theme=dark")).toEqual(NO_CONSENT);expect(readConsentFromCookieHeader(null)).toEqual(NO_CONSENT);@@ -222,6 +236,7 @@ describe("with a Cookiebot domain group", () => {analytics: true,monitoring: true,advertising: false,+ preferences: false,});expect(readConsentFromCookieHeader(header)).toEqual(getConsent());});diff --git a/autogpt_platform/frontend/src/services/consent/consent.ts b/autogpt_platform/frontend/src/services/consent/consent.tsindex 6093be470e..2ef3ba8c2b 100644--- a/autogpt_platform/frontend/src/services/consent/consent.ts+++ b/autogpt_platform/frontend/src/services/consent/consent.ts@@ -10,13 +10,20 @@ import {// "monitoring" gates Sentry session replay only. Error monitoring is part of// running the service (legitimate interest), so it needs no consent.-export type ConsentCategory = "analytics" | "monitoring" | "advertising";+// "preferences" gates nothing in the app; it is only passed on to the Google+// tag as Consent Mode's functionality and personalization signals.+export type ConsentCategory =+ | "analytics"+ | "monitoring"+ | "advertising"+ | "preferences";export type ConsentState = Readonly<Record<ConsentCategory, boolean>>;export const NO_CONSENT: ConsentState = Object.freeze({analytics: false,monitoring: false,advertising: false,+ preferences: false,});const COOKIEBOT_CATEGORY: Record<@@ -26,6 +33,7 @@ const COOKIEBOT_CATEGORY: Record<analytics: "statistics",monitoring: "statistics",advertising: "marketing",+ preferences: "preferences",};/**@@ -44,6 +52,7 @@ export function toConsentState(analytics: cookiebot[COOKIEBOT_CATEGORY.analytics],monitoring: cookiebot[COOKIEBOT_CATEGORY.monitoring],advertising: cookiebot[COOKIEBOT_CATEGORY.advertising],+ preferences: cookiebot[COOKIEBOT_CATEGORY.preferences],};}@@ -67,7 +76,8 @@ export function isSameConsent(a: ConsentState, b: ConsentState): boolean {return (a.analytics === b.analytics &&a.monitoring === b.monitoring &&- a.advertising === b.advertising+ a.advertising === b.advertising &&+ a.preferences === b.preferences);}