【最新】在Next.js中引入Google Analytics4(谷歌分析)
warning:
这篇文章距离上次修改已过456天,其中的内容可能已经有所变动。
在Next.js中引入Google Analytics 4(GTM),你需要使用next-gtm这个npm包。以下是一个简单的步骤说明和代码示例:
- 安装
next-gtm包:
npm install next-gtm- 在
_app.js文件中引入next-gtm并使用它包装你的<App>:
// 在_app.js中
import { GTM } from 'next-gtm';
function MyApp({ Component, pageProps }) {
return (
<>
<GTM id="GTM-XXXXXXX" />
<Component {...pageProps} />
</>
);
}
export default MyApp;- 替换
"GTM-XXXXXXX"为你的Google Tag Manager容器ID。
这样就完成了Google Analytics 4在Next.js中的引入。记得在Google Analytics管理界面配置相应的测量ID和网站信息。
评论已关闭