# @exodus/segment-metrics 中文文档

规范地址：https://npmcn.com/package/%40exodus/segment-metrics/

版本：5\.1\.1
npm 元数据更新时间：2026-10-13T17:14:26.077Z
中文译文更新时间：2026-11-14T03:58:48.025Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40exodus/segment-metrics
原文对照：https://npmcn.com/package/%40exodus/segment-metrics/?lang=original
项目主页：https://github.com/ExodusMovement/exodus/tree/master/libs/libraries/segment-metrics
源码仓库：https://github.com/ExodusMovement/exodus

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：21.4 kB，6 个文件
- 模块格式：ES Module
- TypeScript 类型：未声明
- 运行依赖：4 个
- 首次发布：2022-04-22T21:35:02.259Z
- 最新版本发布：2026-10-13T17:14:25.891Z
- 近 30 天下载量：39675（2026-10-14 至 2026-11-12，npm 官方下载统计）
- 已发布版本数：34（近 12 个月 2 个）

## 中文简介

这个库允许任何应用程序将事件发送到分节进行度量分析.

## 安装

```sh
npm install @exodus/segment-metrics
```

也可以使用：`pnpm add @exodus/segment-metrics`、`yarn add @exodus/segment-metrics`、`bun add @exodus/segment-metrics`

## README

# 线段测量

这个库允许任何应用程序将事件发送到分节进行度量分析.

## 格式化

根据遥测小组的惯例,事件一般应遵循下列格式结构:

```js
const event = {
  event: 'EventInPascalCase',
  anonymousId: SOME_DERIVED_ID_FOR_THE_USER,
  properties: {
    property_keys_in_snake_case: 'property-values-in-kebab-case',

    // Generally try not to nest properties
    nested_properties: {
      are_generally: 'discouraged',
    },
  },
}
```

此库执行如下:

1.  `event` 名称将自动为 `PascalCased`
2.  浅 `properties` 键将自动为 `snake_cased`

## 使用量

```js
import Tracker from '@exodus/segment-metrics'

const segment = new Tracker({
  writeKey: SEGMENT_WRITE_KEY,
  apiBaseUrl: 'https://api.segment.io/v1/',
  fetch: globalThis.fetch,
  // optional
  validateEvent: ({ event, properties }) => {
    if (event === 'abc' && properties.someProperty === '123') {
      throw new Error('absolutely not, 123 is private user data')
    }
  },
  logger: console,
})

// optionally add/remove default properties that will get sent with every event
segment.setDefaultProperty('anonymousId', SOME_DERIVED_ID_FOR_THE_USER)
segment.setDefaultProperties({}) // pass in an object of all default properties to set
segment.removeDefaultProperty('anonymousId')
segment.removeAllDefaultProperties()

// if you don't know the user's ID yet, e.g. during onboarding, set a temporary anonymousId
segment.setAnonymousId(id) // defaults to UUIDv4 if not set
// when you know the user's ID, link it to the anonymousId to merge their events
segment.identify({
  userId,
  traits: { telemetryId: userId },
})

// set other persistent user traits for the user
segment.identify({
  traits: {
    dorkLevel: 'DavidCabal',
  },
})

/*
  note that by leveraging the default properties above, the only things actually needed below
  could be narrowed down to the "event" key (and potentially "properties" key if you're sending extra metadata)
*/
segment.track({
  event: 'EventName',
  properties: {
    whateverItIs: 'that-you-want-to-track',
    // add as many fields as you like here
  },
  sensitive: false, // OPTIONAL setting to true will override the anonymousId with a random uuid for each event sent
  timestamp: Date, // OPTIONAL passing event Date value. useful to track events retroactively
  exactTimestamp: true, // OPTIONAL. By default, event timestamps are truncted to the beginning of the UTC day
})
```

## 常见问题

### @exodus/segment-metrics 是什么？

这个库允许任何应用程序将事件发送到分节进行度量分析\.

### 如何安装 @exodus/segment-metrics？

在项目目录运行 npm install @exodus/segment-metrics，或使用 pnpm add / yarn add / bun add。

### @exodus/segment-metrics 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/exodus\_\_segment-metrics 是否存在。

### @exodus/segment-metrics 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @exodus/segment-metrics 可以商用吗？

@exodus/segment-metrics 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @exodus/segment-metrics 的下载量有多少？

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

### @exodus/segment-metrics 还在维护吗？

最新版本 5\.1\.1 发布于 2026/10/14（1 个月前）；至今共发布 34 个版本，其中过去 12 个月发布了 2 个；首次发布于 2022/04/23。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

