feat: implement comprehensive reading analytics system with bot detection
Features: - Add article view tracking with automatic deduplication (per user/IP) - Implement intelligent bot detection (35+ patterns: Google, Bing, Baidu, GPTBot, etc) - Create admin analytics dashboard with statistics and filtering - Display view count and comment count on article cards - Add search-based article filter (replaced dropdown for scalability) Analytics Dashboard: - Global stats: total views, human/bot views, unique IPs/users - Top 20 articles ranking with detailed metrics - Detailed view records with time, article, user, IP, user-agent - Filters: article title search, IP search, show/hide bots - Pagination support (50 records per page) Technical Implementation: - Async view recording (non-blocking) - Dual deduplication (application + database layer) - Database indexes for performance optimization - Batch query for comment counts - Full i18n support (Chinese/English) Files Added: - models/article_view.go: View tracking model - models/bot_detector.go: Bot detection logic - handlers/admin_analytics.go: Analytics page handler - templates/admin/analytics_views.html: Analytics UI - test_analytics.sh: Testing script - Documentation: implementation guide, usage guide, changelog Files Modified: - handlers/home.go: Add view recording and comment count queries - models/db.go: Add ArticleView to auto-migration - main.go: Add analytics routes - i18n/i18n.go: Add 35+ translation keys - templates/admin/dashboard.html: Add analytics entry link - templates/pages/home.html: Display view/comment counts on cards Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
# 阅读统计系统 - 更新日志
|
||||
|
||||
## 2026-06-22 - v1.1
|
||||
|
||||
### ✨ 改进:文章筛选方式优化
|
||||
|
||||
**问题**:之前使用下拉框选择文章,当文章数量很多时不便使用。
|
||||
|
||||
**解决方案**:改为输入框模糊搜索
|
||||
|
||||
#### 变更内容
|
||||
|
||||
**旧版本(v1.0)**:
|
||||
- 使用 `<select>` 下拉框
|
||||
- 需要遍历所有文章填充选项
|
||||
- 文章多时下拉框很长,难以查找
|
||||
- 只能精确匹配文章ID
|
||||
|
||||
**新版本(v1.1)**:
|
||||
- 使用 `<input>` 文本输入框
|
||||
- 支持关键字模糊搜索
|
||||
- 输入文章标题的部分内容即可筛选
|
||||
- 使用 SQL `LIKE` 查询,支持中英文
|
||||
|
||||
#### 使用方法
|
||||
|
||||
在"筛选条件"区域的"文章标题"输入框中:
|
||||
- 输入完整标题:`Getting Started`
|
||||
- 或输入部分关键字:`start`
|
||||
- 支持中文:`入门`
|
||||
- 点击"应用筛选"查看结果
|
||||
|
||||
#### 技术细节
|
||||
|
||||
**Handler 变更**:
|
||||
```go
|
||||
// 旧代码
|
||||
articleIDStr := c.Query("article_id")
|
||||
if articleIDStr != "" {
|
||||
query = query.Where("article_id = ?", articleIDStr)
|
||||
}
|
||||
|
||||
// 新代码
|
||||
articleTitle := c.Query("article_title")
|
||||
if articleTitle != "" {
|
||||
query = query.Joins("JOIN articles ON article_views.article_id = articles.id").
|
||||
Where("articles.title LIKE ?", "%"+articleTitle+"%")
|
||||
}
|
||||
```
|
||||
|
||||
**模板变更**:
|
||||
```html
|
||||
<!-- 旧代码 -->
|
||||
<select name="article_id">
|
||||
<option value="">全部文章</option>
|
||||
{{range .Articles}}
|
||||
<option value="{{.ID}}">{{.Title}}</option>
|
||||
{{end}}
|
||||
</select>
|
||||
|
||||
<!-- 新代码 -->
|
||||
<input type="text" name="article_title"
|
||||
placeholder="搜索文章标题"
|
||||
value="{{.Filters.ArticleTitle}}">
|
||||
```
|
||||
|
||||
**翻译变更**:
|
||||
- `analytics_filter_article`: "文章" → "文章标题"
|
||||
- `analytics_filter_all_articles`: 删除(不再需要)
|
||||
- 新增 `analytics_filter_article_placeholder`: "搜索文章标题"
|
||||
|
||||
#### 优势
|
||||
|
||||
1. **可扩展性**:支持任意数量的文章
|
||||
2. **用户体验**:无需下拉滚动,直接输入
|
||||
3. **灵活性**:模糊匹配,不必输入完整标题
|
||||
4. **性能优化**:不再需要预加载所有文章列表
|
||||
|
||||
#### 兼容性
|
||||
|
||||
- ✅ 向后兼容:URL参数从 `article_id` 改为 `article_title`
|
||||
- ✅ 数据库兼容:使用标准SQL JOIN和LIKE
|
||||
- ✅ 支持SQLite和MySQL
|
||||
|
||||
---
|
||||
|
||||
## 完整功能列表(v1.1)
|
||||
|
||||
### 筛选功能
|
||||
- ✅ **文章标题**:输入框模糊搜索(新)
|
||||
- ✅ **IP地址**:输入框部分匹配
|
||||
- ✅ **显示爬虫**:复选框切换
|
||||
|
||||
### 统计展示
|
||||
- ✅ 全局统计卡片(5个指标)
|
||||
- ✅ 文章排行榜(前20篇)
|
||||
- ✅ 详细访问记录(分页)
|
||||
|
||||
### 核心功能
|
||||
- ✅ 自动记录阅读
|
||||
- ✅ 智能爬虫识别
|
||||
- ✅ 去重机制
|
||||
- ✅ 异步处理
|
||||
- ✅ 中英文支持
|
||||
|
||||
---
|
||||
|
||||
## 升级方法
|
||||
|
||||
如果你正在使用 v1.0:
|
||||
|
||||
1. **重新编译**:
|
||||
```bash
|
||||
go build -o go_blog
|
||||
```
|
||||
|
||||
2. **重启应用**:
|
||||
```bash
|
||||
./go_blog
|
||||
```
|
||||
|
||||
3. **无需数据库迁移**:数据结构未变更
|
||||
|
||||
4. **清除浏览器缓存**:确保加载新模板
|
||||
|
||||
---
|
||||
|
||||
## 后续计划
|
||||
|
||||
考虑添加的功能:
|
||||
- 📅 时间范围筛选(按日期范围)
|
||||
- 👤 用户名搜索(类似文章搜索)
|
||||
- 📊 导出筛选结果(CSV/Excel)
|
||||
- 🔍 高级搜索(组合多个条件)
|
||||
|
||||
---
|
||||
|
||||
**感谢反馈!** 🙏
|
||||
|
||||
这个改进让系统更适合大规模文章数量的场景。
|
||||
Reference in New Issue
Block a user