diff --git a/web/lib/hooks/useConsentState.ts b/web/lib/hooks/useConsentState.ts index 9d7345212..5e855c614 100644 --- a/web/lib/hooks/useConsentState.ts +++ b/web/lib/hooks/useConsentState.ts @@ -1,8 +1,8 @@ import { useEffect, useState } from 'react' import { useRouter } from 'next/router' -import { Flags } from '../../common/helpers/datasetHelpers' import { CookieType } from '../../types' import { checkCookieConsent } from '../../common/helpers/checkCookieConsent' +import { useProd } from './useProd' //COOKIEBOT declare global { @@ -15,6 +15,7 @@ declare global { function useConsentState(consentType: CookieType[], callback: () => void, cleanup?: () => void) { const [consent, changeConsent] = useState(false) const router = useRouter() + const enableConsentLogic = useProd() useEffect(() => { const manageCookies = () => { @@ -30,15 +31,12 @@ function useConsentState(consentType: CookieType[], callback: () => void, cleanu useEffect(() => { // Disable Radix.equinor.com due to SiteImprove (possibly) collecting wrong data - const host = window?.location.origin - const isLocalHost = host.includes('localhost') - const enableConsentLogic = !isLocalHost && (Flags.IS_DEV || !host.includes('radix.equinor.com')) if (enableConsentLogic && consent) { callback() return () => { if (cleanup) cleanup() } } - }, [router.asPath, consent, callback, cleanup]) + }, [router.asPath, consent, callback, cleanup, enableConsentLogic]) } export default useConsentState diff --git a/web/lib/hooks/useProd.ts b/web/lib/hooks/useProd.ts new file mode 100644 index 000000000..302b08300 --- /dev/null +++ b/web/lib/hooks/useProd.ts @@ -0,0 +1,14 @@ +import { Flags } from '../../common/helpers/datasetHelpers' +import { useEffect, useState } from 'react' + +export function useProd() { + const [isProd, setIsProd] = useState(false) + + useEffect(() => { + const host = window?.location.origin + const isLocalHost = host.includes('localhost') + setIsProd(!isLocalHost && (Flags.IS_DEV || !host.includes('radix.equinor.com'))) + }, []) + + return isProd +} diff --git a/web/pageComponents/SiteImprove.ts b/web/pageComponents/SiteImprove.ts deleted file mode 100644 index 9b3839257..000000000 --- a/web/pageComponents/SiteImprove.ts +++ /dev/null @@ -1,11 +0,0 @@ -export const loadSiteImproveScript = () => { - const script = document.createElement('script') - script.src = 'https://siteimproveanalytics.com/js/siteanalyze_6003171.js' - script.id = 'siteimprove' - script.async = true - document.head.appendChild(script) -} - -export const cleanUpSiteImproveScript = () => { - document.getElementById('siteimprove')?.remove() -} diff --git a/web/pages/_app.tsx b/web/pages/_app.tsx index 249ff4203..99e2859cf 100644 --- a/web/pages/_app.tsx +++ b/web/pages/_app.tsx @@ -11,7 +11,6 @@ import Script from 'next/script' import { ErrorBoundary } from 'react-error-boundary' import { ErrorFallback } from '../pageComponents/pageTemplates/ErrorFallback' import useConsentState from '../lib/hooks/useConsentState' -import { loadSiteImproveScript, cleanUpSiteImproveScript } from '../pageComponents/SiteImprove' import { enableDynatrace, disableDynatrace } from '../pageComponents/Dynatrace' import { SWRConfig } from 'swr' @@ -21,6 +20,7 @@ import { SWRConfig } from 'swr' import { PreviewContextProvider } from '../lib/contexts/PreviewContext' import { defaultLanguage } from '../languages' import { default as NextLink } from 'next/link' +import { useProd } from '../lib/hooks/useProd' /** * TODO: @@ -69,6 +69,7 @@ function MyApp({ Component, pageProps }: CustomAppProps): JSX.Element { const router = useRouter() const getLayout = Component.getLayout || ((page: ReactNode): ReactNode => page) const IS_LIVE = process.env.NODE_ENV !== 'development' + const isProd = useProd() useEffect(() => { if (!GTM_ID) return @@ -92,12 +93,10 @@ function MyApp({ Component, pageProps }: CustomAppProps): JSX.Element { }, [router.asPath]) const enableStatisticsCookies = () => { - loadSiteImproveScript() enableDynatrace() } const disableStatisticsCookies = () => { - cleanUpSiteImproveScript() disableDynatrace() } @@ -126,6 +125,7 @@ function MyApp({ Component, pageProps }: CustomAppProps): JSX.Element { + {isProd &&