-
-
Notifications
You must be signed in to change notification settings - Fork 341
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
Add Google Analytics #1049
Add Google Analytics #1049
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,46 @@ | ||
import * as React from 'react'; | ||
import Head from 'next/head'; | ||
import App, { AppContext, AppProps } from 'next/app'; | ||
import App, { AppContext, AppProps, NextWebVitalsMetric } from 'next/app'; | ||
import { ThemeProvider } from '@mui/material/styles'; | ||
import CssBaseline from '@mui/material/CssBaseline'; | ||
import { CacheProvider, EmotionCache } from '@emotion/react'; | ||
import { QueryClientProvider } from '@tanstack/react-query'; | ||
import { LicenseInfo } from '@mui/x-data-grid-pro'; | ||
import { useRouter } from 'next/router'; | ||
import theme from '../src/theme'; | ||
import createEmotionCache from '../src/createEmotionCache'; | ||
import { MUI_X_PRO_LICENSE } from '../src/constants'; | ||
import { queryClient } from '../src/api'; | ||
import { reportWebVitalsToGA, setGAPage } from '../src/utils/ga'; | ||
|
||
LicenseInfo.setLicenseKey(MUI_X_PRO_LICENSE); | ||
|
||
// Client-side cache, shared for the whole session of the user in the browser. | ||
const clientSideEmotionCache = createEmotionCache(); | ||
|
||
export const reportWebVitals = (metric: NextWebVitalsMetric): void => { | ||
reportWebVitalsToGA(metric); | ||
}; | ||
|
||
interface MyAppProps extends AppProps { | ||
emotionCache?: EmotionCache; | ||
} | ||
|
||
export default function MyApp(props: MyAppProps) { | ||
const { Component, emotionCache = clientSideEmotionCache, pageProps } = props; | ||
|
||
const router = useRouter(); | ||
|
||
React.useEffect(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took it from this example https://github.com/vercel/next.js/tree/canary/examples/with-google-analytics, just rechecked again and as far as I can tell:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems to run for the iframe routes too, not sure if that's helpful There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might make sense to disable for the app canvas There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok actually it turns out this effect is doing nothing at all... the page view events are being sent automatically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and because the page view events are sent automatically doesn't seem simple to filter out the ones from the canvas... - we can just ignore that data in the google analytics dashboard itself if we want to use page view data, im gonna make a PR that just removes the effect |
||
router.events.on('routeChangeComplete', setGAPage); | ||
router.events.on('hashChangeComplete', setGAPage); | ||
|
||
return () => { | ||
router.events.off('routeChangeComplete', setGAPage); | ||
router.events.off('hashChangeComplete', setGAPage); | ||
}; | ||
}, [router.events]); | ||
|
||
return ( | ||
<CacheProvider value={emotionCache}> | ||
<Head> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { NextWebVitalsMetric } from 'next/app'; | ||
import config from '../config'; | ||
|
||
export const GA_ID = config.gaId; | ||
|
||
export const setGAPage = (pagePath: string): void => { | ||
if (GA_ID) { | ||
window.gtag('config', GA_ID, { page_path: pagePath }); | ||
} | ||
}; | ||
|
||
export const reportWebVitalsToGA = ({ id, label, name, value }: NextWebVitalsMetric): void => { | ||
if (GA_ID) { | ||
window.gtag('event', name, { | ||
event_category: label === 'web-vital' ? 'Web Vitals' : 'Next.js custom metric', | ||
value: Math.round(name === 'CLS' ? value * 1000 : value), // values must be integers | ||
event_label: id, // id unique to current page load | ||
non_interaction: true, // avoids affecting bounce rate. | ||
}); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@apedroferreira Any idea on how this passed the
prettier
CI check up until now?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same with
docs/next.config.js
. It gets updated when I runyarn prettier:all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really, maybe it's not running for files with the
.mjs
extension?i probably just saved the file and prettier added this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
weird... if i run it locally this file always gets fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did
prettier
get updated recently? CI only checks for changed files I believe. Perhaps we should runprettier:all
in CI?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, i merged this from renovatebot 735011e
that was probably it