Changeset e3a5aed in Klonkt for src/routes/admin-stats.js


Ignore:
Timestamp:
06/30/2026 10:11:03 PM (2 months ago)
Author:
roboburr <roboburr@…>
Branches:
main
Children:
d9c5eb8
Parents:
65a0697
Message:

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@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/routes/admin-stats.js

    r65a0697 re3a5aed  
    1414import { requireGod } from '../middleware/auth.js';
    1515import { premiumUnlocked } from '../services/PatreonService.js';
    16 import { getStats } from '../services/StatsService.js';
     16import { getStats, currentIp, getExcludedIps, setExcludedIps } from '../services/StatsService.js';
    1717
    1818const router = express.Router();
     
    3232  }
    3333  const days = [7, 14, 30, 90].includes(parseInt(req.query.days, 10)) ? parseInt(req.query.days, 10) : 14;
     34  const myIp = currentIp(req);
    3435  renderPage(req, res, 'pages/admin-stats', {
    3536    pageTitleKey: 'admin.t_stats',
     
    3738    stats: getStats(days),
    3839    linkClicks,
     40    myIp,
     41    ipExcluded: !!myIp && getExcludedIps().includes(myIp),
    3942  });
    4043});
    4144
     45// Toggle whether the admin's current IP is counted in statistics.
     46router.post('/exclude-ip', requireGod, (req, res) => {
     47  const ip = currentIp(req);
     48  if (ip) {
     49    const list = getExcludedIps();
     50    const i = list.indexOf(ip);
     51    if (i >= 0) list.splice(i, 1); else list.push(ip);
     52    setExcludedIps(list);
     53  }
     54  res.redirect('/admin/stats');
     55});
     56
    4257export default router;
Note: See TracChangeset for help on using the changeset viewer.