Use localstorage for cookiconsent response instead of setting a cookie.
This commit is contained in:
parent
fec3d75d9c
commit
767119d014
3 changed files with 37 additions and 49 deletions
|
|
@ -3,39 +3,29 @@
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
if (typeof Cookies !== 'undefined') {
|
const cookiebanner = document.querySelector('.cookieconsent');
|
||||||
const cookieconsent = document.querySelector('.cookieconsent');
|
const cookieconsent = localStorage.getItem('cookieconsent');
|
||||||
|
|
||||||
if (typeof Cookies.get('cookieconsent') === 'undefined' && cookieconsent) {
|
if (cookiebanner && !cookieconsent) {
|
||||||
cookieconsent.classList.remove('hidden');
|
cookiebanner.classList.remove('hidden');
|
||||||
cookieconsent.classList.add('js-cookieconsent-open');
|
cookiebanner.classList.add('js-cookieconsent-open');
|
||||||
}
|
|
||||||
|
|
||||||
const cookie_buttons = Array.prototype.slice.call(
|
|
||||||
document.querySelectorAll('button[data-consent]')
|
|
||||||
);
|
|
||||||
const sitedomain = window.location.hostname.split('.').slice(-2);
|
|
||||||
const cookiedomain = sitedomain.join('.');
|
|
||||||
let cookie_options = [];
|
|
||||||
cookie_options['domain'] = cookiedomain;
|
|
||||||
cookie_options['sameSite'] = 'strict';
|
|
||||||
cookie_options['expires'] = 365;
|
|
||||||
if (window.location.protocol === 'https:') {
|
|
||||||
cookie_options['secure'] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
cookie_buttons.forEach(function (button) {
|
|
||||||
button.addEventListener('click', function () {
|
|
||||||
if (button.getAttribute('data-consent') === 'true') {
|
|
||||||
Cookies.set('cookieconsent', 'accept', cookie_options);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Cookies.set('cookieconsent', 'decline', cookie_options);
|
|
||||||
}
|
|
||||||
cookieconsent.classList.remove('js-cookieconsent-open');
|
|
||||||
cookieconsent.classList.add('hidden');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cookie_buttons = Array.prototype.slice.call(
|
||||||
|
document.querySelectorAll('button[data-consent]')
|
||||||
|
);
|
||||||
|
|
||||||
|
cookie_buttons.forEach(function (button) {
|
||||||
|
button.addEventListener('click', function () {
|
||||||
|
if (button.getAttribute('data-consent') === 'true') {
|
||||||
|
localStorage.setItem('cookieconsent', 'accept');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
localStorage.setItem('cookieconsent', 'decline');
|
||||||
|
}
|
||||||
|
cookiebanner.classList.remove('js-cookieconsent-open');
|
||||||
|
cookiebanner.classList.add('hidden');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
|
|
@ -5,26 +5,26 @@
|
||||||
* A JavaScript file for analytic tracking.
|
* A JavaScript file for analytic tracking.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var cookieConsent = {{ .Site.Params.cookieConsent | default false }};
|
var cookiebanner = {{ .Site.Params.cookieConsent | default false }};
|
||||||
|
var cookieconsent = localStorage.getItem('cookieconsent');
|
||||||
var idSite = '{{ .Site.Params.piwikSiteID }}';
|
var idSite = '{{ .Site.Params.piwikSiteID }}';
|
||||||
var matomoTrackingApiUrl = 'https://{{ .Site.Params.piwikTrackerUrl }}/matomo.php';
|
var matomoTrackingApiUrl = 'https://{{ .Site.Params.piwikTrackerUrl }}/matomo.php';
|
||||||
|
|
||||||
var _paq = window._paq || [];
|
|
||||||
if (cookieConsent) {
|
|
||||||
_paq.push(['requireConsent']);
|
|
||||||
}
|
|
||||||
_paq.push(['setTrackerUrl', matomoTrackingApiUrl]);
|
|
||||||
_paq.push(['setSiteId', idSite]);
|
|
||||||
_paq.push(['trackPageView']);
|
|
||||||
_paq.push(['enableLinkTracking']);
|
|
||||||
if (typeof Cookies !== 'undefined' && Cookies.get('cookieconsent') === 'accept') {
|
|
||||||
_paq.push(['setConsentGiven']);
|
|
||||||
}
|
|
||||||
|
|
||||||
var googleAnalytics = '{{ .Site.GoogleAnalytics }}';
|
var googleAnalytics = '{{ .Site.GoogleAnalytics }}';
|
||||||
if (cookieConsent) {
|
|
||||||
if (typeof Cookies === 'undefined' || Cookies.get('cookieconsent') !== 'accept') {
|
if (idSite) {
|
||||||
window['ga-disable-' + googleAnalytics] = true;
|
var _paq = window._paq || [];
|
||||||
|
if (cookiebanner) {
|
||||||
|
_paq.push(['requireConsent']);
|
||||||
|
}
|
||||||
|
_paq.push(['setTrackerUrl', matomoTrackingApiUrl]);
|
||||||
|
_paq.push(['setSiteId', idSite]);
|
||||||
|
_paq.push(['trackPageView']);
|
||||||
|
_paq.push(['enableLinkTracking']);
|
||||||
|
if (cookiebanner && cookieconsent === 'accept') {
|
||||||
|
_paq.push(['setConsentGiven']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (googleAnalytics && cookiebanner && cookieconsent === 'decline') {
|
||||||
|
window['ga-disable-' + googleAnalytics] = true;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,6 @@
|
||||||
<script defer src="{{ $mobilemenu.RelPermalink }}"></script>
|
<script defer src="{{ $mobilemenu.RelPermalink }}"></script>
|
||||||
{{ end -}}
|
{{ end -}}
|
||||||
{{ if $.Param "cookieconsent" -}}
|
{{ if $.Param "cookieconsent" -}}
|
||||||
{{ $jscookie := resources.Get "js/lib/js.cookie.min.js" | fingerprint -}}
|
|
||||||
<script src="{{ $jscookie.RelPermalink }}"></script>
|
|
||||||
{{ $cookieconsent := resources.Get "js/cookieconsent.js" | minify | fingerprint -}}
|
{{ $cookieconsent := resources.Get "js/cookieconsent.js" | minify | fingerprint -}}
|
||||||
<script defer src="{{ $cookieconsent.RelPermalink }}"></script>
|
<script defer src="{{ $cookieconsent.RelPermalink }}"></script>
|
||||||
{{ end -}}
|
{{ end -}}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue