How to install Google Consent Mode v2 manually?

How to install Google Consent Mode v2 manually?

Nelson
Written by NelsonLast update 17 days ago

Google allows the use of its own consent mode by including APIs to control tag cookie storage based on user consent choices without disabling services provided by Google, such as Google Analytics, Google Ads, Floodlight, and Conversion Linker.

You can implement Google Consent Mode v2 on your website or app manually. However, this implementation method is not recommended since you will have to follow up the changes of privacy regulation and update the code yourself.

To implement Google Consent Mode v2 manually, you need to include the code snippet into your website or app.

To enable Google Consent Mode v2 with consent modes set by default to denied, include this code snippet just before including gtag.js or GTM snippet:

<script>
    window.dataLayer = window.dataLayer || [];
    
    function gtag() {
        dataLayer.push(arguments);
    }

    gtag("consent", "default", {
        ad_storage: "denied",
        analytics_storage: "denied",
        ad_user_data: "denied",
        ad_personalization: "denied",
        wait_for_update: 500
    });

    gtag("set", "ads_data_redaction", true);
    gtag("set", "developer_id.dMmY1Mm", true);
</script>

The following code snippet shows how to update the consent status to granted, when user agrees to allow advertising cookies:

<script>
    window.dataLayer = window.dataLayer || [];
    
    function gtag() {
        dataLayer.push(arguments);
    }

    gtag("consent", "default", {
        ad_storage: "granted",
        analytics_storage: "granted",
        ad_user_data: "granted",
        ad_personalization: "granted",
        wait_for_update: 500
    });

    gtag("set", "ads_data_redaction", true);
    gtag("set", "developer_id.dMmY1Mm", true);
</script>

Implementing Google Consent Mode manually, you must include this code before your gtag.js or GTM code snippet. In case your dataLayer was renamed, you should also rename it in the code above.

Without the GTM implementation (using gTag.js) you might want to block third-party scripts from firing before cookie consent. This can be done using third-party script blocking.

Enable Google Consent Mode v2 in the cookie banner settings

Google Consent Mode v2 should be enabled in your cookie banner settings too. Go to your banner settings > Behavior tab > Frameworks section > Enable Google Consent Mode V2

You can optionally change a category that allows Ads Storage, Analytics Storage, Functionality Storage, Personalization Storage, and Security Storage in Google Consent mode. 

Other Google Consent Mode v2 installation methods:

How to implement Google Consent Mode v2 with the Google Tag Manager (recommended)?

How to implement Google Consent Mode v2 using a Wordpress plugin?

Note: To implement Google Consent Mode v2, use only one method.

Note: If you implemented consent mode v2 manually, implement the Google Ads script manually as well. Just make sure that your cookie banner script loads first, and only then- the Google Ads script.

For more information, read the following documentation from Google:

Google documentation for web:

Google documentation for app:

Did this answer your question?