Giter VIP home page Giter VIP logo

react-usercentrics's Introduction

npm version Release

@s-group/react-usercentrics

React utils for interacting with the Usercentrics Browser CMP.

npm install --save @s-group/react-usercentrics

This code is used in production:

Motivation

While the official @usercentrics/cmp-browser-sdk npm module is very useful, it is unnecessarily large and would still require some utility functions built around it to be really useful when working in a React application. It has 10 dependencies and an unpacked filesize size of 1.7 MB.

This package aims to be tiny and simple, and has 0 dependencies and an unpacked filesize of 32.7 KB.

Setup

You will need to set up a Usercentrics service and note down its settingsId. You will also need to enable the Window Event in the admin interface, and note down its name (for example, ucEvent).

After this you need to render the UsercentricsScript component to load the Browser API, and then finally wrap your application in the UsercentricsProvider.

import { UsercentricsScript, UsercentricsProvider } from '@s-group/react-usercentrics'
import ReactDOM from 'react-dom'
import React from 'react'

import { USERCENTRICS_SETTINGS_ID, USERCENTRICS_EVENT_NAME } from './config'
import MyApplication from './app'

ReactDOM.render(
  <>
    <UsercentricsScript settingsId={USERCENTRICS_SETTINGS_ID} />
    <UsercentricsProvider windowEventName={USERCENTRICS_EVENT_NAME}>
      <MyApplication /** You can interact with Usercentrics inside the provider */ />
    </UsercentricsProvider>
  </>,
  document.body
)

Augmented type-checks for ServiceId

Service-specific code needs to target a specific serviceId, for example to check its consent status. You can re-declare a special module to override the default string type of ServiceId with an enum to support stricter type-level checks in your application code.

enum MyServiceId {
  Service1 = 'service-id-1'
  Service2 = 'service-id-2'
}

declare module '@s-group/react-usercentrics/augmented' {
  export type ServiceId = MyServiceId
}

or even import the type from another file:

declare module '@s-group/react-usercentrics/augmented' {
  export type ServiceId = import('../config/usercentrics').ServiceId
}

API

Types

ConsentType

When giving consent using the API (instead of customer clicking the Dialog), consent can be either explicit (e.g. when clicking some custom button) or implicit.

UCWindow

Augmented window type, possibly including the UC_UI API. Do not declare this globally, but prefer to use the included utility functions instead.

Components

UsercentricsScript

The script tag that loads the Usercentrics Browser API.

interface UsercentricsScriptProps {
    settingsId: string
    version?: 'production' | 'preview'
}

/** Default production mode */
() => <UsercentricsScript settingsId="1234" />

/** Preview mode for development */
() => <UsercentricsScript settingsId="1234" version="preview" />

UsercentricsProvider

The provider component for listening to events from the Usercentrics Browser API. Render this once and wrap your application in it.

interface UsercentricsProviderProps {
    children: ReactNode
    /**
     * Whether to throw if invalid Service Id has been used.
     * @default false
     */
    strictMode?: boolean
    /**
     * The timeout value in milliseconds after which loading of the Usercentrics
     * script will be assumed to have failed.
     * @default 5000
     */
    timeout?: number
    /**
     * The configured window event name from Usercentrics admin interface.
     * @see https://docs.usercentrics.com/#/v2-events?id=usage-as-window-event
     */
    windowEventName: string
}

/** Basic usage */
() => (
  <UsercentricsProvider windowEventName="ucEvent">
    <App />
  </UsercentricsProvider>
)

/** Custom timeout in milliseconds */
() => (
  <UsercentricsProvider timeout={100} windowEventName="ucEvent">
    <App />
  </UsercentricsProvider>
)

UsercentricsDialogToggle

A button component for opening the Usercentrics dialog where users can adjust their consent settings, and read info about services.

The button will be disabled until Usercentrics has been initialized, so for example ad-blockers might prevent it from ever activating.

interface UsercentricsDialogToggleProps extends ButtonHTMLAttributes<HTMLButtonElement> {
    children: ReactNode
}

