Jump to content

Sample code for integrating Google's consent mode with Google Ads

Go to solution Solved by Parker_SQSP,

Recommended Posts

  • Solution

If you’re using Google Ads on your site, and targeting users in the EEA, you might have received an email from Google suggesting that you implement their consent mode feature.

Here is some example code, that I’ve added to my website to integrate consent mode. This enables me to collect a consent choice from my visitor via the website cookie banner, and then send that consent choice to Google.

Before you begin:

First, you need to enable the website cookie banner, and ensure you’ve selected the “Opt In & Out” banner type. 

Code:

Next, you would go to Website > Website Tools > Code Injection. You could add the following code to your header code injection (removing any existing Google Ads code first):

<script async src="https://www.googletagmanager.com/gtag/js?id=AW-{AW-ID}"></script>

<script>

 window.dataLayer = window.dataLayer || [];

 function gtag(){dataLayer.push(arguments);}

 gtag('consent', 'default', {

  'ad_storage': 'denied',

  'ad_user_data': 'denied',

  'ad_personalization': 'denied'

 });

 gtag('js', new Date());

 gtag('config', 'AW-{AW-ID}');



 let squarespaceCookies = {};

 if (window.getSquarespaceCookies) {

   squarespaceCookies = window.getSquarespaceCookies();

 }

 const consentValue = squarespaceCookies.marketing === 'accepted' ? 'granted' :  'denied';

 const consentObj = { 

  'ad_storage': consentValue,

  'ad_user_data': consentValue,

  'ad_personalization': consentValue

 };

 gtag('consent', 'update', consentObj);

 

 window.onCookieBannerInteraction = () => {

   let squarespaceCookies = {};

   if (window.getSquarespaceCookies) {

     squarespaceCookies = window.getSquarespaceCookies();

   }

   const consentValue = squarespaceCookies.marketing === 'accepted' ? 'granted' :  'denied';
  const consentObj = { 

     'ad_storage': consentValue,

     'ad_user_data': consentValue,

     'ad_personalization': consentValue

   };

   gtag('consent', 'update', consentObj);

 }

</script>

Consent mode is a service provided by Google, so for any questions, verification, or testing support, you can reach out to their support team.

Link to comment
  • 3 weeks later...
  • 3 weeks later...

<!-- Add this script to your HTML -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('consent', 'default', {
    ad_storage: 'denied', // Set default consent to deny ad storage
    analytics_storage: 'denied' // Set default consent to deny analytics storage
  });
  gtag('set', 'ads_data_redaction', true); // Enable ads data redaction
  gtag('js', new Date());
  gtag('config', 'GA_MEASUREMENT_ID');
</script>

 

Replace GA_MEASUREMENT_ID with your actual Google Analytics Measurement ID.

This code sets the default consent for ad storage and analytics storage to denied and enables ads data redaction. It integrates with Google Ads by ensuring that ads data is not stored or used for users who have not provided consent.

Link to comment

Thank you @Alisonsmith12 for helping us to configure google tag manager with google consent mode.

Excuse my ignorance, but why should we use The google Analytics measurement ID (G-XXXXXX) instead of the google tag manager (GTM-XXXX). 

Should this code replace the goggle tag manager code injection or it should go above or beneath.

<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXX');</script>
<!-- End Google Tag Manager -->

 

Thank you

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.