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>
171 lines
10 KiB
HTML
171 lines
10 KiB
HTML
{{define "analytics_views"}}
|
|
{{template "header" .}}
|
|
<section class="max-w-7xl mx-auto px-4 py-12">
|
|
<div class="mb-8">
|
|
<h2 class="text-3xl font-bold text-gray-900">{{index .Tr "analytics_views_title"}}</h2>
|
|
<p class="text-gray-500 mt-1">{{index .Tr "analytics_views_desc"}}</p>
|
|
</div>
|
|
|
|
<!-- Statistics Overview -->
|
|
<div class="grid grid-cols-1 md:grid-cols-5 gap-4 mb-8">
|
|
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
|
|
<p class="text-sm text-gray-500 uppercase tracking-wider">{{index .Tr "analytics_stats_total"}}</p>
|
|
<p class="text-2xl font-bold text-gray-900 mt-1">{{.Stats.TotalViews}}</p>
|
|
</div>
|
|
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
|
|
<p class="text-sm text-gray-500 uppercase tracking-wider">{{index .Tr "analytics_stats_human_views"}}</p>
|
|
<p class="text-2xl font-bold text-green-600 mt-1">{{.Stats.HumanViews}}</p>
|
|
</div>
|
|
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
|
|
<p class="text-sm text-gray-500 uppercase tracking-wider">{{index .Tr "analytics_stats_bot_views"}}</p>
|
|
<p class="text-2xl font-bold text-orange-600 mt-1">{{.Stats.BotViews}}</p>
|
|
</div>
|
|
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
|
|
<p class="text-sm text-gray-500 uppercase tracking-wider">{{index .Tr "analytics_stats_unique_ips"}}</p>
|
|
<p class="text-2xl font-bold text-blue-600 mt-1">{{.Stats.UniqueIPs}}</p>
|
|
</div>
|
|
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
|
|
<p class="text-sm text-gray-500 uppercase tracking-wider">{{index .Tr "analytics_stats_unique_users"}}</p>
|
|
<p class="text-2xl font-bold text-purple-600 mt-1">{{.Stats.UniqueUsers}}</p>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Article Statistics -->
|
|
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6 mb-8">
|
|
<h3 class="text-xl font-semibold text-gray-800 mb-4">{{index .Tr "analytics_article_stats"}}</h3>
|
|
<div class="overflow-x-auto">
|
|
<table class="w-full text-left text-sm">
|
|
<thead class="bg-gray-50 border-b border-gray-200">
|
|
<tr>
|
|
<th class="px-4 py-3 font-semibold text-gray-600">{{index .Tr "analytics_col_article"}}</th>
|
|
<th class="px-4 py-3 font-semibold text-gray-600 text-right">{{index .Tr "analytics_col_total_views"}}</th>
|
|
<th class="px-4 py-3 font-semibold text-gray-600 text-right">{{index .Tr "analytics_col_human_views"}}</th>
|
|
<th class="px-4 py-3 font-semibold text-gray-600 text-right">{{index .Tr "analytics_col_bot_views"}}</th>
|
|
<th class="px-4 py-3 font-semibold text-gray-600 text-right">{{index .Tr "analytics_col_unique_ips"}}</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody class="divide-y divide-gray-100">
|
|
{{range .ArticleStats}}
|
|
<tr class="hover:bg-gray-50">
|
|
<td class="px-4 py-3 text-gray-800">{{.Title}}</td>
|
|
<td class="px-4 py-3 text-right font-medium">{{.ViewCount}}</td>
|
|
<td class="px-4 py-3 text-right text-green-600">{{.HumanViews}}</td>
|
|
<td class="px-4 py-3 text-right text-orange-600">{{.BotViews}}</td>
|
|
<td class="px-4 py-3 text-right text-blue-600">{{.UniqueIPs}}</td>
|
|
</tr>
|
|
{{else}}
|
|
<tr>
|
|
<td colspan="5" class="px-4 py-8 text-center text-gray-500">{{index $.Tr "analytics_no_data"}}</td>
|
|
</tr>
|
|
{{end}}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Filters -->
|
|
<div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6 mb-6">
|
|
<h3 class="text-lg font-semibold text-gray-800 mb-4">{{index .Tr "analytics_filters"}}</h3>
|
|
<form method="get" action="/admin/analytics/views" class="grid grid-cols-1 md:grid-cols-4 gap-4">
|
|
<div>
|
|
<label class="block text-sm font-medium text-gray-700 mb-1">{{index .Tr "analytics_filter_article"}}</label>
|
|
<input type="text" name="article_title" value="{{.Filters.ArticleTitle}}" placeholder="{{index .Tr "analytics_filter_article_placeholder"}}"
|
|
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-medium text-gray-700 mb-1">{{index .Tr "analytics_filter_ip"}}</label>
|
|
<input type="text" name="ip" value="{{.Filters.IP}}" placeholder="{{index .Tr "analytics_filter_ip_placeholder"}}"
|
|
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-medium text-gray-700 mb-1">{{index .Tr "analytics_filter_show_bots"}}</label>
|
|
<label class="flex items-center mt-2">
|
|
<input type="checkbox" name="show_bots" value="1" {{if .Filters.ShowBots}}checked{{end}}
|
|
class="w-4 h-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500">
|
|
<span class="ml-2 text-sm text-gray-700">{{index .Tr "analytics_include_bots"}}</span>
|
|
</label>
|
|
</div>
|
|
<div class="flex items-end">
|
|
<button type="submit" class="w-full bg-blue-600 text-white px-4 py-2 rounded-lg font-medium hover:bg-blue-700 transition-colors">
|
|
{{index .Tr "analytics_apply_filters"}}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<!-- View Records -->
|
|
<div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
|
|
<div class="px-6 py-4 border-b border-gray-200">
|
|
<h3 class="text-lg font-semibold text-gray-800">{{index .Tr "analytics_recent_views"}}</h3>
|
|
</div>
|
|
<div class="overflow-x-auto">
|
|
<table class="w-full text-left text-sm">
|
|
<thead class="bg-gray-50 border-b border-gray-200">
|
|
<tr>
|
|
<th class="px-4 py-3 font-semibold text-gray-600">{{index .Tr "analytics_col_time"}}</th>
|
|
<th class="px-4 py-3 font-semibold text-gray-600">{{index .Tr "analytics_col_article"}}</th>
|
|
<th class="px-4 py-3 font-semibold text-gray-600">{{index .Tr "analytics_col_user"}}</th>
|
|
<th class="px-4 py-3 font-semibold text-gray-600">{{index .Tr "analytics_col_ip"}}</th>
|
|
<th class="px-4 py-3 font-semibold text-gray-600">{{index .Tr "analytics_col_user_agent"}}</th>
|
|
<th class="px-4 py-3 font-semibold text-gray-600 text-center">{{index .Tr "analytics_col_is_bot"}}</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody class="divide-y divide-gray-100">
|
|
{{range .Views}}
|
|
<tr class="hover:bg-gray-50 {{if .IsBot}}bg-orange-50{{end}}">
|
|
<td class="px-4 py-3 text-gray-600 whitespace-nowrap">{{.CreatedAt.Format "2006-01-02 15:04"}}</td>
|
|
<td class="px-4 py-3 text-gray-800">
|
|
{{if .Article.Title}}
|
|
<a href="/article/{{.Article.Slug}}" target="_blank" class="text-blue-600 hover:text-blue-800">{{.Article.Title}}</a>
|
|
{{else}}
|
|
<span class="text-gray-400">{{index $.Tr "analytics_article_deleted"}}</span>
|
|
{{end}}
|
|
</td>
|
|
<td class="px-4 py-3 text-gray-700">
|
|
{{if .User}}
|
|
{{if .User.DisplayName}}{{.User.DisplayName}}{{else}}{{.User.Username}}{{end}}
|
|
{{else}}
|
|
<span class="text-gray-400">{{index $.Tr "analytics_anonymous"}}</span>
|
|
{{end}}
|
|
</td>
|
|
<td class="px-4 py-3 text-gray-600 font-mono text-xs">{{.IP}}</td>
|
|
<td class="px-4 py-3 text-gray-500 text-xs max-w-xs truncate" title="{{.UserAgent}}">{{.UserAgent}}</td>
|
|
<td class="px-4 py-3 text-center">
|
|
{{if .IsBot}}
|
|
<span class="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-orange-100 text-orange-700">
|
|
🤖 {{index $.Tr "analytics_bot"}}
|
|
</span>
|
|
{{else}}
|
|
<span class="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-green-100 text-green-700">
|
|
👤 {{index $.Tr "analytics_human"}}
|
|
</span>
|
|
{{end}}
|
|
</td>
|
|
</tr>
|
|
{{else}}
|
|
<tr>
|
|
<td colspan="6" class="px-4 py-8 text-center text-gray-500">{{index .Tr "analytics_no_views"}}</td>
|
|
</tr>
|
|
{{end}}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
{{if .Pagination.HasMore}}
|
|
<div class="px-6 py-4 border-t border-gray-200 bg-gray-50">
|
|
<div class="flex justify-between items-center">
|
|
<p class="text-sm text-gray-600">
|
|
{{index .Tr "analytics_showing"}} {{len .Views}} {{index .Tr "analytics_of"}} {{.Pagination.Total}}
|
|
</p>
|
|
<a href="?page={{.Pagination.NextPage}}{{if .Filters.ArticleTitle}}&article_title={{.Filters.ArticleTitle}}{{end}}{{if .Filters.IP}}&ip={{.Filters.IP}}{{end}}{{if .Filters.ShowBots}}&show_bots=1{{end}}"
|
|
class="bg-blue-600 text-white px-4 py-2 rounded-lg font-medium hover:bg-blue-700 transition-colors text-sm">
|
|
{{index .Tr "analytics_load_more"}}
|
|
</a>
|
|
</div>
|
|
</div>
|
|
{{end}}
|
|
</div>
|
|
</section>
|
|
{{template "footer" .}}
|
|
{{end}}
|