-
Notifications
You must be signed in to change notification settings - Fork 28.2k
Move Google Analytics script to the <Head/> #43838
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
Conversation
Google cannot read the Google Analytics script if it is placed at the bottom of the <body />. It can only be read when it's placed here in the <Head /> This has been documented by other NextJS users online, and by Google themselves, which directs users to place the script in the head. Hope this helps NextJS users!
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.
I believe the change is valid per Google Analytics support article.
Since the inline script tag is very lightweight (it only defines an array (window.dataLayer
) and a function gtag()
), this shouldn't impact Web Vitals. The main Google Tag Manager script is still loaded via next/script
component.
But in this case, the documentation is suggested to load both scripts on HEAD. After some tests, Google recognizes the tag even when both scripts are at the end of the body. @SukkaW Do you think this usage of script inside of HEAD is absolutely necessary? |
Fixes #[37957](#37957) Google cannot read the Google Analytics script if it is placed at the bottom of the `<body />`. It can only be read when it's placed here in the `<Head />` This has been documented by other NextJS users online, and by Google themselves, which directs users to place the script in the head. Hope this helps NextJS users! ## Bug - [x] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
Hey guys this change duplicates the code into Head and before end of Body. The code is now present in both _app and _document. |
There is a open PR that solves this #43894 |
Fixes #37957
Google cannot read the Google Analytics script if it is placed at the bottom of the
<body />
.It can only be read when it's placed here in the
<Head />
This has been documented by other NextJS users online, and by Google themselves, which directs users to place the script in the head.
Hope this helps NextJS users!
Bug
fixes #number
contributing.md
Feature
fixes #number
contributing.md
Documentation / Examples
pnpm build && pnpm lint