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
4 changes: 2 additions & 2 deletions app/sponsor/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Metadata } from "next";
import { unstable_cache } from "next/cache";
import { getSupabaseAdmin } from "@/lib/supabase";
import { ownerTrafficSnapshot, parseSponsorTraffic } from "@/lib/sponsorship-analytics";
import { parseSponsorTraffic } from "@/lib/sponsorship-analytics";
import { PartnerPage } from "@/components/partner-page";

export const metadata: Metadata = {
Expand Down Expand Up @@ -41,5 +41,5 @@ export default async function PartnerRoute({ searchParams }: PartnerRouteProps)

const now = new Date();
const traffic = parseSponsorTraffic(await getCachedAudience(), now);
return <PartnerPage checkoutStatus={checkoutStatus} traffic={traffic ?? ownerTrafficSnapshot(now)} liveTraffic={traffic !== null} />;
return <PartnerPage checkoutStatus={checkoutStatus} traffic={traffic} />;
}
10 changes: 6 additions & 4 deletions components/partner-page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Navbar } from "@/components/navbar";
import { PartnerCheckoutForm } from "@/components/partner-checkout-form";
import { SPONSOR_CONTACT, sponsorshipMonthLabel } from "@/lib/sponsorship-config";
import { formatMonthlyVisitors, trafficPeriodLabel, type SponsorTraffic } from "@/lib/sponsorship-analytics";
import { formatMonthlyVisitors, ownerTrafficSnapshot, snapshotReportedMonth, trafficPeriodLabel, type SponsorTraffic } from "@/lib/sponsorship-analytics";

