@exodus/segment-metrics

中文文档
5.1.1发布于 2026/10/14MITES Module近 30 天下载 4万

This library allows any app to send events to Segment for metrics analysis.

原始使用文档

segment-metrics

This library allows any app to send events to Segment for metrics analysis.

Formatting

Per convention of the Telemetry team, events should generally follow the following formatting structure:

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',
    },
  },
}

This library enforces the following:

  1. event names will automatically be PascalCased
  2. Shallow properties keys will automatically be snake_cased

Usage

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 共发布 34 个版本,首次发布于 2022/04/23;过去 12 个月发布了 2 个版本。

版本发布日期距今
5.1.1latest2026/10/141 个月前
5.1.02026/05/096 个月前
5.0.02025/05/241.5 年前
4.0.32024/11/022 年前
4.0.22024/08/222 年前
4.0.12024/08/202 年前

关于 @exodus/segment-metrics 的常见问题

@exodus/segment-metrics 是什么?

This library allows any app to send events to Segment for metrics analysis.

如何安装 @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 和提交记录判断。