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.

WIP new referrals UI

Tim Wilson91372891f4bb
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/activity-list.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/activity-list.tsx
new file mode 100644
index 0000000000..d05c447c70
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/activity-list.tsx
@@ -0,0 +1,145 @@
+"use client";
+
+import { clickEventEnrichedSchema } from "@/lib/zod/schemas/clicks";
+import { leadEventEnrichedSchema } from "@/lib/zod/schemas/leads";
+import { saleEventEnrichedSchema } from "@/lib/zod/schemas/sales";
+import { EventList } from "@/ui/blocks/event-list";
+import { useRouterStuff } from "@dub/ui";
+import { CursorRays, Globe, InvoiceDollar, UserCheck } from "@dub/ui/src/icons";
+import { COUNTRIES, timeAgo } from "@dub/utils";
+import { z } from "zod";
+
+type ActivityListProps = {
+ page: number;
+ hasNextPage?: boolean;
+} & (
+ | {
+ event: "clicks";
+ events: z.infer<typeof clickEventEnrichedSchema>[];
+ }
+ | {
+ event: "leads";
+ events: z.infer<typeof leadEventEnrichedSchema>[];
+ }
+ | {
+ event: "sales";
+ events: z.infer<typeof saleEventEnrichedSchema>[];
+ }
+);
+
+export function ActivityList({
+ event,
+ page,
+ hasNextPage,
+ events,
+}: ActivityListProps) {
+ const { queryParams } = useRouterStuff();
+
+ return (
+ <EventList
+ events={events.map((e) => {
+ const Icon = {
+ clicks: CursorRays,
+ leads: UserCheck,
+ sales: InvoiceDollar,
+ }[event];
+ return {
+ icon: <Icon className="size-4.5" />,
+ content: {
+ clicks: <ClickDescription event={e} />,
+ leads: <LeadDescription event={e} />,
+ sales: <SaleDescription event={e} />,
+ }[event],
+ right: timeAgo(new Date(e.timestamp), { withAgo: true }),
+ };
+ })}
+ onPrevious={() =>
+ queryParams({ set: { page: Math.max(page - 1, 1).toString() } })
+ }
+ onNext={() => queryParams({ set: { page: (page + 1).toString() } })}
+ hasPrevious={page > 1}
+ hasNext={hasNextPage}
+ />
+ );
+}
+
+function ClickDescription({
+ event,
+}: {
+ event: z.infer<typeof clickEventEnrichedSchema>;
+}) {
+ return (
+ <div className="flex items-center gap-2">
+ <p>Someone from</p>
+ {event.country ? (
+ <img
+ alt={event.country}
+ src={`https://flag.vercel.app/m/${event.country}.svg`}
+ className="h-3 w-5"
+ />
+ ) : (
+ <Globe className="size-3 text-gray-700" />
+ )}
+ <p>
+ <span className="font-semibold text-gray-700">
+ {event.country ? COUNTRIES[event.country] : "Planet Earth"}
+ </span>{" "}
+ clicked on your link
+ </p>
+ </div>
+ );
+}
+
+function LeadDescription({
+ event,
+}: {
+ event: z.infer<typeof leadEventEnrichedSchema>;
+}) {
+ return (
+ <div className="flex items-center gap-2">
+ <p>Someone from</p>
+ {event.country ? (
+ <img
+ alt={event.country}
+ src={`https://flag.vercel.app/m/${event.country}.svg`}
+ className="h-3 w-5"
+ />
+ ) : (
+ <Globe className="size-3 text-gray-700" />
+ )}
+ <p>
+ <span className="font-semibold text-gray-700">
+ {event.country ? COUNTRIES[event.country] : "Planet Earth"}
+ </span>{" "}
+ signed up for an account
+ </p>
+ </div>
+ );
+}
+
+function SaleDescription({
+ event,
+}: {
+ event: z.infer<typeof saleEventEnrichedSchema>;
+}) {
+ return (
+ <div className="flex items-center gap-2">
+ <p>Someone from</p>
+ {event.country ? (
+ <img
+ alt={event.country}
+ src={`https://flag.vercel.app/m/${event.country}.svg`}
+ className="h-3 w-5"
+ />
+ ) : (
+ <Globe className="size-3 text-gray-700" />
+ )}
+ <p>
+ <span className="font-semibold text-gray-700">
+ {event.country ? COUNTRIES[event.country] : "Planet Earth"}
+ </span>{" "}
+ upgraded their account
+ </p>
+ </div>
+ );
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/event-tabs.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/event-tabs.tsx
new file mode 100644
index 0000000000..de3c9bd2b1
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/event-tabs.tsx
@@ -0,0 +1,20 @@
+"use client";
+
+import { EventType } from "@/lib/analytics/types";
+import { useRouterStuff } from "@dub/ui";
+import { ToggleGroup } from "@dub/ui/src/toggle-group";
+
+export function EventTabs({ event }: { event: EventType }) {
+ const { queryParams } = useRouterStuff();
+ return (
+ <ToggleGroup
+ options={[
+ { value: "clicks", label: "Clicks" },
+ { value: "leads", label: "Leads" },
+ { value: "sales", label: "Sales" },
+ ]}
+ selected={event}
+ selectAction={(event) => queryParams({ set: { event } })}
+ />
+ );
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page.tsx
index 433703f787..07b84f1f92 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page.tsx
@@ -1,104 +1,104 @@
+export const dynamic = "force-dynamic";
+
+import { EVENT_TYPES } from "@/lib/analytics/constants";
import { dub } from "@/lib/dub";
-import { Button, CopyButton, Wordmark } from "@dub/ui";
-import { CursorRays, Globe, InvoiceDollar, UserCheck } from "@dub/ui/src/icons";
-import { COUNTRIES, getPrettyUrl, timeAgo } from "@dub/utils";
+import { Button, CopyButton } from "@dub/ui";
+import { Check } from "@dub/ui/src/icons";
+import { getPrettyUrl } from "@dub/utils";
import { Mail } from "lucide-react";
+import { ActivityList } from "./activity-list";
+import { EventTabs } from "./event-tabs";
import ReferralsPageClient from "./page-client";
-export default function ReferralsPage({
+export default async function ReferralsPage({
params: { slug },
+ searchParams,
}: {
params: { slug: string };
+ searchParams: { event?: string; page?: string };
}) {
const link = `https://refer.dub.co/${slug}`;
+ const event = EVENT_TYPES.find((e) => e === searchParams.event) ?? "clicks";
+ const page = parseInt(searchParams.page ?? "1") || 1;
+
+ const eventsParams = {
+ domain: "refer.dub.co",
+ key: slug,
+ event,
+ };
+
+ const events = await dub.events.list({
+ ...eventsParams,
+ page: page - 1,
+ });
+
+ // Ideally the Dub SDK/API would give us hasNext info, but at least this could speed things up with caching
+ const nextEvents = await dub.events.list({
+ ...eventsParams,
+ page: page,
+ });
+
return (
<ReferralsPageClient>
<div>
- <h1 className="text-2xl font-semibold tracking-tight text-black">
- Referrals
- </h1>
- <div className="my-4 grid gap-4 lg:grid-cols-3">
- <div className="relative flex flex-col items-center justify-center overflow-hidden rounded-xl border border-gray-200 bg-gradient-to-tr from-gray-100 p-4">
- <h2 className="text-2xl font-semibold text-gray-900">500 clicks</h2>
- <p className="text-sm text-gray-500">Referral Bonus</p>
- <Wordmark className="absolute inset-x-0 -bottom-8 mx-auto h-20 text-gray-200" />
- </div>
- <div className="flex gap-3 lg:col-span-2 lg:flex-col">
- <p className="font-medium text-gray-700">Why refer someone?</p>
- <div className="flex items-center gap-x-2">
- <UserCheck className="size-4 text-gray-500" />
- <p className="text-sm text-gray-500">
- Earn 500 extra clicks quota per month per signup (up to 16,000
- total)
- </p>
- </div>
- <div className="flex items-center gap-x-2">
- <InvoiceDollar className="size-4 text-gray-500" />
- <p className="text-sm text-gray-500">
- Earn 10% recurring revenue per paying customer (up to 1 year)
- </p>
- </div>
- <div className="grid gap-1.5">
- <p className="text-xs text-gray-500">Referral Link</p>
- <div className="grid max-w-md grid-cols-[1fr_auto] gap-x-2">
- <div className="flex h-9 items-center justify-between gap-x-2 rounded-lg border border-gray-300 bg-white py-1.5 pl-4 pr-2">
- <p className="text-sm text-gray-500">{getPrettyUrl(link)}</p>
- <CopyButton
- value={link}
- variant="neutral"
- className="p-1.5 text-gray-500"
- />
+ <div className="rounded-xl border border-gray-200 p-9">
+ <h1 className="text-2xl font-semibold text-black">Refer and earn</h1>
+ <div className="mt-6 flex flex-col gap-6">
+ {[
+ {
+ title: "10% recurring revenue",
+ description: "per paying customer (up to 1 year)",
+ },
+ {
+ title: "500 extra clicks quota per month",
+ description: "per signup (up to 16,000 total)",
+ },
+ ].map(({ title, description }) => (
+ <div className="flex items-center gap-3">
+ <div className="flex size-9 items-center justify-center rounded-full border border-gray-200 bg-gradient-to-t from-gray-100 to-white">
+ <div className="rounded-full bg-green-500 p-0.5">
+ <Check className="size-3.5 text-white" />
+ </div>
+ </div>
+ <div>
+ <p className="text-base font-medium text-gray-800">{title}</p>
+ <p className="text-xs text-gray-500">{description}</p>
</div>
- <Button
- text="Invite via email"
- icon={<Mail className="size-4" />}
- className="h-9 rounded-lg"
+ </div>
+ ))}
+ </div>
+ <div className="mt-8 grid gap-1.5">
+ <p className="text-xs text-gray-500">Referral Link</p>
+ <div className="grid max-w-md grid-cols-[1fr_auto] gap-x-2">
+ <div className="flex h-9 items-center justify-between gap-x-2 rounded-lg border border-gray-300 bg-white py-1.5 pl-4 pr-2">
+ <p className="text-sm text-gray-500">{getPrettyUrl(link)}</p>
+ <CopyButton
+ value={link}
+ variant="neutral"
+ className="p-1.5 text-gray-500"
/>
</div>
+ <Button
+ text="Invite via email"
+ icon={<Mail className="size-4" />}
+ className="h-9 rounded-lg"
+ />
</div>
</div>
</div>
- <div className="w-full border-t border-gray-200" />
- <ClickEvents slug={slug} />
- </div>
- </ReferralsPageClient>
- );
-}
-
-async function ClickEvents({ slug }: { slug: string }) {
- const events = await dub.events.list({
- domain: "refer.dub.co",
- key: slug,
- });
-
- return (
- <div className="grid divide-y divide-gray-200">
- {events.map(({ clickId, country, timestamp }) => (
- <div key={clickId} className="flex justify-between gap-2 p-4">
- <div className="flex items-center gap-2 text-sm text-gray-500">
- <CursorRays className="size-4 text-gray-500" />
- <p>Someone from</p>
- {country ? (
- <img
- alt={country}
- src={`https://flag.vercel.app/m/${country}.svg`}
- className="h-3 w-5"
- />
- ) : (
- <Globe className="size-3 text-gray-700" />
- )}
- <p>
- <span className="font-semibold text-gray-700">
- {country ? COUNTRIES[country] : "Planet Earth"}
- </span>{" "}
- clicked on your link
- </p>
+ <div className="mt-12">
+ <div className="mb-5 flex items-end justify-between">
+ <h2 className="text-xl font-semibold text-gray-800">Activity</h2>
+ <EventTabs event={event} />
</div>
- <p className="text-sm text-gray-400">
- {timeAgo(new Date(timestamp), { withAgo: true })}
- </p>
+ <ActivityList
+ event={event}
+ page={page}
+ hasNextPage={nextEvents.length > 0}
+ events={events as any}
+ />
</div>
- ))}
- </div>
+ </div>
+ </ReferralsPageClient>
);
}
diff --git a/apps/web/scripts/seed-data.ts b/apps/web/scripts/seed-data.ts
index 35787d1205..25672ea8e3 100644
--- a/apps/web/scripts/seed-data.ts
+++ b/apps/web/scripts/seed-data.ts
@@ -34,14 +34,25 @@ const links = [
},
];
-const countries = ["US", "IN", "DE", "FR", "GB", "NL", "SG", "CA", "AU", "BR"];
+const countries = [
+ { continent: "NA", country: "US" },
+ { continent: "AS", country: "IN" },
+ { continent: "EU", country: "DE" },
+ { continent: "EU", country: "FR" },
+ { continent: "EU", country: "GB" },
+ { continent: "EU", country: "NL" },
+ { continent: "AS", country: "SG" },
+ { continent: "NA", country: "CA" },
+ { continent: "OC", country: "AU" },
+ { continent: "SA", country: "BR" },
+];
const devices = ["Desktop", "Mobile", "Tablet"];
async function main() {
- // await seedLinkMetadata();
- // await seedClicks();
- // await seedLeads(3269);
+ await seedLinkMetadata();
+ await seedClicks();
+ await seedLeads(3269);
await seedSales(50);
}
@@ -77,6 +88,8 @@ async function seedLinkMetadata() {
async function seedClicks(count = 10000) {
const data = Array.from({ length: count }).map(() => {
const link = links[Math.floor(Math.random() * links.length)];
+ const { country, continent } =
+ countries[Math.floor(Math.random() * countries.length)];
return {
timestamp: new Date(
@@ -85,7 +98,8 @@ async function seedClicks(count = 10000) {
click_id: nanoid(16),
link_id: link.id,
url: link.url,
- country: countries[Math.floor(Math.random() * countries.length)],
+ country,
+ continent,
device: devices[Math.floor(Math.random() * devices.length)],
alias_link_id: "",
ip: "63.141.57.109",
@@ -132,6 +146,8 @@ async function seedClicks(count = 10000) {
async function seedLeads(count = 10) {
const data = Array.from({ length: count }).map(() => {
const link = links[Math.floor(Math.random() * links.length)];
+ const { country, continent } =
+ countries[Math.floor(Math.random() * countries.length)];
return {
// random date in the last 30 days
@@ -144,9 +160,11 @@ async function seedLeads(count = 10) {
click_id: nanoid(16),
link_id: link.id,
url: link.url,
- country: countries[Math.floor(Math.random() * countries.length)],
+ country,
+ continent,
device: devices[Math.floor(Math.random() * devices.length)],
metadata: "",
+ ip: "63.141.57.109",
city: "San Francisco",
region: "CA",
latitude: "37.7695",
@@ -162,6 +180,7 @@ async function seedLeads(count = 10) {
cpu_architecture: "Unknown",
ua: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
bot: 0,
+ qr: 0,
referer: "(direct)",
referer_url: "(direct)",
};
@@ -189,6 +208,8 @@ async function seedLeads(count = 10) {
async function seedSales(count = 5) {
const data = Array.from({ length: count }).map(() => {
const link = links[Math.floor(Math.random() * links.length)];
+ const { country, continent } =
+ countries[Math.floor(Math.random() * countries.length)];
return {
// random date in the last 30 days
@@ -196,11 +217,13 @@ async function seedSales(count = 5) {
Date.now() - Math.floor(Math.random() * 30 * 24 * 60 * 60 * 1000),
).toISOString(),
event_id: nanoid(16),
+ event_name: "Subscription creation",
customer_id: "xxxx",
click_id: nanoid(16),
link_id: link.id,
url: link.url,
- country: countries[Math.floor(Math.random() * countries.length)],
+ country,
+ continent,
device: devices[Math.floor(Math.random() * devices.length)],
invoice_id: nanoid(16),
// random amount between $24 and $99
@@ -208,6 +231,7 @@ async function seedSales(count = 5) {
currency: "USD",
payment_processor: "stripe",
metadata: "",
+ ip: "63.141.57.109",
city: "San Francisco",
region: "CA",
latitude: "37.7695",
@@ -223,6 +247,7 @@ async function seedSales(count = 5) {
cpu_architecture: "Unknown",
ua: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
bot: 0,
+ qr: 0,
referer: "(direct)",
referer_url: "(direct)",
};
diff --git a/apps/web/ui/blocks/event-list.tsx b/apps/web/ui/blocks/event-list.tsx
new file mode 100644
index 0000000000..db5b025644
--- /dev/null
+++ b/apps/web/ui/blocks/event-list.tsx
@@ -0,0 +1,70 @@
+"use client";
+
+import { PropsWithChildren, ReactNode } from "react";
+
+export type EventListProps = PropsWithChildren<{
+ events: { icon: ReactNode; content: ReactNode; right?: ReactNode }[];
+ hasNext?: boolean;
+ hasPrevious?: boolean;
+ onNext?: () => void;
+ onPrevious?: () => void;
+}>;
+
+const buttonClassName = [
+ "flex h-7 items-center justify-center gap-2 whitespace-nowrap rounded-md border border-gray-200 bg-white px-2 text-sm text-gray-600",
+ "outline-none hover:bg-gray-50 focus-visible:border-gray-500",
+ "disabled:cursor-not-allowed disabled:border-gray-200 disabled:text-gray-400 disabled:bg-white",
+].join(" ");
+
+export function EventList({
+ events,
+ hasNext,
+ hasPrevious,
+ onNext,
+ onPrevious,
+}: EventListProps) {
+ return (
+ <div className="rounded-xl border border-gray-200">
+ <div className="flex flex-col divide-y divide-gray-200">
+ {events.map((event, index) => (
+ <div
+ key={index}
+ className="flex items-center justify-between px-3.5 py-5"
+ >
+ <div className="flex items-center gap-2.5 text-gray-500">
+ <div>{event.icon}</div>
+ <div className="text-xs">{event.content}</div>
+ </div>
+ {event.right !== undefined && (
+ <div className="text-xs text-gray-400">{event.right}</div>
+ )}
+ </div>
+ ))}
+ </div>
+ {onPrevious && onNext && (
+ <div className="flex items-center justify-end gap-2 border-t border-gray-200 px-3.5 py-2">
+ {onPrevious && (
+ <button
+ type="button"
+ className={buttonClassName}
+ onClick={onPrevious}
+ disabled={!hasPrevious}
+ >
+ Previous
+ </button>
+ )}
+ {onNext && (
+ <button
+ type="button"
+ className={buttonClassName}
+ onClick={onNext}
+ disabled={!hasNext}
+ >
+ Next
+ </button>
+ )}
+ </div>
+ )}
+ </div>
+ );
+}
diff --git a/packages/tinybird/pipes/v1_events.pipe b/packages/tinybird/pipes/v1_events.pipe
index f10c1cd926..99874a5c8f 100644
--- a/packages/tinybird/pipes/v1_events.pipe
+++ b/packages/tinybird/pipes/v1_events.pipe
@@ -73,6 +73,7 @@ SQL >
wl.domain as domain,
wl.key as key,
dle.url as url,
+ dle.continent as continent,
dle.country as country,
dle.city as city,
dle.device as device,
@@ -124,6 +125,7 @@ SQL >
wl.domain as domain,
wl.key as key,
dse.url as url,
+ dse.continent as continent,
dse.country as country,
dse.city as city,
dse.device as device,
diff --git a/packages/ui/src/toggle-group.tsx b/packages/ui/src/toggle-group.tsx
index be09606289..b775b981fc 100644
--- a/packages/ui/src/toggle-group.tsx
+++ b/packages/ui/src/toggle-group.tsx
@@ -1,3 +1,5 @@
+"use client";
+
import { cn } from "@dub/utils";
import { LayoutGroup, motion } from "framer-motion";
import { useId } from "react";