跳转至主内容
Version: 2.0.0-beta.21 🚧

📦 plugin-google-gtag

默认的全局网站代码 (gtag.js) 插件。 It is a JavaScript tagging framework and API that allows you to send event data to Google Analytics, Google Ads, and Google Marketing Platform. 本章节介绍如何配置 Docusaurus 站点以为 Google 分析启用全局网站代码。

tip

你可以使用 Google's Tag Assistant 工具来检查你的 gtag 是否设置正常!

production only

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

安装

npm install --save @docusaurus/plugin-google-gtag
tip

若您已安装 @docusaurus/preset-classic,您则并不需要安装此依赖。

You can configure this plugin through the preset options.

配置

Accepted fields:

NameTypeDefaultDescription
trackingIDstringRequiredThe tracking ID of your gtag service.
anonymizeIPbooleanfalseWhether the IP should be anonymized when sending requests.

示例配置

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

tip

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-226F0LR9KE',
anonymizeIP: true,
},
},
],
],
};