Add clearcookieconsent link/button option.

This commit is contained in:
Fredrik Jonsson 2023-07-17 10:51:24 +02:00
commit cf0c43760c
2 changed files with 23 additions and 2 deletions

View file

@ -508,9 +508,18 @@ Javascript is used for spam protection and to display error/success messages.
Allow users to opt-in to tracking. Matomo and Google analytic are supported out of the box.
See `assets/js/tracking.js` for example how to implement it.
See `assets/js/tracking.js` for example how to implement it for other cookies.
The name of the cookie set is "cookieconsent" and it can have the values "accept" or "decline" depending on what button the visitor clicks.
The users choice is stored in localStorage item "cookieconsent".
It it recommended to add a link or button to allow users to change their choice. Adding the class "clearcookieconsent" is all that is needed.
```html
<button class="clearcookieconsent">Cookie settings</button>
<a src="" class="clearcookieconsent">Cookie settings</a>
```
When a user clickes the button/link the localStorage item "cookieconsent" is cleared and the cookieconsent dialog is shown again.
![Lighthouse report](https://raw.githubusercontent.com/frjo/hugo-theme-zen/main/images/cookieconsent.png)

View file

@ -29,4 +29,16 @@
});
});
const clear_buttons = document.querySelectorAll('.clearcookieconsent');
clear_buttons.forEach(function (button) {
button.addEventListener('click', function (e) {
e.preventDefault();
if (cookiebanner && cookieconsent) {
localStorage.removeItem('cookieconsent');
cookiebanner.classList.remove('hidden');
cookiebanner.classList.add('js-cookieconsent-open');
}
});
});
})();