source: Klonkt/src/routes/admin-stats.js@ 1794fac

main
Last change on this file since 1794fac was 1794fac, checked in by roboburr <roboburr@…>, 3 months ago

Pro stats (premium feature #5): referrers + period selector + all-time

Extension of the cookie-free StatsService.

  • DB: stat_referrer (site_id, host, count).
  • StatsService: recordPageview now also counts the external referrer host (Referer header; empty/own-site/invalid skipped, www- normalised). getStats(days) clamps to 7/14/30/90 + returns top-10 referrers (instance-wide) + all-time (pageviews, visitor days).
  • admin-stats route: ?days=7|14|30|90.
  • admin-stats view: period buttons, dynamic labels, Sources section, all-time row.

node --check passed.

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

  • Property mode set to 100644
File size: 1.5 KB
Line 
1/**
2 * Admin: Statistieken (premium-module, god-only).
3 *
4 * GET /admin/stats -> cookievrije statistieken: bezoekers/weergaven per dag,
5 * plays, en de populairste posts/tracks.
6 *
7 * Premium-gated via premiumUnlocked() (premium-laag uit = gewoon beschikbaar;
8 * aan = Patreon vereist). Tracking zit in StatsService (geen cookies).
9 */
10
11import express from 'express';
12import db from '../config/database.js';
13import { renderPage } from '../middleware/render.js';
14import { requireGod } from '../middleware/auth.js';
15import { premiumUnlocked } from '../services/PatreonService.js';
16import { getStats } from '../services/StatsService.js';
17
18const router = express.Router();
19
20router.get('/', requireGod, (req, res) => {
21 if (!premiumUnlocked()) {
22 return res.status(403).send('Statistieken is een premium-functie — koppel Patreon in Beheer → Instellingen.');
23 }
24 // Link-in-bio klikken (premium #6) voor de huidige site.
25 let linkClicks = [];
26 if (res.locals.site) {
27 try {
28 linkClicks = db.prepare(
29 'SELECT url, clicks FROM link_clicks WHERE site_id = ? AND clicks > 0 ORDER BY clicks DESC LIMIT 50'
30 ).all(res.locals.site.id);
31 } catch { linkClicks = []; }
32 }
33 const days = [7, 14, 30, 90].includes(parseInt(req.query.days, 10)) ? parseInt(req.query.days, 10) : 14;
34 renderPage(req, res, 'pages/admin-stats', {
35 pageTitle: 'Statistieken',
36 bodyClass: 'on-admin',
37 stats: getStats(days),
38 linkClicks,
39 });
40});
41
42export default router;
Note: See TracBrowser for help on using the repository browser.