@devbie/web-monitor-sdk

中文文档
1.6.0发布于 2026/11/06MITTypeScriptCommonJS零依赖近 30 天下载 2896

Browser-side web monitoring SDK: PV, AJAX, resource, JS error and custom event reporting. No heartbeat, no credential logic.

此软件包已被标记为弃用

Renamed to @devbie/frontend-monitor-sdk. Use @devbie/frontend-monitor-sdk@^1.8.1.

原始使用文档
文档目录14 节
  1. Features
  2. Installation
  3. Quick start
  4. Use with Next.js
  5. Init options
  6. Returned API
  7. addError(error)
  8. addCustom(event)
  9. setConfig(config)
  10. Business error reporting
  11. Report types and transport
  12. Global access
  13. Related packages
  14. License

@devbie/web-monitor-sdk

npm version license

Browser-side web monitoring SDK. It automatically collects page-view and page performance data, AJAX calls (both XMLHttpRequest and fetch), resource loading, JavaScript errors and custom events, and reports them to a collector endpoint. The SDK handles monitoring only — no heartbeat and no credential logic.

Features

  • Page performance — load timing, first-entry metadata, screen size, referrer and performance entries.
  • AJAX monitoring — transparently instruments XMLHttpRequest and fetch, recording method, URL, status, response size, timing and optional trace ID.
  • Resource monitoring — slow and failed resource loads via PerformanceObserver and resource error events.
  • Error monitoring — window.onerror, unhandledrejection, resource load failures and console.error, with parsed stack traces.
  • Custom events — report business events with throttled batching.
  • Business error codes — plug in an errcodeReport resolver to flag business-level failures inside HTTP 200 responses.
  • Anonymous identity — per-user, per-page, unique-visitor and device marks generated in the browser.

Installation

npm install @devbie/web-monitor-sdk

Quick start

Initialize the SDK as early as possible in your application — for example in your root layout or application entry:

import init from "@devbie/web-monitor-sdk";

init({
  api: "https://collector.example.com/api/web-monitor/report",
  token: "<report token>",
});

The init call returns helper functions for manual reporting:

const monitor = init({
  api: "https://collector.example.com/api/web-monitor/report",
  token: "<report token>",
});

monitor.addCustom({
  customName: "checkout",
  customContent: { orderId: "1001", amount: 99 },
});

Use with Next.js

The Fewbie framework ships a ready-made registry wrapper:

npx @devbie/fewbie-cli add web-monitor-sdk

Then add the scaffolded <WebMonitor /> component to your root layout and configure NEXT_PUBLIC_WEB_MONITOR_ENDPOINT and NEXT_PUBLIC_WEB_MONITOR_TOKEN. The component is a no-op until both environment variables are present.

Init options

OptionTypeDefaultRequiredDescription
apistring—YesCollector endpoint that receives report payloads.
tokenstring—YesReport token issued for the deployment; the collector resolves it to the application partition.
isPagebooleantrueNoCollect page-view / page performance data.
isAjaxbooleantrueNoInstrument XHR and fetch requests.
isResourcebooleantrueNoCollect resource timing and slow/failed resources.
isErrorbooleantrueNoCollect JS errors, rejections, resource errors and console errors.
filterUrlsstring[][]NoURLs excluded from AJAX/resource collection. The collector endpoint (/api/web-monitor/report) is always filtered automatically.
userobject{}NoInitial user identity: { uid?, p? } (p should be pre-encrypted).
isTraceIdbooleanfalseNoCapture the trace ID response header from AJAX calls.
traceIdHeaderNamestringx-trace-idNoResponse header name to read when isTraceId is enabled.
customsThrottleMsnumber1000NoThrottle window for batching custom events, in milliseconds.
errcodeReportfunction—NoBusiness error resolver, see below.

Returned API

init() returns an object with the following members:

addError(error)

Manually push a script error for immediate reporting:

monitor.addError({
  msg: "Something went wrong",
  line: 42,
  col: 10,
  resourceUrl: "https://example.com/app.js",
});

addCustom(event)

Queue a custom event. Object content is serialized to JSON; queued events are flushed (and batched) after customsThrottleMs:

monitor.addCustom({
  customName: "button-click",
  customContent: "pay-now",
  customFilter: { page: "checkout" }, // optional, must be an object
});

setConfig(config)

Update the user identity after initialization. When p is supplied it is encrypted before storage:

monitor.setConfig({ uid: "10086", p: "plain-text-secret" });

Business error reporting

HTTP 200 responses can still represent business failures. Provide an errcodeReport resolver to inspect parsed response data and decide whether to report an error:

init({
  api: "https://collector.example.com/api/web-monitor/report",
  token: "<report token>",
  errcodeReport: (responseData) => {
    return {
      isReport: responseData.code !== 0,
      errMsg: responseData.message,
      code: responseData.code,
    };
  },
});

Report types and transport

Reports carry one of the following types:

TypeContent
PagePerfPage performance, resources and errors on navigation.
AjaxPerfAJAX activity and errors between page reports.
ErrorErrors captured immediately (debounced).
CustomBatched custom events.
UnloadFinal report on page unload.
SdkErrorErrors thrown inside the SDK itself.

Payloads are POSTed with Content-Type: text/plain via fetch (so monitoring requests are never re-instrumented), and the final Unload report uses navigator.sendBeacon when available. If the collector becomes unreachable, reporting is paused to avoid request storms.

Global access

For integrations that cannot import the module directly, the default initializer is also exposed on the browser as window._frontendMonitor.

License

MIT

版本与发布记录

@devbie/web-monitor-sdk 共发布 17 个版本,首次发布于 2026/09/27;过去 12 个月发布了 17 个版本。

版本发布日期距今
1.6.0latest2026/11/065 天前
1.5.32026/11/065 天前
1.5.22026/11/065 天前
1.5.12026/11/066 天前
1.5.02026/11/066 天前
1.4.12026/11/056 天前

关于 @devbie/web-monitor-sdk 的常见问题

@devbie/web-monitor-sdk 是什么?

Browser-side web monitoring SDK: PV, AJAX, resource, JS error and custom event reporting. No heartbeat, no credential logic.注意:该包已被作者标记为弃用——Renamed to @devbie/frontend-monitor-sdk. Use @devbie/frontend-monitor-sdk@^1.8.1.

如何安装 @devbie/web-monitor-sdk?

在项目目录运行 npm install @devbie/web-monitor-sdk,或使用 pnpm add / yarn add / bun add。

@devbie/web-monitor-sdk 支持 TypeScript 吗?

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

@devbie/web-monitor-sdk 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@devbie/web-monitor-sdk 需要什么版本的 Node.js?

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

@devbie/web-monitor-sdk 可以商用吗?

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

@devbie/web-monitor-sdk 的下载量有多少?

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

@devbie/web-monitor-sdk 还在维护吗?

最新版本 1.6.0 发布于 2026/11/06(5 天前);至今共发布 17 个版本,其中过去 12 个月发布了 17 个;首次发布于 2026/09/27。作者已将其标记为弃用,新项目建议寻找替代方案。