📦 plugin-google-analytics
The default Google Analytics plugin. It is a JavaScript library for measuring how users interact with your website in the production build. If you are using Google Analytics 4 you might need to consider using plugin-google-gtag instead.
This plugin is deprecated and became useless on July 1, 2023.
Google is moving away from Universal Analytics.
If you are still using this plugin with a UA-* tracking id, you should create a Google Analytics 4 account as soon as possible, and use @docusaurus/plugin-google-gtag instead of this plugin. More details here.
This plugin is always inactive in development and only active in production to avoid polluting the analytics statistics.
Installation
- npm
- Yarn
- pnpm
- Bun
npm install --save @docusaurus/plugin-google-analytics
yarn add @docusaurus/plugin-google-analytics
pnpm add @docusaurus/plugin-google-analytics
bun add @docusaurus/plugin-google-analytics
If you use the preset @docusaurus/preset-classic, you don't need to install this plugin as a dependency.
You can configure this plugin through the preset options.
Configuration
接受的字段:
| 参数 | 类型 | 默认值 | 描述 | 
|---|---|---|---|
| trackingID | string | Required | 你的分析服务的 tracking ID。 | 
| anonymizeIP | boolean | false | 发送请求时是否使用匿名 IP。 | 
Example configuration
你可以通过预设选项或插件选项来配置这个插件。
大多数 Docusaurus 用户通过预设选项配置此插件。
- 预设选项
- 插件选项
如果你使用预设,你可以通过预设选项配置这个插件:
module.exports = {
  presets: [
    [
      '@docusaurus/preset-classic',
      {
        googleAnalytics: {
          trackingID: 'UA-141789564-1',
          anonymizeIP: true,
        },
      },
    ],
  ],
};
如果你用的是独立插件,直接向插件提供选项:
module.exports = {
  plugins: [
    [
      '@docusaurus/plugin-google-analytics',
      {
        trackingID: 'UA-141789564-1',
        anonymizeIP: true,
      },
    ],
  ],
};