How to configure the cookie widget with Googleconsent Mode
With Gtag tool from Google, it is now possible for a few solutions to use Google Consent Mode, a tag management system based on consent. This method, actually in Beta works only with Google Analytics, Floodlight, Google Ads and Conversion Linker.
<script>
// google tools use queues to push information
// before their libraries are loaded. We need to
// define them if this code runs first
window.adsbygoogle = window.adsbygoogle || [];
window.dataLayer = window.dataLayer || [];
function gtag() {
window.dataLayer.push(arguments);
}
// Axeptio use the same technique. _axcb holds the
// callback functions that will be executed when
// our lib is loaded
window._axcb = window._axcb || [];
// we pause ad requests by default
// https://support.google.com/adsense/answer/9042142
window.adsbygoogle.pauseAdRequests = 1;
// first we need to push a "js" command to the queue. This will
// register the exact time of the DOM being ready
// as stated here: https://developers.google.com/analytics/devguides/collection/gtagjs
gtag("js", new Date());
// this must be the first thing to be sent to the dataLayer
// cf. https://developers.google.com/gtagjs/devguide/consent#configure_default_behavior
// "call the gtag('consent', 'default', ...) command on every page
// of your site before any commands that send measurement data
// (such as config or event)."
gtag("consent", "default", {
ad_storage: "denied",
analytics_storage: "denied"
});
// after that we wand to update the consent
// when the user gives his/her consent
window._axcb.push(function(axeptio) {
// now that axeptio is loaded, we add completion
// handler to execute when the user give his/her consent.
// this will also happen when we parse the axeptio_cookie
// which contains the preferences of a user
axeptio.on("cookies:complete", function(choices) {
// if you renamed your vendors in Axeptio's admin panel
// change the content of these variables.
var gaVendorName = 'google_analytics';
var adsVendorName = 'Google_Ads';
var consentSettings = {
ad_storage: "denied",
analytics_storage: "denied"
};
if (choices[gaVendorName]) {
consentSettings.analytics_storage = "granted";
// We decided to send a manual pageview to make sure
// we do not lose the first visit tracking information
gtag("send", "pageview");
}
if (choices[adsVendorName]) {
consentSettings.ad_storage = "granted";
} else {
//When ad_storage is set to 'denied', Google tags will not create or save cookies.
//However, you can optionally elect to pass information through URL parameters
//across pages in order to improve measurement quality.
gtag("set", "url_passthrough", true);
window.adsbygoogle.requestNonPersonalizedAds = 1;
}
// Finally, we update the Google Consent Mode variable to reflect
// the choice of the user
gtag("consent", "update", consentSettings);
// Ads can be resumed.
window.adsbygoogle.pauseAdRequests = 0;
});
});
</script>