Skip to content

Commit ef0bd4c

Browse files
authored
Merge pull request #55 from 21TORR/consent-manager-integration
Add `hasAnyConsnet` to `useCookiebot` + Add `ConsentManagerIntegration`
2 parents 4b45d36 + 16a7c94 commit ef0bd4c

4 files changed

Lines changed: 102 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2.8.0
2+
=====
3+
4+
* (feature) Add `ConsentManagerIntegration`.
5+
* (improvement) Add `hasAnyConsnet` to `useCookiebot`.
6+
7+
18
2.7.7
29
=====
310

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"zod": "^4.3.6"
2121
},
2222
"optionalDependencies": {
23-
"next": "^15.5.12",
23+
"next": "^15.5.12 || ^16.1.6",
2424
"react": "^19.2.4",
2525
"react-dom": "^19.2.4"
2626
},
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"use client";
2+
3+
import React, {Fragment, type ReactElement, type ReactNode} from "react";
4+
import {Cookiebot, type ConsentManagerProps} from "./Cookiebot";
5+
import {Usercentrics, type UsercentricsProps} from "./Usercentrics";
6+
import {UsercentricsV3, type UsercentricsV3Props} from "./UsercentricsV3";
7+
import {useCookiebot} from "../../../react/hooks/cookiebot";
8+
import {useUsercentrics} from "../../../react/hooks/usercentrics";
9+
10+
type CookiebotIntegration = Readonly<Omit<ConsentManagerProps, "id"> & {
11+
cookiebot: ConsentManagerProps["id"];
12+
}>;
13+
14+
type UsercentricsV2Integration = Readonly<Omit<UsercentricsProps, "id"> & {
15+
usercentricsV2: UsercentricsProps["id"];
16+
}>;
17+
18+
type UsercentricsV3Integration = Readonly<Omit<UsercentricsV3Props, "id"> & {
19+
usercentricsV3: UsercentricsV3Props["id"];
20+
}>;
21+
22+
type ConsentIntegration = CookiebotIntegration | UsercentricsV2Integration | UsercentricsV3Integration;
23+
24+
function isCookiebot (consent: ConsentIntegration): consent is CookiebotIntegration
25+
{
26+
return "cookiebot" in consent;
27+
}
28+
29+
function isUsercentricsV2 (consent: ConsentIntegration): consent is UsercentricsV2Integration
30+
{
31+
return "usercentricsV2" in consent;
32+
}
33+
34+
function isUsercentricsV3 (consent: ConsentIntegration): consent is UsercentricsV3Integration
35+
{
36+
return "usercentricsV3" in consent;
37+
}
38+
39+
type ConsentManagerIntegrationProps = Readonly<{
40+
embedOnAnyConsent?: ReactNode;
41+
consent: ConsentIntegration;
42+
}>;
43+
44+
export function ConsentManagerIntegration (props: ConsentManagerIntegrationProps): ReactElement | null
45+
{
46+
const {consent, embedOnAnyConsent} = props;
47+
48+
const cookiebot = useCookiebot();
49+
const usercentrics = useUsercentrics();
50+
51+
if (isCookiebot(consent))
52+
{
53+
const {cookiebot: cookieBotId, ...rest} = consent;
54+
55+
return (
56+
<Fragment>
57+
<Cookiebot id={cookieBotId} {...rest} />
58+
{cookiebot.hasAnyConsent && embedOnAnyConsent}
59+
</Fragment>
60+
);
61+
}
62+
63+
if (isUsercentricsV2(consent))
64+
{
65+
const {usercentricsV2: usercentricsId, ...rest} = consent;
66+
67+
return (
68+
<Fragment>
69+
<Usercentrics id={usercentricsId} {...rest} />
70+
{usercentrics.hasAnyConsent && embedOnAnyConsent}
71+
</Fragment>
72+
);
73+
}
74+
75+
if (isUsercentricsV3(consent))
76+
{
77+
const {usercentricsV3: usercentricsId, ...rest} = consent;
78+
79+
return (
80+
<Fragment>
81+
<UsercentricsV3 id={usercentricsId} {...rest} />
82+
{usercentrics.hasAnyConsent && embedOnAnyConsent}
83+
</Fragment>
84+
);
85+
}
86+
87+
throw new Error("No supported consent option given.");
88+
}

src/react/hooks/cookiebot.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ type WindowWithCookiebot = Window & Readonly<{
1212
statistics: boolean;
1313
method: "explicit";
1414
stamp: string;
15-
}
15+
};
16+
consented: boolean;
1617
};
1718
}>;
1819

@@ -26,13 +27,15 @@ type ConsentSettings = {
2627
type CookiebotSettings = Readonly<{
2728
openConsentManager(): void;
2829
consent: Omit<ConsentSettings, "stamp">;
30+
hasAnyConsent: boolean;
2931
}>;
3032

3133
/**
3234
* Hook to interact with Cookiebot.
3335
*/
3436
export function useCookiebot () : CookiebotSettings
3537
{
38+
const [hasAnyConsent, setHasAnyConsent] = useState(false);
3639
const [consent, setConsent] = useState<ConsentSettings>({
3740
stamp: "unset",
3841
marketing: false,
@@ -51,6 +54,7 @@ export function useCookiebot () : CookiebotSettings
5154
return;
5255
}
5356

57+
setHasAnyConsent(global.Cookiebot.consented);
5458
setConsent({
5559
...global.Cookiebot.consent,
5660
});
@@ -71,5 +75,6 @@ export function useCookiebot () : CookiebotSettings
7175
{
7276
(window as WindowWithCookiebot).Cookiebot?.renew();
7377
},
78+
hasAnyConsent,
7479
};
7580
}

0 commit comments

Comments
 (0)