Skip to main content

Configuring and initializing the library

Configuration

To configure the cookie-manager for your project, it needs to be supplied a config file on initialization. Below is an example configuration:

{
  userPreferences: {
    cookieName: 'service-name-cookie-preferences',
  },
  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',
          }
      ]
  },
  preferencesForm: {
    class:'cookie-preferences-form'
  },
  cookieManifest: [
    {
      categoryName: 'essential',
      optional: false,
      matchBy: 'exact',
      cookies: [
        'essential-cookie-one'
      ]
    },
    {
      categoryName: 'analytics',
      optional: true,
      cookies: [
        'analytics-cookie-one',
        'analytics-cookie-two',
      ]
    }
  ]
}

The configuration is made up of 4 main elements. These are

  • userPreferences - This section is dedicated to the configuration of the user preferences cookie (used by cookie-manager).
  • cookieBanner - This section is for configuring how the cookie-manager library handles a cookie banner element.
  • preferencesForm - This section is for configuring how the cookie-manager library handles a form dedicated to cookie consent preferences.
  • cookieManifest - This section is used for listing the cookies expected to be encountered on the service, it is broken up into categories where the cookies listed are specified as essential or non-essential.
  • additionalOptions - This section is used for non-recommended (or dangerous) configuration options, it is rarely needed.

More about the configuring the library can be found within the configuration options section.

Warning Cookie Manager will attempt to validate the config passed to it on initialization. If any malformed, missing properties etc. are supplied, the library will be disabled until it is resolved. A warning will be printed in the console alerting of the problematic property.

Initializing

         
import cookieManager from '@hmcts/cookie-manager';

const config = { ... }

cookieManager.init(config);


init (config)

Initializes the @hmcts-cookie/manager library using the provided config.

  • param CookieManagerConfig – config – Configuration object for the library to use., see configuration options