Last active
March 11, 2024 07:40
-
-
Save webtoffee-git/9d7992d6afb329f4700a6984636695c2 to your computer and use it in GitHub Desktop.
Code to make the plugin compatible with Google Consent Mode V2 - By WebToffee (GDPR Cookie Consent)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //Do not copy this line of code | |
add_action('wp_head', 'wt_cli_consent_mode_integration', 9); | |
function wt_cli_consent_mode_integration() { | |
if (!class_exists('Cookie_Law_Info')) { | |
return; | |
} | |
$cli_bypass = isset($_GET['cli_bypass']) ? 1 : 0; | |
$cli_advertisement_cookie = isset($_COOKIE['cookielawinfo-checkbox-advertisement']) ? $_COOKIE['cookielawinfo-checkbox-advertisement'] : 'no'; | |
$cli_functional_cookie = isset($_COOKIE['cookielawinfo-checkbox-functional']) ? $_COOKIE['cookielawinfo-checkbox-functional'] : 'no'; | |
$cli_analytics_cookie = isset($_COOKIE['cookielawinfo-checkbox-analytics']) ? $_COOKIE['cookielawinfo-checkbox-analytics'] : 'no'; | |
?> | |
<script> | |
var redact_ad_data = false; | |
// set "true" to increase the fidelity of advertising storage restrictions | |
var wt_cli_advertisement_cookie_val = '<?php echo $cli_advertisement_cookie; ?>'; | |
var wt_cli_ad_storage = (wt_cli_advertisement_cookie_val === 'yes') ? 'granted' : 'denied'; | |
//set default value "denied" or "granted" for advertisement cookies storage | |
var wt_cli_analytics_cookie_val = '<?php echo $cli_analytics_cookie; ?>'; | |
var wt_cli_analytics_storage = (wt_cli_analytics_cookie_val === 'yes') ? 'granted' : 'denied'; | |
//set default value "denied" or "granted" for analytics cookies storage | |
var wt_cli_functional_cookie_val = '<?php echo $cli_functional_cookie; ?>'; | |
var wt_cli_functionality_storage = (wt_cli_functional_cookie_val === 'yes') ? 'granted' : 'denied'; | |
//set default value "denied" or "granted" for functionality cookies storage | |
var wt_cli_waiting_period = 2000; | |
//set default waiting time as 2000 | |
var wt_cli_bypass = <?php echo $cli_bypass; ?>; | |
// for cookie scan support | |
if( wt_cli_bypass ) { | |
wt_cli_ad_storage = wt_cli_analytics_storage = wt_cli_functionality_storage ='granted'; | |
wt_cli_waiting_period = 100; | |
} | |
window.dataLayer = window.dataLayer || []; | |
function gtag() { | |
dataLayer.push(arguments); | |
} | |
// Set default consent options | |
gtag("consent", "default", { | |
ad_storage: wt_cli_ad_storage, | |
ad_user_data: wt_cli_ad_storage, | |
ad_personalization: wt_cli_ad_storage, | |
analytics_storage: wt_cli_analytics_storage, | |
functionality_storage: wt_cli_functionality_storage, | |
personalization_storage: wt_cli_functionality_storage, | |
security_storage: "granted", | |
wait_for_update: wt_cli_waiting_period, | |
}); | |
gtag("set", "ads_data_redaction", true); | |
gtag("set", "url_passthrough", true); | |
// Function to update consent based on user acceptance | |
function CookieLawInfo_Accept_Callback() { | |
wt_cli_ad_storage = 'denied'; | |
wt_cli_analytics_storage = 'denied'; | |
wt_cli_functionality_storage = 'denied'; | |
if (CLI.consent['advertisement'] == true) { | |
wt_cli_ad_storage = 'granted'; | |
} | |
if (CLI.consent['analytics'] == true) { | |
wt_cli_analytics_storage = 'granted'; | |
} | |
if (CLI.consent['functional'] == true) { | |
wt_cli_functionality_storage = 'granted'; | |
} | |
gtag('consent', 'update', { | |
'ad_storage': wt_cli_ad_storage, | |
'ad_user_data': wt_cli_ad_storage, | |
'ad_personalization': wt_cli_ad_storage, | |
'analytics_storage': wt_cli_analytics_storage, | |
'functionality_storage': wt_cli_functionality_storage, | |
'personalization_storage': wt_cli_functionality_storage, | |
}); | |
set_ads_data_redaction(); | |
} | |
// Function to update consent on reject all works with plugin version 2.5.7 or greater | |
function CookieLawInfo_Reject_Callback() { | |
// Set all consent as denied | |
gtag("consent", "update", { | |
ad_storage: "denied", | |
ad_user_data: "denied", | |
ad_personalization: "denied", | |
analytics_storage: "denied", | |
functionality_storage: "denied", | |
personalization_storage: "denied", | |
security_storage: "granted", | |
}); | |
set_ads_data_redaction(); | |
} | |
function set_ads_data_redaction() { | |
if (redact_ad_data && wt_cli_ad_storage == 'denied') { | |
gtag('set', 'ads_data_redaction', true); | |
} | |
} | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment