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.

Start getting different event types working

Tim Wilson0b864c3d2e88
diff --git a/apps/web/lib/zod/schemas/leads.ts b/apps/web/lib/zod/schemas/leads.ts
index 28e51d89f1..dcbdc36ea5 100644
--- a/apps/web/lib/zod/schemas/leads.ts
+++ b/apps/web/lib/zod/schemas/leads.ts
@@ -69,3 +69,20 @@ export const leadEventSchemaTB = clickEventSchemaTB
metadata: z.string().default(""),
}),
);
+
+export const leadEventEnrichedSchema = z.object({
+ timestamp: z.string(),
+ event_id: z.string(),
+ click_id: z.string(),
+ link_id: z.string(),
+ customer_id: z.string(),
+ event_name: z.string(),
+ metadata: z.string(),
+ url: z.string(),
+ country: z.string().nullable(),
+ city: z.string().nullable(),
+ device: z.string().nullable(),
+ browser: z.string().nullable(),
+ os: z.string().nullable(),
+ referer: z.string().nullable(),
+});
diff --git a/apps/web/lib/zod/schemas/sales.ts b/apps/web/lib/zod/schemas/sales.ts
index 814b6745a8..ee672571e4 100644
--- a/apps/web/lib/zod/schemas/sales.ts
+++ b/apps/web/lib/zod/schemas/sales.ts
@@ -60,3 +60,20 @@ export const saleEventSchemaTB = clickEventSchemaTB
metadata: z.string().default(""),
}),
);
+
+export const saleEventEnrichedSchema = z.object({
+ timestamp: z.string(),
+ event_id: z.string(),
+ click_id: z.string(),
+ link_id: z.string(),
+ customer_id: z.string(),
+ payment_processor: z.string(),
+ amount: z.number(),
+ url: z.string(),
+ country: z.string().nullable(),
+ city: z.string().nullable(),
+ device: z.string().nullable(),
+ browser: z.string().nullable(),
+ os: z.string().nullable(),
+ referer: z.string().nullable(),
+});
diff --git a/apps/web/ui/analytics/events/events-table.tsx b/apps/web/ui/analytics/events/events-table.tsx
index ef114fdbee..43ca13ba6e 100644
--- a/apps/web/ui/analytics/events/events-table.tsx
+++ b/apps/web/ui/analytics/events/events-table.tsx
@@ -36,6 +36,7 @@ type FakeDatum = {
export default function EventsTable() {
const { searchParams, queryParams } = useRouterStuff();
+ const tab = searchParams.get("tab") || "clicks";
const order = searchParams.get("order") === "asc" ? "asc" : "desc";
@@ -128,8 +129,9 @@ export default function EventsTable() {
const { queryString, totalEvents } = useContext(AnalyticsContext);
- const { data, isLoading } = useSWR<FakeDatum[]>(
+ const { data, isLoading, error } = useSWR<FakeDatum[]>(
`/api/analytics/events?${editQueryString(queryString, {
+ event: tab,
offset: (pagination.pageIndex * pagination.pageSize).toString(),
limit: pagination.pageSize.toString(),
order,
@@ -144,7 +146,7 @@ export default function EventsTable() {
data: data ?? defaultData,
columns,
getCoreRowModel: getCoreRowModel(),
- rowCount: totalEvents?.clicks ?? 0,
+ rowCount: totalEvents?.[tab] ?? 0,
onPaginationChange: setPagination,
state: {
pagination,
@@ -157,77 +159,82 @@ export default function EventsTable() {
return (
<div className="border border-gray-200 bg-white sm:rounded-xl">
<div className="relative rounded-[inherit]">
- {/* Could make this scrollable */}
- <div className="scrollbar-hide min-h-[400px] overflow-y-auto rounded-[inherit]">
- <table
- className={cn(
- "w-full border-separate border-spacing-0",
+ {!error && !!data?.length ? (
+ <div className="min-h-[400px] rounded-[inherit]">
+ <table
+ className={cn(
+ "w-full border-separate border-spacing-0",
- // Remove side borders from table to avoid interfering with outer border
- "[&_thead_tr:first-child>*]:border-t-0", // Top row
- "[&_tbody_tr:last-child>*]:border-b-0", // Bottom row
- "[&_tr>*:first-child]:border-l-0", // Left column
- "[&_tr>*:last-child]:border-r-0", // Right column
- )}
- >
- <thead>
- {table.getHeaderGroups().map((headerGroup) => (
- <tr key={headerGroup.id}>
- {headerGroup.headers.map((header) => {
- const isDateColumn = header.column.id === "timestamp";
- return (
- <th
- key={header.id}
- className={cn(tableCellClassName, "font-medium")}
- style={{ width: `${header.getSize()}px` }}
- >
- <button
- className="flex items-center gap-2"
- disabled={!isDateColumn}
- onClick={() =>
- queryParams({
- set: {
- order: order === "asc" ? "desc" : "asc",
- },
- })
- }
- aria-label="Sort by column"
+ // Remove side borders from table to avoid interfering with outer border
+ "[&_thead_tr:first-child>*]:border-t-0", // Top row
+ "[&_tbody_tr:last-child>*]:border-b-0", // Bottom row
+ "[&_tr>*:first-child]:border-l-0", // Left column
+ "[&_tr>*:last-child]:border-r-0", // Right column
+ )}
+ >
+ <thead>
+ {table.getHeaderGroups().map((headerGroup) => (
+ <tr key={headerGroup.id}>
+ {headerGroup.headers.map((header) => {
+ const isDateColumn = header.column.id === "timestamp";
+ return (
+ <th
+ key={header.id}
+ className={cn(tableCellClassName, "font-medium")}
+ style={{ width: `${header.getSize()}px` }}
>
- <span>
- {header.isPlaceholder
- ? null
- : flexRender(
- header.column.columnDef.header,
- header.getContext(),
- )}
- </span>
- {isDateColumn && <SortOrder order={order} />}
- </button>
- </th>
- );
- })}
- </tr>
- ))}
- </thead>
- <tbody>
- {table.getRowModel().rows.map((row) => (
- <tr key={row.id}>
- {row.getVisibleCells().map((cell) => (
- <td
- key={cell.id}
- className={cn(tableCellClassName, "text-gray-600")}
- >
- {flexRender(
- cell.column.columnDef.cell,
- cell.getContext(),
- )}
- </td>
- ))}
- </tr>
- ))}
- </tbody>
- </table>
- </div>
+ <button
+ className="flex items-center gap-2"
+ disabled={!isDateColumn}
+ onClick={() =>
+ queryParams({
+ set: {
+ order: order === "asc" ? "desc" : "asc",
+ },
+ })
+ }
+ aria-label="Sort by column"
+ >
+ <span>
+ {header.isPlaceholder
+ ? null
+ : flexRender(
+ header.column.columnDef.header,
+ header.getContext(),
+ )}
+ </span>
+ {isDateColumn && <SortOrder order={order} />}
+ </button>
+ </th>
+ );
+ })}
+ </tr>
+ ))}
+ </thead>
+ <tbody>
+ {table.getRowModel().rows.map((row) => (
+ <tr key={row.id}>
+ {row.getVisibleCells().map((cell) => (
+ <td
+ key={cell.id}
+ className={cn(tableCellClassName, "text-gray-600")}
+ >
+ {flexRender(
+ cell.column.columnDef.cell,
+ cell.getContext(),
+ )}
+ </td>
+ ))}
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ </div>
+ ) : (
+ <div className="flex h-64 h-full w-full items-center justify-center text-sm text-gray-500">
+ {error ? "Failed to fetch data" : "No data available"}
+ </div>
+ )}
<div className="sticky bottom-0 flex items-center justify-between rounded-b-[inherit] border-t border-gray-200 bg-white px-4 py-3.5 text-sm leading-6 text-gray-600">
<div>
Viewing{" "}
@@ -263,7 +270,7 @@ export default function EventsTable() {
</div>
</div>
- {/* Loading overlay */}
+ {/* Loading/error overlay */}
<AnimatePresence>
{isLoading && (
<motion.div
diff --git a/apps/web/ui/analytics/events/events-tabs.tsx b/apps/web/ui/analytics/events/events-tabs.tsx
new file mode 100644
index 0000000000..548a65b80e
--- /dev/null
+++ b/apps/web/ui/analytics/events/events-tabs.tsx
@@ -0,0 +1,34 @@
+import { useRouterStuff } from "@dub/ui";
+import { capitalize, cn } from "@dub/utils";
+import { useContext } from "react";
+import { AnalyticsContext } from "../analytics-provider";
+
+export default function EventsTabs() {
+ const { searchParams, queryParams } = useRouterStuff();
+
+ const tab = searchParams.get("tab");
+ const { totalEvents } = useContext(AnalyticsContext);
+
+ return (
+ <div className="grid grid-cols-3 gap-4">
+ {["clicks", "leads", "sales"].map((event) => (
+ <button
+ className={cn(
+ "rounded-xl border border-gray-200 bg-white px-5 py-4 text-left transition-all",
+ tab === event && "border-black shadow-[0_0_0_1px_black_inset]",
+ )}
+ onClick={() =>
+ queryParams({
+ set: { tab: event },
+ })
+ }
+ >
+ <p className="text-sm text-gray-600">{capitalize(event)}</p>
+ <p className="mt-2 text-2xl">
+ {(totalEvents?.[event] ?? 0).toLocaleString()}
+ </p>
+ </button>
+ ))}
+ </div>
+ );
+}
diff --git a/apps/web/ui/analytics/events/index.tsx b/apps/web/ui/analytics/events/index.tsx
index 0a28eaff19..69f1a72b10 100644
--- a/apps/web/ui/analytics/events/index.tsx
+++ b/apps/web/ui/analytics/events/index.tsx
@@ -3,6 +3,7 @@
import AnalyticsProvider from "../analytics-provider";
import Toggle from "../toggle";
import EventsTable from "./events-table";
+import EventsTabs from "./events-tabs";
export default function AnalyticsEvents({
staticDomain,
@@ -20,6 +21,7 @@ export default function AnalyticsEvents({
<div className="py-10">
<Toggle heading="Events" />
<div className="mx-auto grid max-w-screen-xl gap-5 px-2.5 lg:px-20">
+ <EventsTabs />
<EventsTable />
</div>
</div>
diff --git a/packages/tinybird/pipes/v1_events.pipe b/packages/tinybird/pipes/v1_events.pipe
new file mode 100644
index 0000000000..9b961b72c1
--- /dev/null
+++ b/packages/tinybird/pipes/v1_events.pipe
@@ -0,0 +1,131 @@
+DESCRIPTION >
+ Top countries
+
+
+TOKEN "v1_events_endpoint_read_2795" READ
+
+NODE workspace_links
+SQL >
+
+ %
+ SELECT link_id, domain, key
+ from dub_links_metadata_latest FINAL
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'ws_cl7pj5kq4006835rbjlt2ofka',
+ description="The ID of the workspace",
+ required=True,
+ )
+ }}
+ AND deleted == 0
+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
+ {% if defined(tagId) %} AND has(tag_ids, {{ tagId }}) {% end %}
+ {% if defined(root) %}
+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}
+ {% end %}
+
+
+
+NODE click_events
+SQL >
+
+ %
+ SELECT *, wl.domain, wl.key
+ FROM
+ dub_click_events_mv dce
+ {% if not defined(linkId) and defined(workspaceId) %}
+ PREWHERE link_id in (SELECT link_id from workspace_links)
+ {% end %}
+ JOIN workspace_links wl ON dce.link_id = wl.link_id
+ WHERE
+ true
+ {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}
+ {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}
+ {% if defined(country) %} AND country = {{ country }} {% end %}
+ {% if defined(city) %} AND city = {{ city }} {% end %}
+ {% if defined(device) %} AND device = {{ device }} {% end %}
+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND os = {{ os }} {% end %}
+ {% if defined(referer) %} AND referer = {{ referer }} {% end %}
+ {% if defined(url) %} AND url = {{ url }} {% end %}
+ AND timestamp >= {{ DateTime(start, '2024-06-01 00:00:00') }}
+ AND timestamp < {{ DateTime(end, '2024-06-04 00:00:00') }}
+ ORDER BY timestamp {% if order == 'asc' %} ASC {% else %} DESC {% end %}
+ LIMIT {{ Int32(limit, 50) }}
+ {% if defined(offset) %} OFFSET {{ Int32(offset, 0) }} {% end %}
+
+
+
+NODE lead_events
+SQL >
+
+ %
+ SELECT *
+ FROM
+ dub_lead_events_mv
+ {% if not defined(linkId) and defined(workspaceId) %}
+ PREWHERE link_id in (SELECT link_id from workspace_links)
+ {% end %}
+ WHERE
+ true
+ {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}
+ {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}
+ {% if defined(country) %} AND country = {{ country }} {% end %}
+ {% if defined(city) %} AND city = {{ city }} {% end %}
+ {% if defined(device) %} AND device = {{ device }} {% end %}
+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND os = {{ os }} {% end %}
+ {% if defined(referer) %} AND referer = {{ referer }} {% end %}
+ {% if defined(url) %} AND url = {{ url }} {% end %}
+ AND timestamp >= {{ DateTime64(start, '2024-06-04 00:00:00.000') }}
+ {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
+ ORDER BY timestamp {% if order == 'asc' %} ASC {% else %} DESC {% end %}
+ LIMIT {{ Int32(limit, 50) }}
+ {% if defined(offset) %} OFFSET {{ Int32(offset, 0) }} {% end %}
+
+
+
+NODE sale_events
+SQL >
+
+ %
+ SELECT *
+ FROM
+ dub_sale_events_mv
+ {% if not defined(linkId) and defined(workspaceId) %}
+ PREWHERE link_id in (SELECT link_id from workspace_links)
+ {% end %}
+ WHERE
+ true
+ {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}
+ {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}
+ {% if defined(country) %} AND country = {{ country }} {% end %}
+ {% if defined(city) %} AND city = {{ city }} {% end %}
+ {% if defined(device) %} AND device = {{ device }} {% end %}
+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND os = {{ os }} {% end %}
+ {% if defined(referer) %} AND referer = {{ referer }} {% end %}
+ {% if defined(url) %} AND url = {{ url }} {% end %}
+ AND timestamp >= {{ DateTime64(start, '2024-06-04 00:00:00.000') }}
+ {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
+ ORDER BY timestamp {% if order == 'asc' %} ASC {% else %} DESC {% end %}
+ LIMIT {{ Int32(limit, 50) }}
+ {% if defined(offset) %} OFFSET {{ Int32(offset, 0) }} {% end %}
+
+
+
+NODE endpoint
+SQL >
+
+ %
+ SELECT *
+ FROM
+ {% if eventType == 'leads' %} lead_events
+ {% elif eventType == 'sales' %} sale_events
+ {% else %} click_events
+ {% end %}
+
+
diff --git a/packages/ui/src/icons/sort-order.tsx b/packages/ui/src/icons/sort-order.tsx
index 6c7c21b6f2..ca03d6d4d0 100644
--- a/packages/ui/src/icons/sort-order.tsx
+++ b/packages/ui/src/icons/sort-order.tsx
@@ -19,7 +19,7 @@ export default function SortOrder({
xmlns="http://www.w3.org/2000/svg"
>
<motion.path
- animate={{ d: order === "asc" ? upPath : downPath }}
+ animate={{ d: order === "asc" ? downPath : upPath }}
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
@@ -27,7 +27,7 @@ export default function SortOrder({
/>
<motion.path
className="opacity-40"
- animate={{ d: order === "asc" ? downPath : upPath }}
+ animate={{ d: order === "asc" ? upPath : downPath }}
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"