export function PartnerPage({ checkoutStatus, traffic = null, liveTraffic = false }: { checkoutStatus?: "success" | "cancelled"; traffic?: SponsorTraffic | null; liveTraffic?: boolean }) {
export function PartnerPage({ checkoutStatus, traffic = null }: { checkoutStatus?: "success" | "cancelled"; traffic?: SponsorTraffic | null }) {
const snapshot = ownerTrafficSnapshot();
const snapshotMonth = snapshotReportedMonth(snapshot);
const monthLabel = sponsorshipMonthLabel(new Date());
return (
<div className="flex min-h-screen flex-col bg-[#FFFDF8] text-zinc-900">
Expand All @@ -15,9 +17,9 @@ export function PartnerPage({ checkoutStatus, traffic = null, liveTraffic = fals
<p className="mt-5 max-w-xl text-base leading-relaxed text-zinc-600">A small, clearly labeled spot for your product, where developers reverse-engineer codebases and websites.</p>
<dl className="mt-8 grid max-w-lg grid-cols-2 gap-6">
<div><dd className="text-3xl font-bold tracking-tight sm:text-4xl">250K+</dd><dt className="mt-1 text-sm text-zinc-600">Lifetime visitors</dt></div>
<div title={traffic ? `${trafficPeriodLabel(traffic)}. Returning visitors may count again on different days.` : "Monthly traffic is temporarily unavailable."}><dd className="text-3xl font-bold tracking-tight sm:text-4xl">{traffic ? formatMonthlyVisitors(traffic.visitors) : "—"}</dd><dt className="mt-1 text-sm text-zinc-600">visitors last month</dt></div>
<div title={traffic ? `${trafficPeriodLabel(traffic)}. Returning visitors may count again on different days.` : `Owner-reported monthly audience, supplied ${snapshotMonth}.`}><dd className="text-3xl font-bold tracking-tight sm:text-4xl">{formatMonthlyVisitors(traffic?.visitors ?? snapshot.visitors)}</dd><dt className="mt-1 text-sm text-zinc-600">{traffic ? "visitors last month" : "Monthly visitors"}</dt></div>
</dl>
<p className="mt-3 text-xs text-zinc-400">{liveTraffic ? "Monthly traffic updates hourly" : traffic ? "Audience snapshot · October 2026" : "Monthly traffic temporarily unavailable"}</p>
<p className="mt-3 text-xs text-zinc-400">{traffic ? "Monthly traffic updates hourly" : `Audience snapshot · ${snapshotMonth}`}</p>
</header>
<section aria-label="Previous sponsors" className="my-10 border-y border-zinc-200 py-5">
<p className="text-xs text-zinc-500">Previous sponsors</p>
Expand Down
21 changes: 11 additions & 10 deletions lib/sponsorship-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ export function previousCalendarMonth(now: Date) {
};
}

/** The owner supplied this historical snapshot; it is never rolled into a different month. */
export function ownerTrafficSnapshot(now: Date): SponsorTraffic | null {
const { start } = previousCalendarMonth(now);
return start.toISOString() === "2026-09-01T00:00:00.000Z" ? {
visitors: 35000,
periodStart: "2026-09-01T00:00:00.000Z",
periodEnd: "2026-10-01T00:00:00.000Z",
measuredAt: "2026-10-08T07:32:00.000Z",
} : null;
/** Owner-reported monthly audience, dated when supplied rather than assigned an unverified period. */
export function ownerTrafficSnapshot() {
return {
visitors: 44256,
reportedAt: "2026-11-09T00:00:00.000Z",
};
}

export function formatMonthlyVisitors(visitors: number) {
return new Intl.NumberFormat("en-US", { notation: "compact", maximumFractionDigits: 0 }).format(visitors);
return new Intl.NumberFormat("en-US").format(visitors);
}

export function snapshotReportedMonth(snapshot: ReturnType<typeof ownerTrafficSnapshot>) {
return new Intl.DateTimeFormat("en-US", { month: "long", year: "numeric", timeZone: "UTC" }).format(new Date(snapshot.reportedAt));
}

export function trafficPeriodLabel(traffic: SponsorTraffic) {
Expand Down
19 changes: 14 additions & 5 deletions tests/sponsorship-analytics.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import assert from "node:assert/strict";
import test from "node:test";
import { formatMonthlyVisitors, ownerTrafficSnapshot, parseSponsorTraffic, previousCalendarMonth } from "../lib/sponsorship-analytics";
import { formatMonthlyVisitors, ownerTrafficSnapshot, parseSponsorTraffic, previousCalendarMonth, snapshotReportedMonth } from "../lib/sponsorship-analytics";
const now = new Date("2026-10-08T08:00:00Z");
const row = {window_key:"previous_calendar_month",window_start:"2026-09-01T00:00:00Z",window_end:"2026-10-01T00:00:00Z",daily_unique_visits:35087,missing_device_events:0,measured_at:"2026-10-08T07:43:00Z"};

test("previous complete calendar month handles year rollover", () => {
assert.equal(previousCalendarMonth(new Date("2027-01-01T00:00:00Z")).start.toISOString(), "2026-12-01T00:00:00.000Z");
});

test("monthly traffic displays 35K and never relabels last month's stale period", () => {
test("monthly traffic displays exact counts and never relabels last month's stale period", () => {
const result = parseSponsorTraffic([row],now);
assert.equal(result?.visitors,35087);
assert.equal(formatMonthlyVisitors(result!.visitors),"35K");
assert.equal(formatMonthlyVisitors(result!.visitors),"35,087");
assert.equal(parseSponsorTraffic([row],new Date("2026-11-01T00:00:00Z")),null);
assert.equal(ownerTrafficSnapshot(now)?.visitors,35000);
assert.equal(ownerTrafficSnapshot(new Date("2026-11-01T00:00:00Z")),null);
});

test("monthly analytics rejects errors, unsafe numbers, missing identifiers and stale measurements", () => {
for (const value of [null,{},[],[{...row,daily_unique_visits:-1}],[{...row,daily_unique_visits:null}],[{...row,daily_unique_visits:""}],[{...row,daily_unique_visits:"NaN"}],[{...row,missing_device_events:1}],[{...row,measured_at:"2026-10-06T00:00:00Z"}],[{...row,window_start:"2026-08-01T00:00:00Z"}],[{...row,window_key:"rolling_30_days"}]]) assert.equal(parseSponsorTraffic(value,now),null);
assert.equal(parseSponsorTraffic([{...row,daily_unique_visits:0}],now)?.visitors,0);
});

test("owner-reported monthly audience keeps the exact count and its supplied date", () => {
const snapshot = ownerTrafficSnapshot();
assert.equal(snapshot.visitors, 44256);
assert.equal(formatMonthlyVisitors(snapshot.visitors), "44,256");
assert.equal(snapshot.reportedAt, "2026-11-09T00:00:00.000Z");
assert.equal(snapshotReportedMonth(snapshot), "November 2026");
// A reported snapshot has no invented calendar-month range or automatic expiry.
assert.equal("periodStart" in snapshot, false);
assert.equal("periodEnd" in snapshot, false);
});
Loading