Skip to main content

A self-hosted website visit counter with zero-config frontend integration

Project description

web-counter

一个开源、可自托管的网站访问计数服务。一行命令部署,一行 <script> 接入,数据完全由你掌控。

统计指标

指标 含义
今日访问量 (PV Today) 今天所有页面的总访问次数
今日访客数 (UV Today) 今天的独立访客数(IP + salt 哈希去重)
总访问量 (PV Site) 建站以来所有页面的累计访问次数
总访客数 (UV Site) 建站以来的累计独立访客数
页面阅读量 (PV Page) 当前页面的累计访问次数

快速开始

# 1. 安装
pip install web-counter

# 2. 设置 salt(必填,用于哈希访客 IP 保护隐私)
export COUNTER_SALT="$(openssl rand -hex 16)"

# 3. 启动
web-counter start

# 4. 创建管理员(用于访问统计看板)
web-counter createsuperuser

# 5. 将终端输出的代码粘贴到网页 </body> 前,完成接入

前端接入

在网页 </body> 前添加以下代码:

<script async src="https://你的域名/counter.js"></script>

<!-- 显示计数(放在 display:none 容器中,JS 加载后自动显示) -->
<span style="display:none" class="counter-container" data-counter-style="card">
  本站访问次数 <span data-pv-site></span> 次 ·
  今日访问量 <span data-pv-today></span> 次 ·
  今日访客 <span data-uv-today></span> 人 ·
  总访客 <span data-uv-site></span></span>

数据属性一览

属性 含义
data-pv-today 今日访问量
data-uv-today 今日访客数
data-pv-site 总访问量
data-uv-site 总访客数
data-pv-page 当前页面阅读量
data-counter-style 展示风格:default / badge / card / bordered
data-counter-api 手动指定 API 基地址(JS 独立部署时使用)

SPA 支持

对于 Vue/React 等单页应用,路由切换不会触发计数。可在路由切换后手动上报:

fetch('https://你的域名/api/visit', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ path: window.location.pathname })
})

CLI 命令

web-counter start           # 后台启动服务
web-counter restart         # 重启服务
web-counter stop            # 停止服务
web-counter createsuperuser # 创建管理员账号
web-counter export-js       # 导出 counter.js 到 stdout

配置

优先级:CLI 参数 > 环境变量 > .env 文件

参数 环境变量 默认值 说明
--salt COUNTER_SALT 无(必填) IP 哈希盐值
--host COUNTER_HOST 0.0.0.0 监听地址
--port COUNTER_PORT 8000 监听端口
--db-path COUNTER_DB_PATH ./data/counter.db 数据库路径
--pid-file COUNTER_PID_FILE ./data/counter.pid PID 文件路径
--allowed-origins COUNTER_ALLOWED_ORIGINS * CORS 允许域名
--rate-limit COUNTER_RATE_LIMIT 60 每 IP 每分钟限流

API 接口

方法 路径 说明
POST /api/visit 记录访问
GET /api/count?paths=/page1,/page2 批量查询计数
GET /counter.js 获取前端 JS 脚本
GET /api/health 健康检查
GET /dashboard 统计看板(需登录)
POST /api/admin/login 管理员登录(JSON)
POST /api/admin/logout 退出登录
POST /api/admin/reset 重置数据(需登录)
GET/POST /api/admin/offset 查看/设置起始值(需登录)

生产环境部署

使用反代 (Caddy)

your-domain.com {
    reverse_proxy localhost:8000
}

使用 systemd

[Unit]
Description=web-counter
After=network.target

[Service]
Type=forking
PIDFile=/opt/web-counter/data/counter.pid
Environment="COUNTER_SALT=your-random-salt"
ExecStart=/usr/bin/web-counter start
ExecStop=/usr/bin/web-counter stop
ExecReload=/usr/bin/web-counter restart
Restart=on-failure

[Install]
WantedBy=multi-user.target

使用 Docker

docker compose up -d

数据管理

后台登录 /dashboard 后可进行:

  • 数据重置:支持重置今日/全部/指定页面的访问数据
  • 起始值设置:为累计指标设置偏移量,适用于从其他统计工具迁移

隐私设计

  • 不存储原始 IP 地址
  • 不设置 Cookie(后台 session cookie 除外)
  • 不做浏览器指纹
  • 访客唯一性通过 SHA256(IP + salt) 标识
  • salt 由部署者自行设置,无法被外部反向破解

注意:salt 设定后不要更换,否则历史访客标识失联,UV 统计将清零重来。

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

web_counter-0.1.0.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

web_counter-0.1.0-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

Details for the file web_counter-0.1.0.tar.gz.

File metadata

  • Download URL: web_counter-0.1.0.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for web_counter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ac401ea8ae72b374e3a70825f596179fb9a3a49cd92b357fb6452af29cc95391
MD5 3324827ffa93bfada88911c9d7679786
BLAKE2b-256 d40a25d21a378ff13ff8dfa63ae0c0205e2a37da16cc143e26b1afdb78229c2a

See more details on using hashes here.

File details

Details for the file web_counter-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: web_counter-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for web_counter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 47d4f186e96c3a9595acb855f28df4f72726c68ac66492185cce9ae03b032af5
MD5 4d34d765c3ffc9829b7379239684602f
BLAKE2b-256 c90c62dc24dcd7780c4a588424d94f66ba983e79dff3081edceaa8217477a3f0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page