Files
immich-folio/app/api/admin/analytics/route.ts
Ralksta a3a6319c29 release: v0.16.0
See CHANGELOG.md for the full v0.16.0 entry.

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: ralksta <ralksta@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Ralf <rohlenbo79@gmail.com>
Co-authored-by: Paul Thies <110185070+ImScheinox@users.noreply.github.com>
Co-authored-by: lancetm714 <108419425+lancetm714@users.noreply.github.com>
2026-09-22 14:12:13 +02:00

55 lines
1.6 KiB
TypeScript

import { NextResponse } from 'next/server';
import { withAdmin } from '@/lib/admin/withAdmin';
import fs from 'fs/promises';
import path from 'path';
import { getConfigOrNull } from '@/lib/config';
const ANALYTICS_FILE = path.join(process.cwd(), 'content', 'analytics.json');
export const GET = withAdmin(async () => {
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 },
);
}
});