Instantly share code, notes, and snippets.
Created
November 24, 2020 13:15
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save virtualize/be856a4a24ef8224584f56e2a807dd5f to your computer and use it in GitHub Desktop.
Cookie consent bar example
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Cookie Consent Test</title> | |
<style> | |
.cookie-bar-wrapper { | |
display: flex; | |
justify-content: center; | |
} | |
.cookie-bar { | |
padding: 1em 15px 0 15px; | |
margin: 15px; | |
position: fixed; | |
background: #FFF; | |
bottom: 0; | |
border: 1px solid grey; | |
display: none; | |
align-items: center; | |
flex-wrap: wrap; | |
z-index: 10000; | |
} | |
.cookie-bar--visible { | |
display: flex; | |
} | |
.cookie-bar__btn-wrapper { | |
padding-bottom: 1em; | |
} | |
.cookie-bar__btn:not(:last-child) { | |
margin-right: 0.25em; | |
} | |
.cookie-bar__text { | |
padding-bottom: 1em; | |
margin-right: 1em; | |
flex: 1 550px; /* grow up to 550px, then break */ | |
} | |
</style> | |
</head> | |
<body> | |
<div class="cookie-bar-wrapper"> | |
<div id="cookie-bar" class="cookie-bar"> | |
<div class="cookie-bar__text"> | |
Wir verwenden Cookies zur anonymen Reichweitenmessung mit Google Analytics. | |
Alle Details findest Du in unserer | |
<a href="#" target="_blank">Datenschutzerklärung</a>. | |
</div> | |
<div class="cookie-bar__btn-wrapper"> | |
<button type="button" class="js-cookie-bar-denied cookie-bar__btn"> | |
Ablehnen | |
</button> | |
<button type="button" class="js-cookie-bar-consent cookie-bar__btn"> | |
Zustimmen | |
</button> | |
</div> | |
</div> | |
</div> | |
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script> | |
<script> | |
var siteModules = siteModules || {}; | |
siteModules.cookieConsent = function ($, window, undefined) { | |
var init; | |
init = function () { | |
var cookieName = '_cookie_consent' | |
$cookieBar = $('#cookie-bar'), | |
$cookieBarConsentBtn = $('.js-cookie-bar-consent'), | |
$cookieBarDeniedBtn = $('.js-cookie-bar-denied'), | |
_cookieStr = `${cookieName}=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/`, | |
_hasCookie = document.cookie.match(RegExp('(?:^|;\\s*)' + cookieName + '=([^;]*)')); | |
if (_hasCookie === null) { | |
$cookieBar.addClass('cookie-bar--visible'); | |
} | |
$cookieBarConsentBtn.on('click', function (e) { | |
e.preventDefault(); | |
document.cookie = _cookieStr; | |
$cookieBar.removeClass('cookie-bar--visible'); | |
}); | |
$cookieBarDeniedBtn.on('click', function (e) { | |
e.preventDefault(); | |
gaOptOut(); | |
document.cookie = _cookieStr; | |
$cookieBar.removeClass('cookie-bar--visible'); | |
}); | |
}; | |
return { | |
init: init | |
}; | |
}(jQuery, window); | |
// run cookie script | |
siteModules.cookieConsent.init(); | |
</script> | |
<!-- Start Google Analytics including Opt-Out Cookie --> | |
<script type="text/javascript"> | |
var gaProperty = '<insert-your-google-analytics-id-here>'; | |
var disableStr = 'ga-disable-' + gaProperty; | |
if (document.cookie.indexOf(disableStr + '=true') > -1) { | |
window[disableStr] = true; | |
} | |
function gaOptOut() { | |
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/'; | |
window[disableStr] = true; | |
} | |
(function (i, s, o, g, r, a, m) { | |
i['GoogleAnalyticsObject'] = r; | |
i[r] = i[r] || function () { | |
(i[r].q = i[r].q || []).push(arguments) | |
}, i[r].l = 1 * new Date(); | |
a = s.createElement(o), | |
m = s.getElementsByTagName(o)[0]; | |
a.async = 1; | |
a.src = g; | |
m.parentNode.insertBefore(a, m) | |
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga'); | |
ga('create', gaProperty, 'auto'); | |
ga('set', 'anonymizeIp', true); | |
ga('send', 'pageview'); | |
</script> | |
<!-- End Google Analytics --> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment