@esri/telemetry-google-analytics

中文文档
3.1.2发布于 2026/10/23Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 1万

Google Analytics plugin for 'Telemetry.js' library

原始使用文档
文档目录9 节
  1. Table of Contents
  2. Installation
  3. How to use
  4. Browser
  5. Server
  6. Custom Metrics and/or Custom Dimensions (Optional)
  7. Issues
  8. Contributing
  9. License

@esri/telemetry-google-analytics

This is a plugin for the Telemetry.js package to send data to Google Analytics using Google Analytics 4.

Table of Contents

Installation

npm install @esri/telemetry
npm install @esri/telemetry-google-analytics

How to use

@esri/telemetry-google-analytics package works in the client-side browser and server-side Node.js. To use, install the package, include it in your project, create an instance of the plugin, and add it to the plugins array with @esri/telemetry

Below is an example of how to use the browser plugin.

Browser

import { Telemetry } from '@esri/telemetry'
import { GoogleAnalytics } from '@esri/telemetry-google-analytics'

const googleAnalyticsTracker = new GoogleAnalytics({
  measurementIds: [...], // required: fill in measurementIds
  metrics: {}, // optional
  dimensions: {} // optional
})

const telemetryOptions = {
  plugins: [googleAnalyticsTracker],
  portal: {
    subscriptionInfo: {},
    user: {},
  },
}

const telemetry = new Telemetry(telemetryOptions)
await telemetry.init();

telemetry.logPageView({page: 'home'}) // Required
telemetry.logEvent({
  action: 'Dataset', // Required, or will become 'other'
})


telemetry.logPageView({ page: 'home', event: {duration: 60, serviceQuery: 19 }})
telemetry.logEvent({ action: 'Dataset', duration: 4, datasetID: 'ID-XXXXXXX' })

Server

import { Telemetry } from '@esri/telemetry';
import { GoogleAnalytics } from '@esri/telemetry-google-analytics';

const googleAnalyticsTracker = new GoogleAnalytics({
  measurementIds: [...], // required: fill in measurementIds
  metrics: {}, // optional
  dimensions: {} // optional
});

const telemetryOptions = {
  plugins: [googleAnalyticsTracker],
  portal: {
    subscriptionInfo: {},
    user: {},
  },
};

const telemetry = new Telemetry(telemetryOptions);

const scriptTags = telemetry.getScriptTags();

After initializing Telemetry.js with the telemetry-google-analytics plugin, telemetry data will be sent to the Google Analytics 4 instance associated with the provided Measurement ID.

Custom Metrics and/or Custom Dimensions (Optional)

To use custom metrics or custom dimensions, you will need to pass in the metrics and dimensions when you create your instance of GoogleAnalytics. Each should be in the format of dimensionName: Index and metricName: Index, like so:

const googleAnalyticsTracker = new GoogleAnalytics({
  dimensions: {
    datasetID: 1,
    attribute: 2,
    serviceQuery: 3,
  },
  metrics: {
    duration: 1,
    size: 2,
  },
});

Later, when you call logPageView and logEvent and would like to refer to these custom metrics, you can pass them in within the event data in the format of metricName: metricAmount and dimensionName: dimensionValue, like so:

googleAnalyticsTracker.logPageView({page: 'homepage', event: {duration: 30, serviceQuery: 1}});

googleAnalyticsTracker.logEvent({ action: 'Dataset', duration: 20, datasetID: 220130});

If you need to disable tracking you can set disabled: true when initializing the Telemetry object. Then you can continue to call the methods on your instance of Telemetry without throwing exceptions or logging errors.

Post initialization, it is possible to disable & enable specific trackers using disableTracker and enableTracker methods.

telemetry.disableTracker('googleAnalytics');

telemetry.logPageView(); // no google analytics page view logged

telemetry.logEvent(); // no google analytics event logged

telemetry.enableTracker('googleAnalytics');

telemetry.logPageView(); // google analytics page view logged

telemetry.logEvent(); // google analytics event logged

Issues

If something isn't working, please take a look at previously logged issues first. Have you found a new bug? Create an issue here.

Contributing

Esri welcomes contributions from anyone and everyone. Please see our guidelines for contributing.

License

版本与发布记录

@esri/telemetry-google-analytics 共发布 29 个版本,首次发布于 2022/11/05;过去 12 个月发布了 1 个版本。

版本发布日期距今
3.1.2latest2026/10/2322 天前
3.1.12025/07/121.3 年前
3.1.02025/07/111.3 年前
3.0.02025/07/101.3 年前
2.0.22025/04/161.6 年前
2.0.12024/03/212 年前

其他发布通道:next → 1.1.4。

关于 @esri/telemetry-google-analytics 的常见问题

@esri/telemetry-google-analytics 是什么?

Google Analytics plugin for 'Telemetry.js' library

如何安装 @esri/telemetry-google-analytics?

在项目目录运行 npm install @esri/telemetry-google-analytics,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@esri/telemetry(^8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@esri/telemetry-google-analytics 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@esri/telemetry-google-analytics 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@esri/telemetry-google-analytics 可以商用吗?

@esri/telemetry-google-analytics 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@esri/telemetry-google-analytics 的下载量有多少?

根据 npm 官方下载统计,@esri/telemetry-google-analytics 在 2026/10/14 – 2026/11/12 这 30 天内被下载 10,458 次,日均约 349 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@esri/telemetry-google-analytics 还在维护吗?

最新版本 3.1.2 发布于 2026/10/23(22 天前);至今共发布 29 个版本,其中过去 12 个月发布了 1 个;首次发布于 2022/11/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。