Skip to content

Commit

Permalink
Merge pull request #827 from DTS-STN/fix/catch-errors
Browse files Browse the repository at this point in the history
Make adobeDataLayer optional and catch all errors in _app
  • Loading branch information
Charles-Pham authored Feb 18, 2025
2 parents 809b143 + 95e4145 commit 0c33244
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AppProps } from 'next/app'
import { NextPage } from 'next'
config.autoAddCss = false

declare const window: Window & { adobeDataLayer: Record<string, unknown>[] }
declare const window: Window & { adobeDataLayer?: Record<string, unknown>[] }

export type NextPageWithLayout<P = object, IP = P> = NextPage<P, IP> & {
getLayout?: (page: ReactElement) => ReactNode
Expand All @@ -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' })
}
}

Expand All @@ -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])

Expand Down

0 comments on commit 0c33244

Please sign in to comment.