Skip to content

Commit

Permalink
Merge pull request #766 from DTS-STN/aa-error-page
Browse files Browse the repository at this point in the history
Push error events instead of pageloads on error pages
  • Loading branch information
Charles-Pham authored Dec 3, 2024
2 parents 5a0400c + a449bd7 commit e83e1f5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 2 additions & 0 deletions pages/404.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const getStaticProps: GetStaticProps = async ({ locale }) => {
accessRights: '1',
},
}

return {
props: {
locale,
Expand All @@ -80,6 +81,7 @@ export const getStaticProps: GetStaticProps = async ({ locale }) => {
isAuth: process.env.AUTH_DISABLED ?? true,
aaPrefix: `ESDC-EDSC_MSCA-MSDC-SCH:404 Error`,
aaMenuPrefix: `ESDC-EDSC_MSCA-MSDC-SCH:Nav Menu`,
errType: '404',
},
}
}
Expand Down
1 change: 1 addition & 0 deletions pages/500.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const getStaticProps: GetStaticProps = async ({ locale }) => {
isAuth: process.env.AUTH_DISABLED ?? true,
aaPrefix: `ESDC-EDSC_MSCA-MSDC-SCH:500 Error`,
aaMenuPrefix: `ESDC-EDSC_MSCA-MSDC-SCH:Nav Menu`,
errType: '500',
},
}
}
Expand Down
23 changes: 10 additions & 13 deletions pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,19 @@ export default function MyApp({ Component, pageProps, router }) {
* @see https://github.com/vercel/next.js/blob/canary/examples/with-google-analytics
* */
useEffect(() => {
const handleRouteChange = () => {
// only push event if pathname is different
if (window.location.pathname !== appPreviousLocationPathname) {
// only push event if pathname is different
if (window.location.pathname !== appPreviousLocationPathname) {
if (pageProps.errType !== undefined) {
window.adobeDataLayer?.push?.({
event: 'error',
error: pageProps.errType,
})
} else {
window.adobeDataLayer?.push?.({ event: 'pageLoad' })
appPreviousLocationPathname = window.location.pathname
}
appPreviousLocationPathname = window.location.pathname
}

handleRouteChange()
router.events.on('routeChangeComplete', handleRouteChange)
router.events.on('hashChangeComplete', handleRouteChange)
return () => {
router.events.off('routeChangeComplete', handleRouteChange)
router.events.off('hashChangeComplete', handleRouteChange)
}
}, [router.events])
}, [router.asPath, pageProps.errType])

/* istanbul ignore next */
return (
Expand Down

0 comments on commit e83e1f5

Please sign in to comment.