In order to comply with Google Consent Mode v2 there are a couple of extra steps needed.
1. Enable Consent Mode in Google Tag Manager
Ensure you have enabled consent mode inside Google Tag Manager(GTM) and correctly configure your tags to be linked with appropriate consent types.
2. Add the Silktide Cookie Banner
Open Google Tag Manager and follow the below steps to add the Silktide code snippet (which you created in the installation wizard).
- Click Tags on the left
- Click New in the top right
- Name the tag: Consent Manager (Silktide)
- Choose the tag type of Custom HTML
- Add your Silktide Cookie Banner script link and config options
<script src="path-to-js/silktide-cookie-banner.js"></script>
<script>
silktideCookieBannerManager.updateCookieBannerConfig({
// Your config options here
});
</script>
- Under Advanced settings, set Tag firing options to Once per page and set Consent Settings to No additional consent required.
- Set Firing Triggers to Consent Initialization.
3. Setting default consent
Default consent should be set for each consent type before your Google Tag Manager script is loaded. This prevents any data being shared before consent has been given.
On each page load we check if the user has previously set consent for each consent type and pass these to Google Tag Manager as the default values.
On your website, add the below code before your existing Google Tag Manager code. This is usually inside a header file or template that is loaded on every page of your website.
<script>
// Initialize the dataLayer
window.dataLayer = window.dataLayer || [];
// Create the gtag function that pushes to the dataLayer
function gtag() {
dataLayer.push(arguments);
}
// Set consent defaults
gtag('consent', 'default', {
analytics_storage: localStorage.getItem('silktideCookieChoice_analytics') === 'true' ? 'granted' : 'denied',
ad_storage: localStorage.getItem('silktideCookieChoice_marketing') === 'true' ? 'granted' : 'denied',
ad_user_data: localStorage.getItem('silktideCookieChoice_marketing') === 'true' ? 'granted' : 'denied',
ad_personalization: localStorage.getItem('silktideCookieChoice_marketing') === 'true' ? 'granted' : 'denied',
functionality_storage: localStorage.getItem('silktideCookieChoice_necessary') === 'true' ? 'granted' : 'denied',
security_storage: localStorage.getItem('silktideCookieChoice_necessary') === 'true' ? 'granted' : 'denied'
});
</script>
NOTE: If the cookie type ids
in the config have been modified from the defaults, you should also update them to match in the code above inside the
function.gtag()
We grouped the consent types as below, but you are free to change these based on your needs:
ad_storage
,ad_user_data
andad_personalization
: Grouped under “advertising“.functionality_storage
andsecurity_storage
: Grouped under “necessary“.
4. Handling consent changes
The onAccept
and onReject
callback functions for each cookie type in the config can be used to update the user’s consent.
If you’ve used our install page and selected
consent types, this will be part of the generated config already.gtag()
Here is an example for the analytics
cookie type id
that is set to use the analytics_storage
consent type:
onAccept: function() {
gtag('consent', 'update', {
analytics_storage: 'granted',
});
dataLayer.push({
'event': 'consent_accepted_analytics',
});
},
onReject: function() {
gtag('consent', 'update', {
analytics_storage: 'denied',
});
}
onAccept
– we use gtag()
to update the consent value to granted and fire an event that we can later use as a trigger inside Google Tag Manager to load additional scripts/tags related to this particular consent type.
onReject
– we use gtag()
to update the consent value to denied and no events are fired.
5. Triggers
Next we need to setup a trigger in Google Tag Manager for each cookie type we are sending an event for when the onAccept
callback is triggered.
In this example we’ll configure the trigger for the analytics
cookie type.
Inside Google Tag Manager:
- Click Triggers on the left
- Click New in the top right
- Choose the trigger type of Custom Event
- Name the trigger: Consent Trigger – Analytics (Granted)
- Set the event name to
consent_accepted_analytics
(this must match the event name set in theonAccept
callback) - Set the event to fire on All Custom Events
6. Tags
Next we need to attached the triggers we’ve just created to the appropriate tags. In this example we are going to load an analytics related scripts from within Google Tag Manager but only if the user has granted analytics_storage
consent.
Inside Google Tag Manager:
- Click Tags in the left
- Open your tag
- Under Advanced settings, set Tag firing options to Once per page.
- Under Consent settings, select “Required additional consent for tag to fire” and add
analytics_storage
. Some standard tags (Adwords, GA4) come with built-in consent types that will show here – these may meet your requirements already based on your setup, in which case no additional consent checks are required. - Under Triggering, add your Consent Trigger – Analytics (Granted) trigger