Build a Google Tag Manager variable that reports each vendor category
Create one Custom JavaScript Variable per vendor category in Google Tag Manager, and each returns unset, granted, or denied from the answer TermsFeed Privacy Consent saved in the browser. A trigger condition then reads the variable, so a returning visitor who accepted Analytics fires your Analytics tags on every page. The setup takes 4 steps and about 10 minutes.
You need edit access to the container, and the container and the Privacy Consent snippet on the same pages. The "Vendors" page of the TermsFeed app lists the vendor categories your tags belong to.
What Privacy Consent stores in the browser
Privacy Consent keeps the answer in the localStorage key termsfeed_privacy_consent_state, once per origin, and does not set a cookie. No page-level getter of the full state exists, so the variable reads this key. Before the first answer, the key holds only {"user_token": "..."}. After an answer it holds the consent state, shortened in this example to the parts the variable reads.
{
"consentState": {
"categories": [
{ "id": "essential", "enabled": true, "vendors": [] },
{ "id": "analytics", "enabled": true, "vendors": [{ "id": "termsfeed-7a8b9c0d-1e2f", "enabled": true }] },
{ "id": "advertising", "enabled": false, "vendors": [{ "id": "termsfeed-3a4b5c6d-7e8f", "enabled": false }] }
],
"consentedDate": 1790000000
},
"user_token": "3f9c0a7b12d4e865"
}
consentedDate is the time of the last answer in Unix seconds. Read every field of the record in Stored on the Device.
The consent states the variable returns
Trigger conditions compare against these exact strings, so copy them as written.
| Value | When the variable returns it |
|---|---|
unset |
The key does not hold an answer, the answer is older than 365 days (12 months), or the browser blocks localStorage |
granted |
The vendor category is on, or it has vendors and every one of them is on |
denied |
Any other case, a partly accepted vendor category included |
A partly accepted vendor category counts as denied, so a tag that serves the whole vendor category waits. Read the vendor-accurate alternative in Callbacks and Consent Semantics.
The 365-day check is in the code because Privacy Consent ignores an expired answer and leaves it in localStorage until the next answer replaces it. Without the check, the variable reports an answer Privacy Consent no longer uses. Check what happens when an answer expires in Consent Expiry.
Step 1: Create the variable for one vendor category
In Google Tag Manager, open Variables, click New under User-Defined Variables, choose Custom JavaScript, and name the variable TF Consent - analytics. Then paste this function and save the variable. It reads the key on every evaluation and never writes to it.
function() {
var CATEGORY = 'analytics';
var VALID_DAYS = 365;
try {
var raw = window.localStorage.getItem('termsfeed_privacy_consent_state');
var state = raw ? JSON.parse(raw).consentState : null;
if (!state || !state.consentedDate) { return 'unset'; }
if (state.consentedDate + VALID_DAYS * 86400 < Date.now() / 1000) { return 'unset'; }
var categories = state.categories || [];
for (var i = 0; i < categories.length; i++) {
if (categories[i].id !== CATEGORY) { continue; }
if (categories[i].enabled === true) { return 'granted'; }
var vendors = categories[i].vendors || [];
if (vendors.length === 0) { return 'denied'; }
for (var j = 0; j < vendors.length; j++) {
if (vendors[j].enabled !== true) { return 'denied'; }
}
return 'granted';
}
return 'denied';
} catch (e) {
return 'unset';
}
}
Step 2: Repeat the variable for each vendor category you hold
Create one variable per vendor category, because a Custom JavaScript Variable takes no argument. Duplicate TF Consent - analytics from its menu, rename the copy, and change the value of CATEGORY to functionality, advertising, or marketing. The essential vendor category is always on and needs no variable.
Advertising alone sets the Google advertising signals, so a Google Ads tag waits for the advertising variable. Match each tag to its vendor category with Category Mapping.
Step 3: Hold each tag until the variable reports granted
Open Triggers, click New, choose Page View, and select Some Page Views. Set the condition to TF Consent - analytics equals granted, and name the trigger Page View - Analytics granted.
Open each Analytics tag, remove its All Pages trigger, and add the new trigger in its place. Do not publish the container until the check in step 4 passes, because Preview mode runs your unpublished changes.
Step 4: Check that the variable holds the tag
Close every private window before each new visit in this check, because private windows share storage while any of them stays open.
- Sign in to Google Tag Manager in a new private window, click Preview, and enter your site address. The Variables tab of the Container Loaded event shows
TF Consent - analyticsasunset, and the Tags tab lists the Analytics tag under Tags Not Fired. - Open Chrome DevTools on your site, select the Network tab, and type
google-analytics.comin the filter box, or the host of the tag you hold. No request appears before you answer. - Click I Agree, then reload the page. The Application tab shows
consentedDateintermsfeed_privacy_consent_stateunder Local Storage, the variable readsgranted, and the Network tab shows thegoogle-analytics.comrequest. - Close every private window, repeat step 1, click I Decline, and reload. The variable reads
denied, and the Network tab stays empty forgoogle-analytics.com.
Why the variable reflects recorded answers only
The variable reads what the visitor saved, which differs from what Privacy Consent runs in 3 cases. A visit on the opt-out model runs every vendor with no consent banner and no answer saved, so the variable returns unset while those vendors run. Check which visits follow that model in Cookie Banner by Visitor Location.
A vendor you add after the visitor answered is off until the next answer, and the saved record does not list it. The variable then reports granted for a vendor category that Privacy Consent treats as partly accepted.
A saved acceptance from a visitor who now sends Global Privacy Control (GPC) reads granted until Privacy Consent loads and saves the opt-out. A container in your page head therefore reads the old value on that page view. Give tags that must follow what Privacy Consent runs their own vendors, with GTM Vendor Events.
Why the variable does not update within a page view
Google Tag Manager evaluates a trigger only when it processes a data layer event, and Privacy Consent pushes no event when the visitor answers. The variable returns granted right after I Agree, but no trigger reads it again until the next page load. A first-time visitor who accepts on a landing page and leaves never fires the held tags.
A tag that already fired stays on the page after a visitor withdraws consent, and Privacy Consent does not delete the cookies it set. The variable changes the result at the next page load. Fire tags the moment the visitor answers with Data Layer Events.