Changeset e3a5aed in Klonkt for src/services


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

Location:
src/services
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/services/StatsService.js

    r65a0697 re3a5aed  
    5454}
    5555
     56// The client IP (trust-proxy gives the real one), normalised: drop an IPv4-mapped-IPv6 prefix
     57// and a trailing :port so it matches what the admin sees + stores.
     58function clientIp(req) {
     59  let ip = (req && (req.ip || (req.socket && req.socket.remoteAddress))) || '';
     60  if (ip.startsWith('::ffff:')) ip = ip.slice(7);
     61  if (/^\d{1,3}(\.\d{1,3}){3}:\d+$/.test(ip)) ip = ip.split(':')[0];
     62  return ip;
     63}
     64export function currentIp(req) { return clientIp(req); }
     65
     66// Admin-configured IPs to skip — so an owner browsing logged-OUT (incognito, another browser)
     67// doesn't inflate their own stats. Stored as a comma-separated app_setting.
     68export function getExcludedIps() {
     69  return (getSetting('stats_exclude_ips', '') || '').split(',').map((s) => s.trim()).filter(Boolean);
     70}
     71export function setExcludedIps(list) {
     72  const clean = [...new Set((list || []).map((s) => String(s).trim()).filter(Boolean))].slice(0, 20);
     73  setSetting('stats_exclude_ips', clean.join(','));
     74}
     75function isExcludedIp(req) {
     76  try { const ip = clientIp(req); return !!ip && getExcludedIps().includes(ip); } catch { return false; }
     77}
     78
    5679// Lazy prepares — tables only exist after initializeDatabase(); this module is
    5780// imported before that call.
     
    90113
    91114export function recordPageview(siteId, req) {
    92   if (!siteId || isOperator(req) || isBot(req)) return;
     115  if (!siteId || isOperator(req) || isBot(req) || isExcludedIp(req)) return;
    93116  try {
    94117    const d = today();
     
    100123
    101124export function recordPostView(post, req) {
    102   if (!post || !post.id || isOperator(req) || isBot(req)) return;
     125  if (!post || !post.id || isOperator(req) || isBot(req) || isExcludedIp(req)) return;
    103126  try {
    104127    stmts().bumpPost.run(post.id);
  • src/services/i18n.js

    r65a0697 re3a5aed  
    356356    'astat.title': 'Statistieken',
    357357    'astat.intro': 'Cookievrij gemeten — geen tracking-cookies, geen toestemmingsbanner. Bezoekers worden per dag geteld via een dagelijks roterende, anonieme hash (IP/browser worden niet bewaard). Je eigen beheerder-bezoeken en bekende bots/crawlers tellen niet mee.',
     358    'astat.your_ip': 'Jouw IP', 'astat.ip_counted': 'wordt meegeteld.', 'astat.ip_not_counted': 'wordt NIET meegeteld.', 'astat.ip_exclude': 'Tel mijn bezoeken niet mee', 'astat.ip_count': 'Wel meetellen',
    358359    'astat.visitor_days': 'Bezoeker-dagen ({n}d)',
    359360    'astat.pageviews_days': 'Weergaven ({n}d)',
     
    12741275    'astat.title': 'Statistics',
    12751276    'astat.intro': 'Measured cookie-free — no tracking cookies, no consent banner. Visitors are counted per day via a daily-rotating, anonymous hash (IP/browser are not stored). Your own admin visits and known bots/crawlers are not counted.',
     1277    'astat.your_ip': 'Your IP', 'astat.ip_counted': 'is being counted.', 'astat.ip_not_counted': 'is NOT counted.', 'astat.ip_exclude': "Don't count my visits", 'astat.ip_count': 'Count my visits',
    12761278    'astat.visitor_days': 'Visitor-days ({n}d)',
    12771279    'astat.pageviews_days': 'Views ({n}d)',
     
    21912193    'astat.title': 'Statistiken',
    21922194    'astat.intro': 'Cookiefrei gemessen — keine Tracking-Cookies, kein Zustimmungsbanner. Besucher werden pro Tag über einen täglich rotierenden, anonymen Hash gezählt (IP/Browser werden nicht gespeichert). Deine eigenen Admin-Besuche und bekannte Bots/Crawler werden nicht mitgezählt.',
     2195    'astat.your_ip': 'Deine IP', 'astat.ip_counted': 'wird mitgezählt.', 'astat.ip_not_counted': 'wird NICHT mitgezählt.', 'astat.ip_exclude': 'Meine Besuche nicht zählen', 'astat.ip_count': 'Meine Besuche zählen',
    21932196    'astat.visitor_days': 'Besuchertage ({n}T)',
    21942197    'astat.pageviews_days': 'Aufrufe ({n}T)',
Note: See TracChangeset for help on using the changeset viewer.