@a2f0/ct-client

中文文档
0.1.8发布于 2026/11/06UNLICENSEDTypeScriptES Module零依赖构建来源已验证近 30 天下载 7507

Explicitly initialized, batched click tracking with browser opt-out support

原始使用文档

@a2f0/ct-client

Framework-free, batched click tracking. Importing or constructing a tracker starts no listeners, timers, storage writes, or requests. Call init explicitly in the deployed browser bootstrap, behind an exact check for the registered production or staging origin; keep that call out of shared components and tests.

Install with bun add @a2f0/ct-client or npm install @a2f0/ct-client.

import { createTracker } from "@a2f0/ct-client";
const tracker = createTracker({
  site: "a2f0-net",
  app: "desktop",
  environment: "production",
  capture: "all",
  page: () => "/skyline-3d",
});
tracker.init();
await tracker.flush();
tracker.setOptOut(true);
tracker.destroy();

The site must be registered with its exact origin at the API. The default endpoint is https://ct.a2f0.net/v1/clicks. Browser ESM is self-contained; TypeScript types are included. There are no runtime dependencies. init throws for an invalid site/app name, environment, or endpoint URL, before adding listeners. Validate configuration before initializing the tracker.

Options

OptionDefaultBehavior
siteRequiredPublic site identifier, lowercase letters/numbers/hyphens
appnullDefault app slug, lowercase letters/numbers/hyphens, 1–64 characters
environmentInferred by serverproduction or staging; must match registered site and exact origin
page() => '/'Stable route template without record IDs or tokens, maximum 200 characters
captureinteractiveall elements, interactive controls, or marked elements
batchSize201–50 clicks per request
flushIntervalMs5000100–60000 milliseconds
maxQueueSize500Bounded between batch size and 1000
enabledtruefalse makes init inert
overrideGpcfalseOnly true ignores Global Privacy Control; other opt-outs still apply
endpointCT endpointHTTPS, or HTTP on localhost for development

capture values are 'all', 'interactive', and 'marked'. Assign a stable label with data-ct="open-building". Never put user content in this attribute. Use data-ct-ignore on an element or ancestor to exclude a subtree. Inputs, textareas, selects, and editable content are always excluded. Open shadow DOM and SVG work.

Wrap an embedded app with data-ct-app="skyline". The nearest such attribute in the click's composed path overrides the tracker default, including unlabeled canvas clicks inside an open shadow root. An invalid or empty nearest attribute produces null instead of inheriting another app. Use one tracker per hosting website; the site remains a2f0-net while its desktop, Skyline and other embedded apps get separate app values. Closed shadow roots expose only their host, and cross-origin iframe clicks require a tracker inside that frame.

Environment is fixed per tracker and sent once per batch. The collector derives it from the registered site and exact request Origin; an explicit mismatched value rejects the entire batch. Old clients may omit both dimensions: app is null and environment is inferred. Never put user data in app names. Initialize only on deliberately registered production or staging origins, never on local, preview, test or automated pages.

init refuses WebDriver-controlled browsers: it returns false and adds no listeners. Only trusted clicks are captured; synthetic clicks are ignored. init is idempotent and returns whether capture is currently allowed. destroy removes listeners, aborts requests, and discards the queue; a later init can start it again. flush sends one batch and schedules remaining work. Network errors, timeouts, 408 and 429 retry with capped backoff; other 4xx responses discard the rejected batch. Event IDs make retries idempotent.

Opt out

setOptOut(true) saves ct:opt-out=1 in localStorage, clears the queue and stops capture for this site. setOptOut(false) removes that preference. Trackers also honor DNT, Global Privacy Control and the CT extension's data-ct-disabled marker. To explicitly override only Global Privacy Control, pass overrideGpc: true to createTracker before calling init. The default is false. This applies to capture and delivery for that tracker only; DNT, saved and in-memory opt-outs, the extension, storage errors, and automated-browser guards still disable tracking. An opt-out cannot retract a request the server already received. Storage errors disable tracking. The extension applies across all sites and must be installed separately in each browser/profile. Preferences are not synchronized between them.

The queue is in memory. Clicks are dropped when it is full. Page hiding attempts a keepalive flush, but closing/crashing/offline navigation can lose unsent events. This is best-effort analytics, not an audit trail or billing source. No cookies, visitor IDs, local click history, text, hrefs, DOM IDs, classes, or form values are collected by the SDK. page defaults to '/' so dynamic routes are never inferred.

The default CT collector adds the full request IP and approximate country, state/region, city, continent, and timezone when a batch arrives. These fields are stored for up to 90 days and visible to provisioned administrators on the authenticated dashboard and data API. The SDK sends no dashboard credentials. IPs can link activity; this is not anonymous analytics. Accepted batches receive this metadata even when overrideGpc is explicitly enabled. See the collector data policy when deciding what to disclose to your site's visitors.

版本与发布记录

@a2f0/ct-client 共发布 10 个版本,首次发布于 2026/10/22;过去 12 个月发布了 10 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.8latest2026/11/061 天前
0.1.72026/11/044 天前
0.1.62026/10/2810 天前
0.1.52026/10/2315 天前
0.1.42026/10/2315 天前
0.1.32026/10/2315 天前

关于 @a2f0/ct-client 的常见问题

@a2f0/ct-client 是什么?

Explicitly initialized, batched click tracking with browser opt-out support

如何安装 @a2f0/ct-client?

在项目目录运行 npm install @a2f0/ct-client,或使用 pnpm add / yarn add / bun add。

@a2f0/ct-client 支持 TypeScript 吗?

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

@a2f0/ct-client 支持 ESM 还是 CommonJS?

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

@a2f0/ct-client 可以商用吗?

@a2f0/ct-client 使用 UNLICENSED 许可。作者声明为 UNLICENSED,即不授予公开使用许可(与公共领域的 Unlicense 不同)。使用前需获得作者授权。以上为概括,不构成法律意见,请以许可原文为准。

@a2f0/ct-client 的下载量有多少?

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

@a2f0/ct-client 还在维护吗?

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