Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 6 additions & 28 deletions app/(marketing)/stats/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@ export const revalidate = 3600
export const metadata = buildPageMetadata({
title: "Stats",
description:
"Open numbers for 23rd: traffic over the last 30 days, components shipped, GitHub stars, forks, contributors, and commits over the past year.",
"Open numbers for 23rd: lifetime traffic, components shipped, GitHub stars, forks, contributors, and commits over the past year.",
path: "/stats",
keywords: ["stats", "open source", "open metrics", "analytics"],
})

const numberFormat = new Intl.NumberFormat("en")
const changeFormat = new Intl.NumberFormat("en", {
maximumFractionDigits: 0,
signDisplay: "exceptZero",
})
const dayFormat = new Intl.DateTimeFormat("en", {
month: "short",
day: "numeric",
Expand All @@ -32,26 +28,17 @@ const surface =
function StatTile({
label,
value,
change,
className,
}: {
label: string
value: number | null
change?: number | null
className?: string
}) {
return (
<div className={cn("flex flex-col gap-1", surface, className)}>
<dt className="text-sm text-muted-foreground">{label}</dt>
<dd className="flex items-baseline gap-2">
<span className="text-3xl font-semibold tracking-tight tabular-nums">
{value === null ? "–" : numberFormat.format(value)}
</span>
{change != null ? (
<span className="text-sm text-muted-foreground tabular-nums">
{changeFormat.format(change)}%
</span>
) : null}
<dd className="text-3xl font-semibold tracking-tight tabular-nums">
{value === null ? "–" : numberFormat.format(value)}
</dd>
</div>
)
Expand Down Expand Up @@ -197,21 +184,12 @@ export default async function StatsPage() {
id="traffic-heading"
className="text-xl font-semibold tracking-tight"
>
Last 30 days
Traffic
</h2>
<dl className="grid grid-cols-2 gap-3">
<StatTile
label="Visitors"
value={traffic.visitors.value}
change={traffic.visitors.changePercent}
/>
<StatTile
label="Page views"
value={traffic.views.value}
change={traffic.views.changePercent}
/>
<StatTile label="Visitors" value={traffic.visitors} />
<StatTile label="Page views" value={traffic.views} />
</dl>
<BarList title="Top pages" rows={traffic.pages} />
<div className="grid gap-8 sm:grid-cols-2">
<BarList title="Sources" rows={traffic.sources} />
<BarList title="Countries" rows={traffic.countries} />
Expand Down
60 changes: 21 additions & 39 deletions lib/tracwell.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
const MCP_URL = "https://tracwell.app/mcp"
const PROJECT_ID = "fa269223-7fa6-4ff7-a44a-3752aec9d15e"
const RANGE_DAYS = 30
// First day of history (imported from Umami). Tracwell caps a report at 732
// days, so lifetime is clamped to the most recent 731.
const FIRST_DAY = Date.UTC(2026, 7, 16)
const MAX_RANGE_DAYS = 731
const DAY_MS = 24 * 60 * 60 * 1000

type Metric = { value: number; previous: number; changePercent: number | null }
type Breakdown = { value: string; count: number; views: number }
type Breakdown = { value: string; count: number }

type Overview = {
metrics: { views: Metric; visitors: Metric }
trend: { date: string; views: number; visitors: number }[]
breakdowns: {
pages: Breakdown[]
sources: Breakdown[]
countries: Breakdown[]
deviceTypes: Breakdown[]
}
metrics: { views: { value: number }; visitors: { value: number } }
breakdowns: { sources: Breakdown[]; countries: Breakdown[] }
}

export type Ranked = { label: string; value: number }
export type Day = { date: string; visitors: number; views: number }

export type Traffic = {
views: Metric
visitors: Metric
days: Day[]
pages: Ranked[]
views: number
visitors: number
sources: Ranked[]
countries: Ranked[]
devices: Ranked[]
}

const regionNames = new Intl.DisplayNames(["en"], { type: "region" })
Expand Down Expand Up @@ -55,19 +47,20 @@ function top(
}

/**
* The last 30 full days of traffic from Tracwell's read-only MCP endpoint.
* One stateless tools/call; the answer comes back as a single SSE message.
* Today is left out so the chart doesn't end on a half day.
* Lifetime traffic from Tracwell's read-only MCP endpoint. One stateless
* tools/call; the answer comes back as a single SSE message.
*/
export async function getTraffic(): Promise<Traffic | null> {
const key = process.env.TRACWELL_MCP_KEY
if (!key) return null

const now = new Date()
const today = Date.UTC(
new Date().getUTCFullYear(),
new Date().getUTCMonth(),
new Date().getUTCDate()
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate()
)
const from = Math.max(FIRST_DAY, today - MAX_RANGE_DAYS * DAY_MS)

try {
const res = await fetch(MCP_URL, {
Expand All @@ -85,8 +78,8 @@ export async function getTraffic(): Promise<Traffic | null> {
name: "analytics_overview",
arguments: {
projectId: PROJECT_ID,
from: isoDate(today - RANGE_DAYS * DAY_MS),
to: isoDate(today - DAY_MS),
from: isoDate(from),
to: isoDate(today),
},
},
}),
Expand All @@ -113,23 +106,12 @@ export async function getTraffic(): Promise<Traffic | null> {
return null
}

const { metrics, trend, breakdowns } = overview
const { metrics, breakdowns } = overview
return {
views: metrics.views,
visitors: metrics.visitors,
days: trend.map(({ date, views, visitors }) => ({
date,
views,
visitors,
})),
pages: top(breakdowns.pages, 8),
views: metrics.views.value,
visitors: metrics.visitors.value,
sources: top(breakdowns.sources, 6),
countries: top(breakdowns.countries, 6, countryName),
devices: top(
breakdowns.deviceTypes.filter((row) => row.value !== "unknown"),
3,
(value) => value.charAt(0).toUpperCase() + value.slice(1)
),
}
} catch (error) {
console.warn("Tracwell: request failed", error)
Expand Down
Loading