Giter Club home page Giter Club logo

govuk-cookie-consent's Introduction

cookie-consent

A solution for managing cookies on GOV.UK services. Based on NHS Cookie Consent.

Quickstart

npm install
npm start

Go to http://localhost:8080/tests/example/ for an example site using the cookie javascript.

Usage

Include the cookie javascript in your page

<script src="/path/to/javascript.js" type="text/javascript"></script>

Any scripts that use cookies must be given a type="text/plain" attribute to stop the javascript from running, and a data-cookieconsent attribute so that cookie-consent knows which scripts to enable based on the user's consent settings.

Possible data-cookieconsent values are preferences, statistics and marketing.

<script
  src="/path/to/js-that-uses-cookies.js"
  data-cookieconsent="marketing"
  type="text/plain"
></script>

Cookie Settings page

There is also an optional form for you to add to your cookies page.

To use this, add the following markup to your Cookies Page:

<h2 class="govuk-heading-l">Change your cookie settings</h2>
<div
  id="govuk-cookie-preferences"
  data-analytics
  data-marketing
  data-preferences
>
  <p class="govuk-body">
    We cannot change your cookie settings at the moment because JavaScript is
    not running in your browser. To fix this, try:
  </p>
  <ul class="govuk-list govuk-list--bullet">
    <li>turning on JavaScript in your browser settings</li>
    <li>reloading this page</li>
  </ul>
</div>

You can alter the data-analytics, data-marketing or data-preferences data attributes, depending on what options you need to display. For example, the following only show options for analytics cookies:

<div id="govuk-cookie-preferences" data-analytics>...</div>

Script Options

data-service-name

By default, the banner shows the service name as GOV.UK. To set this to your service, add a data-service-name attribute to the script tag:

<script
  src="./cookie-consent.js"
  data-service-name="Check the MOT Status of a vehicle"
  type="text/javascript"
></script>

data-no-banner

If you want to prevent the cookie banner from showing automatically, add a data-nobanner attribute to the script tag.

<script src="./cookie-consent.js" data-nobanner type="text/javascript"></script>

If you disable the banner, you will have to write your own logic and interact with the javascript API to set user cookie consent.

data-policy-url

By default, the cookie policy link takes users to /our-policies/cookies-policy/. If you need the link to use a different url, you can set the data-policy-url attribute.

<script
  src="./cookie-consent.js"
  data-policy-url="/custom/policy/url"
  type="text/javascript"
></script>

The cookie banner will not show on the policy page, even if you have embedded the cookie-consent.js script. This is because the user does not need to be presented with a cookie banner if they are on the page which can manage cookies.

Javascript API

The javascript API is exposed on a GovUkCookieConsent global variable.

// shows the current cookie consent library version
console.log(GovUkCookieConsent.VERSION);

Methods

  • getPreferences()
  • getStatistics()
  • getMarketing()

These methods get the status of the cookie consent for that type of cookie. Returns a boolean.

  • getConsented()

This method gets the status of whether the user has positively interacted with the banner. It is primarily used to hide the banner once consent has been given.

  • setPreferences(value)
  • setStatistics(value)
  • setMarketing(value)

These methods set the status of the cookie consent for that type of cookie. set methods should only be used in response to a user interaction accepting that type of cookie. Expects a boolean value argument.

  • setConsented(value)

This method is used to set the consent that the user has given. It should be set to true when the user has taken an action which gives their consent. It should not be used to make the banner appear again for a user, as that is handled by the expiry date of the cookie.

Properties

  • VERSION the current version as defined in package.json

Compiling

This project uses Webpack and Babel

To compile the javascript in development mode, run

npm run build

For production mode, run

npm run build:production

Compiled javascript will be saved to dist/main.js

Environment variables

Environment variables can be used at compile-time to change the cookie script behaviour.

SERVICE_NAME

By default, the banner shows the service name as GOV.UK. To set this to your service, you can set this variable

SERVICE_NAME="Check the MOT status of a vehicle" npm run build:production

NO_BANNER

