Skip to content

Commit 0966157

Browse files
authored
Merge pull request #34 from s-group-dev/web-cmp-v3
Migrate to Usercentrics Web CMP v3
2 parents 408e531 + 88a369c commit 0966157

23 files changed

+676
-900
lines changed

MIGRATION.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
## 3.0.0
2+
3+
- The Usercentrics Web CMP has been updated to v3 and includes a lot of changes.
4+
- Read more about the CMP v3 here: https://usercentrics.com/docs/web/v3/
5+
- Read the Usercentrics Migration guide here for more context, although it shouldn't be relevant when using this package: https://usercentrics.com/docs/web/migration/migration-from-v2/
6+
- The `windowEvent` prop for `<UsercentricsProvider />` is no longer supported and should be removed. The CMP v3 handles this automatically with a new event internally.
7+
- The `uiVersion` prop for `<UsercentricsScript />` is no longer supported and should be removed. The CMP v3 loader script currently supports only the "latest" version.
8+
- This also means that hard-coding a version number and its [Subresource Integrity](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity) hash is no longer supported. Instead, use a random `nonce` value when implementing a [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP).
9+
- The `showSecondLayer()` util no longer supports passing a service id argument to directly open to the service info. Instead, use the new `showServiceDetails(serviceId: ServiceId)` util.
10+
- The `getServicesFromLocalStorage()` util has been replaced with `getConsentsFromLocalStorage()` with a different format.
11+
- The following utils and hooks have been removed because CMP v3 no longer supports them:
12+
- `getServicesBaseInfo()`
13+
- `useServiceInfo()`
14+
- `getServicesFullInfo()`
15+
- `useServiceFullInfo()`
16+
- All the utils are now `async` to match the CMP v3 implementations
17+
118
## 2.0.0
219

320
- No functional changes. This version is only released to sync the package version with the supported Usercentrics Web CMP v2 version.

README.md

