Check what a visitor answered on the cookie banner before your code runs
Read the TermsFeed Privacy Consent (CMP) record yourself only where your own application logic depends on the cookie banner answer. A third-party script belongs in a vendor, where Privacy Consent runs it after consent with no code from you. Once your code reads the record, your code is responsible for honoring the answer. Privacy Consent ignores what your reader decides, and it does not offer a page-level getter of the full consent state.
The setup takes 5 steps and about 20 minutes. You need access to the <head> of your site templates and Chrome DevTools to test the result.
The 3 consent states of a vendor category
A vendor category is in 1 of 3 consent states for each visitor: granted, denied, or not chosen. Your code handles all 3, because a visitor who has not chosen has refused nothing. The table shows how each state appears in the record under the localStorage key termsfeed_privacy_consent_state.
| State | What the stored record shows | What your code does |
|---|---|---|
not_chosen |
No consentState, no consentedDate, or an answer past its 365-day (12-month) lifetime |
Waits, and treats the vendor category as off for now |
granted |
The vendor category has enabled: true, or it has vendors and every one has enabled: true |
Runs the feature |
denied |
Any other value, including a vendor category with some vendors on | Does not run the feature |
A vendor category the visitor accepted only in part counts as denied, the same conservative reading the callbacks use. Read the reasoning in mixed vendor categories count as denied.
Step 1: Add the Privacy Consent reader function
Paste the tfConsent() function into an inline script in your <head>, above the Privacy Consent snippet. The function reads localStorage only, so it answers at once, before Privacy Consent has loaded. It never throws, including in a browser that blocks localStorage.
<script>
function tfConsent() {
var ids = ['essential', 'functionality', 'analytics', 'advertising', 'marketing'];
var result = { chosen: false };
ids.forEach(function (id) {
result[id] = id === 'essential' ? 'granted' : 'not_chosen';
});
var state;
try {
state = JSON.parse(window.localStorage.getItem('termsfeed_privacy_consent_state') || '{}').consentState;
} catch (error) {
return result;
}
if (!state || !state.consentedDate) return result;
var expires = new Date(state.consentedDate * 1000);
expires.setDate(expires.getDate() + 365);
if (expires.getTime() < Date.now()) return result;
result.chosen = true;
ids.slice(1).forEach(function (id) { result[id] = 'denied'; });
(state.categories || []).forEach(function (category) {
if (category.id === 'essential' || ids.indexOf(category.id) === -1) return;
var vendors = category.vendors || [];
var accepted = category.enabled === true || (vendors.length > 0 && vendors.every(function (vendor) {
return vendor.enabled === true;
}));
result[category.id] = accepted ? 'granted' : 'denied';
});
return result;
}
</script>
Check the output against a saved answer that accepts the Analytics vendor category and declines the Advertising vendor category. On a site with no Functionality or Marketing vendors, tfConsent() returns this object.
{
"chosen": true,
"essential": "granted",
"functionality": "denied",
"analytics": "granted",
"advertising": "denied",
"marketing": "denied"
}
What the Privacy Consent reader does not see
The reader reports what the visitor saved, not what Privacy Consent runs on the current page. A first visit under the opt-out model shows the difference: every vendor runs and no answer is saved, so tfConsent() returns chosen: false with not_chosen for 4 vendor categories. Read isOptOutModel inside an initialized callback to tell that visit from one that still waits for an answer, with the block in Step 3.
A vendor added in the TermsFeed app after the visitor answered is missing from the record until the next answer, and Privacy Consent keeps it off under the opt-in model. A vendor category therefore reads granted while its new vendor stays off, so read that vendor with the function in Step 4. A saved Global Privacy Control override reads as denied, and the record marks it with answeredBy: "gpc".
A script that has to follow exactly what Privacy Consent runs belongs in a vendor tag, which needs no reader. Set one up with add a custom vendor.
Step 2: Gate your own feature on a vendor category
Wrap your feature in a check on the vendor category it needs. Call tfConsent() at the moment your code decides, because the value changes whenever the visitor saves a new answer. The block loads your own analytics script only for a granted Analytics vendor category. Replace /js/own-analytics.js with the path of your script.
function startOwnAnalytics() {
var script = document.createElement('script');
script.src = '/js/own-analytics.js';
script.async = true;
document.head.appendChild(script);
}
if (tfConsent().analytics === 'granted') {
startOwnAnalytics();
}
Step 3: React to a change without a reload
Run the same check from the callbacks, so your feature starts on the same page view. Privacy Consent saves the record before it calls a callback, so a read inside a callback returns the new answer. The initialized callback covers returning visitors and reads the consent model of the visit.
The block replaces the one in Step 2 and starts your feature at most once per page view. Put it in your <head>, above the Privacy Consent snippet and below the reader, as the only set of callbacks on the page.
<script>
var ownAnalyticsStarted = false;
function startOwnAnalytics() {
if (ownAnalyticsStarted) return;
ownAnalyticsStarted = true;
var script = document.createElement('script');
script.src = '/js/own-analytics.js';
script.async = true;
document.head.appendChild(script);
}
function applyOwnAnalyticsConsent() {
if (tfConsent().analytics === 'granted') startOwnAnalytics();
}
var callbacks = {
acceptedAll: applyOwnAnalyticsConsent,
acceptedSome: applyOwnAnalyticsConsent,
rejectedAll: applyOwnAnalyticsConsent,
initialized: function () {
var consent = tfConsent();
var visitor = window.termsfeed_privacy_consent.v1.visitor;
if (consent.analytics === 'granted' || (!consent.chosen && visitor.isOptOutModel)) {
startOwnAnalytics();
}
}
};
if (window.termsfeed_privacy_consent !== undefined)
window.termsfeed_privacy_consent.v1.callbacks(callbacks);
else
window.termsfeed_privacy_consent_callbacks = callbacks;
</script>
A script that has already run stays loaded after a withdrawal, and Privacy Consent does not stop it. Stop your feature in the callback where it offers a stop method; otherwise the withdrawal takes effect on the next page load. The storage event does not help in the tab that saved the answer, since browsers fire it only in other tabs of the same origin.
Step 4: Read a single vendor
Decide per vendor where one vendor category holds vendors with different purposes. The function returns granted, denied, or not_chosen for one vendor, and it returns not_chosen for a vendor the saved answer does not list. Call it with the vendor ID from the address of the "Edit vendor" page in the TermsFeed app, in the form termsfeed-1a2b3c4d-5e6f.
function tfVendorConsent(vendorId) {
if (!tfConsent().chosen) return 'not_chosen';
var answer = 'not_chosen';
try {
var state = JSON.parse(window.localStorage.getItem('termsfeed_privacy_consent_state')).consentState;
(state.categories || []).forEach(function (category) {
(category.vendors || []).forEach(function (vendor) {
if (vendor.id !== vendorId) return;
answer = vendor.enabled === true || category.enabled === true ? 'granted' : 'denied';
});
});
} catch (error) {
return 'not_chosen';
}
return answer;
}
Why your code never writes to the Privacy Consent record
Privacy Consent owns the termsfeed_privacy_consent_state key, so your code only reads it. A value your code writes either changes which vendors run on the next page load or disappears at the next save, with no warning. The consent log does not hold a matching event either, so the record stops matching your proof of consent.
Let the visitor change the answer instead. The next line opens the Preferences Center, where the visitor saves a new answer and Privacy Consent writes the record. A call made before the script is ready does nothing and prints [ee][478058ef].
window.termsfeed_privacy_consent.v1.openPrivacyPreferences();
Step 5: Verify your consent state reader
Test the reader in each state before your code relies on it. Open your site in a private window with Chrome DevTools open, select the Console tab, and type tfConsent() at each step. These steps assume a visit that shows the consent banner.
- Leave the consent banner unanswered and confirm that the result shows
chosen: falseandnot_chosenfor 4 vendor categories. - Click I Decline and confirm that the result shows
chosen: trueanddeniedfor the same 4. - Close every private window, open a new one, click I Agree, and confirm that all 5 vendor categories read
granted. - Open the Preferences Center, switch off 1 of 2 Analytics vendors, and click Save Preferences.
- Confirm that
analyticsreadsdeniedand thattfVendorConsent()returnsgrantedfor the vendor still on. - Select the Application tab, open Local storage, and confirm that
termsfeed_privacy_consent_stateholds aconsentStatewith a newconsentedDate. - Select the Network tab, type
events.pushin the filter box, save again, and confirm that the save adds 1 request with status 201.