如何创建Google Tag Manager触发器实现合规加载Google再营销代码
I’ve tackled this exact scenario many times—here’s a step-by-step, reliable method to get your Google Remarketing code loading only after the user accepts marketing cookies, fully compliant with GDPR:
Step 1: Update Your Cookie Consent Banner to Signal Consent to GTM
First, you need to send a clear signal to GTM when the user agrees to marketing cookies. Modify your consent banner’s "Accept Marketing" button handler to push an event and variable to the GTM data layer once consent is given.
Add this code to your banner’s click action:
window.dataLayer = window.dataLayer || []; dataLayer.push({ 'event': 'marketing_consent_granted', 'allow_marketing_cookies': true }); // Optional: Save consent to a cookie for returning visitors document.cookie = "marketing_consent=true; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/";
For returning users who’ve already consented, push the event on page load too:
// Check for existing consent cookie when the page loads if (document.cookie.includes('marketing_consent=true')) { window.dataLayer = window.dataLayer || []; dataLayer.push({ 'event': 'marketing_consent_granted', 'allow_marketing_cookies': true }); }
Step 2: Create a Data Layer Variable in GTM
Set up a variable to read the consent status from the data layer:
- Go to Variables > User-Defined Variables > New
- Select the Data Layer Variable type
- Set the Data Layer Variable Name to
allow_marketing_cookies - Name the variable something clear like
DL - Allow Marketing Cookiesand save it
Step 3: Build a Custom Trigger for Consent
Create a trigger that only fires when consent is confirmed:
- Go to Triggers > New
- Choose the Custom Event trigger type
- Set the Event Name to
marketing_consent_granted(matches the event we pushed earlier) - Under This trigger fires on, select Some Custom Events
- Add a condition:
DL - Allow Marketing Cookiesequalstrue - Name the trigger
Trigger - Marketing Consent Grantedand save it
Step 4: Add Your Google Remarketing Code as a Custom HTML Tag
Finally, add your remarketing code to GTM and bind it to the consent trigger:
- Go to Tags > New
- Select the Custom HTML tag type
- Paste your full Google Remarketing code into the HTML field (leave out
async/defer—GTM will handle loading it only when the trigger fires) - Under Triggering, select the
Trigger - Marketing Consent Grantedtrigger you created - Name the tag
Tag - Google Remarketingand save it
Testing the Setup
- Use GTM’s Preview Mode to validate: load your site, click the consent banner’s accept button, and confirm the
marketing_consent_grantedevent fires. Check your browser’s Network tab to ensure the remarketing script is only requested after consent. - For returning visitors, verify the tag loads automatically on page load if the consent cookie exists.
内容的提问来源于stack exchange,提问作者Leonida-Alexandru Diaconu