Lines changed: 34 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ The next major version `@s-group/react-usercentrics@3` will only support [Userce
2020

2121
## Setup
2222

23-
You will need to set up a Usercentrics service and note down its `settingsId`. You will also need to enable the [Window Event](https://docs.usercentrics.com/#/v2-events?id=usage-as-window-event) in the admin interface, and note down its name (for example, `ucEvent`).
24-
25-
After this you need to render the `UsercentricsScript` component to load the Browser API, and then finally wrap your application in the `UsercentricsProvider`.
23+
You will need to set up a Usercentrics service and note down its `settingsId`. After this you need to render the `UsercentricsScript` component to load the Browser API, and then finally wrap your application in the `UsercentricsProvider`.
2624

2725
```tsx
2826
import { UsercentricsScript, UsercentricsProvider } from '@s-group/react-usercentrics'
@@ -80,14 +78,14 @@ consent can be either explicit (e.g. when clicking some custom button) or implic
8078

8179
#### `UCWindow`
8280

83-
Augmented window type, possibly including the `UC_UI` API.
81+
Augmented window type, possibly including the `__ucCmp` API.
8482
Do not declare this globally, but prefer to use the included utility functions instead.
8583

8684
### Components
8785

8886
#### `UsercentricsScript`
8987

90-
The script tag that loads the Usercentrics Browser UI and API.
88+
The script tag that loads the Usercentrics Browser CMP.
9189

9290
```tsx
9391
interface UsercentricsScriptProps
@@ -102,15 +100,6 @@ interface UsercentricsScriptProps
102100

103101
settingsId: string
104102

105-
/**
106-
* The specific version of Usercentrics UI to load instead of "latest", as a string value
107-
*
108-
* @default "latest"
109-
* @example "3.24.0"
110-
* @see https://releases.usercentrics.com/en?category=browser+ui&role=cmpv1%3Bcmpv2%3B
111-
*/
112-
uiVersion?: string
113-
114103
/**
115104
* Whether to run Usercentrics in "production" or "preview" mode
116105
* @default "production"
@@ -126,9 +115,6 @@ interface UsercentricsScriptProps
126115
/** Preview mode for development */
127116
;() => <UsercentricsScript settingsId="1234" version="preview" />
128117

129-
/* Fixed UI version instead of latest */
130-
;() => <UsercentricsScript settingsId="1234" uiVersion="3.24.0" />
131-
132118
/* Fixed language code */
133119
;() => <UsercentricsScript settingsId="1234" language="fi" />
134120
```
@@ -152,23 +138,18 @@ interface UsercentricsProviderProps {
152138
* @default 5000
153139
*/
154140
timeout?: number
155-
/**
156-
* The configured window event name from Usercentrics admin interface.
157-
* @see https://docs.usercentrics.com/#/v2-events?id=usage-as-window-event
158-
*/
159-
windowEventName: string
160141
}
161142

162143
/** Basic usage */
163144
() => (
164-
<UsercentricsProvider windowEventName="ucEvent">
145+
<UsercentricsProvider>
165146
<App />
166147
</UsercentricsProvider>
167148
)
168149

169150
/** Custom timeout in milliseconds */
170151
() => (
171-
<UsercentricsProvider timeout={100} windowEventName="ucEvent">
152+
<UsercentricsProvider timeout={100}>
172153
<App />
173154
</UsercentricsProvider>
174155
)
@@ -302,183 +283,87 @@ Returns `true` if the user has interacted with the Usercentrics dialog and given
302283
}
303284
```
304285

305-
#### `useServiceInfo`
306-
307-
Returns basic info for specific Usercentrics service, or null if not found.
286+
### Utils
308287

309-
The typing is _not complete_ and contains only the info used internally:
288+
#### `hasUserInteracted`
310289

311-
- `id` of the service, autogenerated by Usercentrics
312-
- `name` of the service, configured in the admin interface
313-
- `consent.status` of the service
290+
A method to check if user has interacted with the consent prompt and given consent information.
314291

315-
See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=getservicesbaseinfo
292+
See also `setUserHasInteracted`.
316293

317294
```tsx
318-
;() => {
319-
const serviceInfo = useServiceInfo('my-service-id')
295+
const userInteracted = hasUserInteracted()
320296

321-
useEffect(() => {
322-
if (!serviceInfo) {
323-
console.error('Service not found for "my-service-id"')
324-
} else {
325-
console.info(`Consent for ${serviceInfo.name}: ${serviceInfo.consent.status}`)
326-
}
327-
}, [serviceInfo])
297+
if (userInteracted) {
298+
actionRequiredConsentInfoGiven()
328299
}
329300
```
330301

331-
#### `useServiceFullInfo`
332-
333-
Returns full info for specific Usercentrics service, or null if not found.
334-
This triggers an extra API call and also returns `null` while loading.
335-
336-
The typing is _not complete_ and contains only the info used internally:
302+
#### `setUserHasInteracted`
337303

338-
- `id` of the service, autogenerated by Usercentrics
339-
- `name` of the service, configured in the admin interface
340-
- `consent.status` of the service
341-
- `description` text of the service
304+
A method to set that user has interacted with the consent prompt and given consent information.
342305

343-
See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=getservicesfullinfo
306+
See also `hasUserInteracted`.
344307

345308
```tsx
346-
;() => {
347-
const serviceInfo = useServiceFullInfo('my-service-id')
348-
349-
useEffect(() => {
350-
if (serviceInfo) {
351-
console.info(`The ${serviceInfo.name} service is used to ${serviceInfo.description}`)
352-
}
353-
}, [serviceInfo])
354-
}
309+
setUserHasInteracted()
355310
```
356311

357-
### Utils
358-
359312
#### `showFirstLayer`
360313

361314
Programmatic way to show First Layer.
362315

363-
See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=showfirstlayer
316+
See also https://usercentrics.com/docs/web/features/api/control-ui/#showfirstlayer
364317

365318
```tsx
366-
showFirstLayer()
319+
await showFirstLayer()
367320
```
368321

369322
#### `showSecondLayer`
370323

371-
Programmatic way to show Second Layer. If a service/vendor Id value is passed,
372-
Second Layer will open the right tab, scroll to the given service/vendor entry and expand it.
373-
If no Id is passed, Second Layer will be shown without srcolling to any specific service/vendor.
374-
375-
See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=showsecondlayer
376-
377-
```tsx
378-
showSecondLayer()
379-
```
380-
381-
```tsx
382-
showSecondLayer('my-service-id')
383-
```
384-
385-
#### `getServicesBaseInfo`
386-
387-
A method to get array of all services with their basic information
324+
Programmatic way to show Second Layer.
388325

389-
See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=getservicesbaseinfo
326+
See also https://usercentrics.com/docs/web/features/api/control-ui/#showsecondlayer
390327

391328
```tsx
392-
const services = getServicesBaseInfo()
393-
394-
const myService = services.find((service) => service.id === 'my-service-id')
329+
await showSecondLayer()
395330
```
396331

397-
#### `getServicesFullInfo`
398-
399-
A method to get array of all services with their full information.
400-
An extra api request will be made, therefore the return represents
401-
the eventual completion (or failure) of an asynchronous operation
402-
and its returning value.
403-
404-
See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=getservicesfullinfo
405-
406-
```tsx
407-
const services = await getServicesFullInfo()
408-
409-
const myService = services.find((service) => service.id === 'my-service-id')
410-
```
332+
#### `showServiceDetails`
411333

412-
#### `hasServiceConsent`
334+
Programmatic way to show the details of a service.
413335

414-
Returns true if Usercentrics service has been given consent
336+
See also https://usercentrics.com/docs/web/features/api/control-ui/#showservicedetails
415337

416338
```tsx
417-
const services = getServicesBaseInfo()
418-
const myService = services.find((service) => service.id === 'my-service-id')
419-
const hasConsent = hasServiceConsent(myService)
420-
421-
if (hasConsent) {
422-
loadMyService()
423-
}
339+
await showServiceDetails('my-service-id')
424340
```
425341

426-
#### `areAllConsentsAccepted`
342+
#### `getConsentsFromLocalStorage`
427343

428-
Returns true if all Usercentrics services have been given consent
344+
A method to get consent status saved to localStorage.
429345

430346
```tsx
431-
const hasAllConsents = areAllConsentsAccepted()
432-
433-
if (hasAllConsents) {
434-
loadMyService()
435-
}
347+
const consents = getConsentsFromLocalStorage()
348+
const hasConsent = consents['my-service-id']?.consent === true
436349
```
437350

438-
#### `acceptService`
439-
440-
A method for accepting a single service.
441-
442-
See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=acceptservice
443-
444-
```tsx
445-
await acceptService('my-service-id')
446-
```
447-
448-
```tsx
449-
await acceptService('my-service-id', ConsentType.Explicit)
450-
```
451-
452-
```tsx
453-
await acceptService('my-service-id', ConsentType.Implicit)
454-
```
455-
456-
#### `hasUserInteracted`
457-
458-
A method to check if user has interacted with the consent prompt and given consent information.
459-
460-
```tsx
461-
const userInteracted = hasUserInteracted()
462-
if (userInteracted) {
463-
actionRequiredConsentInfoGiven()
464-
}
465-
```
351+
#### `updateServicesConsents`
466352

467-
#### `getServicesFromLocalStorage`
353+
Updates consents for individual or multiple services.
468354

469-
A method to get array of all services from local storage
355+
See also https://usercentrics.com/docs/web/features/api/control-functionality/#updateservicesconsents
470356

471357
```tsx
472-
const services = getServicesFromLocalStorage()
473-
const myService = services.find((service) => service.id === 'my-service-id')
358+
updateServicesConsents([{ id: 'my-service-id', consent: true }])
474359
```
475360

476361
#### `updateLanguage`
477362

478363
Programmatic way to set language for the CMP.
479364
The param `countryCode` is a two character country code, e.g. "en" = set language to English
480365

481-
See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=updatelanguage
366+
See also https://usercentrics.com/docs/web/features/api/control-functionality/#changelanguage
482367

483368
```tsx
484369
updateLanguage('fi')

src/components/UsercentricsDialogToggle.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const UsercentricsDialogToggle: FC<UsercentricsDialogToggleProps> = ({ on
2525

2626
event.preventDefault()
2727
if (isUsercentricsInitialized) {
28-
showFirstLayer()
28+
void showFirstLayer()
2929
}
3030
},
3131
[isUsercentricsInitialized, onClick],

0 commit comments

Comments
 (0)