diff --git a/README.md b/README.md index 8a7a2de..8e34d8c 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/app/assets/javascripts/cookies_eu.js b/app/assets/javascripts/cookies_eu.js index 5694280..f959caf 100644 --- a/app/assets/javascripts/cookies_eu.js +++ b/app/assets/javascripts/cookies_eu.js @@ -29,6 +29,8 @@ var cookiesEu = { var container = document.querySelector('.js-cookies-eu'); container.parentNode.removeChild(container); + + document.dispatchEvent(new CustomEvent('cookies-eu-acknowledged')); } };