Pass the cookie banner answer to your server, a server container, and conversions APIs
TermsFeed Privacy Consent (CMP) keeps each cookie banner answer in the browser localStorage, which no request to your server carries. Your backend, a server-side Google Tag Manager container, and every conversions API you call from your server see nothing unless browser code sends the answer along. TermsFeed has no API that tells your server what a visitor answered, and the consent log stays inside the TermsFeed app.
Check consent on the server before any event leaves it, because Privacy Consent controls vendors in the browser only. Find what else stays outside its control in the blocking limits.
The setup takes 4 steps and about 1 hour. Steps 1 and 2 carry the answer from the browser, Step 3 checks it in your backend, and Step 4 verifies the result. You need edit access to your site <head> and to the backend code that forwards events.
Step 1: Send consent with each event
Read the stored record in the browser and attach it to every event you send to your own endpoint. Use this approach where your own code already sends events to your backend. The consent values travel in the same request as the event, so they always match the page the event came from.
The block defines tfConsent(), which returns granted, denied, or not_chosen per vendor category, and sends a page_view event to /api/track from the initialized callback. By then Privacy Consent has dropped an expired answer and applied any Global Privacy Control override. Put it in your <head>, above the Privacy Consent snippet, and replace /api/track with your own endpoint.
<script>
function tfConsent() {
var ids = ['functionality', 'analytics', 'advertising', 'marketing'];
var result = { chosen: false, consentedDate: null };
ids.forEach(function (id) { result[id] = '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;
result.consentedDate = state.consentedDate;
ids.forEach(function (id) { result[id] = 'denied'; });
(state.categories || []).forEach(function (category) {
if (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;
}
function tfSendEvent(name) {
var consent = tfConsent();
fetch('/api/track', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
event: name,
page_location: window.location.href,
consent: {
functionality: consent.functionality,
analytics: consent.analytics,
advertising: consent.advertising,
marketing: consent.marketing
}
})
});
}
var callbacks = {
initialized: function () { tfSendEvent('page_view'); }
};
if (window.termsfeed_privacy_consent !== undefined)
window.termsfeed_privacy_consent.v1.callbacks(callbacks);
else
window.termsfeed_privacy_consent_callbacks = callbacks;
</script>
Privacy Consent does not read this request or its consent object. A vendor category the visitor accepted only in part reads denied, and you find the reason in mixed vendor categories count as denied.
Step 2: Mirror consent into a first-party cookie
Write a small first-party cookie from the callbacks, so every request to your domain carries the answer. Use this approach for a server container or a backend that receives requests your code does not build, such as form posts and page requests. Privacy Consent does not set or read a cookie, so the tf_consent cookie belongs to you and changes nothing in the consent banner.
Replace the callbacks object of Step 1 with the next block. All 4 callbacks run the same function, because Privacy Consent saves the record before it calls an answer callback. The cookie holds the granted vendor category IDs joined with dots, such as functionality.analytics, or none, and it ends when the answer expires.
function tfMirrorConsent() {
var consent = tfConsent();
if (!consent.chosen) {
document.cookie = 'tf_consent=; path=/; max-age=0; SameSite=Lax; Secure';
return;
}
var granted = ['functionality', 'analytics', 'advertising', 'marketing'].filter(function (id) {
return consent[id] === 'granted';
});
var maxAge = consent.consentedDate + 365 * 24 * 60 * 60 - Math.floor(Date.now() / 1000);
document.cookie = 'tf_consent=' + (granted.length ? granted.join('.') : 'none') +
'; path=/; max-age=' + maxAge + '; SameSite=Lax; Secure';
}
var callbacks = {
acceptedAll: tfMirrorConsent,
acceptedSome: tfMirrorConsent,
rejectedAll: tfMirrorConsent,
initialized: function () {
tfMirrorConsent();
tfSendEvent('page_view');
}
};
Add ; domain=example.com to both cookie strings where your server container runs on a subdomain, such as sgtm.example.com. Do this only on a site with one origin that answers the consent banner, and follow consent across subdomains on a site with several. Keep the cookie to vendor category IDs, because a consent cookie that carries an identifier becomes a tracking cookie itself.
Step 3: Check consent in your backend
Check the consent value in your backend before each call to an analytics service or a conversions API. The Meta Conversions API, the TikTok Events API, and the LinkedIn Conversions API all send data straight from your server, where Privacy Consent never runs. A call with the identifiers removed still tells the platform that a visit or a purchase happened, so return early instead of sending a reduced event.
The Node.js server uses Express and reads both sources: the consent object of Step 1 and the tf_consent cookie of Step 2. It forwards analytics events only for a granted Analytics vendor category, and advertising events only for a granted Advertising vendor category. Replace the 2 console.log calls with your forwarding code.
const express = require('express');
const app = express();
app.use(express.json());
function tfGrantedCategories(req) {
if (req.body && req.body.consent) {
return Object.keys(req.body.consent).filter((id) => req.body.consent[id] === 'granted');
}
const match = /(?:^|;\s*)tf_consent=([^;]*)/.exec(req.headers.cookie || '');
if (!match || match[1] === '' || match[1] === 'none') return [];
return match[1].split('.');
}
app.post('/api/track', (req, res) => {
const granted = tfGrantedCategories(req);
if (granted.includes('analytics')) {
console.log('Forward to analytics:', req.body.event);
}
if (granted.includes('advertising')) {
console.log('Forward to the conversions API:', req.body.event);
}
res.sendStatus(204);
});
app.listen(3000);
Google Consent Mode v2 in a server-side container
A server-side Google Tag Manager container receives the Google Consent Mode v2 state from the Google tag in the browser. The Google tag attaches the state to each request it sends to the container, in the gcs parameter, and Google tags in the container read it. Set up the defaults and the updates in the browser with Google Consent Mode v2.
Tags from other vendors in the container do not read that parameter. Create a Cookie Value variable for tf_consent in the server container and add a condition on it to the trigger of every other tag. A trigger condition such as tf_consent contains analytics holds an analytics tag until the visitor grants the Analytics vendor category.
Consent is unknown until the visitor chooses
Treat a request with no consent value as a refusal. A visitor on the first page view has no stored answer yet, so Step 1 sends not_chosen and Step 2 has written no cookie. Do not send tracking data for that request, because the visitor has not had a chance to decide.
A first visit under the opt-out model looks the same to your server, although every vendor runs in the browser. Privacy Consent does not save an answer on that visit, so the record stays empty until the visitor saves an answer. Tell that visit apart in the browser with isOptOutModel, using the initialized block in read consent state in your own code.
Step 4: Verify that your server receives consent
Check the browser, the request, and a real refusal before your server relies on the setup. A broken setup does not show an error while your server keeps sending data for visitors who refused. These steps assume a visit that shows the consent banner.
- Open your site in a private window with Chrome DevTools open and click I Decline.
- Select the Application tab, open Cookies, and confirm that
tf_consentreadsnone. - Select the Network tab, type
api/trackin the filter box, and reload the page. - Confirm that the request body carries
"analytics":"denied"and the request headers carrytf_consent=none. - Trigger a test conversion, such as a test form submission, and confirm that your server log does not show a forwarding line.
- Close every private window, open a new one, click I Agree, and reload the page.
- Confirm that the cookie reads
functionality.analytics.advertising.marketingand that both forwarding lines appear.