Skip to content

Commit

Permalink
Extend UserCentrics Snippet and funtionality
Browse files Browse the repository at this point in the history
  • Loading branch information
carlgu committed Jul 10, 2024
1 parent 6ef23dc commit 59402e2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/lib/locale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Splits locale by hyphen
*
* @internal
*/
export function splitLocale (locale : string) : string[]
{
return locale.split("-");
}
10 changes: 9 additions & 1 deletion src/next/components/Snippet/Usercentrics.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import React, {ReactElement} from "react";
import Script from "next/script";
import {splitLocale} from "../../../lib/locale";

export type UsercentricsProps = Readonly<{
id: string;
production?: boolean;
locale: string;
}>;

export function Usercentrics (props: UsercentricsProps): ReactElement | null
{
const isProd = true === props.production;
const language = splitLocale(props.locale).at(0) ?? "en";

return (
<Script
id="usercentrics-cmp"
data-settings-id={props.id}
src="https://app.usercentrics.eu/browser-ui/latest/loader.js"
data-version={!isProd ? "preview" : undefined}
data-settings-id={props.id}
data-language={language}
async
/>
);
Expand Down
12 changes: 12 additions & 0 deletions src/react/hooks/usercentrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {useEffect, useState} from "react";
type WindowWithUsercentrics = Window & Readonly<{
UC_UI?: {
showFirstLayer(): void;
showSecondLayer(): void;
restartEmbeddings(): void;
};
}>;

Expand All @@ -20,6 +22,8 @@ type UsercentricsEvent = CustomEvent<{

type UsercentricsSettings = Readonly<{
openConsentManager(): void;
showSecondLayer(): void;
restartEmbeddings(): void;
hasAnyConsent: boolean;
/**
* Contains all categories with consent apart from the essential categories
Expand Down Expand Up @@ -68,5 +72,13 @@ export function useUsercentrics () : UsercentricsSettings
{
(window as WindowWithUsercentrics).UC_UI?.showFirstLayer();
},
showSecondLayer (): void
{
(window as WindowWithUsercentrics).UC_UI?.showSecondLayer();
},
restartEmbeddings ()
{
(window as WindowWithUsercentrics).UC_UI?.restartEmbeddings();
},
}
}

0 comments on commit 59402e2

Please sign in to comment.