Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Jany-M
Last active May 21, 2021 13:55
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jany-M/4b7ecff166db07582dd44ee58cfe35ea to your computer and use it in GitHub Desktop.
Save Jany-M/4b7ecff166db07582dd44ee58cfe35ea to your computer and use it in GitHub Desktop.
[GDPR] Scripts to ask / check for user consent (WordPress and JS examples)
<?php
/* --------------------------------------------------------------------------------
*
* JS stand-alone (CSS not included - Get it here https://pastebin.com/uNKPdMVj)
*
-------------------------------------------------------------------------------- */
?>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<!-- The google ad script needs to run BEFORE the ads are shown, so we can set all the necessary stuff, that's why it all goes in the <head> -->
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script type='text/javascript'>
// Pause Google Ads and anonymize until we know the consent status
(adsbygoogle = window.adsbygoogle || []).pauseAdRequests = 1;
(adsbygoogle = window.adsbygoogle || []).requestNonPersonalizedAds = 1;
// Check if we have a preference cookie
var CONSENT_COOKIE = "consent_cookie";
if ( getCookie(CONSENT_COOKIE) ) {
// Check if we have consent AND browser is not sending a DoNotTrack request
var google_ads_personalized_consent = (document.cookie.indexOf('consent_cookie=1') >= 0 && (navigator.doNotTrack != 'unspecified' && navigator.doNotTrack != '1'));
// Convert bool to integer
var non_personalized_ads = !google_ads_personalized_consent ? 1 : 0;
// If we have both consents set the method = 0 otherwise = 1
(adsbygoogle = window.adsbygoogle || []).requestNonPersonalizedAds = non_personalized_ads;
// Resume Google Ads
(adsbygoogle = window.adsbygoogle || []).pauseAdRequests = 0;
}
jQuery(function($) {
"use strict";
$(document).ready(function(){
// Cookie Banner with consent
$( 'body' ).prepend(
'<div class="cookie-banner">Cookies help us to deliver our services. By using these services, you agree to the use of cookies. <a href="#">Learn More</a><div class="accettazione"><p>I accept the Cookies</p><label class="switch"><input type="checkbox" class="checkbox_check" checked><span class="slider round"></label></span><button class="myButton" id="accept-cookie">Save Preference</button></div></div><h1 id="scroll-to"></h1>'
);
$('.cookie-banner').hide();
if (!getCookie(CONSENT_COOKIE) ) {
// CONSENT NOT SET
// Cookie Banner with consent
$('.cookie-pop').show();
// Pause Google Ads
(adsbygoogle = window.adsbygoogle || []).pauseAdRequests = 1;
} else {
// CONSENT SET
$( 'body' ).prepend(
'<div class="manage-consent"><a href="#" id="consenso">Manage Consent</a></div>'
);
// Consenso salvando preferenze
$( '#consenso' ).click(function () {
$('.manage-consent').hide();
$('.cookie-banner').show();
});
// If preference was No, then uncheck toggle
if(getCookie(CONSENT_COOKIE) == 0) {
$('input.checkbox_check').prop('checked', false);
}
}
// Save Preference
$( '#accept-cookie' ).click(function () {
// Cookie Accepted
if ($('input.checkbox_check').is(':checked')) {
setCookie(CONSENT_COOKIE, "1", 30);
(adsbygoogle=window.adsbygoogle || []).requestNonPersonalizedAds = 0;
(adsbygoogle=window.adsbygoogle || []).pauseAdRequests = 0;
window.location.reload();
} else {
// Cookie Rejected
setCookie(CONSENT_COOKIE, "0", 30);
(adsbygoogle=window.adsbygoogle||[]).requestNonPersonalizedAds = 1;
(adsbygoogle=window.adsbygoogle||[]).pauseAdRequests = 0;
window.location.reload();
}
});
});
});
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
</script>
<?php
/* --------------------------------------------------------------------------------
*
* Script that works with a WordPress plugin called GDPR
* https://wordpress.org/plugins/gdpr/
*
* All of this should be in the head, because it MUST run before the actual ads in the page, specifically before <script>(adsbygoogle = window.adsbygoogle || []).push({});</script>)
*
* I haven't tested this fully, but from preliminary tests, it seems to work fine. Suggestions are welcome!
*
-------------------------------------------------------------------------------- */
if(function_exists('is_allowed_cookie')) {
$cookie_bool = is_allowed_cookie('IDE') ? 'true' : 'false'; // name of cookie that Google Ads uses
?>
<!-- The google ad script needs to run BEFORE the ads are shown, so we can set all the necessary stuff, that's why it all goes in the <head> -->
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script type='text/javascript'>
// Pause Google Ads and anonymize until we know the consent status
(adsbygoogle = window.adsbygoogle || []).pauseAdRequests = 1;
(adsbygoogle = window.adsbygoogle || []).requestNonPersonalizedAds = 1;
// Check consent
var hascookie = <?php echo $cookie_bool; ?>;
if(typeof hascookie === 'boolean' && hascookie === true) {
hasconsent = 1;
} else {
hasconsent = 0;
}
// Check if we have consent AND browser is not sending a DoNotTrack request
var google_ads_personalized_consent = (hascookie >= 0 && (navigator.doNotTrack != 'unspecified' && navigator.doNotTrack != '1'));
// Convert bool to integer
var personalized_ads = !google_ads_personalized_consent ? 1 : 0;
// If we have both consents set the method = 0 otherwise = 1
(adsbygoogle = window.adsbygoogle || []).requestNonPersonalizedAds = personalized_ads;
// Resume Google Ads
(adsbygoogle = window.adsbygoogle || []).pauseAdRequests = 0;
</script>
<?php }
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment