Google Analytics Consent Mode Implementation
Advanced technical documentation outlining how to set up default consents or how to define conditions for Google Analytics collection.
This covers the advanced implementation of the Google Analytics Consent Mode setting. This covers how to set default consents prior to Google Analytics activation and/or how to tell Google Analytics to wait on CMP interaction. This is an advanced approach that involves placing custom code on your site or making settings adjustments to a Google Tag Manager implementation. However despite being an advanced approach, we encourage this to be done for publishers based in the European Union.
Understand How Google Analytics is Added to Your Site
The first step in this process is to know where and how Google Analytics is inserted on your site. There are typically three ways that Google Analytics has been added to your site: a gtag script, a Google Tag manager script, or with a dedicated Google Analytics plugin.
Gtag Implementation
A standard gtag script would look something like this:
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX'"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXX');
</script>
This type of script is typically inserted through a custom code plugin, through a theme setting, or in same cases hardcoded onto your theme/template files.
Once found you will need to copy/paste this snippet of code above your Google Analytics code:
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('consent', 'default', {
'ad_storage': 'denied',
'analytics_storage': 'denied',
'ad_user_data':'denied',
'ad_personalization': 'denied',
'wait_for_update': 500
});
dataLayer.push({
'event': 'default_consent'
});
</script>
Placing this above your gatg script declares that the default state for Google Analytics consents is “denied” until the CMP signals if a reader in the EU has consented. If they are in a region that doesn’t require consent, then a backend process will change consent to “granted” automatically.
If you do not insert this default consent script above your Google Analytics code, then your Google Analytics may collect data prior to being granted consent.
Potential Additional Step For Google Analytics Consent
The last consideration is to make sure that the ad script wrapper runs before Google Analytics runs. Since our ad script loads the consent management platform(CMP) pop-up, having it load prior to Google Analytics will help with the timing of gathering consents.
You can write in to publishers@mediavine.com and the client experience team can check the placement of your scripts and make any appropriate recommendations.
Google Tag Manager Implementation
If you have a Google Tag Manager profile set up, there can be multiple ways to insert Google Analytics code. You will typically find it in the Tags section as a “Google Tag” type, but you may also find it inserted with a “Custom HTML” tag type.
Adjusting your Google Tag Manager settings for your Google Analytics script involves telling your Google Analytics tag to only fire after consent has been granted in the CMP.
The first step in accomplishing this is to create a new Variable. This can be done by clicking on Variables in the left menu area and clicking the “New” button under “User-Defined Variables”:
Click inside the Variable Configuration area and Data Layer Variable
Set the Data Layer Variable Name and the variable name to cmpConsentVendors
Next click on Triggers in the left side menu and click New:
Click in the Trigger Configuration area, scroll down and select Custom Event:
Name your trigger cmpEvent, set the Event Name to cmpEvent, select “Some Custom Events”, then select , and enter ,s26, next to the “Contains” dropdown:
Lastly, you’ll need to find your Tag that contains the Google Analytics code that’s inserted on your site. Click on Tags in the left side menu and find your Google Tag type and select it. If there is a trigger like “All Pages - Pageview” or “Initilization - All Pages”, then you’ll want to remove those triggers.
Once removed, you can add a new Trigger and look for cmpEvent to select
Save your tag and then Publish your changes.
Analytics Plugin Implementation
If you have a plugin like Google Site Kit, MonsterInsights, or ExactMetrics installed in your WordPress Admin, those could be used to insert Google Analytics code on your site. Check their settings first to see if they are actively in use on your site.
Unlike other Google Analytics implementations, you don’t have control over where the analytics code has been inserted in relation to our ad script. Due to this situation, it can be very difficult to ensure the CMP executes before Google Analytics does.
If analytics consent is a priority for you, but using one of these plugins is not a priority, we recommend disabling the plugin and implementing Google Analytics with a gtag script or a Google Tag Manager script.
However if analytics consent and continuing to use one of these plugins are both priorities, we recommend reaching out to a developer and/or their plugin support to see if it’s possible to manipulate the placement of the analytics script so that it’s below our ad script. If that is possible, then you’d need this script placed above your analytics script:
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('consent', 'default', {
'ad_storage': 'denied',
'analytics_storage': 'denied',
'ad_user_data':'denied',
'ad_personalization': 'denied',
'wait_for_update': 500
});
dataLayer.push({
'event': 'default_consent'
});
</script>