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 composite chart and sales amount to demo

Tim Wilsonda365dca6410
diff --git a/apps/web/ui/analytics/analytics-area-chart.tsx b/apps/web/ui/analytics/analytics-area-chart.tsx
index cc71df6cc4..56b8eaab53 100644
--- a/apps/web/ui/analytics/analytics-area-chart.tsx
+++ b/apps/web/ui/analytics/analytics-area-chart.tsx
@@ -18,7 +18,7 @@ export default function AnalyticsAreaChart({
const { data } = useSWR<
{ start: Date; clicks: number; leads: number; sales: number }[]
- >(`${baseApiPath}/timeseries?${queryString}`, fetcher, {
+ >(`${baseApiPath}/composite/timeseries?${queryString}`, fetcher, {
shouldRetryOnError: !requiresUpgrade,
});
diff --git a/apps/web/ui/analytics/devices.tsx b/apps/web/ui/analytics/devices.tsx
index 6f05604d51..d773ec3833 100644
--- a/apps/web/ui/analytics/devices.tsx
+++ b/apps/web/ui/analytics/devices.tsx
@@ -24,7 +24,7 @@ export default function Devices() {
({
[key in DeviceTabs]: string;
} & { clicks: number })[]
- >(`${baseApiPath}/${tab}?${queryString}`, fetcher, {
+ >(`${baseApiPath}/${selectedTab}/${tab}?${queryString}`, fetcher, {
shouldRetryOnError: !requiresUpgrade,
});
diff --git a/apps/web/ui/analytics/index.tsx b/apps/web/ui/analytics/index.tsx
index 3f7365c2f6..ae9dceae8b 100644
--- a/apps/web/ui/analytics/index.tsx
+++ b/apps/web/ui/analytics/index.tsx
@@ -25,7 +25,6 @@ import TopLinks from "./top-links";
export const AnalyticsContext = createContext<{
basePath: string;
baseApiPath: string;
- baseApiPathGeneric: string;
selectedTab: string;
domain?: string;
key?: string;
@@ -41,7 +40,6 @@ export const AnalyticsContext = createContext<{
}>({
basePath: "",
baseApiPath: "",
- baseApiPathGeneric: "",
selectedTab: "clicks",
domain: "",
queryString: "",
@@ -65,7 +63,7 @@ export default function Analytics({
}) {
const searchParams = useSearchParams();
const pathname = usePathname();
- const { id, slug } = useWorkspace();
+ const { id, slug, betaTester } = useWorkspace();
const [requiresUpgrade, setRequiresUpgrade] = useState(false);
let { key } = useParams() as {
@@ -93,36 +91,33 @@ export default function Analytics({
};
}, [searchParams?.get("start"), searchParams?.get("end")]);
- const selectedTab = searchParams.get("tab") || "clicks";
+ const selectedTab =
+ demo || betaTester ? searchParams.get("tab") || "composite" : "clicks";
- const { basePath, domain, baseApiPath, baseApiPathGeneric } = useMemo(() => {
+ const { basePath, domain, baseApiPath } = useMemo(() => {
if (admin) {
return {
basePath: `/analytics`,
- baseApiPath: `/api/analytics/admin/${selectedTab || "clicks"}`,
- baseApiPathGeneric: `/api/analytics/admin`,
+ baseApiPath: `/api/analytics/admin`,
domain: domainSlug,
};
} else if (demo) {
return {
basePath: `/analytics/demo`,
- baseApiPath: `/api/analytics/demo/${selectedTab || "clicks"}`,
- baseApiPathGeneric: `/api/analytics/demo`,
+ baseApiPath: `/api/analytics/demo`,
domain: domainSlug,
};
} else if (slug) {
return {
basePath: `/${slug}/analytics`,
- baseApiPath: `/api/analytics/${selectedTab || "clicks"}`,
- baseApiPathGeneric: `/api/analytics`,
+ baseApiPath: `/api/analytics`,
domain: domainSlug,
};
} else {
// Public stats page, e.g. dub.co/stats/github, stey.me/stats/weathergpt
return {
basePath: `/stats/${key}`,
- baseApiPath: `/api/analytics/edge/${selectedTab || "clicks"}`,
- baseApiPathGeneric: `/api/analytics/edge`,
+ baseApiPath: `/api/analytics/edge`,
domain: staticDomain,
};
}
@@ -154,7 +149,7 @@ export default function Analytics({
useEffect(() => setRequiresUpgrade(false), [queryString]);
const { data: totalClicks } = useSWR<number>(
- `${baseApiPath}/count?${queryString}`,
+ `${baseApiPath}/${selectedTab}/count?${queryString}`,
fetcher,
{
onSuccess: () => setRequiresUpgrade(false),
@@ -184,8 +179,7 @@ export default function Analytics({
<AnalyticsContext.Provider
value={{
basePath, // basePath for the page (e.g. /stats/[key], /[slug]/analytics)
- baseApiPath, // baseApiPath for the API with the selected resource (e.g. /api/analytics/clicks)
- baseApiPathGeneric, // baseApiPathGeneric for the API without the selected resource (e.g. /api/analytics)
+ baseApiPath, // baseApiPath for analytics API endpoints (e.g. /api/analytics)
selectedTab, // selected tab (clicks, leads, sales)
queryString,
domain: domain || undefined, // domain for the link (e.g. dub.sh, stey.me, etc.)
diff --git a/apps/web/ui/analytics/locations.tsx b/apps/web/ui/analytics/locations.tsx
index 1f3efa8794..f0fa2ed80b 100644
--- a/apps/web/ui/analytics/locations.tsx
+++ b/apps/web/ui/analytics/locations.tsx
@@ -20,7 +20,7 @@ export default function Locations() {
useContext(AnalyticsContext);
const { data } = useSWR<{ country: string; city: string; clicks: number }[]>(
- `${baseApiPath}/${tab}?${queryString}`,
+ `${baseApiPath}/${selectedTab}/${tab}?${queryString}`,
fetcher,
{ shouldRetryOnError: !requiresUpgrade },
);
diff --git a/apps/web/ui/analytics/main.tsx b/apps/web/ui/analytics/main.tsx
index 5269aa0c6a..af3facabeb 100644
--- a/apps/web/ui/analytics/main.tsx
+++ b/apps/web/ui/analytics/main.tsx
@@ -27,13 +27,8 @@ type Tab = {
export default function Main() {
const { betaTester } = useWorkspace();
- const {
- baseApiPathGeneric,
- queryString,
- requiresUpgrade,
- demo,
- selectedTab,
- } = useContext(AnalyticsContext);
+ const { baseApiPath, queryString, requiresUpgrade, demo, selectedTab } =
+ useContext(AnalyticsContext);
const searchParams = useSearchParams();
const domain = searchParams.get("domain");
const key = searchParams.get("key");
@@ -75,20 +70,17 @@ export default function Main() {
[betaTester],
);
- const tab =
- betaTester || demo
- ? tabs.find(({ id }) => id === selectedTab) || {
- id: "clicks",
- show: ["clicks", "leads", "sales"],
- }
- : tabs[0];
+ const tab = tabs.find(({ id }) => id === selectedTab) || {
+ id: "composite",
+ show: ["clicks", "leads", "sales"],
+ };
const { data } = useSWR<{
clicks: number;
leads: number;
sales: number;
amount: number;
- }>(`${baseApiPathGeneric}/composite/count?${queryString}`, fetcher, {
+ }>(`${baseApiPath}/composite/count?${queryString}`, fetcher, {
shouldRetryOnError: !requiresUpgrade,
});
@@ -99,7 +91,7 @@ export default function Main() {
const total = {
clicks: data?.clicks,
leads: data?.leads,
- sales: data?.sales,
+ sales: data?.amount,
}[id];
return (
@@ -151,10 +143,14 @@ export default function Main() {
</div>
<div className="mt-1 flex">
{total || total === 0 ? (
- <NumberTooltip value={total} unit={label.toLowerCase()}>
+ <NumberTooltip
+ value={id === "sales" ? data?.sales : total}
+ unit={label.toLowerCase()}
+ >
<CountingNumbers
as="h1"
className="text-3xl font-medium"
+ prefix={id === "sales" && "$"}
{...(id === "sales" && { fullNumber: true })}
>
{total}
diff --git a/apps/web/ui/analytics/referer.tsx b/apps/web/ui/analytics/referer.tsx
index 66a8f5a8bb..689b68b9dd 100644
--- a/apps/web/ui/analytics/referer.tsx
+++ b/apps/web/ui/analytics/referer.tsx
@@ -12,7 +12,7 @@ export default function Referer() {
useContext(AnalyticsContext);
const { data } = useSWR<{ referer: string; clicks: number }[]>(
- `${baseApiPath}/referers?${queryString}`,
+ `${baseApiPath}/${selectedTab}/referers?${queryString}`,
fetcher,
{ shouldRetryOnError: !requiresUpgrade },
);
diff --git a/apps/web/ui/analytics/share-popover.tsx b/apps/web/ui/analytics/share-popover.tsx
index 6449fb77cb..9e988d4b56 100644
--- a/apps/web/ui/analytics/share-popover.tsx
+++ b/apps/web/ui/analytics/share-popover.tsx
@@ -15,10 +15,7 @@ import { AnalyticsContext } from ".";
export default function SharePopover() {
const [openSharePopover, setopenSharePopoverPopover] = useState(false);
- const { baseApiPath, queryString, domain, key } = useContext(
- AnalyticsContext,
- ) as {
- baseApiPath: string;
+ const { queryString, domain, key } = useContext(AnalyticsContext) as {
queryString: string;
domain: string;
key: string; // coerce to string since <SharePopover is not shown if key is undefined)
diff --git a/apps/web/ui/analytics/top-links.tsx b/apps/web/ui/analytics/top-links.tsx
index 8400052616..a17f79ffdd 100644
--- a/apps/web/ui/analytics/top-links.tsx
+++ b/apps/web/ui/analytics/top-links.tsx
@@ -35,7 +35,7 @@ export default function TopLinks() {
({ domain: string; key: string } & {
[key in TopLinksTabs]: string;
} & { clicks: number })[]
- >(`${baseApiPath}/top_${tab}s?${queryString}`, fetcher, {
+ >(`${baseApiPath}/${selectedTab}/top_${tab}s?${queryString}`, fetcher, {
shouldRetryOnError: !requiresUpgrade,
});
diff --git a/apps/web/ui/charts/areas.tsx b/apps/web/ui/charts/areas.tsx
index 5f88db6657..ece6f5ed97 100644
--- a/apps/web/ui/charts/areas.tsx
+++ b/apps/web/ui/charts/areas.tsx
@@ -2,8 +2,8 @@ import { cn } from "@dub/utils";
import { LinearGradient } from "@visx/gradient";
import { Group } from "@visx/group";
import { Area, AreaClosed, Circle } from "@visx/shape";
-import { motion } from "framer-motion";
-import { Fragment, useMemo } from "react";
+import { AnimatePresence, motion } from "framer-motion";
+import { useMemo } from "react";
import { useChartContext, useChartTooltipContext } from "./chart-context";
export default function Areas({
@@ -25,85 +25,90 @@ export default function Areas({
return (
<Group left={margin.left} top={margin.top}>
- {series
- .filter(({ isActive }) => isActive)
- .map((s) => (
- // Prevent ugly x-scale animations when start/end dates change with unique key
- <Fragment
- key={`${s.id}_${startDate.toString()}_${endDate.toString()}`}
- >
- {/* Area background gradient */}
- <LinearGradient
- className={cn(
- s.colorClassName ?? "text-blue-500",
- seriesClassNames?.find(({ id }) => id === s.id)?.gradient,
- )}
- id={`${s.id}-background`}
- fromOffset="20%"
- from="currentColor"
- fromOpacity={0.01}
- to="currentColor"
- toOpacity={0.2}
- x1={0}
- x2={0}
- y1={1}
- />
-
- {/* Area */}
- <AreaClosed
- data={data}
- x={(d) => xScale(d.date)}
- y={(d) => yScale(s.valueAccessor(d) ?? 0)}
- yScale={yScale}
+ <AnimatePresence>
+ {series
+ .filter(({ isActive }) => isActive)
+ .map((s) => (
+ // Prevent ugly x-scale animations when start/end dates change with unique key
+ <motion.g
+ initial={{ opacity: 1 }}
+ exit={{ opacity: 0 }}
+ transition={{ duration: 0.1 }}
+ key={`${s.id}_${startDate.toString()}_${endDate.toString()}`}
>
- {({ path }) => {
- return (
+ {/* Area background gradient */}
+ <LinearGradient
+ className={cn(
+ s.colorClassName ?? "text-blue-500",
+ seriesClassNames?.find(({ id }) => id === s.id)?.gradient,
+ )}
+ id={`${s.id}-background`}
+ fromOffset="20%"
+ from="currentColor"
+ fromOpacity={0.01}
+ to="currentColor"
+ toOpacity={0.2}
+ x1={0}
+ x2={0}
+ y1={1}
+ />
+
+ {/* Area */}
+ <AreaClosed
+ data={data}
+ x={(d) => xScale(d.date)}
+ y={(d) => yScale(s.valueAccessor(d) ?? 0)}
+ yScale={yScale}
+ >
+ {({ path }) => {
+ return (
+ <motion.path
+ initial={{ d: path(zeroedData) || "", opacity: 0 }}
+ animate={{ d: path(data) || "", opacity: 1 }}
+ fill={`url(#${s.id}-background)`}
+ />
+ );
+ }}
+ </AreaClosed>
+
+ {/* Line */}
+ <Area
+ data={data}
+ x={(d) => xScale(d.date)}
+ y={(d) => yScale(s.valueAccessor(d) ?? 0)}
+ >
+ {({ path }) => (
<motion.path
- initial={{ d: path(zeroedData) || "", opacity: 0 }}
- animate={{ d: path(data) || "", opacity: 1 }}
- fill={`url(#${s.id}-background)`}
+ initial={{ d: path(zeroedData) || "" }}
+ animate={{ d: path(data) || "" }}
+ className={cn(
+ s.colorClassName ?? "text-blue-700",
+ seriesClassNames?.find(({ id }) => id === s.id)?.line,
+ )}
+ stroke="currentColor"
+ strokeOpacity={0.8}
+ strokeWidth={2}
+ fill="transparent"
/>
- );
- }}
- </AreaClosed>
+ )}
+ </Area>
- {/* Line */}
- <Area
- data={data}
- x={(d) => xScale(d.date)}
- y={(d) => yScale(s.valueAccessor(d) ?? 0)}
- >
- {({ path }) => (
- <motion.path
- initial={{ d: path(zeroedData) || "" }}
- animate={{ d: path(data) || "" }}
+ {/* Latest value dot */}
+ {!tooltipData && (
+ <Circle
+ cx={xScale(data.at(-1)!.date)}
+ cy={yScale(s.valueAccessor(data.at(-1)!))}
+ r={4}
className={cn(
s.colorClassName ?? "text-blue-700",
seriesClassNames?.find(({ id }) => id === s.id)?.line,
)}
- stroke="currentColor"
- strokeOpacity={0.8}
- strokeWidth={2}
- fill="transparent"
+ fill="currentColor"
/>
)}
- </Area>
-
- {/* Latest value dot */}
- {!tooltipData && (
- <Circle
- cx={xScale(data.at(-1)!.date)}
- cy={yScale(s.valueAccessor(data.at(-1)!))}
- r={4}
- className={cn(
- s.colorClassName ?? "text-blue-700",
- seriesClassNames?.find(({ id }) => id === s.id)?.line,
- )}
- fill="currentColor"
- />
- )}
- </Fragment>
- ))}
+ </motion.g>
+ ))}
+ </AnimatePresence>
</Group>
);
}
diff --git a/packages/ui/src/counting-numbers.tsx b/packages/ui/src/counting-numbers.tsx
index bb777135c5..5c46a942c2 100644
--- a/packages/ui/src/counting-numbers.tsx
+++ b/packages/ui/src/counting-numbers.tsx
@@ -12,6 +12,7 @@ interface CountingNumbersProps {
asChild?: boolean;
className?: string;
children: ReactNode;
+ prefix?: ReactNode;
duration?: number;
as?: ElementType; // Allows defining the component as 'h1', 'h2', 'p', etc.
fullNumber?: boolean; // If true, will not abbreviate the number
@@ -23,6 +24,7 @@ const CountingNumbers = React.forwardRef<HTMLSpanElement, CountingNumbersProps>(
asChild,
className,
children,
+ prefix,
duration = 250,
as: Component = "span",
...props
@@ -59,6 +61,7 @@ const CountingNumbers = React.forwardRef<HTMLSpanElement, CountingNumbersProps>(
return (
<Comp ref={ref} className={className} {...props}>
+ {prefix}
{nFormatter(
displayValue,
props.fullNumber ? { full: true } : undefined,