/** Basic usage */
() => <UsercentricsDialogToggle>Manage my consents</UsercentricsDialogToggle>

Hooks

useHasServiceConsent

Returns true if the specific Usercentrics service has been given consent. If it returns false, the service should not be loaded or used.

() => {
  const hasConsent = useHasServiceConsent('my-service-id')

  useEffect(() => {
    if (hasConsent) {
      loadMyService()
    }
  }, [hasConsent])
}

useIsFailed

Returns true if Usercentrics failed to load inside the timeout configured in UsercentricsProvider. This means consent status is unknown and will default to false, so no services can be used.

() => {
  const isFailed = useIsFailed()

  useEffect(() => {
    if (isFailed) {
      console.error('Failed to load consents! This site might not work properly.')
    }
  }, [isFailed])
}

useIsInitialized

Returns true if Usercentrics has been initialized and consents can be given.

() => {
  const isInitialized = useIsInitialized()

  useEffect(() => {
    if (isInitialized) {
      console.info('Usercentrics has initialized!')
    }
  }, [isInitialized])
}

useIsOpen

Returns true if the Usercentrics dialog is currently open on the page.

() => {
  const isOpen = useIsOpen()

  useEffect(() => {
    if (isOpen) {
      console.debug('Usercentrics dialog is open and probably covers the whole screen')
    } else {
      console.debug('Usercentrics dialog is now closed')
    }
  }, [isOpen])
}

useServiceInfo

Returns basic info for specific Usercentrics service, or null if not found.

The typing is not complete and contains only the info used internally:

  • id of the service, autogenerated by Usercentrics
  • name of the service, configured in the admin interface
  • consent.status of the service

See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=getservicesbaseinfo

() => {
  const serviceInfo = useServiceInfo('my-service-id')

  useEffect(() => {
    if (!serviceInfo) {
      console.error('Service not found for "my-service-id"')
    } else {
      console.info(`Consent for ${serviceInfo.name}: ${serviceInfo.consent.status}`)
    }
  }, [serviceInfo])
}

useServiceFullInfo

Returns full info for specific Usercentrics service, or null if not found. This triggers an extra API call and also returns null while loading.

The typing is not complete and contains only the info used internally:

  • id of the service, autogenerated by Usercentrics
  • name of the service, configured in the admin interface
  • consent.status of the service
  • description text of the service

See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=getservicesfullinfo

() => {
  const serviceInfo = useServiceFullInfo('my-service-id')

  useEffect(() => {
    if (serviceInfo) {
      console.info(`The ${serviceInfo.name} service is used to ${serviceInfo.description}`)
    }
  }, [serviceInfo])
}

Utils

showFirstLayer

Programmatic way to show First Layer.

See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=showfirstlayer

showFirstLayer()

showSecondLayer

Programmatic way to show Second Layer. If a service/vendor Id value is passed, Second Layer will open the right tab, scroll to the given service/vendor entry and expand it. If no Id is passed, Second Layer will be shown without srcolling to any specific service/vendor.

See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=showsecondlayer

showSecondLayer()
showSecondLayer('my-service-id')

getServicesBaseInfo

A method to get array of all services with their basic information

See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=getservicesbaseinfo

const services = getServicesBaseInfo()

const myService = services.find((service) => service.id === 'my-service-id')

getServicesFullInfo

A method to get array of all services with their full information. An extra api request will be made, therefore the return represents the eventual completion (or failure) of an asynchronous operation and its returning value.

See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=getservicesfullinfo

const services = await getServicesFullInfo()

const myService = services.find((service) => service.id === 'my-service-id')

hasServiceConsent

Returns true if Usercentrics service has been given consent

const services = getServicesBaseInfo()
const myService = services.find((service) => service.id === 'my-service-id')
const hasConsent = hasServiceConsent(myService)

if (hasConsent) {
  loadMyService()
}

acceptService

A method for accepting a single service.

See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=acceptservice

await acceptService('my-service-id')
await acceptService('my-service-id', ConsentType.Explicit)
await acceptService('my-service-id', ConsentType.Implicit)

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.