diff --git a/pages/_app.tsx b/pages/_app.tsx index 808b0b4c3..9f7b01a53 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -10,7 +10,7 @@ import { AppProps } from 'next/app' import { NextPage } from 'next' config.autoAddCss = false -declare const window: Window & { adobeDataLayer: Record[] } +declare const window: Window & { adobeDataLayer?: Record[] } export type NextPageWithLayout

= NextPage & { getLayout?: (page: ReactElement) => ReactNode @@ -25,14 +25,14 @@ let appPreviousLocationPathname = '' function aaPushEvent(errType?: string) { if (errType !== undefined) { - window.adobeDataLayer.push({ + window.adobeDataLayer?.push({ event: 'error', error: { name: errType, }, }) } else { - window.adobeDataLayer.push({ event: 'pageLoad' }) + window.adobeDataLayer?.push({ event: 'pageLoad' }) } } @@ -45,10 +45,14 @@ export default function MyApp({ * @see https://github.com/vercel/next.js/blob/canary/examples/with-google-analytics * */ useEffect(() => { - // only push event if pathname is different - if (window.location.pathname !== appPreviousLocationPathname) { - aaPushEvent(pageProps.errType) - appPreviousLocationPathname = window.location.pathname + try { + // only push event if pathname is different + if (window.location.pathname !== appPreviousLocationPathname) { + aaPushEvent(pageProps.errType) + appPreviousLocationPathname = window.location.pathname + } + } catch { + // silently suppress all client side errors } }, [router.asPath, pageProps.errType])