Technical integrationThird party tools

Google Analytics 4

This work is licensed under CC BY-SA 4.0

Prerequisites

Before starting, you'll need:

  • A Google Analytics 4 Measurement ID (format: G-XXXXXXXXXX)

To find your Measurement ID: Sign in to Google Analytics → Admin → Data Streams → Select your web stream → Find the Measurement ID at the top right.

Integrate Google Analytics 4 with Consenter

Important: Before using this code, verify that the service ID and purpose ID values are up to date. These IDs are specific to your Consenter configuration. To find the correct IDs, go to Consenter Manager → Your Site → Active Banner → Hover over the service and purpose labels and click the copy button in the tooltip. See the Control Third Party Data Processing page for detailed instructions.

Copy the code below and paste it into your website's <head> section. Replace the placeholders with your own values:

  • YOUR_SERVICE_ID → Your Google Analytics service ID from Consenter Manager
  • YOUR_PURPOSE_ID → Your purpose ID from Consenter Manager
  • G-XXXXXXXXXX → Your Google Analytics 4 Measurement ID
index.html
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag() {
    dataLayer.push(arguments);
  }

  var serviceId = "YOUR_SERVICE_ID"; // Replace with your service ID from Consenter Manager
  var purposeId = "YOUR_PURPOSE_ID"; // Replace with your purpose ID from Consenter Manager
  var measurementId = "G-XXXXXXXXXX"; 

  gtag("consent", "default", {
    analytics_storage: "denied",
  });

  function loadGoogleAnalytics() {
    var existingScript = document.querySelector(
      'script[src*="googletagmanager.com/gtag/js"]',
    );
    if (!existingScript) {
      var script = document.createElement("script");
      script.async = true;
      script.src = "https://www.googletagmanager.com/gtag/js?id=" + measurementId;
      document.head.appendChild(script);
    }

    gtag("js", new Date());
    gtag("config", measurementId);
  }

  function subscribeToConsenter() {
    window.consenter.subscribe(
      function (hasConsent) {
        if (hasConsent) {
          gtag("consent", "update", {
            analytics_storage: "granted",
          });
          loadGoogleAnalytics();
        } else {
          gtag("consent", "update", {
            analytics_storage: "denied",
          });
        }
      },
      { serviceId: serviceId, purposeIds: purposeId }, 
    );
  }

  if (window.consenter) {
    subscribeToConsenter();
  } else {
    document.addEventListener("consenter:ready", subscribeToConsenter);
  }
</script>

Google Analytics 4 is now connected to Consenter.

How is this guide?

Shape Consenter Together

Consenter is built on an open and participatory process that grows through community collaboration. Whether you share feedback, improve the documentation, or contribute to the Risk Configuration Guides or Technical Integration Guides, your expertise helps make Consenter more privacy-friendly, interoperable, and useful for everyone—including your own users and services: Get finally your benefits and control the risks when sharing personal data.

Last updated on

On this page