Google global site tag (gtag.js) Integration

The global site tag (gtag.js) is a JavaScript tagging framework and API that allows you to send event data to Google Ads, Campaign Manager, Display & Video 360, Search Ads 360, and Google Analytics.

Example of gtag.js and UniConsent Integration

Simple Mode

Put the following tag just before UniConsent CMP tag and before your Google tags or any other advertising tags:

<script type="text/javascript">
__tcfapi("addEventListener", 2, function(tcData, success) {
    if (success && tcData.unicLoad  === true) {
        if(!window._initAds) {
            window._initAds = true;
            // your other tags:
            // var script = document.createElement('script');
            // script.async = true;
            // script.setAttribute('data-ad-client', 'ca-pub-xxxxxxxx');
            // script.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
            // document.head.appendChild(script);
        }
    }
});
</script>

The above codes delay all the Google tag cookies until the user grants consent.

Put the following tag just before UniConsent CMP tag and before your Google tags or any other advertising tags:

<script>
(function() {
    if(!window['gtag']) {
        window['dataLayer'] = window['dataLayer'] || [];
        window['gtag'] = function(){window['dataLayer'].push(arguments);}
    }
    window['gtag']('set', 'developer_id.dZTcxZD', true);
    if(localStorage && localStorage.getItem('__unic_consent_mode') !== null) {
        var consentStatus = JSON.parse(localStorage.getItem('__unic_consent_mode'));
        window['gtag']('consent', 'default', consentStatus);
    } else {
        window['gtag']('consent', 'default', {
            ad_storage: 'denied',
            functionality_storage: 'denied',
            personalization_storage: 'denied',
            analytics_storage: 'denied',
            ad_user_data: 'denied',
            ad_personalization: 'denied',
            security_storage: 'granted',
        });
    }
    window['gtag']("set", "ads_data_redaction", true);
    window['gtag']("set", "url_passthrough", true);
})();
</script>

The above codes communicate with Google services via Consent Mode V2 api. Find more about Google Consent Mode V2