Skip to main content

Cookie banner

By default, the Cookie Manager library is configured to display or hide a cookie banner setup within your service. Without any extra configuration, the library is configured out-of-the-box to work with the template listed below.

In short, the library works by binding event listeners to the button defined within each action, with these actions being specified within the config.

Each action defines:

  • How consent should be affected
  • What confirmation message should be shown

An example action of an action and how it works:

{
    name: 'accept',
    buttonClass: 'cookie-banner-accept-button',
    confirmationClass: 'cookie-banner-accept-message',
    consent: true
}

This action specifies that upon an element with the class cookie-banner-accept-button (within the cookie banner) being clicked:

  1. We consent to all optional cookie categories.
  2. Any elements with the class cookie-banner-accept-message (within the cookie banner) should be shown.
  3. A CookieBannerAction event is then emitted with the activated action’s name (in this case, ‘accept’) being passed to any listening callbacks.

More information about the cookie banner’s functionality and how it can be configured to support a variety of different action/message (stage-based) cookie banners can be found within the cookie banner configuration options.

HTML / Nunjucks template

Here is an example of a cookie banner (utilising the GOV.UK Frontend ‘Cookie Banner’ component). Code has been provided for both HTML and Nunjucks. See the GOV.UK Frontend Design System for more about this component


Warning It is important to ensure the cookie banner is has the `hidden` attribute when the DOM / page is first loaded. The Cookie Manager library will un-hide the banner when appropriate.

Default configuration

The default config for the cookie banner is:

{
  cookieBanner: {
    class: 'cookie-banner',
    actions: [
      {
          name: 'accept',
          buttonClass: 'cookie-banner-accept-button',
          confirmationClass: 'cookie-banner-accept-message',
          consent: true
      },
      {
          name: 'reject',
          buttonClass: 'cookie-banner-reject-button',
          confirmationClass: 'cookie-banner-reject-message',
          consent: false
      },
      {
          name: 'hide',
          buttonClass: 'cookie-banner-hide-button'
      }
    ]
  }
}

You can see the classes in this config used on the messages and buttons on the template above.

Using this default config with the above template should set up a cookie banner that works out of the box with the cookie-manager library,


Note: To disable the cookie banner functionality, set the value of configuration property disableCookieBanner within config category additionalOptions to true when initializing the library.