Add support for an optional hook to know when cookies were acknowledged.

This fixes https://github.com/infinum/cookies_eu/issues/90
This commit is contained in:
Jeremy Fleischman 2021-07-27 01:49:16 -07:00 committed by Gabrijel Škoro
parent 3f91806082
commit 82167b3e3d
2 changed files with 12 additions and 0 deletions

View File

@ -101,6 +101,16 @@ If you wish to customize the style of the div the classes are:
If you wish to customize the HTML, create a folder `cookies_eu`, create a partial `_consent_banner.html.erb` and paste the HTML from [here](app/views/cookies_eu/_consent_banner.html.erb)
If you wish to run some arbitrary code when the user acknowledges the cookie message, you can register a callback. This is useful if you wish to persist the user's preference to the database.
```javascript
document.addEventListener('cookies-eu-acknowledged', function() {
// The user has acknowledged cookies! If they're logged in, make an api call
// so we remember their preference.
fetch('...');
});
```
## Cookies used
This gem uses a cookie called **cookie_eu_consented** to track whether a user has accepted the cookie notice and whether it needs to be shown again or not.

View File

@ -29,6 +29,8 @@ var cookiesEu = {
var container = document.querySelector('.js-cookies-eu');
container.parentNode.removeChild(container);
document.dispatchEvent(new CustomEvent('cookies-eu-acknowledged'));
}
};