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
Simplify with-google-analytics example #43894
Simplify with-google-analytics example #43894
Conversation
FYI to maintainers, there's a PR open that's trying to add more to the _document.js. So it can be closed if this one is approved: #43838 |
@Josehower the reason for adding in _document.js was it resolved #40410, context below. If there have been updates to the framework since then that render this unnecessary then shouldn't be an issue to remove _document.js.
|
Batching with #43897 as recommended by #43897 (comment) |
@maxproske @Josehower I just checked #43838 and I belive that PR is valid. Google Analytics does require a very lightweight inline script in the |
Ok so if
If this is acceptable, the changes from #43838 could be also merged into this PR (or #43838 merged first, and then this PR rebased on that) |
Agree. @Josehower Would you mind changing your PR to only remove |
There would still be another change required - removing the duplicate code that is mentioned in the description of this PR: next.js/examples/with-google-analytics/pages/_app.js Lines 30 to 34 in 9d97a1e
next.js/examples/with-google-analytics/pages/_document.js Lines 13 to 17 in 9d97a1e
|
Actually, I think the proposal by @Josehower was that all Google Analytics content be moved from Then the next.js/examples/with-google-analytics/pages/_app.js Lines 7 to 18 in 9d97a1e
next.js/examples/with-google-analytics/pages/_app.js Lines 22 to 41 in 9d97a1e
|
Also, one more thing, there is another change in this PR aside from the So this change to |
I manage to solve the issue with WebVitals reported on #40410 without the need of _document.js by adding the reproduction example of the fix: https://stackblitz.com/edit/nextjs-jyzaxg?file=pages/_app.js This fix is also compliant with the suggestion done by @SukkaW on #43838 (review) to summarize the changes proposed on this PR.
EDIT: adding more context to change number 3 |
// Disable any default-enabled advertising features (turn them on later when we get consent) | ||
gtag('set', 'allow_google_signals', false); |
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.
IMHO we shouldn't add this in the PR:
- It is not required to set up Google Analytics
- Anyone who follows Google Analytics' instruction can add this easily
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.
This is not part of the PR anymore. ill update the description, sorry for that
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.
https://github.com/vercel/next.js/pull/43894/files
maybe you are looking to an old version of the PR
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.
This is the commit that removes this code 4 days ago
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.
Thanks!
<!-- 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. https://github.com/vercel/next.js/blob/9d97a1e34a8a6e09eb127292c730d1a8df63ebb6/examples/with-google-analytics/pages/_app.js#L30-L34 https://github.com/vercel/next.js/blob/9d97a1e34a8a6e09eb127292c730d1a8df63ebb6/examples/with-google-analytics/pages/_document.js#L13-L17 I am aware of vercel#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 vercel#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 vercel#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 vercel#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 vercel#43897 as recommended by vercel#43897 (comment) --------- Co-authored-by: JJ Kasper <jj@jjsweb.site>
Documentation / Examples
pnpm build && pnpm lint
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.next.js/examples/with-google-analytics/pages/_app.js
Lines 30 to 34 in 9d97a1e
next.js/examples/with-google-analytics/pages/_document.js
Lines 13 to 17 in 9d97a1e
I am aware of #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.
Changes that come from #43897
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 #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.
id="gtag-init"
was added with no context to the example from Add id to inline gtag script #29530If 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 #43897 as recommended by #43897 (comment)