[英]Google Analytics 4 does not work with Nextjs Script tag
I have built a site with Nextjs.我用 Nextjs 建立了一个网站。 I tried integrating GA to the site.
我尝试将 GA 集成到该站点。 I tried to implement with next/script tag but GA does not show reports, users, pageviews, etc. on the dash board.
我尝试使用 next/script 标签来实现,但 GA 不在仪表板上显示报告、用户、网页浏览量等。 But if I use normal
script
tag then it works.但是,如果我使用普通的
script
标签,那么它就可以工作。
Why Google Analytics is not working with next/script
tag.为什么 Google Analytics 不能使用
next/script
标签。
GA Code Snippet with next/script
[not working]带有
next/script
的 GA 代码片段 [不工作]
<Script
src={`https://www.googletagmanager.com/gtag/js?id=${process.env.GA_MEASUREMENT_ID}`}
strategy="afterInteractive"
/>
<Script id="google-analytics-script" strategy="afterInteractive">
{`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${process.env.GA_MEASUREMENT_ID}');
`}
</Script>
Note : I have also tried setting ga code inside dangerouslySetInnerHTML
with next/script
but it still does not work.注意:我也尝试使用
next/script
在dangerouslySetInnerHTML
中设置 ga 代码,但它仍然不起作用。
GA code snippet with regular script
tag [working]带有正则
script
标签的 GA 代码片段 [working]
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${process.env.GA_MEASUREMENT_ID}`}
/>
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${process.env.GA_MEASUREMENT_ID}', { 'send_page_view': true });
`,
}}
/>
Now my simple question is that why google analytics is not working with next/script?现在我的简单问题是,为什么 google analytics 不能使用 next/script?
I faced the same problem.我遇到了同样的问题。 Upgrade to next 12.2.0 solved it.
升级到下一个 12.2.0 解决了它。
I also found this question, but it doesn't work even when the script was moved out of Head component.我也发现了这个问题,但是即使将脚本移出 Head 组件,它也不起作用。 Next 11 and adding Script tags not working.
Next 11 和添加脚本标签不起作用。 No scripts are rendered
没有脚本被渲染
The Next.js Script tag cannot be in the head, it needs to be outside it. Next.js Script标签不能在头部,需要在头部之外。 See this answer: https://stackoverflow.com/a/68059559/2031033
看到这个答案: https://stackoverflow.com/a/68059559/2031033
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.