I have to implement Google Consent v2 together with PrivacyWire for the first time. I tried to summarize the most imporant things and make a quick example. Hope that helps. Improvements welcome.
Basically Google wants you to send the user preferences. This way Google can process at least anonymized data if the user denies permissions (and promises to respect privacy).
Luckily PrivacyWire gives us the possibility to define a custom js function in the module configuration, which is executed whenever user preferences change.
PrivacyWire stores user preferences in localStorage. From there we can fetch this information when needed. So we have to:
1. Integrate Google Tag Manger without modifying the script tag.
2. Set Consent v2 defaults (by default deny every permission):
<!-- GOOGLE CONSENT V2 -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GTM-ID-HERE"></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'
});
gtag('js', new Date());
gtag('config', 'GTM-ID-HERE');
</script>
<!-- End GOOGLE CONSENT V2 -->
3. Use a function called by PrivacyWire, when user changes preferences. Fetch user preferences from localStorage and send them to Google:
function updateConsentFromPrivacyWire() {
console.log('update consent from privacy wire...');
const privacyWireData = localStorage.getItem('privacywire');
if (privacyWireData) {
try {
const consentData = JSON.parse(privacyWireData);
const consentPreferences = {
// Set Google params based on user preferences
'ad_storage': consentData.cookieGroups.marketing ? 'granted' : 'denied',
'analytics_storage': consentData.cookieGroups.statistics ? 'granted' : 'denied',
'ad_user_data': consentData.cookieGroups.marketing ? 'granted' : 'denied',
'ad_personalization': consentData.cookieGroups.marketing ? 'granted' : 'denied'
};
// Update google consent
gtag('consent', 'update', consentPreferences);
console.log(consentPreferences);
} catch (e) {
console.error('Error parsing PrivacyWire-Data:', e);
}
} else {
console.warn('No PrivacyWire-Data found in localStorage');
}
}
// Update consent at pageload
document.addEventListener('DOMContentLoaded', updateConsentFromPrivacyWire);
5. This is what the parameters control:
ad_storage: Controls whether ad-related cookies and identifiers are stored (e.g., for remarketing).
analytics_storage: Controls whether analytics-related cookies are stored (e.g., for Google Analytics tracking).
ad_user_data: Controls the collection and use of user data for advertising purposes.
ad_personalization: Controls the use of data to personalize ads based on user behavior and preferences.
4. Configure the function name (here updateConsentFromPrivacyWire) in PrivacyWire module settings.