在 Nuxt 3.13 使用 Google Analytics 分析工具

做法

安裝 Nuxt Scripts 模組。

1
npx nuxi@latest module add scripts

修改 .env 檔。

1
NUXT_PUBLIC_GOOGLE_TAG_ID=your-id

修改 nuxt.config.ts 檔。

1
2
3
4
5
6
7
8
9
export default defineNuxtConfig({
scripts: {
registry: {
googleAnalytics: {
id: process.env.NUXT_PUBLIC_GOOGLE_TAG_ID as string,
},
},
},
});

參考資料

Nuxt Scripts