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.

Show LTV (`saleAmount`) to partners

Steven Tey8f85b67fb1a7
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts
index d824958ddf..5ca5c7d151 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts
@@ -5,6 +5,7 @@ import { obfuscateCustomerEmail } from "@/lib/api/partner-profile/obfuscate-cust
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { withPartnerProfile } from "@/lib/auth/partner";
import {
+ CUSTOMER_LTV_EXCLUDED_PROGRAM_IDS,
LARGE_PROGRAM_IDS,
LARGE_PROGRAM_MIN_TOTAL_COMMISSIONS_CENTS,
} from "@/lib/constants/partner-profile";
@@ -86,21 +87,27 @@ export const GET = withPartnerProfile(async ({ partner, params }) => {
return NextResponse.json(
PartnerProfileCustomerSchema.extend({
...(customerDataSharingEnabledAt && { name: z.string().nullish() }),
- }).parse({
- ...transformCustomer({
- ...customer,
- firstSaleAt: customer.commissions[0]?.createdAt ?? null,
- email: customer.email
- ? customerDataSharingEnabledAt
- ? customer.email
- : obfuscateCustomerEmail(customer.email)
- : customer.name || generateRandomName(),
+ })
+ .omit({
+ ...(CUSTOMER_LTV_EXCLUDED_PROGRAM_IDS.includes(program.id) && {
+ saleAmount: true,
+ }),
+ })
+ .parse({
+ ...transformCustomer({
+ ...customer,
+ firstSaleAt: customer.commissions[0]?.createdAt ?? null,
+ email: customer.email
+ ? customerDataSharingEnabledAt
+ ? customer.email
+ : obfuscateCustomerEmail(customer.email)
+ : customer.name || generateRandomName(),
+ }),
+ activity: {
+ ...customer,
+ events,
+ link,
+ },
}),
- activity: {
- ...customer,
- events,
- link,
- },
- }),
);
});
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/route.ts
index cf3a7b4e72..7171e2a7ef 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/route.ts
@@ -4,6 +4,7 @@ import { obfuscateCustomerEmail } from "@/lib/api/partner-profile/obfuscate-cust
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { withPartnerProfile } from "@/lib/auth/partner";
import {
+ CUSTOMER_LTV_EXCLUDED_PROGRAM_IDS,
LARGE_PROGRAM_IDS,
LARGE_PROGRAM_MIN_TOTAL_COMMISSIONS_CENTS,
} from "@/lib/constants/partner-profile";
@@ -52,6 +53,16 @@ export const GET = withPartnerProfile(
});
}
+ if (
+ sortBy === "saleAmount" &&
+ CUSTOMER_LTV_EXCLUDED_PROGRAM_IDS.includes(program.id)
+ ) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "This feature is not available for your program.",
+ });
+ }
+
// Get all customers with their first commission date in a single optimized query
const customers = await prisma.customer.findMany({
where: {
@@ -94,22 +105,28 @@ export const GET = withPartnerProfile(
const customersWithData = customers.map((customer) => {
return PartnerProfileCustomerSchema.extend({
...(customerDataSharingEnabledAt && { name: z.string().nullish() }),
- }).parse({
- ...transformCustomer({
- ...customer,
- firstSaleAt: customer.commissions[0]?.createdAt ?? null,
- email: customer.email
- ? customerDataSharingEnabledAt
- ? customer.email
- : obfuscateCustomerEmail(customer.email)
- : customer.name || generateRandomName(),
- }),
- activity: {
- ...customer,
- events: [],
- link: customer.link,
- },
- });
+ })
+ .omit({
+ ...(CUSTOMER_LTV_EXCLUDED_PROGRAM_IDS.includes(program.id) && {
+ saleAmount: true,
+ }),
+ })
+ .parse({
+ ...transformCustomer({
+ ...customer,
+ firstSaleAt: customer.commissions[0]?.createdAt ?? null,
+ email: customer.email
+ ? customerDataSharingEnabledAt
+ ? customer.email
+ : obfuscateCustomerEmail(customer.email)
+ : customer.name || generateRandomName(),
+ }),
+ activity: {
+ ...customer,
+ events: [],
+ link: customer.link,
+ },
+ });
});
return NextResponse.json(customersWithData);
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/customers/(index)/page.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/customers/(index)/page.tsx
index 6b4ba8dcfd..6e3ad8d4d3 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/customers/(index)/page.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/customers/(index)/page.tsx
@@ -1,6 +1,9 @@
"use client";
-import { PARTNER_CUSTOMERS_MAX_PAGE_SIZE } from "@/lib/constants/partner-profile";
+import {
+ CUSTOMER_LTV_EXCLUDED_PROGRAM_IDS,
+ PARTNER_CUSTOMERS_MAX_PAGE_SIZE,
+} from "@/lib/constants/partner-profile";
import usePartnerCustomers from "@/lib/swr/use-partner-customers";
import usePartnerCustomersCount from "@/lib/swr/use-partner-customers-count";
import useProgramEnrollment from "@/lib/swr/use-program-enrollment";
@@ -21,7 +24,13 @@ import {
useTable,
} from "@dub/ui";
import { User } from "@dub/ui/icons";
-import { COUNTRIES, formatDate, getApexDomain, getPrettyUrl } from "@dub/utils";
+import {
+ COUNTRIES,
+ currencyFormatter,
+ formatDate,
+ getApexDomain,
+ getPrettyUrl,
+} from "@dub/utils";
import { useParams, useRouter } from "next/navigation";
import { useMemo } from "react";
import { usePartnerCustomerFilters } from "./use-partner-customer-filters";
@@ -61,6 +70,11 @@ export default function PartnerProgramCustomersPage() {
"link",
"createdAt",
"firstSaleAt",
+ ...(CUSTOMER_LTV_EXCLUDED_PROGRAM_IDS.includes(
+ programEnrollment?.programId ?? "",
+ )
+ ? []
+ : ["saleAmount"]),
"subscriptionCanceledAt",
],
defaultVisible: [
@@ -69,6 +83,11 @@ export default function PartnerProgramCustomersPage() {
"link",
"createdAt",
"firstSaleAt",
+ ...(CUSTOMER_LTV_EXCLUDED_PROGRAM_IDS.includes(
+ programEnrollment?.programId ?? "",
+ )
+ ? []
+ : ["saleAmount"]),
"subscriptionCanceledAt",
],
};
@@ -178,6 +197,24 @@ export default function PartnerProgramCustomersPage() {
"-"
),
},
+ ...(CUSTOMER_LTV_EXCLUDED_PROGRAM_IDS.includes(
+ programEnrollment?.programId ?? "",
+ )
+ ? []
+ : [
+ {
+ id: "saleAmount",
+ header: "LTV",
+ meta: {
+ headerTooltip:
+ "The customer's lifetime value (how much revenue the customer has generated over their lifetime).",
+ },
+ accessorKey: "saleAmount",
+ cell: ({ row }) => (
+ <span>{currencyFormatter(row.original.saleAmount)}</span>
+ ),
+ },
+ ]),
{
id: "subscriptionCanceledAt",
header: "Canceled",
@@ -238,7 +275,12 @@ export default function PartnerProgramCustomersPage() {
onPaginationChange: setPagination,
columnVisibility,
onColumnVisibilityChange: setColumnVisibility,
- sortableColumns: ["createdAt", "firstSaleAt", "subscriptionCanceledAt"],
+ sortableColumns: [
+ "createdAt",
+ "firstSaleAt",
+ "saleAmount",
+ "subscriptionCanceledAt",
+ ],
sortBy,
sortOrder,
onSortChange: ({ sortBy, sortOrder }) =>
diff --git a/apps/web/lib/constants/partner-profile.ts b/apps/web/lib/constants/partner-profile.ts
index 01b735dc04..2fab216195 100644
--- a/apps/web/lib/constants/partner-profile.ts
+++ b/apps/web/lib/constants/partner-profile.ts
@@ -15,3 +15,7 @@ export const PARTNER_NETWORK_EXCLUDED_PROGRAM_IDS = [
export const PARTNER_NETWORK_MIN_COMMISSIONS_CENTS = 10_00; // $10
export const PARTNER_CUSTOMERS_MAX_PAGE_SIZE = 100;
+
+export const CUSTOMER_LTV_EXCLUDED_PROGRAM_IDS = [
+ "prog_qGGSH0jXFZLeogOnq1sLkriY",
+];
diff --git a/apps/web/lib/zod/schemas/partner-profile.ts b/apps/web/lib/zod/schemas/partner-profile.ts
index 7917adfbda..234e680690 100644
--- a/apps/web/lib/zod/schemas/partner-profile.ts
+++ b/apps/web/lib/zod/schemas/partner-profile.ts
@@ -107,6 +107,7 @@ export const PartnerProfileCustomerSchema = CustomerEnrichedSchema.pick({
country: true,
createdAt: true,
firstSaleAt: true,
+ saleAmount: true,
subscriptionCanceledAt: true,
}).extend({
activity: customerActivityResponseSchema,
@@ -264,7 +265,12 @@ export const getPartnerCustomersQuerySchema = z
"A filter on the list based on the customer's `linkId` field (the referral link ID).",
),
sortBy: z
- .enum(["createdAt", "firstSaleAt", "subscriptionCanceledAt"])
+ .enum([
+ "createdAt",
+ "firstSaleAt",
+ "saleAmount",
+ "subscriptionCanceledAt",
+ ])
.optional()
.default("createdAt")
.describe(
diff --git a/apps/web/ui/customers/customer-activity-list.tsx b/apps/web/ui/customers/customer-activity-list.tsx
index 7166f524e5..e1f3b0cf95 100644
--- a/apps/web/ui/customers/customer-activity-list.tsx
+++ b/apps/web/ui/customers/customer-activity-list.tsx
@@ -1,7 +1,12 @@
import { CustomerActivityResponse } from "@/lib/types";
import { DynamicTooltipWrapper, LinkLogo, TimestampTooltip } from "@dub/ui";
import { CursorRays, MoneyBill2, UserCheck } from "@dub/ui/icons";
-import { formatDateTimeSmart, getApexDomain, getPrettyUrl } from "@dub/utils";
+import {
+ currencyFormatter,
+ formatDateTimeSmart,
+ getApexDomain,
+ getPrettyUrl,
+} from "@dub/utils";
import Link from "next/link";
import { useParams } from "next/navigation";
import { MetadataViewer } from "../analytics/events/metadata-viewer";
@@ -99,7 +104,16 @@ const activityData = {
content: (event) => {
return (
<div className="flex flex-col gap-1">
- <span>{event.eventName || "New sale"}</span>
+ <span className="flex min-w-0 items-center gap-1.5">
+ <span className="truncate">{event.eventName || "New sale"}</span>
+ {event.sale?.amount != null && (
+ <span className="shrink-0 rounded-md bg-neutral-100 px-1.5 py-1 font-mono text-xs leading-none text-neutral-700">
+ {currencyFormatter(event.sale.amount, {
+ trailingZeroDisplay: "stripIfInteger",
+ })}
+ </span>
+ )}
+ </span>
{event.metadata && <MetadataViewer metadata={event.metadata} />}
</div>
);