Mark Ku's Blog

Introduction

If you run a website that primarily serves European and American markets and uses multiple third-party tracking tools, you must pay attention to GDPR (General Data Protection Regulation) compliance requirements. With the EU's Digital Markets Act (DMA) going into effect on March 6, 2024, tracking user data now requires explicit consent.

This article will teach you how to use Google Tag Manager (GTM) to set up Consent Mode. This ensures that GA4 e-commerce event parameters are only sent to the Facebook Pixel after the user clicks "Agree," making your tracking practices GDPR-compliant.

Why Do You Need to Understand the DMA?

The Digital Markets Act (DMA) is a regulation introduced by the EU to regulate tech giants (like Google, Apple, Meta, etc.) that officially went into effect in 2024. For websites operating in Europe and the US, this regulation requires that you clearly inform users how their personal data is used and obtain their consent before tracking any data.

How Does This Affect Websites?

If your website serves users in Europe and the US, such as an e-commerce or SaaS platform, you need to comply with these regulations to avoid potentially hefty fines.

How to Implement GDPR-Compliant User Tracking?

Choosing a Method

  1. Use an Off-the-Shelf Cookie Management Tool There are many tools on the market today, like Cookiebot, that can help you achieve compliance quickly. These tools don't require coding and cost around NT$7,000 per year, making them suitable for small to medium-sized businesses.

  2. Custom Development Although custom development requires technical support, it offers more flexibility in designing the user interface and functionality to meet specific needs. This tutorial will take this approach, using GTM and custom code for implementation.

Tutorial Steps

Prerequisites

  1. Ensure your GA4 e-commerce events are already set up. These events will be used to pass data to the Facebook Pixel.

  2. In GTM, create a new variable to store your Facebook Pixel tracking ID.

    Facebook Tracking ID Setup
    Facebook Tracking ID Setup
  3. Configure the GTM event template to ensure events are sent correctly. Tracking Tag Example Since we've already integrated GA e-commerce events, we can forward the properties of these GA events to the Facebook Pixel. Facebook Tracking Tag Example - page view Facebook Tracking Tag Example - add to cart Facebook Tracking Tag Example - begin checkout Facebook Tracking Tag Example - purchase

When a user clicks the "Agree" button, you can use the following code to pass the consent event and its properties to GTM via the data layer.

export function acceptConsentGranted(isAllow: boolean = false) {
    if (!window || !window.dataLayer) {
        return;
    }

    window.dataLayer.push({
        event: 'consent_update',
        gdpr_consent: {
            consentGranted: isAllow, // true 或 false
            timestamp: new Date().toISOString(),
        },
    });
}

P.S. The dataLayer is the foundation of gtag.js; the operation of gtag.js is based on the dataLayer.

Finalizing the Setup in GTM

1. Create a Variable

Create a new variable to receive the value of gdpr_consent.

GDPR Variable Setup
GDPR Variable Setup

2. Modify Trigger Conditions

Set the trigger conditions in GTM so that events only fire when gdpr_consent.consentGranted is true. Here are a few examples:

  • FB Pixel - Add to Cart Add to Cart Trigger Condition

  • FB Pixel - Begin Checkout Begin Checkout Trigger Condition

  • FB Pixel - Page View Page View Trigger Condition

  • FB Pixel - Purchase Purchase Trigger Condition

Testing and Verification

Once the setup is complete, you can use Tag Assistant to test it:

  1. Simulate a user who has not clicked "Agree" Confirm that no tracking events are fired. Test Result Without Consent

  2. Simulate a user who has clicked "Agree" Confirm that all tracking events are successfully fired and data is sent. Test Result After Consent

Conclusion

GDPR might make ad targeting less precise, but achieving GDPR compliance is about more than just following the law; it's also a safeguard for consumers. By correctly configuring GTM and implementing Consent Mode, you can continue to acquire the data your business needs while protecting user privacy.

References

Author

Mark Ku

擁有 10+ 年經驗的資深軟體工程師,現為 AI 應用 Builder,專注於大型平台架構與簡化複雜系統設計,從電商系統到訂閱與收費平台,結合 AI Agent、AI 整合與自動化開發,打造高效率且可持續演進的產品技術基礎。Read More

Found this useful?

The author's free tools, daily podcasts and newsletter are all here.

Mark Ku · This article is licensed under CC BY 4.0. Credit the author and link back to the original when reusing it.

Comments

Subscribe to Newsletter

Subscribe to get new posts delivered instantly — never miss a tech share.

By submitting, you agree to receive emails. You can anytime.

Popular Posts

View all
Mark Ku
··602

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution
Mark Ku
··490

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.
Mark Ku
··333

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki
Mark Ku
··264

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning
Mark Ku
··221

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1
Mark Ku
··215

Setting Up Samba on Ubuntu to Share Folders with Windows 11

Setting Up Samba on Ubuntu to Share Folders with Windows 11