The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add synced timeseries charts
Tim Wilsone87416b9f000
diff --git a/apps/web/app/api/partner-profile/programs/[programId]/analytics/route.ts b/apps/web/app/api/partner-profile/programs/[programId]/analytics/route.tsindex 11561528ef..1e3a38353b 100644--- a/apps/web/app/api/partner-profile/programs/[programId]/analytics/route.ts+++ b/apps/web/app/api/partner-profile/programs/[programId]/analytics/route.ts@@ -17,7 +17,6 @@ export const GET = withPartnerProfile(workspaceId: true,domain: true,key: true,- linkId: true,externalId: true,tenantId: true,})diff --git a/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/links/page-client.tsx b/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/links/page-client.tsxindex 5010a40989..c5735769e3 100644--- a/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/links/page-client.tsx+++ b/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/links/page-client.tsx@@ -1,97 +1,65 @@"use client";+import { intervals } from "@/lib/analytics/constants";import usePartnerLinks from "@/lib/swr/use-partner-links";-import { CardList, CopyButton, LinkLogo } from "@dub/ui";-import { ArrowTurnRight2, CursorRays } from "@dub/ui/icons";-import { getApexDomain, getPrettyUrl, nFormatter } from "@dub/utils";-import Link from "next/link";+import { CardList } from "@dub/ui";+import { ChartTooltipSync } from "@dub/ui/charts";+import { createContext, useContext } from "react";+import { PartnerLinkCard } from "./partner-link-card";++const PartnerLinksContext = createContext<{+ start: Date;+ end: Date;+ interval: (typeof intervals)[number];+} | null>(null);++export function usePartnerLinksContext() {+ const context = useContext(PartnerLinksContext);+ if (!context)+ throw new Error(+ "usePartnerLinksContext must be used within a PartnerLinksContext.Provider",+ );++ return context;+}export function ProgramLinksPageClient() {const { links, error, loading } = usePartnerLinks();- return (- <CardList>- {error ? (- <div className="flex items-center justify-center px-5 py-3">- <p className="text-sm text-neutral-600">Failed to load links.</p>- </div>- ) : loading ? (- [...Array(3)].map((_, i) => (- <CardList.Card- key={i}- innerClassName="flex items-center justify-between gap-4 h-[66px]"- >- <div className="flex min-w-0 items-center gap-3">- <div className="relative hidden size-8 shrink-0 animate-pulse rounded-full bg-neutral-200 sm:flex" />- <div className="flex min-w-0 flex-col gap-1.5">- <div className="h-5 w-32 animate-pulse rounded-md bg-neutral-200" />- <div className="h-4 w-48 animate-pulse rounded-md bg-neutral-200" />- </div>- </div>- <div className="h-7 w-16 animate-pulse rounded-md bg-neutral-200" />- </CardList.Card>- ))- ) : (- links?.map((link) => (- <CardList.Card- key={link.id}- innerClassName="flex items-center justify-between gap-4"- >- <div className="flex min-w-0 items-center gap-3">- <div className="relative hidden shrink-0 items-center justify-center sm:flex">- <div className="absolute inset-0 shrink-0 rounded-full border border-neutral-200">- <div className="h-full w-full rounded-full border border-white bg-gradient-to-t from-neutral-100" />- </div>- <div className="relative p-2.5">- <LinkLogo- apexDomain={getApexDomain(link.url)}- className="size-4 sm:size-6"- />- </div>- </div>+ const start = new Date("2025-01-21");+ const end = new Date("2025-02-21");+ const interval = "24h";- <div className="flex min-w-0 flex-col">- <div className="flex items-center gap-2">- <a- href={link.shortLink}- target="_blank"- rel="noopener noreferrer"- className="truncate text-sm font-semibold leading-6 text-neutral-700 transition-colors hover:text-black"- >- {getPrettyUrl(link.shortLink)}- </a>- <CopyButton- value={link.shortLink}- variant="neutral"- className="p-1.5"- />- </div>- <div className="flex items-center gap-1">- <ArrowTurnRight2 className="h-3 w-3 shrink-0 text-neutral-400" />- <a- href={link.url}- target="_blank"- rel="noopener noreferrer"- className="truncate text-sm text-neutral-500 transition-colors hover:text-neutral-700"- >- {getPrettyUrl(link.url)}- </a>- </div>- </div>+ return (+ <PartnerLinksContext.Provider value={{ start, end, interval }}>+ <ChartTooltipSync>+ <CardList>+ {error ? (+ <div className="flex items-center justify-center px-5 py-3">+ <p className="text-sm text-neutral-600">Failed to load links.</p></div>+ ) : loading ? (+ [...Array(3)].map((_, i) => <LinkCardSkeleton key={i} />)+ ) : (+ links?.map((link) => <PartnerLinkCard key={link.id} link={link} />)+ )}+ </CardList>+ </ChartTooltipSync>+ </PartnerLinksContext.Provider>+ );+}- <div className="flex items-center gap-2">- <Link- href="#"- className="flex items-center gap-1 overflow-hidden rounded-md border border-neutral-200 bg-neutral-50 px-2 py-0.5 text-sm text-neutral-600 transition-colors hover:bg-white"- >- <CursorRays className="h-4 w-4 text-neutral-600" />- <span>{nFormatter(link.clicks)}</span>- </Link>- </div>- </CardList.Card>- ))- )}- </CardList>+function LinkCardSkeleton() {+ return (+ <CardList.Card innerClassName="flex items-center justify-between gap-4 h-[66px]">+ <div className="flex min-w-0 items-center gap-3">+ <div className="relative hidden size-8 shrink-0 animate-pulse rounded-full bg-neutral-200 sm:flex" />+ <div className="flex min-w-0 flex-col gap-1.5">+ <div className="h-5 w-32 animate-pulse rounded-md bg-neutral-200" />+ <div className="h-4 w-48 animate-pulse rounded-md bg-neutral-200" />+ </div>+ </div>+ <div className="h-7 w-16 animate-pulse rounded-md bg-neutral-200" />+ </CardList.Card>);}diff --git a/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/links/page.tsx b/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/links/page.tsxindex 5943ac4905..463c694d52 100644--- a/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/links/page.tsx+++ b/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/links/page.tsx@@ -6,7 +6,7 @@ import { ProgramLinksPageClient } from "./page-client";export default function ProgramLinks() {return (<PageContent title="Links" hideReferButton>- <MaxWidthWrapper>+ <MaxWidthWrapper className="pb-10"><ProgramLinksPageClient /></MaxWidthWrapper></PageContent>diff --git a/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/links/partner-link-card.tsx b/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/links/partner-link-card.tsxnew file mode 100644index 0000000000..a496489b1b--- /dev/null+++ b/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/links/partner-link-card.tsx@@ -0,0 +1,219 @@+import { formatDateTooltip } from "@/lib/analytics/format-date-tooltip";+import usePartnerAnalytics from "@/lib/swr/use-partner-analytics";+import { PartnerLinkProps } from "@/lib/types";+import {+ ArrowTurnRight2,+ CardList,+ CopyButton,+ CursorRays,+ LinkLogo,+ LoadingSpinner,+} from "@dub/ui";+import { Areas, TimeSeriesChart, XAxis } from "@dub/ui/charts";+import {+ currencyFormatter,+ getApexDomain,+ getPrettyUrl,+ nFormatter,+} from "@dub/utils";+import Link from "next/link";+import { ComponentProps, useMemo } from "react";+import { usePartnerLinksContext } from "./page-client";++const CHARTS = [+ {+ key: "clicks",+ label: "Clicks",+ colorClassName: "text-blue-500",+ },+ {+ key: "leads",+ label: "Leads",+ colorClassName: "text-purple-500",+ },+ {+ key: "saleAmount",+ label: "Sales",+ colorClassName: "text-teal-500",+ currency: true,+ },+];++export function PartnerLinkCard({ link }: { link: PartnerLinkProps }) {+ const { start, end, interval } = usePartnerLinksContext();++ const { data: totals } = usePartnerAnalytics({+ linkId: link.id,+ event: "composite",+ interval,+ start,+ end,+ });++ const { data: timeseries, error } = usePartnerAnalytics({+ linkId: link.id,+ groupBy: "timeseries",+ event: "composite",+ interval,+ start,+ end,+ });++ const chartData = useMemo(() => {+ return timeseries?.map(({ start, clicks, leads, saleAmount }) => ({+ date: new Date(start),+ values: { clicks, leads, saleAmount: saleAmount / 100 },+ }));+ }, [timeseries]);++ return (+ <CardList.Card innerClassName="py-4" hoverStateEnabled={false}>+ <div className="flex items-center justify-between gap-4">+ <div className="flex min-w-0 items-center gap-3">+ <div className="relative hidden shrink-0 items-center justify-center sm:flex">+ <div className="absolute inset-0 shrink-0 rounded-full border border-neutral-200">+ <div className="h-full w-full rounded-full border border-white bg-gradient-to-t from-neutral-100" />+ </div>+ <div className="relative p-2.5">+ <LinkLogo+ apexDomain={getApexDomain(link.url)}+ className="size-4 sm:size-6"+ />+ </div>+ </div>++ <div className="flex min-w-0 flex-col">+ <div className="flex items-center gap-2">+ <a+ href={link.shortLink}+ target="_blank"+ rel="noopener noreferrer"+ className="truncate text-sm font-semibold leading-6 text-neutral-700 transition-colors hover:text-black"+ >+ {getPrettyUrl(link.shortLink)}+ </a>+ <CopyButton+ value={link.shortLink}+ variant="neutral"+ className="p-1.5"+ />+ </div>+ <div className="flex items-center gap-1">+ <ArrowTurnRight2 className="h-3 w-3 shrink-0 text-neutral-400" />+ <a+ href={link.url}+ target="_blank"+ rel="noopener noreferrer"+ className="truncate text-sm text-neutral-500 transition-colors hover:text-neutral-700"+ >+ {getPrettyUrl(link.url)}+ </a>+ </div>+ </div>+ </div>++ <div className="flex items-center gap-2">+ <Link+ href="#"+ className="flex items-center gap-1 overflow-hidden rounded-md border border-neutral-200 bg-neutral-50 px-2 py-0.5 text-sm text-neutral-600 transition-colors hover:bg-white"+ >+ <CursorRays className="h-4 w-4 text-neutral-600" />+ <span>{nFormatter(link.clicks)}</span>+ </Link>+ </div>+ </div>+ <div className="mt-4 grid grid-cols-1 gap-4 sm:grid-cols-3 lg:gap-6">+ {CHARTS.map((chart) => (+ <div+ key={chart.key}+ className="rounded-lg border border-neutral-200 px-1.5 py-1.5 lg:px-3"+ >+ <div className="flex flex-col gap-1 pl-3 pt-3 lg:pl-1.5">+ <span className="text-sm font-semibold leading-none text-neutral-800">+ {chart.label}+ </span>+ {totals ? (+ <span className="text-base font-medium leading-none text-neutral-600">+ {chart.currency+ ? currencyFormatter(totals[chart.key] / 100, {+ minimumFractionDigits: 2,+ maximumFractionDigits: 2,+ })+ : nFormatter(totals[chart.key])}+ </span>+ ) : (+ <div className="h-4 w-12 animate-pulse rounded bg-neutral-200" />+ )}+ </div>+ <div className="h-18 sm:h-24">+ {chartData ? (+ <LinkEventsChart+ data={chartData}+ series={{+ id: chart.key,+ valueAccessor: (d) => d.values[chart.key],+ colorClassName: chart.colorClassName,+ isActive: true,+ }}+ currency={chart.currency}+ />+ ) : (+ <div className="flex size-full items-center justify-center">+ <LoadingSpinner className="size-4" />+ </div>+ )}+ </div>+ </div>+ ))}+ </div>+ </CardList.Card>+ );+}++function LinkEventsChart({+ data,+ series,+ currency,+}: {+ data: ComponentProps<typeof TimeSeriesChart>["data"];+ series: ComponentProps<typeof TimeSeriesChart>["series"][number];+ currency?: boolean;+}) {+ const { start, end, interval } = usePartnerLinksContext();++ return (+ <div className="relative size-full">+ <TimeSeriesChart+ data={data}+ series={[series]}+ tooltipClassName="p-0"+ tooltipContent={(d) => {+ return (+ <>+ <div className="flex justify-between gap-6 whitespace-nowrap p-2 text-xs leading-none">+ <span className="font-medium text-neutral-700">+ {formatDateTooltip(d.date, {+ interval,+ start,+ end,+ })}+ </span>+ <p className="text-right text-neutral-500">+ {currency+ ? currencyFormatter(series.valueAccessor(d), {+ minimumFractionDigits: 2,+ maximumFractionDigits: 2,+ })+ : nFormatter(series.valueAccessor(d))}+ </p>+ </div>+ </>+ );+ }}+ >+ <XAxis showAxisLine={false} highlightLast={false} />+ <Areas showLatestValueCircle={false} />+ </TimeSeriesChart>+ </div>+ );+}diff --git a/apps/web/lib/analytics/types.ts b/apps/web/lib/analytics/types.tsindex 7a8c76c90c..f34c732c30 100644--- a/apps/web/lib/analytics/types.ts+++ b/apps/web/lib/analytics/types.ts@@ -53,6 +53,7 @@ const partnerAnalyticsSchema = analyticsQuerySchemastart: true,end: true,groupBy: true,+ linkId: true,}).partial();diff --git a/apps/web/lib/swr/use-partner-analytics.ts b/apps/web/lib/swr/use-partner-analytics.tsindex 763c0b898a..45ec39535c 100644--- a/apps/web/lib/swr/use-partner-analytics.ts+++ b/apps/web/lib/swr/use-partner-analytics.ts@@ -22,6 +22,7 @@ export default function usePartnerAnalytics({event: params?.event ?? "composite",groupBy: params?.groupBy ?? "count",+ ...(params?.linkId && { linkId: params.linkId }),...VALID_ANALYTICS_FILTERS.reduce((acc, filter) => ({...acc,diff --git a/packages/ui/src/charts/areas.tsx b/packages/ui/src/charts/areas.tsxindex ab1dfe0731..1592488809 100644--- a/packages/ui/src/charts/areas.tsx+++ b/packages/ui/src/charts/areas.tsx@@ -8,6 +8,7 @@ import { useChartContext, useChartTooltipContext } from "./chart-context";export function Areas({seriesStyles,+ showLatestValueCircle = true,}: {seriesStyles?: {id: string;@@ -16,6 +17,7 @@ export function Areas({areaFill?: string;lineStroke?: string;}[];+ showLatestValueCircle?: boolean;}) {const { data, series, margin, xScale, yScale, startDate, endDate } =useChartContext();@@ -114,8 +116,8 @@ export function Areas({)}</Area>- {/* Latest value dot */}- {!tooltipData && (+ {/* Latest value circle */}+ {showLatestValueCircle && !tooltipData && (<Circlecx={xScale(data.at(-1)!.date)}cy={yScale(s.valueAccessor(data.at(-1)!))}