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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@christiansousa/cmp-web-sdk

v2.0.0

Published

Usercentrics CMP Browser V3 SDK

Readme

npm version install size npm bundle size npm downloads

Table of Contents

Installing

Package manager

Using npm:

$ npm install @usercentrics/cmp-web-sdk

Using yarn:

$ yarn add @usercentrics/cmp-web-sdk

Once the package is installed, you can import the library using import or require approach:

import WebSdk, {GdprWebSdk} from '@usercentrics/cmp-web-sdk'

You can also use the default export, for a more generic approach:

import WebSdk from '@usercentrics/cmp-web-sdk';

If you use require for importing, only default export is available:

const WebSdk = require('@usercentrics/cmp-web-sdk');

For some bundlers and some ES6 linters you may need to do the following:

import { default as WebSdk } from '@usercentrics/cmp-web-sdk';

Example

Initializing the generic SDK

import WebSdk from '@usercentrics/cmp-web-sdk';

(async () => {
    // Create the SDK instance
    const webSdk = new WebSdk();
    const cmpController = await cmpWebSdk.initBySetting('YOUR_SETTINGS_ID'); // Initialize the SDK with your SettingsId
})();

Initializing the GDPR-specific SDK

import { GdprWebSdk } from '@usercentrics/cmp-web-sdk';

(async () => {
    // Create the SDK instance
    const webSdk = new GdprWebSdk();
    // Initialize the SDK with your SettingsId
    const cmpController = await cmpWebSdk.initBySetting('YOUR_SETTINGS_ID'); 
})();

Initializing the TCF-specific SDK

import { TcfWebSdk } from '@usercentrics/cmp-web-sdk';

(async () => {
    // Create the SDK instance
    const webSdk = new TcfWebSdk();
    // Initialize the SDK with your SettingsId
    const cmpController = await cmpWebSdk.initBySetting('YOUR_SETTINGS_ID'); 
})();

Initializing the US-specific SDK

import { UsWebSdk } from '@usercentrics/cmp-web-sdk';

(async () => {
    // Create the SDK instance
    const webSdk = new UsWebSdk();
    // Initialize the SDK with your SettingsId
    const cmpController = await cmpWebSdk.initBySetting('YOUR_SETTINGS_ID');
})();

Usercentrics API

Applying Consent

acceptAllConsents
await webSdk.acceptAllConsents();
denyAllConsents
await webSdk.denyAllConsents();
updateServiceConsent

Update a single service consent

await cmpWebSdk.updateServicesConsent({
    id: 'SERVICE_ID',
    consent: true, // or false
});
updateServiceConsents

Update a multiple services consent

await cmpWebSdk.updateServicesConsents([
  {
    id: 'SERVICE_ID_1',
    consent: true, // or false
  },
  {
    id: 'SERVICE_ID_2',
    consent: false, // or true
  }
]);
updateCategoryConsent

Update a Category consent

await cmpWebSdk.updateCategoryConsent({
  id: 'CATEGORY_ID',
  consent: true, // or false
});
updateCategoriesConsents

Update a multiple categories consent

await cmpWebSdk.updateCategoriesConsents([
  {
    id: 'CATEGORY_ID_1',
    consent: true, // or false
  },
  {
    id: 'CATEGORY_ID_2',
    consent: false, // or true
  },
]);

saveConsents

Note: Consent usage All of the above methods just change the consent state internally to persist the user decision (e.g. after a save consent button click by the user) you need to persist/save the consents

await cmpWebSdk.saveConsents();

Data

getServices

await cmpWebSdk.getServices();

getServicesBaseInfo

await cmpWebSdk.getServicesBaseInfo();

getCategories

await cmpWebSdk.getCategories();

getLanguages

await cmpWebSdk.getLanguages();

Other methods

changeLanguage

Change the language to a supported language For the list of supported languages, please refer to getLanguages

await cmpWebSdk.changeLanguage('en');

areAllConsentsAccepted

Returns true if all the consents are accepted

const allConsentsAccepted = await cmpWebSdk.areAllConsentsAccepted();

areAllConsentsDenied

Returns true if all the consents are denied

const allConsentsDenied = await cmpWebSdk.areAllConsentsDenied();

getIsConsentRequired

Returns true if a user decision is needed

const isConsentRequired = await cmpWebSdk.getIsConsentRequired();

Implementation Examples

GDPR

// import GDPR optimized GdprWebSdk
import { GdprWebSdk, GdprCmpController, UiView } from '@usercentrics/cmp-web-sdk';

// Vanilla JS view
class CmpView {
  private cmpController: GdprCmpController;
  private cmpElement: HTMLDivElement;

  constructor(cmpController: GdprCmpController) {
    this.cmpController = cmpController;
  }

  public async init() {
    // create and attach the ui view
      
    const cmpElement = document.createElement('div');
    cmpElement.id = 'cmp';

    if (typeof cmpElement.attachShadow === 'function') {
      // optional: attach shadow
      cmpElement.attachShadow({ mode: 'open' });
    }

    this.cmpElement = (cmpElement.shadowRoot as HTMLDivElement) || cmpElement;
    this.setView(this.cmpController.ui.initialView || 'none');

    return new Promise((resolve) => {
      if (document.body) {
        document.body.appendChild(cmpElement);
        resolve();
      }

      document.addEventListener('DOMContentLoaded', () => {
        document.body.append(cmpElement);
        resolve();
      });
    });
  }

  public setView(view: UiView) {
    const consents = JSON.stringify(this.cmpController.dps.getServicesConsents()); // data example

    switch (view) {
      case 'none':
        // do not show the cmp, do not show a cmp button/trigger
        this.cmpElement.innerHTML = '';
        break;
      case 'button':
        // do not show the cmp, show a cmp button/trigger
        this.cmpElement.innerHTML = 'custom button view';
        break;
      case 'first':
        // the cmp needs to be shown
        this.cmpElement.innerHTML = `custom first layer view: ${consents}`;
        break;
      case 'second':
        //  the optional custom secondary layer (never an initial view)
        this.cmpElement.innerHTML = `custom second layer view ${consents}`;
        break;
    }
  }
}

(async () => {
  const gdprWebSdk = new GdprWebSdk();
  const gdprCmpController = await gdprWebSdk.initBySetting('YOUR_GDPR_SETTINGS_ID');

  const cmpView = new CmpView(gdprCmpController);
  await cmpView.init();
})();

License

License: MIT