Skip to content

Instantly share code, notes, and snippets.

@justusbluemer
Created May 7, 2025 12:50
Show Gist options
  • Save justusbluemer/3b92b879da772a2038345b22ef8db258 to your computer and use it in GitHub Desktop.
Save justusbluemer/3b92b879da772a2038345b22ef8db258 to your computer and use it in GitHub Desktop.
Get active TCF data overview
// Execute in browser's JS console
(function () {
/* 1. Is a TCF CMP present? */
if (typeof window.__tcfapi !== 'function') {
console.warn('⚠️ No __tcfapi on this page – either no CMP, it is still booting, or the site only exposes GPP (__gpp).');
return;
}
/* 2. Ping the CMP for basic status ...................................... */
__tcfapi('ping', 2, (ping, ok) => {
if (!ok) { console.error('ping failed'); return; }
console.table(ping); // inspect cmpStatus, gdprApplies …
/* TCF policy version logic
2 → TCF 2.0 / 2.1
4 → TCF 2.2 (latest) */
if (ping.tcfPolicyVersion >= 4) {
console.log(`✅ CMP runs TCF 2.2 (policy v${ping.tcfPolicyVersion})`);
} else {
console.log(`ℹ️ CMP is on policy v${ping.tcfPolicyVersion} (TCF 2.0/2.1)`);
}
/* 3. Get the full TCData once the string is loaded .................... */
__tcfapi('getTCData', 2, (tc, ok) => {
if (!ok) { console.error('getTCData failed'); return; }
/* ——— vendor IDs the user has consented to ——— */
const consentedIds = Object.entries(tc.vendor.consents)
.filter(([, yes]) => yes)
.map(([id]) => +id);
console.log('🟢 Vendor IDs with consent:', consentedIds);
/* 4. (optional) Map IDs ➜ readable names by pulling the vendor list */
__tcfapi('getVendorList', 2, (gvl, ok) => {
if (!ok) { console.warn('Could not retrieve Global Vendor List'); return; }
const withNames = consentedIds.map(id => ({
id,
name: gvl.vendors[id]?.name || '(unknown)'
}));
console.table(withNames);
}, 'LATEST'); // many CMPs accept the literal string "LATEST"
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment