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

@scaleway/cookie-consent

v2.0.0

Published

React provider to handle website end user consent cookie storage based on segment integrations

Downloads

8,572

Readme

@scaleway/cookie-consent

This package is an helper to handle cookie consents with Segment integrations. It will handle the cookie consent for each categories.

This package does not contain design element to display a cookie consent modal, it only handle the storage and the init of cookie consent in a React provider.

QuickStart

In order to use it, first you need to provide a context at the top level of your application

import { PropsWithChildren } from 'react'

const MyApp = ({ children }: PropsWithChildren) => {
  return (
    <CookieConsentProvider
      isConsentRequired // Switch off consents if not defined (usefull for E2E testing)
      essentialIntegrations={[]} // List of mandatory integrations
      config={{
        segment, // Segment configuration used to get dynamically the integration used
      }}
      // not required
      cookiePrefix="_scw_rgpd" // default value
      consentMaxAge={13 * 30 * 24 * 60 * 60} // default value (appx 13 months)
      consentAdvertisingMaxAge={6 * 30 * 24 * 60 * 60} // default value (appx 6 months)
      cookiesOptions={{ sameSite: 'strict', secure: true, path: '/' }} // default value
    >
      {children}
    </CookieConsentProvider>
  )
}

Then in your cookie modal component you could simply use exposed hook to get and modify consents

export function PanelConsent() {
  const { saveConsent, categoriesConsent } = useCookieConsent()

  const setAllConsents = ({
    categoriesConsent,
    value,
  }: {
    categoriesConsent: Partial<Consent>
    value: boolean
  }) =>
    Object.keys(categoriesConsent).reduce(
      (acc, category) => ({ ...acc, [category]: value }),
      {},
    )

  const handleClick = (consentForAll: boolean) => () => {
    saveConsent(setAllConsents({ categoriesConsent, value: consentForAll }))
  }

  const onAgreeAll = handleClick(true)

  const onReject = handleClick(false)

  return (
    <div className={styles.consent}>
      <div>Do you accept consents ?</div>
      <div>
        <Button onClick={onAgreeAll} autoFocus>
          Accept
        </Button>
        <Button onClick={onReject}>Decline</Button>
      </div>
    </div>
  )
}

Segment Consent Middleware

As it's necessary now to have a consent management. https://segment.com/docs/privacy/consent-management/configure-consent-management/

you will have the possibility to add the SegmentConsentMiddleware, be aware that there is a dependency with SegmenttProvider.

User flow

flowchart TD
    Z[Application boot] -..-> A
    subgraph "Cookie Consent booting"
    A[First user navigation in app] --> B{isConsentRequired}
    B --> |false| C[do nothing with cookies]
    B --> |true| D[Fetch segment integrations configuration]
    D --> E[Generate hash of integration and define required consent categories depending on integration]
    E -..->F[Set needConsent to true]
    end
    subgraph "Consent storage"
    F -..-> | | G[/User saveConsent with categories/]
    G --> H[Hash of integration is stored in _scw_rgpd_hash cookie with storage of 6 months]
    G --> I[_scw_rgpd_$category$ cookie is stored for each accepted cookie consent category, 6 months for ad consent, 13 month for others]
    H & I --> J[needConsent is set to false]
    end
    subgraph "User come back on website in futur (within cookie duration)"
    J -..-> K[Application boot]
    K -..-> L[Check value fo cookies _scw_rgpd_hash and _scw_rgpd_$categorie$]
    L --> M[Load in context accepted categories]
    end
    subgraph "User come back after 6 months"
    J -...-> N[Application boot]
    N -..-> O[Check value fo cookies _scw_rgpd_hash and _scw_rgpd_$categorie$]
    O --> B
    end

How to contribute ?

Prerequisites

$ cd packages/cookie-consent

Start

$ pnpm build # Build the package
$ pnpm watch # Build the package and watch for changes