-
Notifications
You must be signed in to change notification settings - Fork 700
Add Usercentrics CMP (cookie consent + GDPR compliance) #3293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
isstuev
wants to merge
1
commit into
main
Choose a base branch
from
fe-usercentrics-cmp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import type { Feature } from './types'; | ||
|
|
||
| import app from '../app'; | ||
| import { getEnvValue, parseEnvJson } from '../utils'; | ||
|
|
||
| interface UsercentricsConfig { | ||
| readonly scriptUrl: string; | ||
| readonly rulesetId: string; | ||
| } | ||
|
|
||
| const title = 'Usercentrics CMP'; | ||
|
|
||
| const config: Feature<{ scriptUrl: string; rulesetId: string }> = (() => { | ||
| if (app.isPrivateMode) { | ||
| return Object.freeze({ title, isEnabled: false as const }); | ||
| } | ||
|
|
||
| const rawConfig = parseEnvJson<UsercentricsConfig>(getEnvValue('NEXT_PUBLIC_USERCENTRICS_CONFIG') ?? ''); | ||
|
|
||
| if (rawConfig?.scriptUrl && rawConfig?.rulesetId) { | ||
| return Object.freeze({ | ||
| title, | ||
| isEnabled: true as const, | ||
| scriptUrl: rawConfig.scriptUrl, | ||
| rulesetId: rawConfig.rulesetId, | ||
| }); | ||
| } | ||
|
|
||
| return Object.freeze({ title, isEnabled: false as const }); | ||
| })(); | ||
|
|
||
| export default config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import React from 'react'; | ||
|
|
||
| import config from 'configs/app'; | ||
|
|
||
| async function checkMarketingConsent(): Promise<boolean> { | ||
| if (!window.__ucCmp) { | ||
| return false; | ||
| } | ||
| const details = await window.__ucCmp.getConsentDetails(); | ||
| return details.categories?.marketing?.state === 'ALL_ACCEPTED'; | ||
| } | ||
|
|
||
| export default function useUsercentricsMarketingConsent(): boolean { | ||
| const [ hasConsent, setHasConsent ] = React.useState<boolean>(!config.features.usercentrics.isEnabled); | ||
|
|
||
| React.useEffect(() => { | ||
| if (!config.features.usercentrics.isEnabled) { | ||
| return; | ||
| } | ||
|
|
||
| const updateConsent = async() => { | ||
| setHasConsent(await checkMarketingConsent()); | ||
| }; | ||
|
|
||
| // Get initial consent state | ||
| updateConsent(); | ||
|
|
||
| // Re-check on every consent change and on CMP initialization | ||
| window.addEventListener('UC_CONSENT', updateConsent); | ||
| window.addEventListener('UC_UI_INITIALIZED', updateConsent); | ||
|
|
||
| return () => { | ||
| window.removeEventListener('UC_CONSENT', updateConsent); | ||
| window.removeEventListener('UC_UI_INITIALIZED', updateConsent); | ||
| }; | ||
| }, []); | ||
|
|
||
| return hasConsent; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import type CspDev from 'csp-dev'; | ||
|
|
||
| import config from 'configs/app'; | ||
| const feature = config.features.usercentrics; | ||
|
|
||
| export function usercentrics(): CspDev.DirectiveDescriptor { | ||
| if (!feature.isEnabled) { | ||
| return {}; | ||
| } | ||
|
|
||
| let scriptOrigin: string; | ||
| try { | ||
| scriptOrigin = new URL(feature.scriptUrl).origin; | ||
| } catch { | ||
| return {}; | ||
| } | ||
|
|
||
| return { | ||
| 'script-src': [ | ||
| scriptOrigin, | ||
| 'https://web.cmp.usercentrics.eu/', | ||
| ], | ||
| 'connect-src': [ | ||
| scriptOrigin, | ||
| 'https://api.usercentrics.eu/', | ||
| 'https://web.cmp.usercentrics.eu/', | ||
| 'https://v1.api.service.cmp.usercentrics.eu/', | ||
| 'https://consent-api.service.consent.usercentrics.eu/', | ||
| ], | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,16 @@ import config from 'configs/app'; | |
| import * as svgSprite from 'ui/shared/IconSvg'; | ||
|
|
||
| const marketplaceFeature = config.features.marketplace; | ||
| const usercentricsFeature = config.features.usercentrics; | ||
| const googleAnalyticsFeature = config.features.googleAnalytics; | ||
|
|
||
| // Inline GA config script; hash is used in CSP policy (nextjs/csp/policies/googleAnalytics.ts) | ||
| const GA_INLINE_SCRIPT = ` | ||
| window.dataLayer = window.dataLayer || []; | ||
| function gtag(){dataLayer.push(arguments);} | ||
| gtag('js', new Date()); | ||
| gtag('config', window.__envs.NEXT_PUBLIC_GOOGLE_ANALYTICS_PROPERTY_ID); | ||
| `; | ||
|
|
||
| class MyDocument extends Document { | ||
| static async getInitialProps(ctx: DocumentContext) { | ||
|
|
@@ -67,6 +77,28 @@ class MyDocument extends Document { | |
| <link rel="apple-touch-icon" sizes="180x180" href="/assets/favicon/apple-touch-icon-180x180.png"/> | ||
| <link rel="icon" type="image/png" sizes="192x192" href="/assets/favicon/android-chrome-192x192.png"/> | ||
| <link rel="preload" as="image" href={ svgSprite.href }/> | ||
|
|
||
| { /* USERCENTRICS */ } | ||
| { usercentricsFeature.isEnabled && ( | ||
| <script id="usercentrics-cmp" src={ usercentricsFeature.scriptUrl } data-settings-id={ usercentricsFeature.rulesetId } async/> | ||
| ) } | ||
|
|
||
| { /* GOOGLE ANALYTICS */ } | ||
| { googleAnalyticsFeature.isEnabled && ( | ||
| <> | ||
| <script | ||
| { ...(usercentricsFeature.isEnabled ? { type: 'text/plain', 'data-usercentrics': 'Google Analytics' } : {}) } | ||
| async | ||
| src={ `https://www.googletagmanager.com/gtag/js?id=${ googleAnalyticsFeature.propertyId }` } | ||
| /> | ||
| { } | ||
| <script | ||
| { ...(usercentricsFeature.isEnabled ? { type: 'text/plain', 'data-usercentrics': 'Google Analytics' } : {}) } | ||
| dangerouslySetInnerHTML={{ __html: GA_INLINE_SCRIPT }} | ||
| /> | ||
| </> | ||
| ) } | ||
|
Comment on lines
+87
to
+100
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it necessary to include the Google Analytics script in the head tag instead of loading it after the page loads? I assume there will be two scripts loaded: one from here and another from the |
||
|
|
||
| </Head> | ||
| <body> | ||
| <Main/> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we provide the complete link to the script instead of just its origin to make the rule stricter?