npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

govuk-single-consent

v3.0.9

Published

Client code for the Cookie Consent Sharing API

Downloads

649

Readme

Single Consent client

The Single Consent client is a small Javascript which can be embedded in a website to enable easily sharing a user's consent or rejection of cookies across different websites.

See the Single Consent service README.

Quick start

See the CommonJS example.

Contact [email protected] to get the URL of the API endpoint.

The library provides the following static methods for finding out which types of cookies a user has consented to.

  • hasConsentedToEssential
  • hasConsentedToUsage
  • hasConsentedToCampaigns
  • hasConsentedToSetting

The method GovSingleConsent.getConsents() provides the current state of the user's consents to all types of cookies.

1. Install with npm

In order to set first-party cookies, the client Javascript must be served with your application.

We recommend installing the Single Consent client using node package manager (npm).

npm i govuk-single-consent

https://www.npmjs.com/package/govuk-single-consent

2. Including the Javascript client

The javascript client can be included in different ways. Choose one below.

CommonJS

See the example.

Typescript

See the example.

HTML script tag (IIFE)

The javascript client makes available the object window.GovSingleConsent for interacting with the API. It needs to be loaded on any page that could be an entry point to your web application, that allows modifying cookie consent, or provides a link to another domain with which you want to share cookie consent status. It is probably easiest to add the script to a base template used for all pages.

On the same pages, you need to load your javascript for interacting with the window.GovSingleConsent object.

See the following examples.

It is common practice to add Javascript tags just before the end </body> tag, eg:

    ...

    <script src="{path_to_client_js}/singleconsent.js"></script>
    <script src="{path_to_cookie_banner_script}.js"></script>
    <script src="{path_to_cookies_page_script}.js"></script>
  </body>
</html>

3. Passing the base URL to the constructor

You can either pass an environment string, or a custom base URL.

If using an environment string, its value should be either staging or production.

e.g

const dummyCallback = () => {}

// With an environemnt string to the staging environment
new GovSingleConsent(dummyCallback, 'staging')

// With an environemnt string to the production environment
new GovSingleConsent(dummyCallback, 'production')

// With a custom base URL
new GovSingleConsent(dummyCallback, 'http://some-development-url.com')

4. Share the user's consent to cookies via the API

When the user interacts with your cookie banner or cookie settings page to consent to or reject cookies you can update the central database by invoking the following function:

exampleCookieConsentStatusObject: Consents = {
  essential: true,
  settings: false,
  usage: true,
  campaigns: false,
}

singleConsentObject.setConsents(exampleCookieConsentStatusObject)

Content Security Policy

If your website is served with a Content-Security-Policy HTTP header or <meta> element, you may need to modify it to allow the client to access the Single Consent service. The value of the header or meta element should contain the following:

connect-src 'self' https://consent-api-nw.a.run.app/api/v1/consent [... other site URLs separated by spaces];

What the library does

The library manages all read/write operations with a cookie that stores the state of a user's consent.

Callback

Websites using the Consent API must provide a callback function. This will be invoked each time the consent has been updated. It will be called with three parameters:

  • consents An object describing the new consent state.
  • consentsPreferencesSet Boolean, the cookie banner must be displayed if this value is false.
  • error An object describing any error, otherwise null. If there is an error, then the consents object will say that the consents have been revoked.

The structure of the consent data object is currently based on the GOV.UK cookies_policy cookie. If your website cookies do not fall into any of the four categories listed, please contact us.

Getting updates

To be notified when there's a new release, you can watch the consent-api Github repository.