Skip to content

Commit

Permalink
Support Google Consent Mode v2 (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBernskiold authored May 30, 2024
1 parent 61ad384 commit c4ec9a6
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
25 changes: 25 additions & 0 deletions assets/scripts/src/consent-mode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {getConsentedCategories} from "./consent";

export function isConsentModeEnabled() {
return ilcc.consentModeEnabled === '1';
}

function hasGtagOnPage() {
return typeof gtag === 'function';
}

export function grantConsent() {
if (!isConsentModeEnabled() || !hasGtagOnPage()) {
return;
}

const consentedCategories = getConsentedCategories();
const hasMarketingConsent = consentedCategories.includes('marketing');

gtag('consent', 'update', {
ad_user_data: hasMarketingConsent ? 'granted' : 'denied',
ad_personalization: hasMarketingConsent ? 'granted' : 'denied',
ad_storage: hasMarketingConsent ? 'granted' : 'denied',
analytics_storage: hasMarketingConsent ? 'granted' : 'denied'
});
}
6 changes: 6 additions & 0 deletions assets/scripts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { hasConsentedTo, hasUserSetPreferences, setConsentedCategories, setHasSe
import { getJsonCookieValue } from "./cookies";
import { log, logDebug, logInfo } from "./log";
import { getBannerStyle, hasMatomo, isConfigurable, isDebugging, settings } from "./settings";
import { isConsentModeEnabled, grantConsent } from "./consent-mode";

log( "=========== COOKIE CONSENT DEBUGGING ===========" );

Expand All @@ -27,6 +28,10 @@ if ( hasUserSetPreferences() ) {
_paq.push( [ "setCookieConsentGiven" ] );
}

if(isConsentModeEnabled() && hasConsentedTo('marketing')) {
grantConsent();
}

} else {
logDebug( "❌ User has not expressed consent." );
document.body.classList.add( "has-ilcc-banner" );
Expand All @@ -51,6 +56,7 @@ if ( document.querySelector( ".js--ilcc-cookie-consent-notice" ) ) {
"marketing",
"analytics"
] );
grantConsent();
} );

if ( isConfigurable() ) {
Expand Down
26 changes: 26 additions & 0 deletions classes/class-consent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

class ILCC_Consent {

public static function hooks() {
add_action('wp_enqueue_scripts', [self::class, 'add_consent_mode_defaults']);
}

/**
* Check if preferences have been set.
*
Expand Down Expand Up @@ -56,4 +60,26 @@ protected static function get_consented_list() {
return [];
}

public static function add_consent_mode_defaults() {
if(!ILCC_Settings::is_consent_mode_integration_enabled()) {
return;
}

if(false === apply_filters('ilcc_enable_default_consent_mode', false)) {
return '';
}

return <<<JS
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'
});
JS;
}

}
30 changes: 30 additions & 0 deletions classes/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,19 @@ public static function is_marketing_shown() {
return apply_filters( 'ilcc_settings_marketing_is_shown', $shown );
}

/**
* Check whether the Google Consent Mode integration is enabled.
*
* @return bool
*/
public static function is_consent_mode_integration_enabled() {
$enabled = get_option( 'ilcc_settings_google_consent_mode_enabled' );
$enabled = $enabled === '1' || $enabled === true || $enabled === 'true';


return apply_filters( 'ilcc_settings_google_consent_mode_enabled', $enabled );
}

/**
* Add settings in the customer.
*
Expand Down Expand Up @@ -592,6 +605,23 @@ public static function customizer( $wp_customize ) {
'type' => 'textarea',
] ) );

/**
* Consent Mode Integration
*/
$wp_customize->add_setting( 'ilcc_settings_google_consent_mode_enabled', [
'type' => 'option',
'capability' => apply_filters( 'ilcc_edit_button_capability', 'edit_theme_options' ),
] );

$wp_customize->add_control( new \WP_Customize_Control( $wp_customize, 'ilcc_settings_google_consent_mode_enabled', [
'type' => 'checkbox',
'label' => __( 'Enable Consent Mode Integration', 'ilmenite-cookie-consent' ),
'description' => __( 'When checked the consent given by the user will integrate with Google Consent Mode v2.', 'ilmenite-cookie-consent' ),
'settings' => 'ilcc_settings_google_consent_mode_enabled',
'section' => 'ilmenite_cookie_banner_marketing',
'priority' => 80,
] ) );

/**
* Save Settings Button Text
*/
Expand Down
7 changes: 7 additions & 0 deletions ilmenite-cookie-consent.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public function init_hooks() {

// Boot other classes.
ILCC_Settings::hooks();
ILCC_Consent::hooks();

do_action( 'ilcc_init' );
}
Expand Down Expand Up @@ -190,6 +191,7 @@ public function scripts() {
'saveSettingsText' => ILCC_Settings::get_save_settings_button_title(),
'settingsTitle' => ILCC_Settings::get_settings_title(),
'settingsDescription' => ILCC_Settings::get_settings_description(),
'consentModeEnabled' => ILCC_Settings::is_consent_mode_integration_enabled() ? '1' : '0',
'debug' => $this->is_debugging(),
] );

Expand All @@ -198,6 +200,11 @@ public function scripts() {
*/
wp_add_inline_script( 'ilcc-vendor', $this->get_allow_and_disallowlists(), 'before' );

/**
* Add consent mode defaults.
*/
wp_add_inline_script('ilmenite-cookie-consent', ILCC_Consent::add_consent_mode_defaults(), 'before');

// Finally, enqueue!
wp_enqueue_script( 'ilcc-vendor' );
wp_enqueue_script( 'ilmenite-cookie-consent' );
Expand Down
7 changes: 6 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Contributors: Erik Bernskiold, annlickander, bernskioldmedia
Tags: cookies, cookie notice, eu cookie law, cookie compliance, cookie banner, cookie consent, gdpr, privacy, tracker prevention, tracker
Requires at least: 5.0
Tested up to: 6.4
Tested up to: 6.5
Requires PHP: 7.2
Stable tag: trunk
License: GPLv2 or later
Expand Down Expand Up @@ -146,6 +146,8 @@ Just set their value somewhere in your code, such as in the functions.php file o

`ilcc_is_active_on_page` - Return false to hide the banner from loading. Can be used to prevent the banner from loading on certain pages or templates.

`ilcc_enable_default_consent_mode?` - Return false to disable the default consent mode. This is useful if you want to provide your own default configuration.

== Screenshots ==

1. The "top" style design of the cookie consent box out of the box.
Expand All @@ -154,6 +156,9 @@ Just set their value somewhere in your code, such as in the functions.php file o

== Changelog ==

= Version 3.4.0 =
- Add Google Consent Mode v2 support

= Version 3.3.0 =
- Update NO and DK translation
- Added greek translations (thanks Michail)
Expand Down

0 comments on commit c4ec9a6

Please sign in to comment.