55import type { Analytics , Integrations , Plugin , UserOptions } from '@segment/analytics-next' ;
66import { AnalyticsBrowser } from '@segment/analytics-next' ;
77import type { CookieOptions } from '@segment/analytics-next/dist/types/core/storage' ;
8- import { usePathname } from 'next/navigation' ;
98import Script from 'next/script' ;
109import PlausibleProvider from 'next-plausible' ;
1110import type { PropsWithChildren } from 'react' ;
1211import { createContext , useContext , useEffect , useState } from 'react' ;
1312
14- import {
15- getConsentCookie ,
16- getOnetrustCookieConsentStatus ,
17- isTrackingCookieAllowed ,
18- setConsentCookie ,
19- } from './lib' ;
13+ import { isTrackingCookieAllowed , setConsentCookie } from './lib' ;
2014import { normalizePrezlyMetaPlugin , sendEventToPrezlyPlugin } from './plugins' ;
2115import { TrackingPolicy } from './types' ;
2216import type {
@@ -64,8 +58,6 @@ interface Props {
6458 ignoreConsent ?: boolean ;
6559}
6660
67- const ONETRUST_INTEGRATION_EVENT = 'OnetrustConsentModalCallback' ;
68-
6961export const AnalyticsContext = createContext < Context | undefined > ( undefined ) ;
7062
7163export function useAnalyticsContext ( ) {
@@ -136,19 +128,7 @@ export function AnalyticsProvider({
136128 uuid,
137129 } = newsroom || { } ;
138130
139- const isOnetrustIntegrationEnabled = newsroom ?. onetrust_cookie_consent . is_enabled ?? false ;
140- const onetrustCookieCategory = newsroom ?. onetrust_cookie_consent ?. category ?? '' ;
141- const onetrustIntegrationScript = newsroom ?. onetrust_cookie_consent ?. script ?? '' ;
142-
143- const [ consent , setConsent ] = useState < boolean | null > ( ( ) => {
144- if ( ignoreConsent ) {
145- return true ;
146- }
147- if ( isOnetrustIntegrationEnabled ) {
148- return getOnetrustCookieConsentStatus ( onetrustCookieCategory ) ;
149- }
150- return getConsentCookie ( ) ;
151- } ) ;
131+ const [ consent , setConsent ] = useState < boolean | null > ( null ) ;
152132 const [ analytics , setAnalytics ] = useState < Analytics | undefined > ( undefined ) ;
153133
154134 useEffect ( ( ) => {
@@ -221,27 +201,10 @@ export function AnalyticsProvider({
221201 ] ) ;
222202
223203 useEffect ( ( ) => {
224- if ( ! ignoreConsent && typeof consent === 'boolean' && ! isOnetrustIntegrationEnabled ) {
204+ if ( ! ignoreConsent && typeof consent === 'boolean' ) {
225205 setConsentCookie ( consent ) ;
226206 }
227- } , [ consent , ignoreConsent , isOnetrustIntegrationEnabled ] ) ;
228-
229- useEffect ( ( ) => {
230- if ( ! isOnetrustIntegrationEnabled || ! onetrustCookieCategory ) {
231- // Only execute the effect if the OneTrust integration is enabled.
232- return noop ;
233- }
234-
235- function handleEvent ( ) {
236- setConsent ( getOnetrustCookieConsentStatus ( onetrustCookieCategory ) ) ;
237- }
238-
239- document . body . addEventListener ( ONETRUST_INTEGRATION_EVENT , handleEvent ) ;
240-
241- return ( ) => {
242- document . body . removeEventListener ( ONETRUST_INTEGRATION_EVENT , handleEvent ) ;
243- } ;
244- } , [ isOnetrustIntegrationEnabled , onetrustCookieCategory ] ) ;
207+ } , [ consent , ignoreConsent ] ) ;
245208
246209 return (
247210 < AnalyticsContext . Provider
@@ -257,9 +220,6 @@ export function AnalyticsProvider({
257220 trackingPolicy,
258221 } }
259222 >
260- { isOnetrustIntegrationEnabled && onetrustIntegrationScript && (
261- < OnetrustCookieIntegration script = { onetrustIntegrationScript } />
262- ) }
263223 < GoogleAnalyticsIntegration analyticsId = { newsroom ?. google_analytics_id ?? null } />
264224 < PlausibleProviderMaybe
265225 isEnabled = { isEnabled || isPlausibleEnabled }
@@ -272,44 +232,6 @@ export function AnalyticsProvider({
272232 ) ;
273233}
274234
275- function OnetrustCookieIntegration ( props : { script : string } ) {
276- const path = usePathname ( ) ;
277-
278- /*
279- * @see https://my.onetrust.com/s/article/UUID-69162cb7-c4a2-ac70-39a1-ca69c9340046?language=en_US#UUID-69162cb7-c4a2-ac70-39a1-ca69c9340046_section-idm46212287146848
280- */
281- useEffect ( ( ) => {
282- document . getElementById ( 'onetrust-consent-sdk' ) ?. remove ( ) ;
283-
284- if ( window . OneTrust ) {
285- window . OneTrust . Init ( ) ;
286-
287- setTimeout ( ( ) => {
288- window . OneTrust ?. LoadBanner ( ) ;
289- } , 1000 ) ;
290- }
291- } , [ path ] ) ;
292-
293- return (
294- < div
295- id = "onetrust-cookie-consent-integration"
296- dangerouslySetInnerHTML = { {
297- __html : `
298- ${ props . script }
299- <script>
300- window.OptanonWrapper = (function () {
301- const prev = window.OptanonWrapper || function() {};
302- return function() {
303- prev();
304- document.body.dispatchEvent(new Event("${ ONETRUST_INTEGRATION_EVENT } ")); // allow listening to the OptanonWrapper callback from anywhere.
305- };
306- })();
307- </script>` ,
308- } }
309- />
310- ) ;
311- }
312-
313235function GoogleAnalyticsIntegration ( props : { analyticsId : string | null } ) {
314236 if ( props . analyticsId ?. startsWith ( 'GTM-' ) ) {
315237 return < GoogleTagManager analyticsId = { props . analyticsId as `GTM-${string } `} /> ;
@@ -366,7 +288,3 @@ function GoogleAnalytics(props: { analyticsId: string }) {
366288 </ >
367289 ) ;
368290}
369-
370- function noop ( ) {
371- // nothing
372- }
0 commit comments