npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

behavior-aliu

v1.1.1

Published

A lightweight front-end behavior monitoring SDK

Downloads

50

Readme

User Behavior Monitor SDK

轻量级、零依赖的前端用户行为监控 SDK。 专为现代 Web 应用设计,支持 PV/UV 统计、精准停留时长计算、用户点击行为追踪以及 SPA(单页应用)路由监控。

核心特性

  • ** 基础指标**: 自动采集 PV (Page View) 和 UV (Unique Visitor)。
  • ** 精准停留**: 基于 visibilitychangebeforeunload 精准计算页面实际停留时长。
  • ** 点击追踪**: 通过 data-track-click 属性实现无侵入式全自动点击埋点。
  • ** SPA 支持**: 完美支持 Vue/React 等单页应用的路由跳转监控(Hash & History 模式)。
  • ** 稳健上报**: 优先使用 navigator.sendBeacon 确保页面关闭时数据不丢失,自动降级 fetch

安装

npm install
npm run build

快速开始

1. 引入 SDK

支持 ES Module (推荐) 或 Script 标签引入。

import { initUserBehaviorMonitor } from './dist/index.esm.js';

initUserBehaviorMonitor({
  projectName: 'my-awesome-project', // 项目标识
  reportUrl: 'http://your-server.com/report', // 数据上报地址
});

2. 自动捕获点击

只需在 HTML 元素上添加 data-track-click 属性,SDK 会自动捕获点击并上报。

<!-- 点击后自动上报: { behavior: 'click', action: 'buy_btn', ... } -->
<button data-track-click="buy_btn">立即购买</button>

运行 Demo

本项目内置了一个基于 Express 的测试环境,方便您直观体验监控效果。

  1. 安装依赖

    npm install
  2. 构建 SDK

    npm run build
  3. 启动 Demo

    npm run demo

    终端会显示:测试服务器已启动! 👉 页面地址: http://localhost:3000/index.html

  4. 体验功能

    • 打开浏览器访问 http://localhost:3000/index.html
    • 点击按钮: 观察终端输出 behavior: 'click'
    • 模拟路由跳转: 点击 "模拟路由跳转",观察 dwell (旧页面停留) 和 pv (新页面) 上报
    • 刷新/关闭页面: 观察 dwell (页面卸载) 上报

数据格式示例

SDK 上报的数据格式如下 (JSON):

PV (页面访问)

{
  "behavior": "pv",
  "projectName": "demo",
  "pageUrl": "http://localhost:3000/",
  "pv": 15,
  "userId": "xxxx-xxxx"
}

Dwell (停留时长)

{
  "behavior": "dwell",
  "dwellTime": 4520, // 毫秒
  "pageUrl": "http://localhost:3000/"
}

开发

  • npm run dev: 监听源码变化并自动重新构建
  • npm run build: 生产环境构建 (输出到 dist/)