Simplify with-google-analytics example (#43894)
<!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change that you're making: --> ## Documentation / Examples - [x] Make sure the linting passes by running `pnpm build && pnpm lint` - [x] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) First of all thanks for this amazing project and all the help you provide with these examples. It seems there is code duplication in this example. After some tests locally seem to _document.js is not necessary for `gtag` to work properly.9d97a1e34a/examples/with-google-analytics/pages/_app.js (L30-L34)
9d97a1e34a/examples/with-google-analytics/pages/_document.js (L13-L17)
I am aware of https://github.com/vercel/next.js/pull/40645 and I would like to ask @dave-hay, @SukkaW and @ijjk to consider this is still necessary. If so why then not move all content of the scripts from _app to _document? In any case, I am open to adding here some comments explaining what is the reason for this code duplication if necessary. <hr/> Changes that come from https://github.com/vercel/next.js/pull/43897 1. The `event` hashChangeComplete should be removed since `/home` and `/home/#section` is not new pageview, but just reference to the same page. If we go from /home to /home/#section (with a button click or a link for example) this shouldn't trigger a new page visit on `gtag`. For this reason, I think we should revert the changes from https://github.com/vercel/next.js/pull/36079. If there is a better argument of why this should stay I am also open to creating comments to clarify this on the example since I don't think should be the default behavior and not useful in most cases. 2. The `id="gtag-init"` was added with no context to the example from https://github.com/vercel/next.js/pull/29530 If there is a reason for this id in the script to existing I am open to adding a comment that clarifies this since in my experience is not necessary at all. Edit: Batching with https://github.com/vercel/next.js/pull/43897 as recommended by https://github.com/vercel/next.js/pull/43897#issuecomment-1344635000 --------- Co-authored-by: JJ Kasper <jj@jjsweb.site>
This commit is contained in:
parent
63d7705f87
commit
268dd6e80b
2 changed files with 18 additions and 44 deletions
|
@ -1,6 +1,7 @@
|
|||
import { useEffect } from 'react'
|
||||
import Script from 'next/script'
|
||||
import Head from 'next/head'
|
||||
import { useRouter } from 'next/router'
|
||||
import Script from 'next/script'
|
||||
import { useEffect } from 'react'
|
||||
import * as gtag from '../lib/gtag'
|
||||
|
||||
const App = ({ Component, pageProps }) => {
|
||||
|
@ -10,35 +11,33 @@ const App = ({ Component, pageProps }) => {
|
|||
gtag.pageview(url)
|
||||
}
|
||||
router.events.on('routeChangeComplete', handleRouteChange)
|
||||
router.events.on('hashChangeComplete', handleRouteChange)
|
||||
return () => {
|
||||
router.events.off('routeChangeComplete', handleRouteChange)
|
||||
router.events.off('hashChangeComplete', handleRouteChange)
|
||||
}
|
||||
}, [router.events])
|
||||
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
<script
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: `
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
function gtag(){dataLayer.push(arguments);}
|
||||
gtag('js', new Date());
|
||||
|
||||
gtag('config', '${gtag.GA_TRACKING_ID}', {
|
||||
page_path: window.location.pathname,
|
||||
});
|
||||
`,
|
||||
}}
|
||||
/>
|
||||
</Head>
|
||||
{/* Global Site Tag (gtag.js) - Google Analytics */}
|
||||
<Script
|
||||
strategy="afterInteractive"
|
||||
src={`https://www.googletagmanager.com/gtag/js?id=${gtag.GA_TRACKING_ID}`}
|
||||
/>
|
||||
<Script
|
||||
id="gtag-init"
|
||||
strategy="afterInteractive"
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: `
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
function gtag(){dataLayer.push(arguments);}
|
||||
gtag('js', new Date());
|
||||
|
||||
gtag('config', '${gtag.GA_TRACKING_ID}', {
|
||||
page_path: window.location.pathname,
|
||||
});
|
||||
`,
|
||||
}}
|
||||
/>
|
||||
<Component {...pageProps} />
|
||||
</>
|
||||
)
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
import { Html, Head, Main, NextScript } from 'next/document'
|
||||
|
||||
export default function Document() {
|
||||
return (
|
||||
<Html>
|
||||
<Head>
|
||||
{/* Global Site Tag (gtag.js) - Google Analytics */}
|
||||
<script
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: `
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
function gtag(){dataLayer.push(arguments);}
|
||||
gtag('js', new Date());
|
||||
|
||||
`,
|
||||
}}
|
||||
/>
|
||||
</Head>
|
||||
<body>
|
||||
<Main />
|
||||
<NextScript />
|
||||
</body>
|
||||
</Html>
|
||||
)
|
||||
}
|
Loading…
Reference in a new issue