@doist/interaction-trace

中文文档
1.1.0发布于 2026/02/07MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 8839

A performance monitoring library for tracking the time from user interactions to next paint (INP) and last long-animation frame (LoAF), with declarative trace signing and third-party reporting.

原始使用文档
文档目录14 节
  1. Installation
  2. Usage
  3. Initialize the Monitor
  4. Sign Traces
  5. React Integration
  6. Configuration
  7. Enrollment Options
  8. Reporter Interface
  9. Browser Support
  10. Known Limitations
  11. Keyboard Interactions
  12. Development
  13. Releasing
  14. License

@doist/interaction-trace

npm version License: MIT Node.js

A performance monitoring library for tracking user interactions using Interaction to Next Paint (INP) and Long Animation Frames (LoAF). Features declarative trace signing and pluggable reporting.

Installation

npm install @doist/interaction-trace

Usage

Initialize the Monitor

import { initInteractionTraceMonitor } from '@doist/interaction-trace'

const cleanup = initInteractionTraceMonitor({
    reporter: (report) => {
        // Send to your analytics service
        console.log('Trace report:', report)
    },
    enrollment: {
        sampleRate: 10, // 10% of sessions
        isEnabled: () => {
            if (user.isInternal) return true    // force enable for internal users
            return undefined                     // everyone else: use sampleRate
        },
    },
    abortSignal: controller.signal, // Optional: auto-cleanup when aborted
})

// Or call cleanup() manually when done

Sign Traces

A trace measures the time from a user click to the browser completing all visual updates. Traces are created automatically on pointerup events—use signInteractionTrace() to name and provide details for the pending trace.

import { signInteractionTrace } from '@doist/interaction-trace'

button.addEventListener('click', () => {
    signInteractionTrace('open modal', { modalId: 'settings' })
    openModal()
})

TypeScript

Create a pre-typed function for compile-time validation of trace names and details:

import { signInteractionTrace } from '@doist/interaction-trace'

type MyTraces = {
    'open modal': { modalId: string }
    'submit form': { formId: string }
}

const signAppTrace = signInteractionTrace.withTypes<MyTraces>()

signAppTrace('open modal', { modalId: 'settings' })   // ✅ Works
signAppTrace('opne modal', { modalId: 'settings' })   // ❌ Error: typo caught
signAppTrace('open modal', { formId: 'x' })           // ❌ Error: wrong details

React Integration

import { useInteractionTrace } from '@doist/interaction-trace/react'

function SettingsModal() {
    useInteractionTrace('open modal', { modalId: 'settings' })
    return <div>...</div>
}

TypeScript

import { useInteractionTrace } from '@doist/interaction-trace/react'

type MyTraces = {
    'open modal': { modalId: string }
    'submit form': { formId: string }
}

const useAppTrace = useInteractionTrace.withTypes<MyTraces>()

function SettingsModal() {
    useAppTrace('open modal', { modalId: 'settings' })
    return <div>...</div>
}

Configuration

Enrollment Options

OptionTypeDescription
sampleRatenumberPercentage of sessions to enroll (0-100)
persistKeystringsessionStorage key for enrollment state. Default: 'interaction-trace-enrolled'
isEnabled() => boolean | undefinedOverride function. true/false override sampling, undefined defers to sampleRate

Reporter Interface

The reporter receives a TraceReport object:

type TraceReport = {
    id: string                        // Unique trace ID (crypto.randomUUID())
    duration: number                  // Total LoAF duration (ms)
    inp: number | undefined           // INP value if captured (ms)
    details: {
        name: string                  // Trace name from signInteractionTrace()
        [key: string]: unknown        // Additional details
    }
    device: {
        memoryGB: string              // Bucketed: "0.25-2", "3-7", "8-plus", or "unknown"
        cpuCores: string              // Bucketed: "1-8", "9-16", "17-plus"
    }
}

Browser Support

Requires Chrome 123+ for Long Animation Frames and Event Timing APIs. In unsupported browsers, the monitor silently no-ops—your app continues to work normally, just without trace collection.

Known Limitations

Keyboard Interactions

This library only tracks pointer-based interactions (pointerup events). Keyboard-initiated interactions (e.g., pressing Enter to submit a form, Space to toggle a checkbox) are not captured. INP metrics will only reflect mouse/touch interactions.

If your application has significant keyboard usage, consider this when interpreting the collected metrics.

Development

Development requires Node.js >= 22.18.0 (see .node-version).

npm install
npm run build
npm test

Releasing

This project uses release-please to automate releases. Commits merged to main with fix: trigger patch releases, feat: triggers minor releases, and feat!: or fix!: triggers major releases.

When commits land on main, release-please creates a release PR. Merging it publishes to npm and GitHub Packages.

License

Released under the MIT License.

版本与发布记录

@doist/interaction-trace 共发布 2 个版本,首次发布于 2026/02/06;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.0latest2026/02/079 个月前
1.0.02026/02/069 个月前

关于 @doist/interaction-trace 的常见问题

@doist/interaction-trace 是什么?

A performance monitoring library for tracking the time from user interactions to next paint (INP) and last long-animation frame (LoAF), with declarative trace signing and third-party reporting.

如何安装 @doist/interaction-trace?

在项目目录运行 npm install @doist/interaction-trace,或使用 pnpm add / yarn add / bun add。

@doist/interaction-trace 支持 TypeScript 吗?

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

@doist/interaction-trace 支持 ESM 还是 CommonJS?

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

@doist/interaction-trace 可以商用吗?

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

@doist/interaction-trace 的下载量有多少?

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

@doist/interaction-trace 还在维护吗?

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