Skip to content

Instantly share code, notes, and snippets.

@pingpoli
Last active May 23, 2024 08:19
Show Gist options
  • Save pingpoli/789f9c0e7226d5ffb10e6faf112a70d1 to your computer and use it in GitHub Desktop.
Save pingpoli/789f9c0e7226d5ffb10e6faf112a70d1 to your computer and use it in GitHub Desktop.
// by default, personalized ads are turned off
var CCM_showPersonalizedAds = false;
// increase this number when your cookie policy has changed, this will show the popup to all users again
var CCM_versionNumber = 0;
var CCM_popup;
// helper function to get the value of a cookie
function CCM_getCookie( name )
{
var name = name+"=";
var decodedCookie = decodeURIComponent( document.cookie );
var cookies = decodedCookie.split( ';' );
for( var i = 0 ; i < cookies.length ; ++i )
{
var c = cookies[i];
while ( c.charAt(0) == ' ' )
{
c = c.substring(1);
}
if ( c.indexOf(name) == 0 )
{
return c.substring( name.length , c.length );
}
}
return "";
}
function CCM_getCookieConsent( forceShow )
{
var consent = CCM_getCookie( "ccm_accepted_v"+CCM_versionNumber );
if ( consent != "yes" || forceShow )
{
// if the consent form hasn't been shown to the user, show it
CCM_showPopup();
}
else
{
var personalizedAds = CCM_getCookie( "ccm_personalizedAds" );
if ( personalizedAds == "yes" ) CCM_showPersonalizedAds = true;
// renew the cookies so frequent users don't get the popup all the time
CCM_setCookies( CCM_showPersonalizedAds );
}
}
function CCM_showPopup()
{
CCM_popup = document.createElement( "div" );
CCM_popup.id = "CCM_popup";
CCM_popup.style.cssText = "position:fixed;width:50%;top:10%;left:25%;padding:10px;background-color:#f8f8f8;border:1px solid #000000;z-index:9999;";
CCM_popup.innerHTML = `
<h3 style="font-family:sans-serif;text-align:center;">Cookie Policy</h3>
Your text explaining which data and cookies you and your third party tools collect.<br><br>
<label><input type="checkbox" id="requiredCookies" checked disabled><span>Required</span></label><br><br>
<label><input type="checkbox" id="personalizedAds"><span>Personalized Google Ads</span></label><br><br>
<button class="button" onclick="CCM_RejectAll()" style="width:100px;height:20px;padding:0px;font-size:10pt;">Reject All</button>&emsp;<button onclick="CCM_AcceptAll()" class="button" style="width:100px;height:20px;padding:0px;font-size:10pt;">Accept All</button><br><br>
By hitting the <i>accept</i> button, you consent to the use of these methods by us and third parties. You can always change your preferences by visiting our Cookie Policy.<br><br>
<span style="display:inline-block;width:100%;text-align:center;"><button onclick="CCM_confirmSelection()" class="button">Accept</button></span>
`;
document.body.appendChild( CCM_popup );
// check if a personalized ad cookie exists and if so use its value
var personalizedAds = CCM_getCookie( "ccm_personalizedAds" );
if ( personalizedAds == "yes" ) document.getElementById("personalizedAds").checked = true;
else document.getElementById("personalizedAds").checked = false;
}
function CCM_RejectAll()
{
document.getElementById("personalizedAds").checked = false;
}
function CCM_AcceptAll()
{
document.getElementById("personalizedAds").checked = true;
}
function CCM_confirmSelection()
{
var personalizedAds = document.getElementById("personalizedAds").checked;
CCM_setCookies( personalizedAds );
// remove the popup
CCM_popup.parentElement.removeChild( CCM_popup );
}
function CCM_setCookies( personalizedAds )
{
if ( personalizedAds )
{
var expires = new Date();
expires.setMonth( expires.getMonth() + 1 );
document.cookie = "ccm_personalizedAds=yes; path=/; expires="+expires.toGMTString()+"; Secure";
CCM_showPersonalizedAds = true;
}
else
{
var expires = new Date();
expires.setMonth( expires.getMonth() + 1 );
document.cookie = "ccm_personalizedAds=no; path=/; expires="+expires.toGMTString()+"; Secure";
CCM_showPersonalizedAds = false;
}
// save that the popup has been seen and accepted
var expires = new Date();
expires.setMonth( expires.getMonth() + 1 );
document.cookie = "ccm_accepted_v"+CCM_versionNumber+"=yes; path=/; expires="+expires.toGMTString()+"; Secure";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment