The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
update dynamic OG image for stats pages
Steven Tey5b4d0c4e87a5
diff --git a/apps/web/app/api/og/analytics/route.tsx b/apps/web/app/api/og/analytics/route.tsxindex d05e51f155..c97f1e2899 100644--- a/apps/web/app/api/og/analytics/route.tsx+++ b/apps/web/app/api/og/analytics/route.tsx@@ -1,24 +1,20 @@import { getAnalytics } from "@/lib/analytics";import { getDomainOrLink } from "@/lib/planetscale";-import { DUB_LOGO, nFormatter, truncate } from "@dub/utils";+import {+ GOOGLE_FAVICON_URL,+ getApexDomain,+ linkConstructor,+ nFormatter,+} from "@dub/utils";import { ImageResponse } from "next/og";import { NextRequest } from "next/server";export const runtime = "edge";-const satoshiBLack = fetch(- new URL("@/styles/Satoshi-Black.ttf", import.meta.url),-).then((res) => res.arrayBuffer());--const satoshiBold = fetch(- new URL("@/styles/Satoshi-Bold.ttf", import.meta.url),-).then((res) => res.arrayBuffer());-export async function GET(req: NextRequest) {- const [satoshiBlackData, satoshiBoldData] = await Promise.all([- satoshiBLack,- satoshiBold,- ]);+ const interMedium = await fetch(+ new URL("@/styles/Inter-Medium.ttf", import.meta.url),+ ).then((res) => res.arrayBuffer());const domain = req.nextUrl.searchParams.get("domain") || "dub.sh";const key = req.nextUrl.searchParams.get("key") || "github";@@ -30,94 +26,89 @@ export async function GET(req: NextRequest) {});}- const timeseries = await getAnalytics({+ const data = await getAnalytics({// projectId can be undefined (for public links that haven't been claimed/synced to a project)...(link.projectId && { projectId: link.projectId }),linkId: link.id,endpoint: "timeseries",- interval: "30d",+ interval: "24h",});- const maxClicks = Math.max(...timeseries.map((t) => t.clicks));- const totalClicks = timeseries.reduce((acc, t) => acc + t.clicks, 0);+ const clicks = data.reduce((acc, { clicks }) => acc + clicks, 0);return new ImageResponse((- <div- style={{- height: "100%",- width: "100%",- display: "flex",- flexDirection: "column",- alignItems: "center",- backgroundColor: "white",- backgroundImage: `url(https://assets.dub.co/background.png)`,- backgroundSize: "100% 100%",- backgroundPosition: "center",- backgroundRepeat: "no-repeat",- }}- >- <img- src={DUB_LOGO}- style={{- width: "80px",- height: "80px",- position: "absolute",- top: "40px",- right: "40px",- }}- />- <h1- style={{- fontSize: "90px",- fontFamily: "Satoshi Black",- background:- "linear-gradient(95.78deg, #C7BF00 21.66%, #E43838 86.47%)",- backgroundClip: "text",- color: "transparent",- marginTop: "50px",- lineHeight: "7rem",- }}- >- {domain}/{truncate(key, 12)}- </h1>- <p- style={{- fontSize: "50px",- fontFamily: "Satoshi Bold",- color: "black",- opacity: 0.8,- marginTop: "0px",- }}- >- {nFormatter(totalClicks)} clicks in the last 30 days- </p>- <div- style={{- position: "absolute",- bottom: "0px",- display: "flex",- flexDirection: "row",- alignItems: "flex-end",- justifyContent: "center",- marginTop: "50px",- }}- >- {timeseries.map(({ start, clicks }) => (- <div- key={start}- style={{- display: "flex",- flexDirection: "column",- alignItems: "center",- justifyContent: "center",- width: "25px",- height: `${(clicks / maxClicks) * 360}px`, // normalize clicks count to scale of 360px- marginRight: "12px",- backgroundColor: "#2563eb",- }}+ <div tw="flex flex-col bg-[#f9fafb] w-full h-full p-16">+ <div tw="flex justify-between items-center">+ <div tw="flex items-center">+ <img+ tw="rounded-full w-10 h-10"+ src={`${GOOGLE_FAVICON_URL}${getApexDomain(link.url || "dub.co")}`}+ alt="favicon"/>- ))}+ <h1 tw="text-4xl font-bold ml-4">+ {linkConstructor({ domain, key, pretty: true })}+ </h1>+ <svg+ xmlns="http://www.w3.org/2000/svg"+ fill="currentColor"+ viewBox="0 0 16 16"+ width="24"+ height="24"+ >+ <path+ fillRule="evenodd"+ d="M6.22 3.22a.75.75 0 011.06 0l4.25 4.25a.75.75 0 010 1.06l-4.25 4.25a.75.75 0 01-1.06-1.06L9.94 8 6.22 4.28a.75.75 0 010-1.06z"+ />+ </svg>+ </div>++ <div tw="flex items-center rounded-md border border-gray-200 bg-white shadow-sm h-12 px-6">+ <svg+ xmlns="http://www.w3.org/2000/svg"+ width="20"+ height="20"+ viewBox="0 0 24 24"+ fill="none"+ stroke="#4B5563"+ strokeWidth="2"+ strokeLinecap="round"+ strokeLinejoin="round"+ >+ <path d="M8 2v4" />+ <path d="M16 2v4" />+ <rect width="18" height="18" x="3" y="4" rx="2" />+ <path d="M3 10h18" />+ </svg>+ <p tw="text-gray-700 ml-2 mt-4">Last 24 hours</p>+ </div>+ </div>+ <div tw="flex flex-col h-full w-full rounded-lg border border-gray-200 bg-white shadow-lg overflow-hidden">+ <div tw="flex flex-col px-12 py-4">+ <div tw="flex items-center">+ <h1 tw="font-bold text-5xl leading-none">{nFormatter(clicks)}</h1>+ <svg+ fill="none"+ shapeRendering="geometricPrecision"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ viewBox="0 0 24 24"+ width="36"+ height="36"+ >+ <path d="M12 20V10" />+ <path d="M18 20V4" />+ <path d="M6 20v-4" />+ </svg>+ </div>+ <p tw="text-lg font-medium uppercase -mt-4 text-gray-600">+ Total Clicks+ </p>+ </div>++ <Chart data={data} /></div></div>),@@ -126,14 +117,53 @@ export async function GET(req: NextRequest) {height: 630,fonts: [{- name: "Satoshi Black",- data: satoshiBlackData,- },- {- name: "Satoshi Bold",- data: satoshiBoldData,+ name: "Inter Medium",+ data: interMedium,},],},);}++const Chart = ({ data }) => {+ // Define SVG size+ const width = 1100;+ const height = 370;++ // Find the max clicks value for Y-axis scaling+ const maxClicks = Math.max(...data.map((d) => d.clicks));++ // Function to convert date to X coordinate+ const scaleX = (index) => (width / (data.length - 1)) * index;++ // Function to convert clicks to Y coordinate+ const scaleY = (clicks) => height - (clicks / maxClicks) * height;++ // Extend the points to the bottom to create a closed shape for the fill+ let points = data+ .map((d, index) => `${scaleX(index)},${scaleY(d.clicks)}`)+ .join(" ");+ // Close the shape by drawing a line to the bottom right corner and bottom left corner+ points += ` ${width},${height} 0,${height}`;++ return (+ <svg+ viewBox={`0 0 ${width} ${height}`}+ style={{ color: "#3B82F6", marginLeft: "-4px", marginTop: "-32px" }}+ >+ <defs>+ <linearGradient id="customGradient" x1="0" y1="0" x2="0" y2="1">+ <stop offset="20%" stop-color="currentColor" stop-opacity="0.01" />+ <stop offset="100%" stop-color="currentColor" stop-opacity="0.2" />+ </linearGradient>+ </defs>++ <polyline+ fill="url(#customGradient)"+ stroke="currentColor"+ stroke-width="3"+ points={points}+ />+ </svg>+ );+};diff --git a/apps/web/lib/analytics.ts b/apps/web/lib/analytics.tsindex b003fecefe..ae556008ca 100644--- a/apps/web/lib/analytics.ts+++ b/apps/web/lib/analytics.ts@@ -52,7 +52,7 @@ export const intervalData = {},"90d": {startDate: new Date(Date.now() - 7776000000),- granularity: "month",+ granularity: "day",},all: {// Dub.co founding datediff --git a/apps/web/ui/analytics/clicks-chart.tsx b/apps/web/ui/analytics/clicks-chart.tsxindex 8ffb73607a..cdbb66d684 100644--- a/apps/web/ui/analytics/clicks-chart.tsx+++ b/apps/web/ui/analytics/clicks-chart.tsx@@ -33,7 +33,6 @@ export default function ClicksChart() {hour: "numeric",minute: "numeric",});- case "90d":case "all":return date.toLocaleDateString("en-US", {month: "short",