Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add analytics tabs

Tim Wilson5d2bb8f3ba06
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/analytics/analytics-chart.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/analytics/analytics-chart.tsx
index 37fe5c7d9d..90f989b80b 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/analytics/analytics-chart.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/analytics/analytics-chart.tsx
@@ -1,6 +1,12 @@
+import { ANALYTICS_SALE_UNIT } from "@/lib/analytics/constants";
import { formatDateTooltip } from "@/lib/analytics/format-date-tooltip";
+import {
+ AnalyticsResponseOptions,
+ AnalyticsSaleUnit,
+} from "@/lib/analytics/types";
import { editQueryString } from "@/lib/analytics/utils";
-import { Combobox, useRouterStuff } from "@dub/ui";
+import { AnalyticsTabs } from "@/ui/analytics/analytics-tabs";
+import { useRouterStuff } from "@dub/ui";
import {
Areas,
ChartContext,
@@ -9,28 +15,40 @@ import {
YAxis,
} from "@dub/ui/charts";
import { LoadingSpinner } from "@dub/ui/icons";
-import { currencyFormatter, fetcher, nFormatter } from "@dub/utils";
-import NumberFlow from "@number-flow/react";
+import { capitalize, currencyFormatter, fetcher, nFormatter } from "@dub/utils";
import { LinearGradient } from "@visx/gradient";
import { useContext, useId, useMemo } from "react";
import useSWR from "swr";
import { ProgramAnalyticsContext } from "./page-client";
-const chartOptions = [
- { value: "sales", label: "Revenue" },
- { value: "leads", label: "Leads" },
- { value: "clicks", label: "Clicks" },
-];
-
export function AnalyticsChart() {
const id = useId();
- const { queryParams } = useRouterStuff();
+ const { queryParams, searchParams } = useRouterStuff();
const { start, end, interval, event, queryString } = useContext(
ProgramAnalyticsContext,
);
+ const saleUnit: AnalyticsSaleUnit = useMemo(
+ () =>
+ ANALYTICS_SALE_UNIT.find((u) => u === searchParams.get("saleUnit")) ||
+ "saleAmount",
+ [searchParams.get("saleUnit")],
+ );
+
+ const { data: totalEvents } = useSWR<{
+ [key in AnalyticsResponseOptions]: number;
+ }>(
+ `/api/analytics?${editQueryString(queryString ?? "", {
+ event: "composite",
+ })}`,
+ fetcher,
+ {
+ keepPreviousData: true,
+ },
+ );
+
const { data, error } = useSWR<
{
start: Date;
@@ -52,59 +70,41 @@ export function AnalyticsChart() {
data?.map((d) => ({
date: new Date(d.start),
values: {
- amount: event === "sales" ? d.saleAmount / 100 : d[event],
+ amount:
+ event === "sales" && saleUnit === "saleAmount"
+ ? d.saleAmount / 100
+ : d[event],
},
})),
- [data, event],
- );
-
- const total = useMemo(
- () => chartData?.reduce((acc, curr) => acc + curr.values.amount, 0),
- [chartData],
+ [data, event, saleUnit],
);
const dataLoading = !chartData && !error;
return (
<div>
- <div>
- <div className="-ml-2 flex justify-between gap-2">
- <Combobox
- selected={chartOptions.find(({ value }) => value === event) || null}
- setSelected={(option) =>
- option && queryParams({ set: { event: option.value } })
- }
- options={chartOptions}
- optionClassName="w-36"
- caret={true}
- hideSearch={true}
- buttonProps={{
- variant: "outline",
- className: "h-7 w-fit px-2 text-content-subtle",
- }}
- />
- </div>
-
- <div className="flex flex-col gap-1">
- {total === undefined ? (
- <div className="h-[45px] w-24 animate-pulse rounded-md bg-neutral-200" />
- ) : (
- <NumberFlow
- value={total}
- className="text-3xl text-neutral-800"
- format={
- event === "sales"
- ? {
- style: "currency",
- currency: "USD",
- }
- : undefined
- }
- />
- )}
- </div>
+ <div className="border-b border-neutral-200">
+ <AnalyticsTabs
+ showConversions={true}
+ totalEvents={totalEvents}
+ tab={event}
+ tabHref={(id) =>
+ queryParams({
+ set: {
+ event: id,
+ },
+ getNewPath: true,
+ }) as string
+ }
+ saleUnit={saleUnit}
+ setSaleUnit={(option) =>
+ queryParams({
+ set: { saleUnit: option },
+ })
+ }
+ />
</div>
- <div className="relative mt-4 h-72 md:h-96">
+ <div className="relative mt-4 h-72 p-6 md:h-96">
{dataLoading ? (
<div className="flex size-full items-center justify-center">
<LoadingSpinner />
@@ -136,14 +136,11 @@ export function AnalyticsChart() {
<div className="flex items-center gap-2">
<div className="h-2 w-2 rounded-sm bg-violet-500 shadow-[inset_0_0_0_1px_#0003]" />
<p className="capitalize text-neutral-600">
- {
- chartOptions.find(({ value }) => value === event)
- ?.label
- }
+ {capitalize(event)}
</p>
</div>
<p className="text-right font-medium text-neutral-900">
- {event === "sales"
+ {event === "sales" && saleUnit === "saleAmount"
? currencyFormatter(d.values.amount, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
@@ -174,7 +171,11 @@ export function AnalyticsChart() {
/>
<YAxis
showGridLines
- tickFormat={event === "sales" ? currencyFormatter : nFormatter}
+ tickFormat={
+ event === "sales" && saleUnit === "saleAmount"
+ ? currencyFormatter
+ : nFormatter
+ }
/>
<Areas
seriesStyles={[
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/analytics/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/analytics/page-client.tsx
index dfe80113ac..752174db40 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/analytics/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/analytics/page-client.tsx
@@ -101,9 +101,7 @@ export function ProgramAnalyticsPageClient() {
value={{ start, end, interval, event, queryString }}
>
<div className="border-border-subtle divide-border-subtle divide-y overflow-hidden rounded-2xl border">
- <div className="p-6">
- <AnalyticsChart />
- </div>
+ <AnalyticsChart />
<div>
<AnalyticsPartnersTable />
</div>
diff --git a/apps/web/ui/analytics/analytics-tabs.tsx b/apps/web/ui/analytics/analytics-tabs.tsx
new file mode 100644
index 0000000000..99ba43f078
--- /dev/null
+++ b/apps/web/ui/analytics/analytics-tabs.tsx
@@ -0,0 +1,170 @@
+import {
+ AnalyticsResponseOptions,
+ AnalyticsSaleUnit,
+ EventType,
+} from "@/lib/analytics/types";
+import { ToggleGroup } from "@dub/ui";
+import { cn } from "@dub/utils";
+import NumberFlow, { NumberFlowGroup } from "@number-flow/react";
+import { ChevronRight, Lock } from "lucide-react";
+import Link from "next/link";
+import { useMemo } from "react";
+
+type Tab = {
+ id: EventType;
+ label: string;
+ colorClassName: string;
+ conversions: boolean;
+};
+
+export function AnalyticsTabs({
+ showConversions,
+ totalEvents,
+ tab,
+ tabHref,
+ saleUnit,
+ setSaleUnit,
+ requiresUpgrade,
+ showPaywall,
+}: {
+ showConversions?: boolean;
+ totalEvents?: { [key in AnalyticsResponseOptions]: number };
+ tab: Tab["id"];
+ tabHref: (id: Tab["id"]) => string;
+ saleUnit: AnalyticsSaleUnit;
+ setSaleUnit: (saleUnit: AnalyticsSaleUnit) => void;
+ requiresUpgrade?: boolean;
+ showPaywall?: boolean;
+}) {
+ const tabs = useMemo(
+ () =>
+ [
+ {
+ id: "clicks",
+ label: "Clicks",
+ colorClassName: "text-blue-500/50",
+ conversions: false,
+ },
+ ...(showConversions
+ ? [
+ {
+ id: "leads",
+ label: "Leads",
+ colorClassName: "text-violet-600/50",
+ conversions: true,
+ },
+ {
+ id: "sales",
+ label: "Sales",
+ colorClassName: "text-teal-400/50",
+ conversions: true,
+ },
+ ]
+ : []),
+ ] as Tab[],
+ [showConversions],
+ );
+
+ return (
+ <div className="grid w-full grid-cols-3 divide-x divide-neutral-200 overflow-y-hidden">
+ <NumberFlowGroup>
+ {tabs.map(({ id, label, colorClassName }, idx) => {
+ return (
+ <div key={id} className="relative z-0">
+ {idx > 0 && (
+ <div className="absolute left-0 top-1/2 z-10 -translate-x-1/2 -translate-y-1/2 rounded-full border border-neutral-200 bg-white p-1.5">
+ <ChevronRight
+ className="h-3 w-3 text-neutral-400"
+ strokeWidth={2.5}
+ />
+ </div>
+ )}
+ <Link
+ className={cn(
+ "border-box relative block h-full min-w-[110px] flex-none px-4 py-3 sm:min-w-[240px] sm:px-8 sm:py-6",
+ "transition-colors hover:bg-neutral-50 focus:outline-none active:bg-neutral-100",
+ "ring-inset ring-neutral-500 focus-visible:ring-1 sm:first:rounded-tl-xl",
+ )}
+ href={tabHref(id)}
+ aria-current
+ >
+ {/* Active tab indicator */}
+ <div
+ className={cn(
+ "absolute bottom-0 left-0 h-0.5 w-full bg-black transition-transform duration-100",
+ tab !== id && "translate-y-[3px]", // Translate an extra pixel to avoid sub-pixel issues
+ )}
+ />
+
+ <div className="flex items-center gap-2.5 text-sm text-neutral-600">
+ <div
+ className={cn(
+ "h-2 w-2 rounded-sm bg-current shadow-[inset_0_0_0_1px_#00000019]",
+ colorClassName,
+ )}
+ />
+ <span>{label}</span>
+ </div>
+ <div className="mt-1 flex h-12 items-center">
+ {totalEvents?.[id] || totalEvents?.[id] === 0 ? (
+ <NumberFlow
+ value={
+ id === "sales" && saleUnit === "saleAmount"
+ ? totalEvents.saleAmount / 100
+ : totalEvents[id]
+ }
+ className={cn(
+ "text-xl font-medium sm:text-3xl",
+ showPaywall && "opacity-30",
+ )}
+ format={
+ id === "sales" && saleUnit === "saleAmount"
+ ? {
+ style: "currency",
+ currency: "USD",
+ // @ts-ignore – trailingZeroDisplay is a valid option but TS is outdated
+ trailingZeroDisplay: "stripIfInteger",
+ }
+ : {
+ notation:
+ totalEvents[id] > 999999
+ ? "compact"
+ : "standard",
+ }
+ }
+ />
+ ) : requiresUpgrade ? (
+ <div className="block rounded-full bg-neutral-100 p-2.5">
+ <Lock className="h-4 w-4 text-neutral-500" />
+ </div>
+ ) : (
+ <div className="h-9 w-16 animate-pulse rounded-md bg-neutral-200" />
+ )}
+ </div>
+ </Link>
+ {id === "sales" && (
+ <ToggleGroup
+ className="absolute right-3 top-3 hidden w-fit shrink-0 items-center gap-1 border-neutral-100 bg-neutral-100 sm:flex"
+ optionClassName="size-8 p-0 flex items-center justify-center"
+ indicatorClassName="border border-neutral-200 bg-white"
+ options={[
+ {
+ label: <div className="text-base">$</div>,
+ value: "saleAmount",
+ },
+ {
+ label: <div className="text-[11px]">123</div>,
+ value: "sales",
+ },
+ ]}
+ selected={saleUnit}
+ selectAction={setSaleUnit}
+ />
+ )}
+ </div>
+ );
+ })}
+ </NumberFlowGroup>
+ </div>
+ );
+}
diff --git a/apps/web/ui/analytics/main.tsx b/apps/web/ui/analytics/main.tsx
index 46ea78f397..1c9df5a885 100644
--- a/apps/web/ui/analytics/main.tsx
+++ b/apps/web/ui/analytics/main.tsx
@@ -1,4 +1,4 @@
-import { AnalyticsSaleUnit, EventType } from "@/lib/analytics/types";
+import { EventType } from "@/lib/analytics/types";
import useWorkspace from "@/lib/swr/use-workspace";
import {
BlurImage,
@@ -9,13 +9,13 @@ import {
useRouterStuff,
} from "@dub/ui";
import { cn } from "@dub/utils";
-import NumberFlow, { NumberFlowGroup } from "@number-flow/react";
-import { ChevronRight, Lock, Play } from "lucide-react";
+import { Play } from "lucide-react";
import Link from "next/link";
import { useContext, useMemo } from "react";
import AnalyticsAreaChart from "./analytics-area-chart";
import { AnalyticsFunnelChart } from "./analytics-funnel-chart";
import { AnalyticsContext } from "./analytics-provider";
+import { AnalyticsTabs } from "./analytics-tabs";
type Tab = {
id: EventType;
@@ -73,116 +73,28 @@ export default function Main() {
return (
<div className="w-full overflow-hidden bg-white">
- <div className="scrollbar-hide grid w-full grid-cols-3 divide-x overflow-y-hidden border border-neutral-200 sm:rounded-t-xl">
- <NumberFlowGroup>
- {tabs.map(({ id, label, colorClassName, conversions }, idx) => {
- return (
- <div key={id} className="relative z-0">
- {idx > 0 && (
- <div className="absolute left-0 top-1/2 z-10 -translate-x-1/2 -translate-y-1/2 rounded-full border border-neutral-200 bg-white p-1.5">
- <ChevronRight
- className="h-3 w-3 text-neutral-400"
- strokeWidth={2.5}
- />
- </div>
- )}
- <Link
- className={cn(
- "border-box relative block h-full min-w-[110px] flex-none px-4 py-3 sm:min-w-[240px] sm:px-8 sm:py-6",
- "transition-colors hover:bg-neutral-50 focus:outline-none active:bg-neutral-100",
- "ring-inset ring-neutral-500 focus-visible:ring-1 sm:first:rounded-tl-xl",
- )}
- href={
- queryParams({
- set: {
- event: id,
- },
- getNewPath: true,
- }) as string
- }
- aria-current
- >
- {/* Active tab indicator */}
- <div
- className={cn(
- "absolute bottom-0 left-0 h-0.5 w-full bg-black transition-transform duration-100",
- tab.id !== id && "translate-y-[3px]", // Translate an extra pixel to avoid sub-pixel issues
- )}
- />
-
- <div className="flex items-center gap-2.5 text-sm text-neutral-600">
- <div
- className={cn(
- "h-2 w-2 rounded-sm bg-current shadow-[inset_0_0_0_1px_#00000019]",
- colorClassName,
- )}
- />
- <span>{label}</span>
- </div>
- <div className="mt-1 flex h-12 items-center">
- {totalEvents?.[id] || totalEvents?.[id] === 0 ? (
- <NumberFlow
- value={
- id === "sales" && saleUnit === "saleAmount"
- ? totalEvents.saleAmount / 100
- : totalEvents[id]
- }
- className={cn(
- "text-xl font-medium sm:text-3xl",
- showPaywall && "opacity-30",
- )}
- format={
- id === "sales" && saleUnit === "saleAmount"
- ? {
- style: "currency",
- currency: "USD",
- // @ts-ignore – trailingZeroDisplay is a valid option but TS is outdated
- trailingZeroDisplay: "stripIfInteger",
- }
- : {
- notation:
- totalEvents[id] > 999999
- ? "compact"
- : "standard",
- }
- }
- />
- ) : requiresUpgrade ? (
- <div className="block rounded-full bg-neutral-100 p-2.5">
- <Lock className="h-4 w-4 text-neutral-500" />
- </div>
- ) : (
- <div className="h-9 w-16 animate-pulse rounded-md bg-neutral-200" />
- )}
- </div>
- </Link>
- {id === "sales" && (
- <ToggleGroup
- className="absolute right-3 top-3 hidden w-fit shrink-0 items-center gap-1 border-neutral-100 bg-neutral-100 sm:flex"
- optionClassName="size-8 p-0 flex items-center justify-center"
- indicatorClassName="border border-neutral-200 bg-white"
- options={[
- {
- label: <div className="text-base">$</div>,
- value: "saleAmount",
- },
- {
- label: <div className="text-[11px]">123</div>,
- value: "sales",
- },
- ]}
- selected={saleUnit}
- selectAction={(option: AnalyticsSaleUnit) => {
- queryParams({
- set: { saleUnit: option },
- });
- }}
- />
- )}
- </div>
- );
- })}
- </NumberFlowGroup>
+ <div className="border border-neutral-200 sm:rounded-t-xl">
+ <AnalyticsTabs
+ showConversions={showConversions}
+ totalEvents={totalEvents}
+ tab={selectedTab}
+ tabHref={(id) =>
+ queryParams({
+ set: {
+ event: id,
+ },
+ getNewPath: true,
+ }) as string
+ }
+ saleUnit={saleUnit}
+ setSaleUnit={(option) =>
+ queryParams({
+ set: { saleUnit: option },
+ })
+ }
+ requiresUpgrade={requiresUpgrade}
+ showPaywall={showPaywall}
+ />
</div>
<div className="relative">
<div