Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/frjo/hugo-theme-zen.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFredrik Jonsson <frjo@xdeb.org>2021-12-13 12:50:23 +0300
committerFredrik Jonsson <frjo@xdeb.org>2021-12-13 12:50:23 +0300
commit767119d014b08b92ca57f4d048a1cea3da1c5229 (patch)
tree8361cfe38483f082251413548e989eae39633bcc
parentfec3d75d9ca5c94d2062fdfd6ee1f304e01a8af4 (diff)
Use localstorage for cookiconsent response instead of setting a cookie.
-rw-r--r--assets/js/cookieconsent.js50
-rw-r--r--assets/js/tracking.js34
-rw-r--r--layouts/partials/scripts.html2
3 files changed, 37 insertions, 49 deletions
diff --git a/assets/js/cookieconsent.js b/assets/js/cookieconsent.js
index deed6c5..326593c 100644
--- a/assets/js/cookieconsent.js
+++ b/assets/js/cookieconsent.js
@@ -3,39 +3,29 @@
'use strict';
- if (typeof Cookies !== 'undefined') {
- const cookieconsent = document.querySelector('.cookieconsent');
+ const cookiebanner = document.querySelector('.cookieconsent');
+ const cookieconsent = localStorage.getItem('cookieconsent');
- if (typeof Cookies.get('cookieconsent') === 'undefined' && cookieconsent) {
- cookieconsent.classList.remove('hidden');
- cookieconsent.classList.add('js-cookieconsent-open');
- }
+ if (cookiebanner && !cookieconsent) {
+ cookiebanner.classList.remove('hidden');
+ 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;
- }
+ 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') {
- Cookies.set('cookieconsent', 'accept', cookie_options);
- }
- else {
- Cookies.set('cookieconsent', 'decline', cookie_options);
- }
- cookieconsent.classList.remove('js-cookieconsent-open');
- cookieconsent.classList.add('hidden');
- });
+ 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');
});
- }
+ });
})();
diff --git a/assets/js/tracking.js b/assets/js/tracking.js
index 75f6807..e28209c 100644
--- a/assets/js/tracking.js
+++ b/assets/js/tracking.js
@@ -5,26 +5,26 @@
* 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 matomoTrackingApiUrl = 'https://{{ .Site.Params.piwikTrackerUrl }}/matomo.php';
+var googleAnalytics = '{{ .Site.GoogleAnalytics }}';
-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']);
+if (idSite) {
+ 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']);
+ }
}
-var googleAnalytics = '{{ .Site.GoogleAnalytics }}';
-if (cookieConsent) {
- if (typeof Cookies === 'undefined' || Cookies.get('cookieconsent') !== 'accept') {
- window['ga-disable-' + googleAnalytics] = true;
- }
+if (googleAnalytics && cookiebanner && cookieconsent === 'decline') {
+ window['ga-disable-' + googleAnalytics] = true;
}
diff --git a/layouts/partials/scripts.html b/layouts/partials/scripts.html
index 50834e0..c578138 100644
--- a/layouts/partials/scripts.html
+++ b/layouts/partials/scripts.html
@@ -11,8 +11,6 @@
<script defer src="{{ $mobilemenu.RelPermalink }}"></script>
{{ end -}}
{{ 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 -}}
<script defer src="{{ $cookieconsent.RelPermalink }}"></script>
{{ end -}}