Skip to content

Commit 1cccc74

Browse files
authored
fix: restore consent from ucData when GTM overwrites SDK storage (supabase#44252)
## Problem After PR supabase#43221 gated `TelemetryTagManager` behind consent, the EU cookie consent banner started reappearing on every page load and when navigating between apps (www, studio, docs). Back in late February we changed `TelemetryTagManager` to only load when the user has accepted consent. This was the right call for GDPR — don't load tracking scripts before consent. But it created a chicken-and-egg problem with how the Usercentrics SDK stores consent. ## What happened When a user clicks Accept, the SDK writes `uc_settings` + `uc_user_interaction: true` to localStorage. Then the GTM script loads (now that consent is granted), and its Usercentrics integration immediately replaces those keys with a compressed `ucString` + `ucData` format — deleting the originals. On the next page load, `UC.init()` only knows how to read `uc_settings`. It can't find it (GTM deleted it), so it treats the user as brand new and shows the banner again. Before supabase#43221, GTM loaded on every page unconditionally, so its integration was already present during `UC.init()` and could interpret the compressed format. Confirmed via production console monitoring — the exact sequence after clicking Accept: ``` setItem("uc_settings", ...) // SDK writes consent setItem("uc_user_interaction", "true") // SDK marks interaction removeItem("uc_settings") // GTM deletes SDK format removeItem("uc_user_interaction") // GTM deletes SDK format setItem("ucString", ...) // GTM writes compressed format setItem("ucData", ...) // GTM writes compressed format ``` ## Changes - Read `ucData` from localStorage **before** `UC.init()` to detect prior consent in the compressed format - If the SDK wants to show the banner but `ucData` shows all services were previously accepted, silently re-accept instead of re-prompting - Added try/catch around the SDK initialization (was fire-and-forget with no error handling, any failure was completely silent) - Error fallback also honors prior `ucData` consent if the SDK fails to initialize ## Testing Can't fully reproduce on staging previews because CSP blocks the GTM script there (so the storage migration never fires). Verified the root cause via production console monitoring with localStorage monkey-patching, and confirmed the `ucData` format persists across page loads on production. Closes FE-2648
1 parent c5518d4 commit 1cccc74

1 file changed

Lines changed: 82 additions & 21 deletions

File tree

packages/common/consent-state.ts

Lines changed: 82 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,35 @@ import { proxy, snapshot, useSnapshot } from 'valtio'
44

55
import { IS_PLATFORM, LOCAL_STORAGE_KEYS } from './constants'
66

7+
/**
8+
* Check if the user previously accepted all consent services by reading
9+
* the compressed ucData format that the GTM/Usercentrics integration writes.
10+
*
11+
* Context (FE-2648): After acceptAllServices(), the GTM script's Usercentrics
12+
* integration replaces uc_settings with ucString/ucData. On the next page load,
13+
* UC.init() can't read that format and treats the user as new. This function
14+
* detects that prior consent so we can silently re-accept.
15+
*/
16+
function hasPreviousConsentInUcData(): boolean {
17+
try {
18+
const ucData = localStorage?.getItem('ucData')
19+
if (!ucData) return false
20+
21+
const data = JSON.parse(ucData)
22+
const services = data?.consent?.services
23+
if (!services || typeof services !== 'object') return false
24+
25+
const serviceValues = Object.values(services)
26+
if (serviceValues.length === 0) return false
27+
28+
return serviceValues.every(
29+
(s) => typeof s === 'object' && s !== null && (s as { consent: boolean }).consent === true
30+
)
31+
} catch {
32+
return false
33+
}
34+
}
35+
736
export const consentState = proxy({
837
// Usercentrics state
938
UC: null as Usercentrics | null,
@@ -73,27 +102,59 @@ async function initUserCentrics() {
73102
return
74103
}
75104

76-
const { default: Usercentrics } = await import('@usercentrics/cmp-browser-sdk')
77-
78-
const UC = new Usercentrics(process.env.NEXT_PUBLIC_USERCENTRICS_RULESET_ID!, {
79-
rulesetId: process.env.NEXT_PUBLIC_USERCENTRICS_RULESET_ID,
80-
useRulesetId: true,
81-
})
82-
83-
const initialUIValues = await UC.init()
84-
85-
consentState.UC = UC
86-
const hasConsented = UC.areAllConsentsAccepted()
87-
88-
// 0 = first layer, aka show consent toast
89-
consentState.showConsentToast = initialUIValues.initialLayer === 0
90-
consentState.hasConsented = hasConsented
91-
consentState.categories = UC.getCategoriesBaseInfo()
92-
93-
// If the user has previously consented (before usercentrics), accept all services
94-
if (!hasConsented && localStorage?.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT) === 'true') {
95-
consentState.acceptAll()
96-
localStorage.removeItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT)
105+
// Check for prior consent BEFORE UC.init(), which can't read the compressed
106+
// ucData format written by the GTM/Usercentrics integration (FE-2648).
107+
const previouslyAccepted = hasPreviousConsentInUcData()
108+
109+
try {
110+
const { default: Usercentrics } = await import('@usercentrics/cmp-browser-sdk')
111+
112+
const UC = new Usercentrics(process.env.NEXT_PUBLIC_USERCENTRICS_RULESET_ID!, {
113+
rulesetId: process.env.NEXT_PUBLIC_USERCENTRICS_RULESET_ID,
114+
useRulesetId: true,
115+
})
116+
117+
const initialUIValues = await UC.init()
118+
119+
consentState.UC = UC
120+
const hasConsented = UC.areAllConsentsAccepted()
121+
122+
// If the SDK wants to show the banner but the user previously accepted
123+
// (ucData exists from a prior GTM-mediated accept), silently re-accept
124+
// instead of showing the banner again.
125+
if (initialUIValues.initialLayer === 0 && !hasConsented && previouslyAccepted) {
126+
consentState.hasConsented = true
127+
consentState.showConsentToast = false
128+
consentState.categories = UC.getCategoriesBaseInfo()
129+
localStorage?.removeItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT)
130+
UC.acceptAllServices()
131+
.then(() => {
132+
consentState.categories = UC.getCategoriesBaseInfo()
133+
})
134+
.catch(() => {
135+
// If re-accept fails, fall back to showing the banner
136+
consentState.hasConsented = false
137+
consentState.showConsentToast = true
138+
})
139+
return
140+
}
141+
142+
// 0 = first layer, aka show consent toast
143+
consentState.showConsentToast = initialUIValues.initialLayer === 0
144+
consentState.hasConsented = hasConsented
145+
consentState.categories = UC.getCategoriesBaseInfo()
146+
147+
// If the user has previously consented (before usercentrics), accept all services
148+
if (!hasConsented && localStorage?.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT) === 'true') {
149+
consentState.acceptAll()
150+
localStorage.removeItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT)
151+
}
152+
} catch (error) {
153+
console.error('Failed to initialize Usercentrics:', error)
154+
// If SDK fails but user previously accepted, honor that
155+
if (previouslyAccepted) {
156+
consentState.hasConsented = true
157+
}
97158
}
98159
}
99160

0 commit comments

Comments
 (0)