@fluidattacks/tracks

中文文档
0.8.0发布于 2026/09/17MPL-2.0ES Module近 30 天下载 1.1万

TypeScript library for usage analytics

原始使用文档

@fluidattacks/tracks

NPM version

Fluid Attacks logo

This library provides a convenient way to report usage analytics from any TypeScript or JavaScript application.

All operations run in the background, and errors are logged but don't propagate to the caller, ensuring your application's flow isn't interrupted.

Usage

import { Tracks } from "@fluidattacks/tracks";

const client = new Tracks();
client.event.create({
  action: "CREATE",
  author: "author",
  date: new Date(),
  mechanism: "API",
  metadata: { foo: "bar" },
  object: "object",
  objectId: "objectId",
});

Anonymous visitors

For unauthenticated surfaces, set the identity once on the client so no call site can omit it. The cookie is issued HttpOnly, so minting it and reading it back belong on the server.

import {
  Tracks,
  createAnonymousId,
  nextSessionId,
  readAnonymousCookie,
  serializeAnonymousCookie,
} from "@fluidattacks/tracks";

const COOKIE_NAME = "my_anon_id";

const anonymousId =
  readAnonymousCookie(cookieHeader, { name: COOKIE_NAME }) ??
  createAnonymousId();
const cookie = serializeAnonymousCookie(anonymousId, {
  domain: ".example.com",
  name: COOKIE_NAME,
});

const client = new Tracks({
  author: anonymousId,
  authorAnonymous: true,
  sessionId: nextSessionId(previousSessionId, lastSeenAt, new Date()),
});

cookieHeader is the incoming Cookie header; previousSessionId and lastSeenAt are whatever the caller persisted. The session id rolls over after 30 minutes of inactivity.

Build that client per request. A client carrying an identity belongs to a single visitor, so a module level one would hand the first visitor id to everyone else the process serves.

author and authorAnonymous travel as a pair: an event that supplies its own author is never marked anonymous by the client identity. The API still defaults author_anonymous to true when the field is absent, and its public route only accepts anonymous events, so a client cannot record an identified author on its own.

mechanism is always required per event: the SDK never guesses it, because a wrong mechanism is indistinguishable from a real one in the analytics.

版本与发布记录

@fluidattacks/tracks 共发布 10 个版本,首次发布于 2025/04/26;过去 12 个月发布了 5 个版本。

版本发布日期距今
0.8.0latest2026/09/171 个月前
0.7.02026/09/162 个月前
0.6.02026/09/092 个月前
0.5.42026/02/129 个月前
0.5.32026/01/219 个月前
0.5.12025/10/031.1 年前

关于 @fluidattacks/tracks 的常见问题

@fluidattacks/tracks 是什么?

TypeScript library for usage analytics

如何安装 @fluidattacks/tracks?

在项目目录运行 npm install @fluidattacks/tracks,或使用 pnpm add / yarn add / bun add。

@fluidattacks/tracks 支持 TypeScript 吗?

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

@fluidattacks/tracks 支持 ESM 还是 CommonJS?

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

@fluidattacks/tracks 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=21。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@fluidattacks/tracks 可以商用吗?

@fluidattacks/tracks 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@fluidattacks/tracks 的下载量有多少?

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

@fluidattacks/tracks 还在维护吗?

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