Tell Google Tag Manager the moment a visitor answers
Push a tf_consent_update event to the data layer from the TermsFeed Privacy Consent (CMP) callbacks, and fire your Google Tag Manager tags on that event. Privacy Consent does not push a data layer event by itself, so a visitor who clicks I Agree fires a held tag only at the next page load. The Consent Mode commands a Google template sends do not fire a trigger either. The setup takes 5 steps and about 15 minutes.
You need access to the head of every page, edit access to the Google Tag Manager container, and the vendor categories your tags belong to.
Step 1: Put the callbacks ahead of the Privacy Consent snippet
Paste this inline script into the head of every page, above the Privacy Consent snippet and above any container snippet you keep there. It installs 4 callbacks, and each push carries a value for every vendor category.
<script>
window.dataLayer = window.dataLayer || [];
(function () {
var IDS = ['essential', 'functionality', 'analytics', 'advertising', 'marketing'];
function pushConsent(accepted, source) {
var message = { event: 'tf_consent_update', tf_consent_source: source };
for (var i = 0; i < IDS.length; i++) {
message['tf_consent_' + IDS[i]] = accepted.indexOf(IDS[i]) > -1 ? 'granted' : 'denied';
}
window.dataLayer.push(message);
}
// The accepted vendor categories of a saved answer, or null when none is in effect.
function savedAnswer() {
try {
var raw = window.localStorage.getItem('termsfeed_privacy_consent_state');
var state = raw ? JSON.parse(raw).consentState : null;
if (!state || !state.consentedDate) { return null; }
if (state.consentedDate + 365 * 86400 < Date.now() / 1000) { return null; }
var accepted = [];
for (var c = 0; c < state.categories.length; c++) {
var category = state.categories[c];
var vendors = category.vendors || [];
var on = category.enabled === true || vendors.length > 0;
for (var v = 0; v < vendors.length && category.enabled !== true; v++) {
if (vendors[v].enabled !== true) { on = false; }
}
if (on) { accepted.push(category.id); }
}
return accepted;
} catch (e) {
return null;
}
}
var callbacks = {
acceptedAll: function (categories) { pushConsent(categories, 'choice'); },
acceptedSome: function (categories) { pushConsent(categories, 'choice'); },
rejectedAll: function () { pushConsent(['essential'], 'choice'); },
// Runs once on every page load. No answer callback runs for a returning visitor.
initialized: function () {
var accepted = savedAnswer();
var visitor = window.termsfeed_privacy_consent.v1.visitor;
if (accepted) {
pushConsent(accepted, 'stored');
} else if (visitor && visitor.isOptOutModel) {
pushConsent(IDS, 'opt_out_default');
}
}
};
if (window.termsfeed_privacy_consent !== undefined)
window.termsfeed_privacy_consent.v1.callbacks(callbacks);
else
window.termsfeed_privacy_consent_callbacks = callbacks;
})();
</script>
The tf_consent_source value says why the event fired. It reads choice for an answer given in the current page view and stored for a saved answer on page load. It reads opt_out_default for a visit on the opt-out model, which runs every vendor with no answer saved. A first-time visitor who sees the consent banner fires the event only after answering.
The callbacks leave out a vendor category with some vendors on and some off. The page-load part applies the same rule, so a partly accepted vendor category arrives as denied in both. Read the vendor-accurate alternative in Callbacks and Consent Semantics.
Step 2: Create a Data Layer Variable for each vendor category
In Google Tag Manager, open Variables, click New under User-Defined Variables, and choose Data Layer Variable. Enter tf_consent_analytics as the Data Layer Variable Name and name the variable DLV - tf_consent_analytics. Repeat for tf_consent_advertising, tf_consent_marketing, and any other vendor category you hold.
Advertising alone sets the Google advertising signals, so a Google Ads tag waits for tf_consent_advertising alone. Match each tag to its variable with Category Mapping.
Step 3: Create a Custom Event trigger on tf_consent_update
Open Triggers, click New, and choose Custom Event. Enter tf_consent_update as the Event name, select Some Custom Events, and set the condition DLV - tf_consent_analytics equals granted. Name the trigger TF Consent - Analytics granted, save it, and create one trigger per vendor category you hold.
Step 4: Attach the trigger and fire each tag once per page
Open the tag, remove All Pages from Triggering, and add TF Consent - Analytics granted. Under Advanced Settings, open Tag firing options and choose Once per page. The event arrives again for each answer on one page, and this option keeps the tag to one fire.
Step 5: Check the event in "Preview" mode and the Network tab
Run this check on a visit that shows the consent banner. Close every private window before the second visit, 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 event list does not show
tf_consent_updatebefore you answer. - 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. The event list shows
tf_consent_update, its Data Layer tab showstf_consent_analytics: "granted"andtf_consent_source: "choice", and the Network tab shows thegoogle-analytics.comrequest. - Reload the page. The event appears again with
tf_consent_source: "stored", and the tag fires once. - Close every private window, repeat step 1, and click I Decline. The event carries
deniedfor every vendor category exceptessential, and the Network tab stays empty forgoogle-analytics.com.
Type dataLayer in the Console tab to read every message in the current page view. With a Google template, the array holds Arguments entries for the Consent Mode commands beside your event.
Common mistakes with Privacy Consent banner data layer events
Setups copied from other consent tools fail on Privacy Consent for 3 reasons, and the code in step 1 avoids each one. The browser storage event does not fire in the tab that changed localStorage, so a storage listener never reports an answer given in that tab. Push from the callbacks instead.
The block runs before the Privacy Consent script loads, when window.termsfeed_privacy_consent does not exist yet. The block therefore sets window.termsfeed_privacy_consent_callbacks, which Privacy Consent reads once when it starts. Keep the code in step 1 ahead of the Privacy Consent snippet, or an answer callback misses a GPC opt-out that Privacy Consent applies on page load.
The callbacks fire on every answer, whether the visitor answers on the consent banner, in the Preferences Center, or in an embed placeholder. Each v1.callbacks() call replaces all 4 callbacks, so a second copy of the code removes the first. Merge every callback into one object.
How to send an event per vendor without callbacks
Put the data layer push in the on-accept vendor tag of a vendor, and the event follows that one vendor instead of its whole vendor category. Privacy Consent inserts the vendor tag when the visitor accepts and on every later page load, so no callback and no page-load code are needed. A visit on the opt-out model fires the event too. Set it up with GTM Vendor Events.
How to combine the data layer event with Google Consent Mode v2
With a Google template, your callbacks push only tf_consent_update, because Privacy Consent sends the Consent Mode update itself and sends it before your callback runs. Tags triggered by the event therefore read the updated consent state.
A custom vendor does not send consent signals. Your own code sends the Consent Mode update first and pushes tf_consent_update second, inside the same callbacks object. The order in the head is your Consent Mode defaults, this block, then the Privacy Consent snippet, and your Google tags or container go in the custom vendor. Copy the defaults and the update for each callback from Set Up Google Consent Mode v2.