Gtag and Google Consent Mode
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.
For more info, check Google's documentation : https://developers.google.com/gtagjs/devguide/consent
1
<script>
2
// google tools use queues to push information
3
// before their libraries are loaded. We need to
4
// define them if this code runs first
5
window.adsbygoogle = window.adsbygoogle || [];
6
window.dataLayer = window.dataLayer || [];
7
function gtag() {
8
window.dataLayer.push(arguments);
9
}
10
// Axeptio use the same technique. _axcb holds the
11
// callback functions that will be executed when
12
// our lib is loaded
13
window._axcb = window._axcb || [];
14
15
// we pause ad requests by default
16
// https://support.google.com/adsense/answer/9042142
17
window.adsbygoogle.pauseAdRequests = 1;
18
19
// first we need to push a "js" command to the queue. This will
20
// register the exact time of the DOM being ready
21
// as stated here: https://developers.google.com/analytics/devguides/collection/gtagjs
22
gtag("js", new Date());
23
24
// this must be the first thing to be sent to the dataLayer
25
// cf. https://developers.google.com/gtagjs/devguide/consent#configure_default_behavior
26
// "call the gtag('consent', 'default', ...) command on every page
27
// of your site before any commands that send measurement data
28
// (such as config or event)."
29
gtag("consent", "default", {
30
ad_storage: "denied",
31
analytics_storage: "denied"
32
});
33
34
35
// after that we wand to update the consent
36
// when the user gives his/her consent
37
window._axcb.push(function(axeptio) {
38
// now that axeptio is loaded, we add completion
39
// handler to execute when the user give his/her consent.
40
// this will also happen when we parse the axeptio_cookie
41
// which contains the preferences of a user
42
axeptio.on("cookies:complete", function(choices) {
43
44
// if you renamed your vendors in Axeptio's admin panel
45
// change the content of these variables.
46
var gaVendorName = 'google_analytics';
47
var adsVendorName = 'Google_Ads';
48
49
var consentSettings = {
50
ad_storage: "denied",
51
analytics_storage: "denied"
52
};
53
54
if (choices[gaVendorName]) {
55
consentSettings.analytics_storage = "granted";
56
// We decided to send a manual pageview to make sure
57
// we do not lose the first visit tracking information
58
gtag("send", "pageview");
59
}
60
61
if (choices[adsVendorName]) {
62
consentSettings.ad_storage = "granted";
63
} else {
64
//When ad_storage is set to 'denied', Google tags will not create or save cookies.
65
//However, you can optionally elect to pass information through URL parameters
66
//across pages in order to improve measurement quality.
67
gtag("set", "url_passthrough", true);
68
window.adsbygoogle.requestNonPersonalizedAds = 1;
69
}
70
// Finally, we update the Google Consent Mode variable to reflect
71
// the choice of the user
72
gtag("consent", "update", consentSettings);
73
// Ads can be resumed.
74
window.adsbygoogle.pauseAdRequests = 0;
75
});
76
});
77
</script>
Copied!
Last modified 6mo ago
Copy link