Skip to content

Commit

Permalink
Add support for an optional hook to know when cookies were acknowledged.
Browse files Browse the repository at this point in the history
This fixes #90
  • Loading branch information
jfly authored and gabskoro committed Jul 31, 2021
1 parent 3f91806 commit 82167b3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions app/assets/javascripts/cookies_eu.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var cookiesEu = {

var container = document.querySelector('.js-cookies-eu');
container.parentNode.removeChild(container);

document.dispatchEvent(new CustomEvent('cookies-eu-acknowledged'));
}
};

Expand Down

0 comments on commit 82167b3

Please sign in to comment.