Start tracking when a visitor accepts the cookie banner, with no page reload
TermsFeed Privacy Consent (CMP) calls a JavaScript callback as soon as a visitor saves a cookie banner answer, so your Google Tag Manager tags start on the same page view. A reload after consent loses the scroll position and any form input. These patterns replace it: push an event, replay the page view, guard against double counts, or let Google Consent Mode v2 update the Google tags.
Vendors need none of this. Privacy Consent inserts the on-accept vendor tags of a vendor at once when the visitor accepts it, so a Google Analytics 4 (GA4) vendor sends its page view by itself. The patterns apply to Google Tag Manager tags and to your own code.
The setup takes 4 steps and about 30 minutes. You need edit access to your site <head> and to your Google Tag Manager container.
Step 1: Add the consent event block before the Privacy Consent snippet
Paste the block inline in your <head>, before the Privacy Consent snippet. It holds the only set of callbacks on the page, so remove any other set first. Privacy Consent does not read the events this block pushes, and window.dataLayer queues them until your container loads.
<script>
window.dataLayer = window.dataLayer || [];
var tfAllCategories = ['functionality', 'analytics', 'marketing', 'advertising', 'essential'];
var tfAnalyticsAtLoad = false;
var tfPageviewReplayed = false;
function tfStoredCategories() {
try {
var state = JSON.parse(window.localStorage.getItem('termsfeed_privacy_consent_state') || '{}').consentState;
if (!state || !state.consentedDate) return null;
var expires = new Date(state.consentedDate * 1000);
expires.setDate(expires.getDate() + 365);
if (expires.getTime() < Date.now()) return null;
var accepted = [];
(state.categories || []).forEach(function (category) {
var vendors = category.vendors || [];
var allOn = category.enabled === true || (vendors.length > 0 && vendors.every(function (vendor) {
return vendor.enabled === true;
}));
if (allOn) accepted.push(category.id);
});
return accepted;
} catch (error) {
return null;
}
}
function tfPushConsent(source, categories) {
function state(id) { return categories.indexOf(id) !== -1 ? 'granted' : 'denied'; }
window.dataLayer.push({
event: 'tf_consent_update',
tf_consent_source: source,
tf_consent_essential: state('essential'),
tf_consent_functionality: state('functionality'),
tf_consent_analytics: state('analytics'),
tf_consent_advertising: state('advertising'),
tf_consent_marketing: state('marketing')
});
}
function tfOnAnswer(categories) {
tfPushConsent('choice', categories);
if (categories.indexOf('analytics') !== -1 && !tfAnalyticsAtLoad && !tfPageviewReplayed) {
tfPageviewReplayed = true;
window.dataLayer.push({
event: 'tf_pageview_after_consent',
page_path: window.location.pathname + window.location.search,
page_title: document.title,
page_location: window.location.href
});
}
}
var callbacks = {
acceptedAll: function (categories) { tfOnAnswer(categories); },
acceptedSome: function (categories) { tfOnAnswer(categories); },
rejectedAll: function () { tfOnAnswer(['essential']); },
initialized: function () {
var stored = tfStoredCategories();
if (stored !== null) {
tfAnalyticsAtLoad = stored.indexOf('analytics') !== -1;
tfPushConsent('stored', stored);
} else if (window.termsfeed_privacy_consent.v1.visitor.isOptOutModel) {
tfAnalyticsAtLoad = true;
tfPushConsent('opt_out_default', tfAllCategories);
}
}
};
if (window.termsfeed_privacy_consent !== undefined)
window.termsfeed_privacy_consent.v1.callbacks(callbacks);
else
window.termsfeed_privacy_consent_callbacks = callbacks;
</script>
Pattern 1: push an event and trigger your Google Tag Manager tags on it
The block pushes tf_consent_update after every answer and once per page load for a visitor who is not waiting to answer. Each push carries granted or denied for 5 vendor categories, and tf_consent_source says where the values came from: choice, stored, or opt_out_default. A vendor category the visitor accepted only in part reads denied. Read why in mixed vendor categories count as denied.
The page-load push sits in initialized because no answer callback runs on load. By then Privacy Consent has dropped an expired answer and applied any Global Privacy Control override, so the push does not report an expired or overridden acceptance. A vendor added after the answer is the exception: its vendor category reads granted while the new vendor stays off under the opt-in model. Check that vendor with Step 4: Read a single vendor. The opt_out_default push covers a first visit under the opt-out model, where every vendor runs with no answer saved. Compare this push with the Google Tag Manager version in send events to the data layer.
Pattern 2: replay the page view with page_path and page_title
The block pushes tf_pageview_after_consent for a visitor who grants analytics partway through the page. A Google Tag Manager tag that waits for both the page view and consent misses this visitor, because the page view came first. The event carries page_path, page_title, and page_location read at the moment of consent, so the tag reports the page where the visitor accepted.
The replay suits a tag that sends a page view into a tracker that is already loaded, such as a GA4 Event tag named page_view. A tag that loads a tracker and counts the page by itself, such as the Google tag with its automatic page view, fires on tf_consent_update and needs no replay.
Pattern 3: guard against double counting
The block fires the replay at most once per page view, and never for a visitor whose analytics was on at load. That visitor already counted the page through the page-load push. Without the guard, a returning visitor who reopens the Preferences Center and saves again counts the page twice.
Step 2: Trigger your tags on the consent events in Google Tag Manager
Create the variables and triggers in your container, then attach them in place of the All Pages trigger on every tag that needs consent.
- Open Variables, click New, choose Data Layer Variable, and enter
tf_consent_analyticsas the name. Repeat fortf_consent_advertisingand any other vendor category your tags use. - Open Triggers, click New, and choose Custom Event. Enter
tf_consent_updateas the event name, select Some Custom Events, and add the conditiontf_consent_analyticsequalsgranted. - Create a second Custom Event trigger with the event name
tf_pageview_after_consent. - Attach the first trigger to each tag that loads an analytics tracker, and the second trigger to each tag that only sends a page view into one.
- Open Advanced Settings on each tag attached to the first trigger, and set Tag firing options to Once per page.
The last step matters because tf_consent_update fires on every save. In a GA4 Event tag, set page_title and page_location from Data Layer Variables of the same names.
Step 3: Rely on Google Consent Mode v2 instead of a replay for Google tags
Keep Google tags on Google Consent Mode v2 from the first page load, and they need no replay. The Google templates in the TermsFeed app run Advanced Consent Mode. The Google tag loads before consent with every signal denied and sends cookieless pings, including one for the page view. An update to granted lets it use cookies from that moment, and a replay then sends a second page view for the same page.
With a Google template, Privacy Consent sends the defaults and every update itself, and this pattern needs no code. Keep hand-pasted Consent Mode code off a page with templates, because the 2 sets of commands conflict. With Google tags in a custom vendor, copy the Basic Consent Mode block from Google Consent Mode v2, and take the signal for each vendor category from the category mapping.
Use wait_for_update for returning visitors
The wait_for_update: 500 setting holds Google tags for up to 500 milliseconds (0.5 seconds) until a consent update arrives. A returning visitor does not fire an answer callback, so the stored answer reaches Google Consent Mode v2 only through a page-load update. A Google template sends that update right after the defaults, with wait_for_update: 500 already set.
Give your own Consent Mode code the same setting, and send the update from the stored record. Without the setting, a Google tag on a returning visit acts on the denied default before the stored answer arrives. Keep the value at 500, because a longer wait delays every Google tag on every page view.
Why Privacy Consent fires the callbacks more than once
Privacy Consent calls an answer callback on every save, even a save that changes nothing. A visitor who accepts, reopens the Preferences Center, and saves again pushes a second tf_consent_update. Accept this vendor on an embed placeholder pushes one too, with no click on the consent banner.
Keep each callback repeatable with no side effect. Push state or set a flag there, and never send a conversion from a callback.
Step 4: Verify that tags fire without a reload
Confirm in Google Tag Manager Preview mode and in the Network tab that your tags fire after the I Agree button, once, with no reload. These steps assume a visit that shows the consent banner.
- Click Preview in Google Tag Manager, enter your site address, and let Tag Assistant open it in a fresh window.
- Scroll halfway down the page and click I Agree on the consent banner.
- Confirm that Tag Assistant lists
tf_consent_updateandtf_pageview_after_consent, that your tags move to Tags Fired, and that the scroll position stays where it was. - Open Chrome DevTools on the same window, select the Network tab, and type
collect?v=2in the filter box. - Confirm that 1 GA4 page view request appears for the page, then reload.
- Confirm that Tag Assistant lists 1
tf_consent_updatewithtf_consent_sourceset tostored, and notf_pageview_after_consent.