Instantly share code, notes, and snippets.
Last activeOctober 4, 2020 23:51
Save kakauandme/f6a93db345729c5897f3fd7c149d04ae to your computer and use it in GitHub Desktop.
Disable Google Analytics and Google Ads cookies/identifiers for GDPR countries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
<scriptasyncdefersrc="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-X"></script> | |
<script> | |
window.dataLayer=window.dataLayer||[]; | |
functiongtag(){dataLayer.push(arguments);} | |
gtag('consent','default',{// disable GA and Ads identifiers for GDPR countries by default | |
'ad_storage':'denied', | |
'analytics_storage':'denied', | |
'region':['AT','BE','BG','HR','CY','CZ','DK','EE','FI','FR','DE','GR','HU','IE','IT','LV','LT','LU','MT','NL','PL','PT','RO','SK','SI','ES','SE','GB','GF','GP','MQ','ME','YT','RE','MF','GI','AX','PM','GL','BL','SX','AW','CW','WF','PF','NC','TF','AI','BM','IO','VG','KY','FK','MS','PN','SH','GS','TC','AD','LI','MC','SM','VA','JE','GG','IS','NO'], | |
}); | |
gtag('js',newDate()); | |
gtag('config','UA-XXXXXXXX-X'); | |
document.getElementById("accept_cookies")// allow identifiers after user gives consent | |
.addEventListener("click",function() | |
{ | |
gtag("consent","update",{ | |
ad_storage:"granted", | |
analytics_storage:"granted", | |
}); | |
}); | |
// source: https://developers.google.com/gtagjs/devguide/consent | |
</script> |
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment