Skip to main content
Version: 3.5.1

📦 plugin-google-gtag

默认的 全局网站代码 (gtag.js) 插件。它是一个 JavaScript 标记框架和 API,可让你将事件数据发送到 Google Analytics、Google Ads 和 Google Marketing Platform。本节介绍如何配置 Docusaurus 站点以启用 Google Analytics 的全局站点标记。

¥The default Global Site Tag (gtag.js) plugin. It is a JavaScript tagging framework and API that allows you to send event data to Google Analytics, Google Ads, and Google Marketing Platform. This section describes how to configure a Docusaurus site to enable global site tag for Google Analytics.

提示

你可以使用 谷歌的标签助手 工具检查你的 gtag 设置是否正确!

¥You can use Google's Tag Assistant tool to check if your gtag is set up correctly!

仅限生产

该插件在开发中始终处于不活动状态,仅在生产中处于活动状态,以避免污染分析统计数据。

¥This plugin is always inactive in development and only active in production to avoid polluting the analytics statistics.

安装

¥Installation

npm install --save @docusaurus/plugin-google-gtag
提示

如果你使用预设 @docusaurus/preset-classic,则无需安装此插件作为依赖。

¥If you use the preset @docusaurus/preset-classic, you don't need to install this plugin as a dependency.

你可以通过 预设选项.conf 配置该插件。

¥You can configure this plugin through the preset options.

配置

¥Configuration

接受的字段:

¥Accepted fields:

名称类型默认描述
trackingIDstring | string[]必需的你的 gtag 服务的跟踪 ID。可以提供多个 id。
anonymizeIPbooleanfalse发送请求时 IP 是否应该匿名。

配置示例

¥Example configuration

你可以通过预设选项或插件选项配置此插件。

¥You can configure this plugin through preset options or plugin options.

提示

大多数 Docusaurus 用户通过预设选项配置此插件。

¥Most Docusaurus users configure this plugin through the preset 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,
},
},
],
],
};