Skip to content

Instantly share code, notes, and snippets.

@stefanatcroud
Last active January 9, 2024 16:45
Show Gist options
  • Save stefanatcroud/b25174c2a18052db13c912f9dcbce2ff to your computer and use it in GitHub Desktop.
Save stefanatcroud/b25174c2a18052db13c912f9dcbce2ff to your computer and use it in GitHub Desktop.
Bespoke CoMo script for US versus non-US visitors
<!-- Google Consent Mode V2 -->
<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',
wait_for_update: 500
});
gtag('consent', 'default', {
ad_storage: 'granted',
ad_user_data: 'granted',
ad_personalization: 'granted',
analytics_storage: 'granted',
region: ['US'],
wait_for_update: 500
});
gtag('set', 'url_passthrough', true);
</script>
<!-- End Google Consent Mode V2 -->
@stefanatcroud
Copy link
Author

This script will set all relevant consent categories to "denied" for all users except those based in the US. US users will be considered as granting consent by default.

'url_passthrough' will mitigate potential loss of attribution data caused by users navigating the site prior to dismissing the consent banner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment