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.

Use sales amount everywhere

Tim Wilson71bd2c8749fc
diff --git a/apps/web/ui/analytics/analytics-area-chart.tsx b/apps/web/ui/analytics/analytics-area-chart.tsx
index c79eef8bac..19a83c5c21 100644
--- a/apps/web/ui/analytics/analytics-area-chart.tsx
+++ b/apps/web/ui/analytics/analytics-area-chart.tsx
@@ -1,3 +1,4 @@
+import { EventType } from "@/lib/analytics/types";
import { editQueryString } from "@/lib/analytics/utils";
import { cn, fetcher, getDaysDifference, nFormatter } from "@dub/utils";
import { Fragment, useCallback, useContext, useMemo } from "react";
@@ -10,15 +11,21 @@ import { AnalyticsLoadingSpinner } from "./analytics-loading-spinner";
import { AnalyticsContext } from "./analytics-provider";
export default function AnalyticsAreaChart({
- show,
+ resource,
}: {
- show: ("clicks" | "leads" | "sales")[];
+ resource: EventType;
}) {
const { baseApiPath, queryString, start, end, interval, requiresUpgrade } =
useContext(AnalyticsContext);
const { data } = useSWR<
- { start: Date; clicks: number; leads: number; sales: number }[]
+ {
+ start: Date;
+ clicks: number;
+ leads: number;
+ sales: number;
+ amount: number;
+ }[]
>(
`${baseApiPath}?${editQueryString(queryString, {
groupBy: "timeseries",
@@ -32,12 +39,13 @@ export default function AnalyticsAreaChart({
const chartData = useMemo(
() =>
- data?.map(({ start, clicks, leads, sales }) => ({
+ data?.map(({ start, clicks, leads, sales, amount }) => ({
date: new Date(start),
values: {
clicks,
leads,
sales,
+ amount: (amount ?? 0) / 100,
},
})) ?? null,
[data],
@@ -90,23 +98,25 @@ export default function AnalyticsAreaChart({
{
id: "clicks",
valueAccessor: (d) => d.values.clicks,
- isActive: show.includes("clicks"),
+ isActive: resource === "clicks",
colorClassName: "text-blue-500",
},
{
id: "leads",
valueAccessor: (d) => d.values.leads,
- isActive: show.includes("leads"),
+ isActive: resource === "leads",
colorClassName: "text-violet-600",
},
{
id: "sales",
- valueAccessor: (d) => d.values.sales,
- isActive: show.includes("sales"),
+ valueAccessor: (d) => d.values.amount,
+ isActive: resource === "sales",
colorClassName: "text-teal-400",
},
];
+ const activeSeries = series.find(({ id }) => id === resource);
+
return (
<div className="flex h-96 w-full items-center justify-center">
{chartData ? (
@@ -115,41 +125,44 @@ export default function AnalyticsAreaChart({
data={chartData}
series={series}
tooltipClassName="p-0"
- tooltipContent={(d) => (
- <>
- <p className="border-b border-gray-200 px-4 py-3 text-sm text-gray-900">
- {formatDate(d.date)}
- </p>
- <div className="grid grid-cols-2 gap-x-6 gap-y-2 px-4 py-3 text-sm">
- {show.map((resource) => {
- const s = series.find(({ id }) => id === resource);
- const value = d.values[resource];
- return (
- <Fragment key={resource}>
- <div className="flex items-center gap-2">
- {s && (
- <div
- className={cn(
- s.colorClassName,
- "h-2 w-2 rounded-sm bg-current opacity-50 shadow-[inset_0_0_0_1px_#0003]",
- )}
- />
- )}
- <p className="capitalize text-gray-600">{resource}</p>
- </div>
- <p className="text-right font-medium text-gray-900">
- {nFormatter(value, { full: true })}
- </p>
- </Fragment>
- );
- })}
- </div>
- </>
- )}
+ tooltipContent={(d) => {
+ const value = d.values[resource === "sales" ? "amount" : resource];
+ return (
+ <>
+ <p className="border-b border-gray-200 px-4 py-3 text-sm text-gray-900">
+ {formatDate(d.date)}
+ </p>
+ <div className="grid grid-cols-2 gap-x-6 gap-y-2 px-4 py-3 text-sm">
+ <Fragment key={resource}>
+ <div className="flex items-center gap-2">
+ {activeSeries && (
+ <div
+ className={cn(
+ activeSeries.colorClassName,
+ "h-2 w-2 rounded-sm bg-current opacity-50 shadow-[inset_0_0_0_1px_#0003]",
+ )}
+ />
+ )}
+ <p className="capitalize text-gray-600">{resource}</p>
+ </div>
+ <p className="text-right font-medium text-gray-900">
+ {resource === "sales" && "$"}
+ {nFormatter(value, { full: true })}
+ </p>
+ </Fragment>
+ </div>
+ </>
+ );
+ }}
>
<Areas />
<XAxis tickFormat={formatDate} />
- <YAxis showGridLines tickFormat={nFormatter} />
+ <YAxis
+ showGridLines
+ tickFormat={
+ resource === "sales" ? (v) => `$${nFormatter(v)}` : nFormatter
+ }
+ />
</TimeSeriesChart>
) : (
<AnalyticsLoadingSpinner />
diff --git a/apps/web/ui/analytics/bar-list.tsx b/apps/web/ui/analytics/bar-list.tsx
index 6d5f75020a..9e68b7e27d 100644
--- a/apps/web/ui/analytics/bar-list.tsx
+++ b/apps/web/ui/analytics/bar-list.tsx
@@ -13,6 +13,7 @@ import LinkPreviewTooltip from "./link-preview";
export default function BarList({
tab,
data,
+ isCurrency,
barBackground,
hoverBackground,
maxValue,
@@ -27,6 +28,7 @@ export default function BarList({
value: number;
linkId?: string;
}[];
+ isCurrency?: boolean;
maxValue: number;
barBackground: string;
hoverBackground: string;
@@ -61,6 +63,7 @@ export default function BarList({
setShowModal={setShowModal}
barBackground={barBackground}
hoverBackground={hoverBackground}
+ isCurrency={isCurrency}
/>
))}
</div>
@@ -100,6 +103,7 @@ export function LineItem({
barBackground,
hoverBackground,
linkData,
+ isCurrency,
}: {
icon: ReactNode;
title: string;
@@ -111,6 +115,7 @@ export function LineItem({
barBackground: string;
hoverBackground: string;
linkData?: LinkProps;
+ isCurrency?: boolean;
}) {
const lineItem = useMemo(() => {
return (
@@ -157,7 +162,10 @@ export function LineItem({
/>
</div>
<NumberTooltip value={value}>
- <p className="z-10 px-2 text-sm text-gray-600">{nFormatter(value)}</p>
+ <p className="z-10 px-2 text-sm text-gray-600">
+ {isCurrency && "$"}
+ {nFormatter(isCurrency ? value / 100 : value)}
+ </p>
</NumberTooltip>
</div>
</Link>
diff --git a/apps/web/ui/analytics/devices.tsx b/apps/web/ui/analytics/devices.tsx
index 64abdd21cf..4c4d3811e6 100644
--- a/apps/web/ui/analytics/devices.tsx
+++ b/apps/web/ui/analytics/devices.tsx
@@ -1,9 +1,10 @@
import { SINGULAR_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
import { DeviceTabs } from "@/lib/analytics/types";
import { useRouterStuff } from "@dub/ui";
-import { useState } from "react";
+import { useContext, useState } from "react";
import { AnalyticsCard } from "./analytics-card";
import { AnalyticsLoadingSpinner } from "./analytics-loading-spinner";
+import { AnalyticsContext } from "./analytics-provider";
import BarList from "./bar-list";
import DeviceIcon from "./device-icon";
import { useAnalyticsFilterOption } from "./utils";
@@ -11,6 +12,9 @@ import { useAnalyticsFilterOption } from "./utils";
export default function Devices() {
const { queryParams } = useRouterStuff();
+ const { selectedTab } = useContext(AnalyticsContext);
+ const dataKey = selectedTab === "sales" ? "amount" : "count";
+
const [tab, setTab] = useState<DeviceTabs>("devices");
const data = useAnalyticsFilterOption(tab);
const singularTabName = SINGULAR_ANALYTICS_ENDPOINTS[tab];
@@ -48,10 +52,11 @@ export default function Devices() {
},
getNewPath: true,
}) as string,
- value: d.count || 0,
+ value: d[dataKey] || 0,
})) || []
}
- maxValue={(data && data[0]?.count) || 0}
+ isCurrency={dataKey === "amount"}
+ maxValue={(data && data[0]?.[dataKey]) || 0}
barBackground="bg-green-100"
hoverBackground="hover:bg-gradient-to-r hover:from-green-50 hover:to-transparent hover:border-green-500"
setShowModal={setShowModal}
diff --git a/apps/web/ui/analytics/events/events-table.tsx b/apps/web/ui/analytics/events/events-table.tsx
index 0e6887b07e..ff788592f9 100644
--- a/apps/web/ui/analytics/events/events-table.tsx
+++ b/apps/web/ui/analytics/events/events-table.tsx
@@ -340,21 +340,22 @@ export default function EventsTable() {
);
// Update export query string
- useEffect(() => {
- console.log("setting export query string with", setExportQueryString);
- setExportQueryString?.(
- editQueryString(
- queryString,
- {
- columns: Object.entries(columnVisibility[tab])
- .filter(([, visible]) => visible)
- .map(([id]) => id)
- .join(","),
- },
- ["page"],
+ useEffect(
+ () =>
+ setExportQueryString?.(
+ editQueryString(
+ queryString,
+ {
+ columns: Object.entries(columnVisibility[tab])
+ .filter(([, visible]) => visible)
+ .map(([id]) => id)
+ .join(","),
+ },
+ ["page"],
+ ),
),
- );
- }, [setExportQueryString, queryString, columnVisibility, tab]);
+ [setExportQueryString, queryString, columnVisibility, tab],
+ );
const { data, isLoading, error } = useSWR<EventDatum[]>(
`/api/analytics/events?${queryString}`,
diff --git a/apps/web/ui/analytics/events/events-tabs.tsx b/apps/web/ui/analytics/events/events-tabs.tsx
index 6a783d1a8f..0c4ffa7126 100644
--- a/apps/web/ui/analytics/events/events-tabs.tsx
+++ b/apps/web/ui/analytics/events/events-tabs.tsx
@@ -20,6 +20,7 @@ type TimeseriesData = {
clicks: number;
leads: number;
sales: number;
+ amount: number;
}[];
export default function EventsTabs() {
@@ -110,8 +111,12 @@ export default function EventsTabs() {
"text-2xl transition-opacity",
isLoadingTotalEvents && "opacity-40",
)}
+ prefix={event === "sales" && "$"}
+ {...(event === "sales" && { fullNumber: true })}
>
- {totalEvents?.[event] ?? 0}
+ {event === "sales"
+ ? (totalEvents?.amount ?? 0) / 100
+ : totalEvents?.[event] ?? 0}
</CountingNumbers>
) : (
<div className="h-8 w-12 animate-pulse rounded-md bg-gray-200" />
@@ -160,7 +165,10 @@ function ChartInner({
() =>
data.map((d) => ({
date: new Date(d.start),
- value: (d?.[event] as number | undefined) ?? 0,
+ value:
+ ((event === "sales" ? d?.amount : d?.[event]) as
+ | number
+ | undefined) ?? 0,
})) ?? null,
[data, event],
);
diff --git a/apps/web/ui/analytics/locations.tsx b/apps/web/ui/analytics/locations.tsx
index 2dc918cfa8..cdfe33a587 100644
--- a/apps/web/ui/analytics/locations.tsx
+++ b/apps/web/ui/analytics/locations.tsx
@@ -1,15 +1,19 @@
import { SINGULAR_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
import { useRouterStuff } from "@dub/ui";
import { COUNTRIES } from "@dub/utils";
-import { useState } from "react";
+import { useContext, useState } from "react";
import { AnalyticsCard } from "./analytics-card";
import { AnalyticsLoadingSpinner } from "./analytics-loading-spinner";
+import { AnalyticsContext } from "./analytics-provider";
import BarList from "./bar-list";
import { useAnalyticsFilterOption } from "./utils";
export default function Locations() {
const { queryParams } = useRouterStuff();
+ const { selectedTab } = useContext(AnalyticsContext);
+ const dataKey = selectedTab === "sales" ? "amount" : "count";
+
const [tab, setTab] = useState<"countries" | "cities">("countries");
const data = useAnalyticsFilterOption(tab);
const singularTabName = SINGULAR_ANALYTICS_ENDPOINTS[tab];
@@ -46,10 +50,11 @@ export default function Locations() {
},
getNewPath: true,
}) as string,
- value: d.count || 0,
+ value: d[dataKey] || 0,
})) || []
}
- maxValue={(data && data[0]?.count) || 0}
+ isCurrency={dataKey === "amount"}
+ maxValue={(data && data[0]?.[dataKey]) || 0}
barBackground="bg-blue-100"
hoverBackground="hover:bg-gradient-to-r hover:from-blue-50 hover:to-transparent hover:border-blue-500"
setShowModal={setShowModal}
diff --git a/apps/web/ui/analytics/main.tsx b/apps/web/ui/analytics/main.tsx
index f427fafda4..4e8561a6bc 100644
--- a/apps/web/ui/analytics/main.tsx
+++ b/apps/web/ui/analytics/main.tsx
@@ -1,3 +1,4 @@
+import { EventType } from "@/lib/analytics/types";
import useWorkspace from "@/lib/swr/use-workspace";
import { CountingNumbers, NumberTooltip, useRouterStuff } from "@dub/ui";
import { cn } from "@dub/utils";
@@ -8,10 +9,9 @@ import AnalyticsAreaChart from "./analytics-area-chart";
import { AnalyticsContext } from "./analytics-provider";
type Tab = {
- id: string;
+ id: EventType;
label: string;
colorClassName: string;
- show: ("clicks" | "leads" | "sales")[];
};
export default function Main() {
@@ -27,7 +27,6 @@ export default function Main() {
id: "clicks",
label: "Clicks",
colorClassName: "text-blue-500/50",
- show: ["clicks"],
},
...(flags?.conversions || demoPage
? [
@@ -35,13 +34,11 @@ export default function Main() {
id: "leads",
label: "Leads",
colorClassName: "text-violet-600/50",
- show: ["leads"],
},
{
id: "sales",
label: "Sales",
colorClassName: "text-teal-400/50",
- show: ["sales"],
},
]
: []),
@@ -49,10 +46,7 @@ export default function Main() {
[flags],
);
- const tab = tabs.find(({ id }) => id === selectedTab) || {
- id: "clicks",
- show: ["clicks"],
- };
+ const tab = tabs.find(({ id }) => id === selectedTab) ?? tabs[0];
return (
<div className="w-full overflow-hidden border border-gray-200 bg-white sm:rounded-xl">
@@ -141,7 +135,7 @@ export default function Main() {
})}
</div>
<div className="p-5 sm:p-10">
- <AnalyticsAreaChart show={tab.show} />
+ <AnalyticsAreaChart resource={tab.id} />
</div>
</div>
);
diff --git a/apps/web/ui/analytics/referer.tsx b/apps/web/ui/analytics/referer.tsx
index 5839592cb6..9608e9bc97 100644
--- a/apps/web/ui/analytics/referer.tsx
+++ b/apps/web/ui/analytics/referer.tsx
@@ -1,14 +1,19 @@
import { BlurImage, useRouterStuff } from "@dub/ui";
import { GOOGLE_FAVICON_URL } from "@dub/utils";
import { Link2 } from "lucide-react";
+import { useContext } from "react";
import { AnalyticsCard } from "./analytics-card";
import { AnalyticsLoadingSpinner } from "./analytics-loading-spinner";
+import { AnalyticsContext } from "./analytics-provider";
import BarList from "./bar-list";
import { useAnalyticsFilterOption } from "./utils";
export default function Referer() {
const { queryParams } = useRouterStuff();
+ const { selectedTab } = useContext(AnalyticsContext);
+ const dataKey = selectedTab === "sales" ? "amount" : "count";
+
const data = useAnalyticsFilterOption("referers");
return (
@@ -44,10 +49,11 @@ export default function Referer() {
},
getNewPath: true,
}) as string,
- value: d.count || 0,
+ value: d[dataKey] || 0,
})) || []
}
- maxValue={(data && data[0]?.count) || 0}
+ isCurrency={dataKey === "amount"}
+ maxValue={(data && data[0]?.[dataKey]) || 0}
barBackground="bg-red-100"
hoverBackground="hover:bg-gradient-to-r hover:from-red-50 hover:to-transparent hover:border-red-500"
setShowModal={setShowModal}
diff --git a/apps/web/ui/analytics/top-links.tsx b/apps/web/ui/analytics/top-links.tsx
index 1e4c55b953..6829db3f41 100644
--- a/apps/web/ui/analytics/top-links.tsx
+++ b/apps/web/ui/analytics/top-links.tsx
@@ -10,7 +10,8 @@ import { useAnalyticsFilterOption } from "./utils";
export default function TopLinks() {
const { queryParams } = useRouterStuff();
- const { domain, key } = useContext(AnalyticsContext);
+ const { selectedTab, domain, key } = useContext(AnalyticsContext);
+ const dataKey = selectedTab === "sales" ? "amount" : "count";
const showUrls = domain && key;
const data = useAnalyticsFilterOption({
@@ -55,11 +56,12 @@ export default function TopLinks() {
},
getNewPath: true,
}) as string,
- value: d.count || 0,
+ value: d[dataKey] || 0,
...(!showUrls && { linkData: d }),
})) || []
}
- maxValue={(data && data[0]?.count) || 0}
+ isCurrency={dataKey === "amount"}
+ maxValue={(data && data[0]?.[dataKey]) || 0}
barBackground="bg-orange-100"
hoverBackground="hover:bg-gradient-to-r hover:from-orange-50 hover:to-transparent hover:border-orange-500"
setShowModal={setShowModal}
diff --git a/apps/web/ui/analytics/utils.ts b/apps/web/ui/analytics/utils.ts
index a93f89d28c..25e690c928 100644
--- a/apps/web/ui/analytics/utils.ts
+++ b/apps/web/ui/analytics/utils.ts
@@ -53,8 +53,8 @@ export function useAnalyticsFilterOption(
return (
data?.map((d) => ({
...d,
- count:
- ((d[selectedTab] ?? d["clicks"]) as number | undefined) ?? undefined,
+ count: d[selectedTab] as number | undefined,
+ amount: d.amount as number | undefined,
})) ?? null
);
}