Google consent mode v2 integration

Tag Manager

Consent Manager

According to Google’s policy, Google ads are displayed only to visitors who have consented to analytics and advertising cookies. They won’t pop up if the visitor hasn’t given their consent.

Google relies on two consent mechanisms: IAB TCF 2.2 and Google consent mode v2. To be compliant with Google’s regulations, you can use Consent Manager and our integration with Google consent mode v2.

In this article, we’ll explain how to set it up.

Before you start

Here are some things to know before you start:

  • This integration will only work if you have a Google tag (gtag.js) installed on your site. To add it, follow Google’s instructions.
  • This integration doesn’t work with Google Tag Manager. To integrate with Google Tag Manager, use these instructions.
  • You need to use Consent Manager, which collects visitors’ consents. This integration will only pass on the information received by Consent Manager.
  • Google consent mode v2 is automatically applied in all countries. However, Piwik PRO’s default setting only requires consent in countries governed by the GDPR and UK GDPR. To extend Piwik PRO’s consent request to all countries, turn off the following setting: Administration > Sites & apps > Privacy > Ask visitors for consent > Consent settings > Ask for consent only in countries under the GDPR and UK GDPR (off).
  • If Google receives any data between the page opening and the consent update, it will be treated as anonymous or null.

To integrate with Google consent mode v2, follow these steps:

  1. Make sure to fire the default consent command on each page before any tracking scripts. If you need help, see Google’s documentation. Here’s an example of the default settings, but make sure it complies with your legal requirements:
    <script>
        window.dataLayer = window.dataLayer || [];
    
        function gtag() {
            dataLayer.push(arguments);
        }
    
        gtag('consent', 'default', {
            'ad_storage': 'denied',
            'ad_user_data': 'denied',
            'ad_personalization': 'denied',
            'analytics_storage': 'denied',
            'wait_for_update': 1500
        });
    </script>
  2. Here’s the code to map consents between the two tools and pass consent data from Piwik PRO to Google. Review it carefully, and then modify it as needed.
    <script>
        function googleConsentModeMapping(consents) {
            if ({{ Consents }}) {
                var consObj = {};
                Object.keys(consents).forEach(function(consent) {
                    consObj[consent] = ({{ Consents }}.current_state[consents[consent]] === 1) ? 'granted' : 'denied';
                });
                gtag('consent', 'update', consObj);
            }
        }
    
    // Call the function with specific consent mappings; these mappings can be adjusted as needed
        googleConsentModeMapping({
            "ad_storage": "remarketing",
            "ad_user_data": "remarketing",
            "ad_personalization": "remarketing",
            "analytics_storage": "analytics"
        });
    </script>
    

    Note:

    • For Google’s consent details, refer to their documentation. For information on Piwik PRO’s consents, see our documentation.
    • To display a consent type on the Piwik PRO consent form, make sure that at least one tag uses that consent type.
  3. Log in to Piwik PRO.
  4. Go to Menu > Tag Manager.
  5. Navigate to Tags.
  6. Click Add a tag.
  7. Name your tag and select the following type: Custom code (async).
  8. Click Next.
  9. In Tag code, type your code from step 2.
    Google consnent mode v2 integration
  10. In Advanced tag settings > Consent type, choose the following type: No consent is required.
  11. In Tag triggers, click Choose existing trigger.
  12. Select the following trigger: All page views.
  13. In Tag triggers, click Add a trigger.
  14. Select the following type: Data layer event.
  15. Set the following condition: Event name contains stg.consentDecisionMade.
    Google consnent mode v2 integration
  16. Click Add.
  17. When you’re done, click Save.
  18. (Optional) Go to Settings > Tag priority and add this tag to Prioritized tags. This will make sure the tag fires first, before others that might send data to Google.
  19. Test your tag in debug mode.
  20. When you’re happy with how the tag works, click Publish.
  21. All done!

    Note: If Google receives any data between the page opening and the consent update, it will be treated as anonymous or null.

Was this article helpful?

Technical support

If you still have any questions, visit our community.
There’s always someone happy to help!

Related articles

Back to help center