跳至主要內容

google-analytics

小于 1 分钟

google-analytics

@vuepress/plugin-google-analytics

Google Analyticsopen in new window 集成到 VuePress 中。

该插件会通过引入 gtag.jsopen in new window 来启用 Google Analytics 4open in new window

使用方法

npm i -D @vuepress/plugin-google-analytics@next
import { googleAnalyticsPlugin } from "@vuepress/plugin-google-analytics";

export default {
  plugins: [
    googleAnalyticsPlugin({
      // 配置项
    }),
  ],
};

上报事件

Google Analytics 会 自动收集部分事件open in new window ,比如 page_view, first_visit 等。

因此,如果你只是想收集站点的一些基础数据,你只需要正确设置 Measurement ID ,不需要再额外做其他事情。

在引入该插件之后,一个全局的 gtag() 函数会被挂载到 window 对象上,你可以使用它进行 自定义事件的上报open in new window

配置项

id

  • 类型: string

  • 详情:

    Google Analytics 4 的 Measurement ID ,应以 'G-' 开头。

    你可以通过 这里open in new window 的指引来找到你的 Measurement ID 。注意区分 Google Analytics 4 的 Measurement ID (即 "G-" 开头的 ID) 和 Universal Analytics 的 Tracking ID (即 "UA-" 开头的 ID)。

  • 示例:

export default {
  plugins: [
    googleAnalyticsPlugin({
      id: "G-XXXXXXXXXX",
    }),
  ],
};

debug

export default {
  plugins: [
    googleAnalyticsPlugin({
      id: "G-XXXXXXXXXX",
      debug: true,
    }),
  ],
};