How to tag your JavaScript scripts.
Instructions
-
Tag any JavaScript
<script>that you're using by changingtype="text/javascript"totype="text/plain". -
Add the corresponding consent level for each tagged
<script>. For example:data-cookie-consent="functionality".
Example
<!-- Login Cookies -->
<script type="text/plain" data-cookie-consent="strictly-necessary" src="/js/login-session.js"></script>
<!-- Google Analytics -->
<script type="text/plain" data-cookie-consent="tracking">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', GOOGLE_PROPERTY_ID_GOES_HERE, 'auto');
ga('send', 'pageview');
</script>
Cookie Consent and Google Consent Mode v2.
Cookie Consent works with Google Consent Mode V2. Create the gtag function with every default consent state set to denied, then load the Google Analytics script. The Cookie Consent callbacks update the consent states after the visitor makes a choice.
Instructions
-
Create the gtag function with the default consent states as denied.
<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' }); </script> -
Load Google Analytics on your page without tagging it with a consent category. Google Analytics adjusts its behavior to the consent states, which keeps analytics measurement running.
<script async src="https://www.googletagmanager.com/gtag/js?id=
TAG_ID"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'TAG_ID'); </script> -
Update your Cookie Consent config code to include the
user_consent_saved_on_page_loadandcallbacksparameters. Theuser_consent_savedcallback updates the consent states from the choice the visitor made, on every page, before Cookie Consent loads any tagged script."user_consent_saved_on_page_load": true, "callbacks": { "user_consent_saved": (details) => { gtag('consent', 'update', { 'analytics_storage': details.tracking ? 'granted' : 'denied', 'ad_storage': details.targeting ? 'granted' : 'denied', 'ad_user_data': details.targeting ? 'granted' : 'denied', 'ad_personalization': details.targeting ? 'granted' : 'denied' }); } }
Cookie Consent and Microsoft UET Consent Mode.
Microsoft UET Consent Mode works with Cookie Consent too. Load the Microsoft UET script, and create the uetq function with the default consent states set to denied. From there, the Cookie Consent callbacks update the consent states after each visitor choice.
Instructions
-
Load the Microsoft UET script.
<script> (function(w,d,t,r,u){var f,n,i;w[u]=w[u]||[],f=function(){var o={ti:"TAG_ID", enableAutoSpaTracking: true};o.q=w[u],w[u]=new UET(o),w[u].push("pageLoad")},n=d.createElement(t),n.src=r,n.async=1,n.onload=n.onreadystatechange=function(){var s=this.readyState;s&&s!=="loaded"&&s!=="complete"||(f(),n.onload=n.onreadystatechange=null)},i=d.getElementsByTagName(t)[0],i.parentNode.insertBefore(n,i)})(window,document,"script","//bat.bing.com/bat.js","uetq"); </script> -
Set the default consent state to denied.
<script> window.uetq = window.uetq || []; window.uetq.push('consent', 'default', { 'ad_storage': 'denied' }); </script> -
Update your Cookie Consent config code to include the
user_consent_saved_on_page_loadandcallbacksparameters. Theuser_consent_savedcallback updates the consent states from the choice the visitor made, on every page, before Cookie Consent loads any tagged script."user_consent_saved_on_page_load": true, "callbacks": { "user_consent_saved": (details) => { window.uetq = window.uetq || []; window.uetq.push('consent', 'update', { 'ad_storage': details.targeting ? 'granted' : 'denied' }); } }




