-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
next/script - Can't get Google Analytics inline script to load beforeInteractive
- before close of <head> tag
#26591
Comments
beforeInteractive
- before close of <head> tagbeforeInteractive
- before close of <head> tag
@simon-olsen the |
@LetItRock thanks for the feedback. That all makes sense. I'll go with the inline script of GA at the end of the body tag. We're starting to use |
Might be related: #26240 |
@simon-olsen maybe can be better to initialize Google Analytics as a common javascript code: #26860 (comment) // gtag.ts
const initialize = () => {
window.dataLayer = window.dataLayer || [];
window.gtag = function gtag() { window.dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', GA_TRACKING_ID);
};
// app.tsx
export default function MyApp({ Component, pageProps }) {
useEffect(initialize, []); // this code will run only once
return <>
<Script src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`} />
<Component {...pageProps} />
</>
} |
Hi. Apologize for the delayed response. I'm working on adding another strategy to load inline scripts with beforeInteractive. I also wanted to know if there was a specific reason for loading GA before first party code? Usually we recommend loading GA with afterInteractive, but wanted to understand if that doesn't work for your usecase? |
All good @janicklas-ralph. That is the recommendation from GA. This is the instructions on their Tracking Code page in the Admin section... This is the Global Site Tag (gtag.js) tracking code for this property. Copy and paste this code as the first item into the of every webpage you want to track. If you already have a Global Site Tag on your page, simply add the config line from the snippet below to your existing Global Site Tag. |
Hi, I have added gtag script with strategy="afterInteractive" in _app.js and in one of my page i am tracking event on componentDidMount i am getting ReferenceError: gtag is not defined. may i know what is the correct way to solve this. |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What version of Next.js are you using?
11.0.1
What version of Node.js are you using?
10.23.0
What browser are you using?
Chrome, Firefox
What operating system are you using?
macOS
How are you deploying your application?
next start
Describe the Bug
I've just upgraded to Next.js v11.0.1 and trying out the <Script> component.
I'm trying to add the Google Analytics tag as they suggest; before the closing tag using
strategy="beforeInteractive"
.Here is my code for the
index.tsx
pageThe first <Script> with the
https://www.googletagmanager.com/gtag...
loads in the but the script doesn't load at all.The only way I can load it is at the bottom of the document using the default
afterInteractive
orlazyOnload
.Is this by design?
Should I just let the inline portion load before the ending body tag and be done with it? I'm only trying to follow what Google recommends in GA.
I've tried the following:
page.tsx
file and in_app.tsx
(docs say NOT to use_document.tsx
)dangerouslySetInnerHTML{{}}
and also adding the inline script to a custom function and loading it that way, aladangerouslySetInnerHTML={setGoogleTags(stationGA)}
and{window.dataLayer = window.dataLayer || [];...}
Expected Behavior
beforeInteractive
adding my inline script inside the tags.To Reproduce
Actual result
No inline script renders at all until you remove
strategy="beforeInteractive"
The text was updated successfully, but these errors were encountered: