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.

display metadata

Kiran Ke402bd3ab335
diff --git a/apps/web/app/api/customers/[id]/activity/route.ts b/apps/web/app/api/customers/[id]/activity/route.ts
index 7e7deef534..bc7f46301f 100644
--- a/apps/web/app/api/customers/[id]/activity/route.ts
+++ b/apps/web/app/api/customers/[id]/activity/route.ts
@@ -7,7 +7,7 @@ import { prisma } from "@dub/prisma";
import { NextResponse } from "next/server";
// GET /api/customers/[id]/activity - get a customer's activity
-export const GET = withWorkspace(async ({ workspace, params, session }) => {
+export const GET = withWorkspace(async ({ workspace, params }) => {
const { id: customerId } = params;
const customer = await getCustomerOrThrow({
diff --git a/apps/web/app/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts b/apps/web/app/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts
index 0dfd98e3a4..c141d63da4 100644
--- a/apps/web/app/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts
+++ b/apps/web/app/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts
@@ -33,6 +33,7 @@ export const GET = withPartnerProfile(async ({ partner, params }) => {
const events = await getCustomerEvents({
customerId: customer.id,
linkIds: links.map((link) => link.id),
+ hideMetadata: true, // don't expose metadata to partners
});
if (events.length === 0) {
diff --git a/apps/web/lib/analytics/get-customer-events.ts b/apps/web/lib/analytics/get-customer-events.ts
index 3ac23ddbb0..e96ab21aa2 100644
--- a/apps/web/lib/analytics/get-customer-events.ts
+++ b/apps/web/lib/analytics/get-customer-events.ts
@@ -14,9 +14,11 @@ import { saleEventResponseSchema } from "../zod/schemas/sales";
export const getCustomerEvents = async ({
customerId,
linkIds,
+ hideMetadata = false,
}: {
customerId: string;
linkIds?: string[];
+ hideMetadata?: boolean;
}) => {
const pipe = tb.buildPipe({
pipe: "v2_customer_events",
@@ -60,6 +62,7 @@ export const getCustomerEvents = async ({
? {
eventId: evt.event_id,
eventName: evt.event_name,
+ metadata: hideMetadata ? null : evt.metadata,
...(evt.event === "sale"
? {
sale: {
diff --git a/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts b/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts
index 094d655915..ec3ee8bac8 100644
--- a/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts
+++ b/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts
@@ -5,7 +5,7 @@ export const verifyAnalyticsAllowedHostnames = ({
allowedHostnames: string[];
req: Request;
}) => {
- if (allowedHostnames.length > 0) {
+ if (allowedHostnames && allowedHostnames.length > 0) {
const source = req.headers.get("referer") || req.headers.get("origin");
const sourceUrl = source ? new URL(source) : null;
const hostname = sourceUrl?.hostname.replace(/^www\./, "");
diff --git a/apps/web/lib/zod/schemas/leads.ts b/apps/web/lib/zod/schemas/leads.ts
index 71fe681731..bd348009e6 100644
--- a/apps/web/lib/zod/schemas/leads.ts
+++ b/apps/web/lib/zod/schemas/leads.ts
@@ -129,6 +129,11 @@ export const leadEventResponseSchema = z
timestamp: z.coerce.string(),
eventId: z.string(),
eventName: z.string(),
+ metadata: z
+ .string()
+ .nullable()
+ .transform((val) => (val === "" ? null : val))
+ .default(null),
// nested objects
click: clickEventSchema,
link: linkEventSchema,
diff --git a/apps/web/lib/zod/schemas/sales.ts b/apps/web/lib/zod/schemas/sales.ts
index bf1b4bcfd6..2518691133 100644
--- a/apps/web/lib/zod/schemas/sales.ts
+++ b/apps/web/lib/zod/schemas/sales.ts
@@ -160,6 +160,11 @@ export const saleEventResponseSchema = z
payment_processor: z
.string()
.describe("Deprecated. Use `sale.paymentProcessor` instead."),
+ metadata: z
+ .string()
+ .nullable()
+ .transform((val) => (val === "" ? null : val))
+ .default(null),
})
.merge(commonDeprecatedEventFields)
.openapi({ ref: "SaleEvent" });
diff --git a/apps/web/ui/customers/customer-activity-list.tsx b/apps/web/ui/customers/customer-activity-list.tsx
index 0434803ab6..06334823dc 100644
--- a/apps/web/ui/customers/customer-activity-list.tsx
+++ b/apps/web/ui/customers/customer-activity-list.tsx
@@ -1,5 +1,10 @@
import { CustomerActivityResponse } from "@/lib/types";
-import { DynamicTooltipWrapper, LinkLogo } from "@dub/ui";
+import {
+ Button,
+ DynamicTooltipWrapper,
+ LinkLogo,
+ useCopyToClipboard,
+} from "@dub/ui";
import { CursorRays, MoneyBill2, UserCheck } from "@dub/ui/icons";
import { formatDateTimeSmart, getApexDomain, getPrettyUrl } from "@dub/utils";
import Link from "next/link";
@@ -80,10 +85,71 @@ const activityData = {
);
},
},
- lead: { icon: UserCheck, content: (event) => event.eventName || "New lead" },
- sale: { icon: MoneyBill2, content: (event) => event.eventName || "New sale" },
+
+ lead: {
+ icon: UserCheck,
+ content: (event) => {
+ const metadata = event.metadata ? JSON.parse(event.metadata) : null;
+
+ return (
+ <div className="flex flex-col gap-1">
+ <span>{event.eventName || "New lead"}</span>
+ {metadata && <MetadataViewer metadata={metadata} />}
+ </div>
+ );
+ },
+ },
+
+ sale: {
+ icon: MoneyBill2,
+ content: (event) => {
+ const metadata = event.metadata ? JSON.parse(event.metadata) : null;
+
+ return (
+ <div className="flex flex-col gap-1">
+ <span>{event.eventName || "New sale"}</span>
+ {metadata && <MetadataViewer metadata={metadata} />}
+ </div>
+ );
+ },
+ },
};
+// Display the event metadata
+function MetadataViewer({ metadata }: { metadata: Record<string, any> }) {
+ const [copied, copyToClipboard] = useCopyToClipboard();
+
+ return (
+ <DynamicTooltipWrapper
+ tooltipProps={{
+ content: (
+ <div className="flex flex-col gap-4 overflow-hidden rounded-md border border-neutral-200 bg-white p-4">
+ <div className="flex h-[300px] w-[350px] overflow-hidden rounded-md border border-neutral-200 bg-white">
+ <div className="w-full overflow-auto">
+ <pre className="p-2 text-xs text-neutral-600">
+ {JSON.stringify(metadata, null, 1)}
+ </pre>
+ </div>
+ </div>
+
+ <Button
+ text={copied ? "Copied!" : "Copy metadata"}
+ onClick={() => copyToClipboard(JSON.stringify(metadata, null, 1))}
+ />
+ </div>
+ ),
+ }}
+ >
+ <button
+ type="button"
+ className="inline-flex items-center gap-1 rounded-md border border-neutral-200 px-1.5 py-0.5 text-xs text-neutral-600 transition-colors hover:bg-neutral-50"
+ >
+ View metadata
+ </button>
+ </DynamicTooltipWrapper>
+ );
+}
+
export function CustomerActivityList({
activity,
isLoading,