Files
immich-folio/app/api/admin/analytics/route.ts
Ralksta 2318409e75 chore(release): v0.10.0 (#400)
Photo Essay mode, client proofing, studio-modern preset, admin panel overhaul, watermark, cookieless analytics, subpage toggle, SEO title templates. Analytics API now admin-only, Trivy scan on publish. Album order mirrors the Immich timeline; map theme, admin status panel, lightbox counter and Docker health check fixed.

See CHANGELOG.md for the full list and the health-check upgrade note.
2026-08-10 09:56:20 +02:00

62 lines
1.8 KiB
TypeScript

import { NextResponse } from 'next/server';
import fs from 'fs/promises';
import path from 'path';
import { isAdminAuthenticated, isAdminEnabled } from '@/lib/admin/auth';
import { getConfigOrNull } from '@/lib/config';
const ANALYTICS_FILE = path.join(process.cwd(), 'content', 'analytics.json');
export async function GET() {
if (!isAdminEnabled()) {
return NextResponse.json({ error: 'Admin not enabled' }, { status: 403 });
}
if (!(await isAdminAuthenticated())) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
try {
const config = getConfigOrNull();
let data;
try {
const raw = await fs.readFile(ANALYTICS_FILE, 'utf8');
data = JSON.parse(raw);
} catch {
data = { summary: { totalViews: 0, lastUpdated: new Date().toISOString() }, days: {} };
}
// Compute top pages across all days
const topPagesMap: Record<string, number> = {};
let desktopCount = 0;
let mobileCount = 0;
Object.values(data.days || {}).forEach((day: any) => {
Object.entries(day.pages || {}).forEach(([p, count]: [string, any]) => {
topPagesMap[p] = (topPagesMap[p] || 0) + count;
});
desktopCount += day.devices?.desktop || 0;
mobileCount += day.devices?.mobile || 0;
});
const topPages = Object.entries(topPagesMap)
.map(([path, count]) => ({ path, count }))
.sort((a, b) => b.count - a.count)
.slice(0, 10);
return NextResponse.json({
trackingEnabled: config?.analytics !== false,
summary: data.summary,
days: data.days,
topPages,
devices: {
desktop: desktopCount,
mobile: mobileCount,
},
});
} catch (err: any) {
return NextResponse.json(
{ error: err.message || 'Failed to fetch analytics' },
{ status: 500 },
);
}
}