Set to true to produce a javascript file that doesn't show the cookie banner. Instead consent will be implied for all cookie types.

NO_BANNER=true npm run build:production

POLICY_URL

By default, the cookie policy link takes users to /our-policies/cookies-policy/. If you need the link to use a different url, you can set this variable

POLICY_URL=/custom/policy/url/ npm run build:production

LOG TO SPLUNK

Set to true a logging URL will be hit when the banner shown, analytics are accepted or analytics are not accepted.

LOG_TO_SPLUNK=true npm run build:production

Tests

To run the Jest tests

npm test

To run only unit tests

npm run test:unit

To run only integration tests

npm run test:integration

N.B. The integration tests rely on there being a test server available on localhost:8080.

Contributing to a release.

A new Tag must be made for the release following the versioning format. We use Semantic Versioning. IE. x.y.z where: x = Major version that causes incompatibilities, y = Minor change that adds a backwards comptible feature, z = Patch version for backwards compatible fixes. more information can be found at https://semver.org/

The release also must contain changes to the package version number to match the new tag.

If the release contains a change that will require the banner to be redisplayed to users, then the COOKIE_VERSION variable in cookieconsent.js must be increased by 1.

Notes

Currently, in-house development for this solution is primarily done on UNIX systems. There may be some difficulties when developing this solution on a windows machine.

govuk-cookie-consent's People

Contributors

beseven avatar bradmorton1 avatar chrimesdev avatar cristinaro avatar dependabot[bot] avatar evilstreak avatar gweaton avatar jacksonj04 avatar leeky avatar mayank1211 avatar mcheung-nhs avatar mikemonteith avatar pezholio avatar renovate[bot] avatar robjs avatar ross-clark avatar thomashdoughty avatar tomdoughty avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

lgtm-migrator

govuk-cookie-consent's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • Update dependency husky to v3.1.0
  • Update dependency jest-puppeteer to v4.4.0
  • Update dependency node-gyp to v9.4.1
  • Update dependency start-server-and-test to v1.15.4
  • Update dependency webpack to v4.47.0
  • Update actions/checkout action to v4
  • Update actions/setup-node action to v4
  • Update dependency babel-loader to v9
  • Update dependency css-loader to v7
  • Update dependency eslint to v9
  • Update dependency html-loader to v5
  • Update dependency husky to v9
  • Update dependency jest-puppeteer to v10
  • Update dependency jsdom to v24
  • Update dependency node to v20
  • Update dependency node-gyp to v10
  • Update dependency node-sass to v9
  • Update dependency puppeteer to v22
  • Update dependency sass-loader to v14
  • Update dependency start-server-and-test to v2
  • Update dependency webpack to v5
  • Update dependency webpack-cli to v5
  • Update jest monorepo to v29 (major) (babel-jest, jest, jest-environment-jsdom)
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v3
  • actions/setup-node v3
nodenv
.node-version
  • node 14.21.3
npm
package.json
  • @babel/cli ^7.2.3
  • @babel/core ^7.4.0
  • @babel/polyfill ^7.4.0
  • @babel/preset-env ^7.4.2
  • babel-core ^7.0.0-bridge.0
  • babel-jest ^24.5.0
  • babel-loader ^8.0.5
  • babel-plugin-rewire ^1.2.0
  • css-loader ^2.1.1
  • eslint ^5.16.0
  • eslint-config-nhsuk ^0.16.0
  • html-loader ^0.5.5
  • http-server ^0.13.0
  • husky ^3.0.9
  • jest ^26.0.0
  • jest-environment-jsdom ^26.0.0
  • jest-puppeteer ^4.1.0
  • jsdom ^16.0.0
  • minimist ^1.2.6
  • node-gyp ^9.0.0
  • node-sass 7.0.2
  • npm-run-all ^4.1.5
  • puppeteer ^1.14.0
  • regenerator-runtime ^0.13.2
  • sass-loader ^7.1.0
  • start-server-and-test ^1.7.12
  • webpack ^4.29.6
  • webpack-cli ^3.3.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.