source: Klonkt/src/views/pages/admin-stats.ejs@ e3a5aed

main
Last change on this file since e3a5aed was e3a5aed, checked in by roboburr <roboburr@…>, 2 months ago

feat(stats): let the admin exclude their own IP from statistics

Logged-in admins were already skipped, but an admin browsing logged out (incognito, another browser)
still inflated the numbers. Admin -> Statistics now shows your current IP with a one-click "Don't
count my visits" toggle, stored in a stats_exclude_ips app_setting and checked on every pageview
(recordPageview/recordPostView). Trust-proxy gives the real client IP; normalised for matching.

Co-Authored-By: Claude <noreply@…>

  • Property mode set to 100644
File size: 10.0 KB
Line 
1<div class="container stats-page">
2 <p><a href="/admin" class="btn">&larr; <%= t('nav.admin') %></a></p>
3 <h1><%= t('astat.title') %></h1>
4 <p class="stats-note"><%= t('astat.intro') %></p>
5
6 <% if (typeof myIp !== 'undefined' && myIp) { %>
7 <form method="post" action="/admin/stats/exclude-ip" class="stats-note" style="margin:.2rem 0 1.2rem;display:flex;align-items:center;gap:.6rem;flex-wrap:wrap;">
8 <span><%= t('astat.your_ip') %> <code><%= myIp %></code> —
9 <% if (ipExcluded) { %><strong><%= t('astat.ip_not_counted') %></strong><% } else { %><%= t('astat.ip_counted') %><% } %></span>
10 <button type="submit" class="btn" style="padding:3px 10px;font-size:.85rem;"><%= ipExcluded ? t('astat.ip_count') : t('astat.ip_exclude') %></button>
11 </form>
12 <% } %>
13
14 <% var s = stats; var _max = Math.max(1, ...s.series.map(function(d){ return d.pageviews; })); %>
15
16 <div class="stats-cards">
17 <div class="stat-card"><div class="stat-num"><%= s.totals.visitors %></div><div class="stat-label"><%= t('astat.visitor_days', { n: s.days }) %></div></div>
18 <div class="stat-card"><div class="stat-num"><%= s.totals.pageviews %></div><div class="stat-label"><%= t('astat.pageviews_days', { n: s.days }) %></div></div>
19 <div class="stat-card"><div class="stat-num"><%= s.totals.plays %></div><div class="stat-label"><%= t('astat.plays_total') %></div></div>
20 <div class="stat-card"><div class="stat-num"><%= s.totals.postViews %></div><div class="stat-label"><%= t('astat.postviews_total') %></div></div>
21 </div>
22
23 <% if (typeof s.allTime !== 'undefined') { %>
24 <p class="stats-note"><%= t('astat.alltime_pre') %> <strong><%= s.allTime.pageviews %></strong> <%= t('astat.alltime_mid') %> · <strong><%= s.allTime.visitorDays %></strong> <%= t('astat.alltime_post') %></p>
25 <% } %>
26
27 <details class="stats-help">
28 <summary><%= t('astat.help_summary') %></summary>
29 <div class="stats-help-body">
30 <dl class="stats-help-defs">
31 <div><dt><%= t('astat.help_vd_term') %></dt><dd><%= t('astat.help_vd_a') %> <em><%= t('astat.help_vd_em') %></em><%= t('astat.help_vd_b') %></dd></div>
32 <div><dt><%= t('astat.help_pv_term') %></dt><dd><%= t('astat.help_pv') %></dd></div>
33 <div><dt><%= t('astat.help_plays_term') %></dt><dd><%= t('astat.help_plays') %></dd></div>
34 <div><dt><%= t('astat.help_postviews_term') %></dt><dd><%= t('astat.help_postviews') %></dd></div>
35 </dl>
36 <p class="stats-help-note"><%= t('astat.help_footer') %></p>
37 </div>
38 </details>
39
40 <div class="stats-period">
41 <%= t('astat.period') %>
42 <% [7,14,30,90].forEach(function(n){ %>
43 <a href="/admin/stats?days=<%= n %>" class="<%= s.days === n ? 'is-on' : '' %>"><%= n %>d</a>
44 <% }); %>
45 </div>
46
47 <section class="stats-chart-wrap">
48 <div class="stats-chart-head">
49 <h2><%= t('astat.last_n_days', { n: s.days }) %></h2>
50 <div class="stats-legend">
51 <span class="lg lg-vis"><%= t('astat.lg_visitor_days') %></span>
52 <span class="lg lg-pv"><%= t('astat.lg_pageviews') %></span>
53 </div>
54 </div>
55 <div class="stats-chart">
56 <% var _step = s.days <= 14 ? 1 : (s.days <= 31 ? 4 : 10);
57 s.series.forEach(function(d, i){
58 var pvPct = Math.round(d.pageviews / _max * 100);
59 var visPct = Math.round(d.visitors / _max * 100);
60 %>
61 <div class="sc-col" title="<%= t('astat.bar_title', { day: d.day, pv: d.pageviews, vd: d.visitors }) %>">
62 <div class="sc-bar">
63 <span class="sc-pv" style="height: <%= pvPct %>%"></span>
64 <span class="sc-vis" style="height: <%= visPct %>%"></span>
65 </div>
66 <span class="sc-day"><%= (i % _step === 0) ? d.day.slice(8) : '' %></span>
67 </div>
68 <% }); %>
69 </div>
70 </section>
71
72 <div class="stats-tops">
73 <section class="stats-top">
74 <h2><%= t('astat.top_posts') %></h2>
75 <% if (!s.topPosts.length || s.topPosts[0].views === 0) { %>
76 <p class="stats-empty"><%= t('astat.no_views') %></p>
77 <% } else { %>
78 <ol>
79 <% s.topPosts.forEach(function(p){ %>
80 <li><a href="/<%= p.slug %>"><%= p.title %></a><span class="stats-count"><%= p.views %></span></li>
81 <% }); %>
82 </ol>
83 <% } %>
84 </section>
85 <section class="stats-top">
86 <h2><%= t('astat.most_played') %></h2>
87 <% if (!s.topTracks.length || s.topTracks[0].plays === 0) { %>
88 <p class="stats-empty"><%= t('astat.no_plays') %></p>
89 <% } else { %>
90 <ol>
91 <% s.topTracks.forEach(function(t){ %>
92 <li><span><%= t.title %></span><span class="stats-count"><%= t.plays %></span></li>
93 <% }); %>
94 </ol>
95 <% } %>
96 </section>
97 </div>
98
99 <% if (typeof s.referrers !== 'undefined' && s.referrers.length) { %>
100 <section class="stats-top" style="margin-top:1.2rem">
101 <h2>🌐 <%= t('astat.sources') %></h2>
102 <ol>
103 <% s.referrers.forEach(function(r){ %>
104 <li><span><%= r.host %></span><span class="stats-count"><%= r.n %></span></li>
105 <% }); %>
106 </ol>
107 </section>
108 <% } %>
109
110 <% if (typeof linkClicks !== 'undefined' && linkClicks.length) { %>
111 <section class="stats-top" style="margin-top:1.2rem">
112 <h2>🔗 <%= t('astat.linkbio_clicks') %></h2>
113 <ol>
114 <% linkClicks.forEach(function(l){ %>
115 <li><a href="<%= l.url %>" target="_blank" rel="noopener"><%= l.url.replace(/^https?:\/\//,'').slice(0,48) %></a><span class="stats-count"><%= l.clicks %></span></li>
116 <% }); %>
117 </ol>
118 </section>
119 <% } %>
120</div>
121
122<style>
123.stats-page { max-width: 820px; margin: 2.5rem auto; padding: 0 1rem; }
124.stats-page h1 { font-family: var(--font-display, serif); font-size: 2rem; margin: .5rem 0 .25rem; }
125.stats-note { color: var(--ink-muted); font-size: .85rem; margin: 0 0 1.5rem; max-width: 60ch; }
126.stats-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: .75rem; margin-bottom: 2rem; }
127.stat-card { background: var(--paper-2); border: 1px solid var(--rule); border-radius: 10px; padding: 1rem 1.1rem; }
128.stat-num { font-family: var(--font-display, serif); font-size: 1.9rem; line-height: 1; color: var(--accent); }
129.stat-label { color: var(--ink-muted); font-size: .82rem; margin-top: .35rem; }
130
131.stats-chart-wrap { background: var(--paper-2); border: 1px solid var(--rule); border-radius: 12px; padding: 1.1rem 1.2rem 1rem; margin-bottom: 2rem; }
132.stats-chart-head { display: flex; justify-content: space-between; align-items: baseline; flex-wrap: wrap; gap: .5rem; }
133.stats-chart-head h2 { font-size: 1.05rem; margin: 0; }
134.stats-legend { display: flex; gap: .9rem; font-size: .78rem; color: var(--ink-muted); }
135.lg::before { content: ""; display: inline-block; width: .7rem; height: .7rem; border-radius: 2px; margin-right: .3rem; vertical-align: -1px; }
136.lg-vis::before { background: var(--accent); }
137.lg-pv::before { background: color-mix(in srgb, var(--accent) 28%, transparent); }
138.stats-chart { display: flex; align-items: flex-end; gap: 2px; height: 160px; margin-top: 1rem; overflow: hidden; }
139.sc-col { flex: 1 1 0; min-width: 0; display: flex; flex-direction: column; align-items: center; height: 100%; justify-content: flex-end; }
140.sc-bar { position: relative; width: 70%; max-width: 26px; min-width: 2px; height: 140px; display: flex; align-items: flex-end; }
141.sc-day { white-space: nowrap; }
142.sc-pv, .sc-vis { position: absolute; bottom: 0; left: 0; width: 100%; border-radius: 3px 3px 0 0; min-height: 2px; }
143.sc-pv { background: color-mix(in srgb, var(--accent) 28%, transparent); }
144.sc-vis { background: var(--accent); }
145.sc-day { font-size: .68rem; color: var(--ink-muted); margin-top: .35rem; }
146
147.stats-tops { display: grid; grid-template-columns: 1fr 1fr; gap: 1.2rem; }
148@media (max-width: 560px) { .stats-tops { grid-template-columns: 1fr; } }
149.stats-top { background: var(--paper-2); border: 1px solid var(--rule); border-radius: 12px; padding: 1rem 1.2rem; }
150.stats-top h2 { font-size: 1.05rem; margin: 0 0 .6rem; }
151.stats-top ol { margin: 0; padding-left: 1.2rem; }
152.stats-top li { padding: .35rem 0; display: flex; justify-content: space-between; gap: .5rem; align-items: baseline; }
153.stats-top a { color: var(--ink); text-decoration: none; }
154.stats-top a:hover { color: var(--accent); }
155.stats-count { color: var(--accent); font-weight: 700; font-variant-numeric: tabular-nums; }
156.stats-empty { color: var(--ink-muted); font-size: .88rem; margin: 0; }
157.stats-period { margin: .2rem 0 1.2rem; font-size: .9rem; color: var(--ink-muted); }
158.stats-period a { display: inline-block; margin-left: .4rem; padding: .2rem .6rem; border: 1px solid var(--rule); border-radius: 999px; text-decoration: none; color: var(--ink); }
159.stats-period a.is-on { background: var(--accent); color: #fff; border-color: var(--accent); }
160.stats-help { border: 1px solid var(--rule); border-radius: 12px; background: var(--paper-2); margin: 0 0 1.25rem; }
161.stats-help summary { cursor: pointer; padding: .7rem 1rem; font-weight: 600; font-size: .9rem; list-style: none; display: flex; align-items: center; gap: .5rem; }
162.stats-help summary::-webkit-details-marker { display: none; }
163.stats-help summary::before { content: '\203A'; font-size: 1.15rem; line-height: 1; color: var(--ink-soft); transition: transform .15s; }
164.stats-help[open] summary::before { transform: rotate(90deg); }
165.stats-help-body { padding: 0 1rem 1rem; }
166.stats-help-defs { margin: 0; }
167.stats-help-defs > div { display: grid; grid-template-columns: 130px 1fr; gap: .2rem 1.1rem; align-items: baseline;
168 padding: .55rem 0; border-top: 1px solid color-mix(in srgb, var(--rule) 55%, transparent); }
169.stats-help-defs dt { font-weight: 700; font-size: .85rem; color: var(--ink); }
170.stats-help-defs dd { margin: 0; font-size: .85rem; line-height: 1.5; color: var(--ink-soft); }
171.stats-help-defs dd em { font-style: italic; color: var(--ink); }
172.stats-help-note { margin: .85rem 0 0; padding-top: .7rem; border-top: 1px solid var(--rule);
173 font-size: .8rem; line-height: 1.55; color: var(--ink-muted); }
174@media (max-width: 480px) { .stats-help-defs > div { grid-template-columns: 1fr; gap: .1rem; } }
175</style>
Note: See TracBrowser for help on using the repository browser.