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.

Fix empty partner analytics (#3686)

Pedro Ladeira343ba40b1f25
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/earnings/earnings-composite-chart.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/earnings/earnings-composite-chart.tsx
index 9817538d5d..4f0e144449 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/earnings/earnings-composite-chart.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/earnings/earnings-composite-chart.tsx
@@ -74,33 +74,45 @@ export function EarningsCompositeChart() {
const [chartData, series] = useMemo(
() => [
- data?.map(({ start, earnings, data }) => ({
+ data?.map(({ start, earnings, data: breakdown }) => ({
date: new Date(start),
- values: { ...data, total: earnings },
+ values: { ...(breakdown ?? {}), total: earnings },
})),
data
- ? [...new Set<string>(data.flatMap(({ data }) => Object.keys(data)))]
- // Sort by total earnings for the period
- .sort((a, b) => {
- const [earningsA, earningsB] = data.reduce(
- (acc, { data }) => [acc[0] + data[a], acc[1] + data[b]],
- [0, 0],
- );
- return earningsB - earningsA;
- })
- .slice(0, MAX_LINES)
- .map((item, idx) => ({
+ ? (() => {
+ const keys = new Set<string>(
+ data.flatMap(({ data: breakdown }) =>
+ Object.keys(breakdown ?? {}),
+ ),
+ );
+ const sortedKeys: string[] =
+ keys.size > 0
+ ? [...keys].sort((a: string, b: string) => {
+ const [earningsA, earningsB] = data.reduce(
+ (acc, { data: breakdown }) => [
+ acc[0] + (breakdown?.[a] ?? 0),
+ acc[1] + (breakdown?.[b] ?? 0),
+ ],
+ [0, 0],
+ );
+ return earningsB - earningsA;
+ })
+ : ["total"];
+ return sortedKeys.slice(0, MAX_LINES).map((item, idx) => ({
id: item,
isActive: true,
valueAccessor: (d) => d.values[item] || 0,
colorClassName:
groupBy === "type"
- ? EVENT_TYPE_LINE_COLORS[item]
+ ? EVENT_TYPE_LINE_COLORS[
+ item as keyof typeof EVENT_TYPE_LINE_COLORS
+ ] ?? LINE_COLORS[idx % LINE_COLORS.length]
: LINE_COLORS[idx % LINE_COLORS.length],
- }))
+ }));
+ })()
: [],
],
- [data],
+ [data, groupBy],
);
return (
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/page-client.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/page-client.tsx
index 564ab52ff0..43a757897b 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/page-client.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/page-client.tsx
@@ -101,11 +101,12 @@ export default function ProgramPageClient() {
link: defaultProgramLink,
});
+ const hasPartnerLink = Boolean(partnerLink);
const isDeactivated = programEnrollment?.status === "deactivated";
return (
<PageWidthWrapper className="pb-10">
- {partnerLink && (
+ {program && (
<AnimatePresence mode="wait" initial={false}>
{!hideDetails && (
<motion.div
@@ -137,70 +138,79 @@ export default function ProgramPageClient() {
Referral link
</span>
<div className="xs:flex-row xs:items-center relative mt-3 flex flex-col gap-2 md:max-w-[50%]">
- {partnerLink ? (
- <input
- type="text"
- readOnly
- value={getPrettyUrl(partnerLink)}
- disabled={isDeactivated}
- className={cn(
- "border-border-default text-content-default focus:border-border-emphasis bg-bg-default h-10 min-w-0 shrink grow rounded-md border px-3 text-sm focus:outline-none focus:ring-neutral-500",
- isDeactivated && "text-content-subtle cursor-default",
- )}
+ <input
+ type="text"
+ readOnly
+ value={
+ hasPartnerLink ? getPrettyUrl(partnerLink) : "No link yet"
+ }
+ disabled={isDeactivated}
+ className={cn(
+ "border-border-default focus:border-border-emphasis bg-bg-default h-10 min-w-0 shrink grow rounded-md border px-3 text-sm focus:outline-none focus:ring-neutral-500",
+ hasPartnerLink
+ ? "text-content-default"
+ : "text-content-subtle",
+ isDeactivated && "text-content-subtle cursor-default",
+ )}
+ />
+ {isDeactivated ? (
+ (() => {
+ const deactivatedBadge = PartnerStatusBadges.deactivated;
+ return (
+ <StatusBadge
+ variant={deactivatedBadge.variant}
+ icon={deactivatedBadge.icon}
+ className="xs:w-fit absolute right-4 top-1/2 -translate-y-1/2 px-1.5 py-0.5"
+ >
+ {deactivatedBadge.label}
+ </StatusBadge>
+ );
+ })()
+ ) : hasPartnerLink ? (
+ <Button
+ icon={
+ <div className="relative size-4">
+ <div
+ className={cn(
+ "absolute inset-0 transition-[transform,opacity]",
+ copied && "translate-y-1 opacity-0",
+ )}
+ >
+ <Copy className="size-4" />
+ </div>
+ <div
+ className={cn(
+ "absolute inset-0 transition-[transform,opacity]",
+ !copied && "translate-y-1 opacity-0",
+ )}
+ >
+ <Check className="size-4" />
+ </div>
+ </div>
+ }
+ text={copied ? "Copied link" : "Copy link"}
+ className="xs:w-fit"
+ onClick={() => {
+ copyToClipboard(partnerLink);
+ }}
/>
) : (
- <div className="h-10 w-16 animate-pulse rounded-md bg-neutral-200 lg:w-72" />
- )}
- {isDeactivated
- ? (() => {
- const deactivatedBadge =
- PartnerStatusBadges.deactivated;
- return (
- <StatusBadge
- variant={deactivatedBadge.variant}
- icon={deactivatedBadge.icon}
- className="xs:w-fit absolute right-4 top-1/2 -translate-y-1/2 px-1.5 py-0.5"
- >
- {deactivatedBadge.label}
- </StatusBadge>
- );
- })()
- : !isDeactivated && (
- <Button
- icon={
- <div className="relative size-4">
- <div
- className={cn(
- "absolute inset-0 transition-[transform,opacity]",
- copied && "translate-y-1 opacity-0",
- )}
- >
- <Copy className="size-4" />
- </div>
- <div
- className={cn(
- "absolute inset-0 transition-[transform,opacity]",
- !copied && "translate-y-1 opacity-0",
- )}
- >
- <Check className="size-4" />
- </div>
- </div>
- }
- text={copied ? "Copied link" : "Copy link"}
- className="xs:w-fit"
- onClick={() => {
- if (partnerLink) {
- copyToClipboard(partnerLink);
- }
- }}
- />
+ <Link
+ href={`/programs/${programSlug}/links`}
+ className={cn(
+ buttonVariants({ variant: "primary" }),
+ "xs:w-fit inline-flex h-10 items-center justify-center rounded-lg px-4 text-sm font-medium",
)}
+ >
+ Create link
+ </Link>
+ )}
</div>
- {programEnrollment.group?.linkStructure === "query" && (
- <QueryLinkStructureHelpText link={defaultProgramLink} />
- )}
+ {programEnrollment.group?.linkStructure === "query" &&
+ defaultProgramLink && (
+ <QueryLinkStructureHelpText link={defaultProgramLink} />
+ )}
<span className="mt-12 text-base font-semibold text-neutral-800">
Rewards
@@ -404,17 +414,21 @@ function EarningsChart() {
</div>
</div>
<div className="relative mt-2 h-44 w-full">
- {data ? (
+ {error ? (
+ <div className="flex size-full items-center justify-center">
+ <span className="text-sm text-neutral-500">
+ Failed to load earnings data.
+ </span>
+ </div>
+ ) : timeseries === undefined ? (
+ <div className="flex size-full items-center justify-center">
+ <LoadingSpinner />
+ </div>
+ ) : data && data.length > 0 ? (
<BrandedChart data={data} currency />
) : (
<div className="flex size-full items-center justify-center">
- {error ? (
- <span className="text-sm text-neutral-500">
- Failed to load earnings data.
- </span>
- ) : (
- <LoadingSpinner />
- )}
+ <span className="text-sm text-neutral-500">No data available</span>
</div>
)}
</div>
@@ -479,7 +493,17 @@ function StatCard({
/>
</div>
<div className="mt-2 h-44 w-full">
- {timeseries ? (
+ {error ? (
+ <div className="flex size-full items-center justify-center">
+ <span className="text-sm text-neutral-500">
+ Failed to load data.
+ </span>
+ </div>
+ ) : timeseries === undefined ? (
+ <div className="flex size-full items-center justify-center">
+ <LoadingSpinner />
+ </div>
+ ) : timeseries.length > 0 ? (
<BrandedChart
data={timeseries.map((d) => ({
date: new Date(d.start),
@@ -488,13 +512,7 @@ function StatCard({
/>
) : (
<div className="flex size-full items-center justify-center">
- {error ? (
- <span className="text-sm text-neutral-500">
- Failed to load data.
- </span>
- ) : (
- <LoadingSpinner />
- )}
+ <span className="text-sm text-neutral-500">No data available</span>
</div>
)}
</div>
diff --git a/apps/web/ui/analytics/events/events-tabs.tsx b/apps/web/ui/analytics/events/events-tabs.tsx
index 11d209eede..5c34b82813 100644
--- a/apps/web/ui/analytics/events/events-tabs.tsx
+++ b/apps/web/ui/analytics/events/events-tabs.tsx
@@ -67,6 +67,15 @@ export default function EventsTabs() {
if (tab !== "sales" && sortBy !== "timestamp") queryParams({ del: "sort" });
}, [tab, searchParams.get("sort")]);
+ const tabNumberValue = (event: "clicks" | "leads" | "sales") => {
+ if (totalEvents === undefined) return undefined;
+ if (Array.isArray(totalEvents)) return 0;
+ if (event === "sales") {
+ return (totalEvents.saleAmount ?? 0) / 100;
+ }
+ return totalEvents[event] ?? 0;
+ };
+
return (
<div className="grid w-full grid-cols-3 gap-2 overflow-x-auto sm:gap-4">
{["clicks", "leads", "sales"].map((event) => (
@@ -83,12 +92,10 @@ export default function EventsTabs() {
<div>
<p className="text-sm text-neutral-600">{capitalize(event)}</p>
<div className="mt-2">
- {totalEvents ? (
+ {totalEvents !== undefined ? (
<NumberFlow
value={
- event === "sales"
- ? totalEvents?.saleAmount / 100
- : totalEvents?.[event]
+ tabNumberValue(event as "clicks" | "leads" | "sales") ?? 0
}
className={cn(
"text-2xl transition-opacity",
@@ -104,7 +111,9 @@ export default function EventsTabs() {
}
: {
notation:
- totalEvents?.[event] > 999999
+ (tabNumberValue(
+ event as "clicks" | "leads" | "sales",
+ ) ?? 0) > 999999
? "compact"
: "standard",
}
diff --git a/packages/ui/src/charts/areas.tsx b/packages/ui/src/charts/areas.tsx
index 2fb2c7c705..a2738a1e66 100644
--- a/packages/ui/src/charts/areas.tsx
+++ b/packages/ui/src/charts/areas.tsx
@@ -117,7 +117,7 @@ export function Areas({
</Area>
{/* Latest value circle */}
- {showLatestValueCircle && !tooltipData && (
+ {showLatestValueCircle && !tooltipData && data.length > 0 && (
<Circle
cx={xScale(data.at(-1)!.date)}
cy={yScale(s.valueAccessor(data.at(-1)!))}
diff --git a/packages/ui/src/charts/time-series-chart.tsx b/packages/ui/src/charts/time-series-chart.tsx
index e1429ec747..0f7feee109 100644
--- a/packages/ui/src/charts/time-series-chart.tsx
+++ b/packages/ui/src/charts/time-series-chart.tsx
@@ -22,7 +22,9 @@ export function TimeSeriesChart<T extends Datum>(
{({ width, height }) => {
return (
width > 0 &&
- height > 0 && (
+ height > 0 &&
+ props.data.length > 0 &&
+ props.series.length > 0 && (
<TimeSeriesChartInner {...props} width={width} height={height} />
)
);
diff --git a/packages/ui/src/charts/use-tooltip.ts b/packages/ui/src/charts/use-tooltip.ts
index a11a58aebd..2f930bcb09 100644
--- a/packages/ui/src/charts/use-tooltip.ts
+++ b/packages/ui/src/charts/use-tooltip.ts
@@ -178,6 +178,10 @@ export function useTooltip<T extends Datum>({
else visxTooltip.hideTooltip();
return;
}
+ if (data.length === 0) {
+ visxTooltip.hideTooltip();
+ return;
+ }
const index = bisectDate(data, x0, 1);
const d0 = data[index - 1];
const d1 = data[index];