'use client'; import * as Icons from './Icons'; import { useAdminFetch } from './useAdminFetch'; import AdminLoadState from './AdminLoadState'; import PageHeader from './PageHeader'; import { lastDays } from './analyticsDays'; interface AnalyticsData { trackingEnabled?: boolean; summary: { totalViews: number; lastUpdated: string; }; days: Record< string, { pageviews: number; pages: Record; devices: Record; } >; topPages: { path: string; count: number }[]; devices: { desktop: number; mobile: number }; } export default function AnalyticsView() { const analytics = useAdminFetch('/api/admin/analytics'); const data = analytics.data; if (!data || analytics.error) { return ( ); } const totalViews = data.summary?.totalViews || 0; const maxPageViews = data.topPages[0]?.count || 1; const hasDays = Object.keys(data.days || {}).length > 0; const daysList = lastDays(data.days || {}, 14, new Date()); const maxDayViews = Math.max(...daysList.map((d) => d.pageviews), 1); const desktopRatio = data.devices.desktop + data.devices.mobile > 0 ? Math.round((data.devices.desktop / (data.devices.desktop + data.devices.mobile)) * 100) : 50; return (
Refresh } /> {data.trackingEnabled === false && (
Analytics Tracking is Currently Disabled. No new visitor views are being recorded. You can re-enable tracking anytime under{' '} Settings → General.
)} {/* Top Metric Cards */}
Total Page Views {totalViews.toLocaleString()} Lifetime aggregate
Active Days Tracked {Object.keys(data.days || {}).length} Days recorded
Desktop vs Mobile {desktopRatio}% / {100 - desktopRatio}% Device distribution
{/* Daily Trends Chart */}

Daily Traffic Trend (Last 14 Days)

{!hasDays ? (

No pageviews recorded yet. Visit portfolio pages to start tracking!

) : (
{daysList.map(({ date, pageviews }) => { const heightPct = Math.round((pageviews / maxDayViews) * 100); const month = date.slice(5, 8); // "MM-" const day = date.slice(8); // "DD" return (
{/* A day with views keeps a sliver of bar; an empty one has none. */}
0 ? Math.max(heightPct, 3) : 0}%` }} > {pageviews}
{/* Hidden at phone width, where only the day fits (A-18). */} {month} {day}
); })}
)}
{/* Top Pages List */}

Top Visited Pages & Subpages

{data.topPages.length === 0 ? (

No pages recorded yet.

) : (
{data.topPages.map((p, idx) => { const widthPct = Math.round((p.count / maxPageViews) * 100); return (
#{idx + 1}
{p.path} {p.count} views
); })}
)}
); }