diff --git a/.env.example b/.env.example
index 3fe9d60..a461aee 100644
--- a/.env.example
+++ b/.env.example
@@ -9,3 +9,6 @@ CREEM_SPONSOR_DIAMOND_CHECKOUT_URL=https://www.creem.io/payment/prod_1lz1o8e7Htg
CREEM_SPONSOR_PLATINUM_CHECKOUT_URL=https://www.creem.io/payment/prod_4kpDvyYwr6XnRuMSysnI3s
CREEM_SPONSOR_GOLD_CHECKOUT_URL=https://www.creem.io/payment/prod_2NW3lMTPFHeOey3XhUYlM
CREEM_SPONSOR_SILVER_CHECKOUT_URL=https://www.creem.io/payment/prod_3uz4xoRD5vVePnDwRIJxoi
+
+# Read-only Tracwell MCP key for the traffic section on /stats.
+TRACWELL_MCP_KEY=
diff --git a/app/(marketing)/stats/page.tsx b/app/(marketing)/stats/page.tsx
index 27625ee..7137473 100644
--- a/app/(marketing)/stats/page.tsx
+++ b/app/(marketing)/stats/page.tsx
@@ -1,112 +1,123 @@
+import Link from "next/link"
+
import { buildPageMetadata } from "@/lib/seo"
+import { getSiteStats } from "@/lib/stats"
+import { getTraffic, type Ranked } from "@/lib/tracwell"
import { cn } from "@/lib/utils"
-import { getSiteStats, type CategoryCount, type WeekCommits } from "@/lib/stats"
export const revalidate = 3600
export const metadata = buildPageMetadata({
title: "Stats",
description:
- "Open numbers for 23rd: components shipped, GitHub stars, forks, contributors, and commits over the past year.",
+ "Open numbers for 23rd: traffic over the last 30 days, components shipped, GitHub stars, forks, contributors, and commits over the past year.",
path: "/stats",
- keywords: ["stats", "open source", "open metrics"],
+ keywords: ["stats", "open source", "open metrics", "analytics"],
})
const numberFormat = new Intl.NumberFormat("en")
-const weekFormat = new Intl.DateTimeFormat("en", {
- month: "short",
- day: "numeric",
- timeZone: "UTC",
+const changeFormat = new Intl.NumberFormat("en", {
+ maximumFractionDigits: 0,
+ signDisplay: "exceptZero",
})
-const monthFormat = new Intl.DateTimeFormat("en", {
+const dayFormat = new Intl.DateTimeFormat("en", {
month: "short",
+ day: "numeric",
timeZone: "UTC",
})
+const surface =
+ "rounded-xl bg-sheet p-4 shadow-[0_1px_2px_rgb(0_0_0/0.04)] sm:p-5"
+
function StatTile({
label,
value,
+ change,
className,
}: {
label: string
value: number | null
+ change?: number | null
className?: string
}) {
return (
-
+
{label}
-
- {value === null ? "–" : numberFormat.format(value)}
+
+
+ {value === null ? "–" : numberFormat.format(value)}
+
+ {change != null ? (
+
+ {changeFormat.format(change)}%
+
+ ) : null}
)
}
-function CommitChart({ weeks }: { weeks: WeekCommits[] }) {
- const max = Math.max(1, ...weeks.map((week) => week.total))
+type Bar = { key: string; label: string; value: number }
+
+function BarChart({
+ title,
+ unit,
+ bars,
+}: {
+ title: string
+ unit: string
+ bars: Bar[]
+}) {
+ const max = Math.max(1, ...bars.map((bar) => bar.value))
return (
-
-
- Commits per week
-
-
+
+ {title}
+
- {weeks.map((week) => {
- const date = new Date(week.week * 1000)
- return (
+ {bars.map((bar) => (
+
-
-
-
- {numberFormat.format(week.total)}
- {" "}
-
- week of {weekFormat.format(date)}
-
-
+ className="w-full rounded-t-[4px] bg-foreground/80 transition-colors group-hover:bg-foreground"
+ style={{
+ height: bar.value
+ ? `${Math.max(2, (bar.value / max) * 100)}%`
+ : "2px",
+ opacity: bar.value ? undefined : 0.15,
+ }}
+ />
+
+
+ {numberFormat.format(bar.value)}
+ {" "}
+ {bar.label}
- )
- })}
+
+ ))}
- {monthFormat.format(new Date(weeks[0].week * 1000))}
-
- {monthFormat.format(new Date(weeks[weeks.length - 1].week * 1000))}
-
+ {bars[0].label}
+ {bars[bars.length - 1].label}
- Commits per week over the past year
+ {title}
- | Week of |
- Commits |
+ Date |
+ {unit} |
- {weeks.map((week) => (
-
- | {weekFormat.format(new Date(week.week * 1000))} |
- {week.total} |
+ {bars.map((bar) => (
+
+ | {bar.label} |
+ {bar.value} |
))}
@@ -116,34 +127,46 @@ function CommitChart({ weeks }: { weeks: WeekCommits[] }) {
)
}
-function CategoryBars({ categories }: { categories: CategoryCount[] }) {
- const max = Math.max(1, ...categories.map((category) => category.count))
+function BarList({
+ title,
+ rows,
+ className,
+}: {
+ title: string
+ rows: Ranked[]
+ className?: string
+}) {
+ const max = Math.max(1, ...rows.map((row) => row.value))
return (
-
-
- Components by category
-
-
- {categories.map((category) => (
+
+ {title}
+
+ {rows.map((row) => (
-
-
- {category.name}
-
-
+ {row.label.startsWith("/") ? (
+
+ {row.label}
+
+ ) : (
+
+ {row.label}
+
+ )}
+
- {category.count}
+ {numberFormat.format(row.value)}
))}
@@ -153,11 +176,11 @@ function CategoryBars({ categories }: { categories: CategoryCount[] }) {
}
export default async function StatsPage() {
- const stats = await getSiteStats()
+ const [stats, traffic] = await Promise.all([getSiteStats(), getTraffic()])
return (
-
+
-
-
-
-
-
-
-
-
- {stats.weeks ?
: null}
+ {traffic ? (
+
+
+ Last 30 days
+
+
+
+
+
+ ({
+ key: day.date,
+ label: dayFormat.format(new Date(day.date)),
+ value: day.visitors,
+ }))}
+ />
+
+
+
+
+
+
+
+ ) : null}
-
+
+
+ Project
+
+
+
+
+
+
+
+
+ {stats.weeks ? (
+ ({
+ key: String(week.week),
+ label: `Week of ${dayFormat.format(new Date(week.week * 1000))}`,
+ value: week.total,
+ }))}
+ />
+ ) : null}
+ ({
+ label: category.name,
+ value: category.count,
+ }))}
+ />
+
)
diff --git a/lib/tracwell.ts b/lib/tracwell.ts
new file mode 100644
index 0000000..21dae2d
--- /dev/null
+++ b/lib/tracwell.ts
@@ -0,0 +1,138 @@
+const MCP_URL = "https://tracwell.app/mcp"
+const PROJECT_ID = "fa269223-7fa6-4ff7-a44a-3752aec9d15e"
+const RANGE_DAYS = 30
+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 Overview = {
+ metrics: { views: Metric; visitors: Metric }
+ trend: { date: string; views: number; visitors: number }[]
+ breakdowns: {
+ pages: Breakdown[]
+ sources: Breakdown[]
+ countries: Breakdown[]
+ deviceTypes: 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[]
+ sources: Ranked[]
+ countries: Ranked[]
+ devices: Ranked[]
+}
+
+const regionNames = new Intl.DisplayNames(["en"], { type: "region" })
+
+function countryName(code: string) {
+ try {
+ return regionNames.of(code) ?? code
+ } catch {
+ return code
+ }
+}
+
+function isoDate(ms: number) {
+ return new Date(ms).toISOString().slice(0, 10)
+}
+
+function top(
+ rows: Breakdown[],
+ limit: number,
+ label = (value: string) => value
+) {
+ return rows
+ .slice(0, limit)
+ .map((row) => ({ label: label(row.value), value: row.count }))
+}
+
+/**
+ * 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.
+ */
+export async function getTraffic(): Promise
{
+ const key = process.env.TRACWELL_MCP_KEY
+ if (!key) return null
+
+ const today = Date.UTC(
+ new Date().getUTCFullYear(),
+ new Date().getUTCMonth(),
+ new Date().getUTCDate()
+ )
+
+ try {
+ const res = await fetch(MCP_URL, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Accept: "application/json, text/event-stream",
+ Authorization: `Bearer ${key}`,
+ },
+ body: JSON.stringify({
+ jsonrpc: "2.0",
+ id: 1,
+ method: "tools/call",
+ params: {
+ name: "analytics_overview",
+ arguments: {
+ projectId: PROJECT_ID,
+ from: isoDate(today - RANGE_DAYS * DAY_MS),
+ to: isoDate(today - DAY_MS),
+ },
+ },
+ }),
+ signal: AbortSignal.timeout(8000),
+ })
+ if (!res.ok) {
+ console.warn(`Tracwell: ${res.status} ${res.statusText}`)
+ return null
+ }
+
+ const text = await res.text()
+ const payload = text.includes("data:")
+ ? text
+ .split("\n")
+ .filter((line) => line.startsWith("data:"))
+ .map((line) => line.slice(5))
+ .join("")
+ : text
+ const result = JSON.parse(payload).result as
+ { structuredContent?: Overview; isError?: boolean } | undefined
+ const overview = result?.structuredContent
+ if (!overview || result.isError) {
+ console.warn("Tracwell: no overview in response")
+ return null
+ }
+
+ const { metrics, trend, breakdowns } = overview
+ return {
+ views: metrics.views,
+ visitors: metrics.visitors,
+ days: trend.map(({ date, views, visitors }) => ({
+ date,
+ views,
+ visitors,
+ })),
+ pages: top(breakdowns.pages, 8),
+ 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)
+ return null
+ }
+}