📦 plugin-google-gtag
The default Global Site Tag (gtag.js) plugin. 它是一个 JavaScript 标签框架和 API,允许你将事件数据发送到 Google Analytics, Google Ads 和 Google Marketing Platform。 本章节会介绍如何配置 Docusaurus 站点以为 Google Analytics 启用全局网站标签。
提示
You can use Google's Tag Assistant tool to check if your gtag is set up correctly!
production only
This plugin is always inactive in development and only active in production to avoid polluting the analytics statistics.
Installation
- npm
- Yarn
- pnpm
npm install --save @docusaurus/plugin-google-gtag
yarn add @docusaurus/plugin-google-gtag
pnpm add @docusaurus/plugin-google-gtag
提示
If you use the preset @docusaurus/preset-classic
, you don't need to install this plugin as a dependency.
你可以通过预设选项来配置这个插件。
Configuration
接受的字段:
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
trackingID | string | string[] | Required | 你的 gtag 服务的 tracking ID。 It is possible to provide multiple ids. |
anonymizeIP | boolean | false | 发送请求时是否使用匿名 IP。 |
Example configuration
你可以通过预设选项或插件选项来配置 这个插件。
提示
大多数 Docusaurus 用户通过预设选项配置此插件。
- Preset options
- Plugin options
If you use a preset, configure this plugin through the preset options:
docusaurus.config.js
module.exports = {
presets: [
[
'@docusaurus/preset-classic',
{
gtag: {
trackingID: 'G-999X9XX9XX',
anonymizeIP: true,
},
},
],
],
};
If you are using a standalone plugin, provide options directly to the plugin:
docusaurus.config.js
module.exports = {
plugins: [
[
'@docusaurus/plugin-google-gtag',
{
trackingID: 'G-999X9XX9XX',
anonymizeIP: true,
},
],
],
};