Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push error events instead of pageloads on error pages #766

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading