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.

add composite API

Steven Tey1451e0c067d8
diff --git a/apps/web/app/api/analytics/demo/composite/[endpoint]/route.ts b/apps/web/app/api/analytics/demo/composite/[endpoint]/route.ts
new file mode 100644
index 0000000000..ac8a6bbab1
--- /dev/null
+++ b/apps/web/app/api/analytics/demo/composite/[endpoint]/route.ts
@@ -0,0 +1,21 @@
+import { getAnalytics } from "@/lib/analytics/get-analytics";
+import { withSession } from "@/lib/auth";
+import {
+ analyticsEndpointSchema,
+ analyticsQuerySchema,
+} from "@/lib/zod/schemas/analytics";
+import { NextResponse } from "next/server";
+
+// GET /api/analytics/demo/composite/[endpoint]
+export const GET = withSession(async ({ params, searchParams }) => {
+ const { endpoint } = analyticsEndpointSchema.parse(params);
+ const parsedParams = analyticsQuerySchema.parse(searchParams);
+
+ const response = await getAnalytics("composite", {
+ ...parsedParams,
+ endpoint,
+ isDemo: true,
+ });
+
+ return NextResponse.json(response);
+});
diff --git a/apps/web/lib/analytics/get-analytics.ts b/apps/web/lib/analytics/get-analytics.ts
index bd2233314a..ba6725289f 100644
--- a/apps/web/lib/analytics/get-analytics.ts
+++ b/apps/web/lib/analytics/get-analytics.ts
@@ -7,12 +7,13 @@ import {
analyticsQuerySchema,
} from "../zod/schemas/analytics";
import { clickAnalyticsResponse } from "../zod/schemas/clicks-analytics";
+import { compositeAnalyticsResponse } from "../zod/schemas/composite-analytics";
import { leadAnalyticsResponse } from "../zod/schemas/leads-analytics";
import { saleAnalyticsResponse } from "../zod/schemas/sales-analytics";
import { INTERVAL_DATA } from "./constants";
import { AnalyticsEndpoints } from "./types";
-type AnalyticsEventType = "clicks" | "leads" | "sales";
+type AnalyticsEventType = "clicks" | "leads" | "sales" | "composite";
export type AnalyticsFilters = z.infer<typeof analyticsQuerySchema> & {
endpoint?: AnalyticsEndpoints;
@@ -24,6 +25,7 @@ const responseSchema = {
clicks: clickAnalyticsResponse,
leads: leadAnalyticsResponse,
sales: saleAnalyticsResponse,
+ composite: compositeAnalyticsResponse,
};
// Fetch data for `/api/analytics/(clicks|leads|sales)/[endpoint]`
@@ -66,13 +68,14 @@ export const getAnalytics = async (
// Create a Tinybird pipe
const pipe = (isDemo ? tbDemo : tb).buildPipe({
- pipe: `${analyticsType}_${endpoint}`,
+ pipe: endpoint,
parameters: analyticsFilterTB,
data: responseSchema[analyticsType][endpoint],
});
const response = await pipe({
...params,
+ eventType: analyticsType,
workspaceId,
start: start.toISOString().replace("T", " ").replace("Z", ""),
end: end.toISOString().replace("T", " ").replace("Z", ""),
@@ -80,7 +83,7 @@ export const getAnalytics = async (
});
// for total clicks|leads, we return just the value;
- // everything else we return an array of values
+ // everything else we return the full response
if (endpoint === "count") {
if (["clicks", "leads"].includes(analyticsType)) {
return response.data[0][analyticsType];
diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.ts
index e4c255beab..c651b48352 100644
--- a/apps/web/lib/zod/schemas/analytics.ts
+++ b/apps/web/lib/zod/schemas/analytics.ts
@@ -109,6 +109,19 @@ export const analyticsQuerySchema = z.object({
// Analytics filter params for Tinybird endpoints
export const analyticsFilterTB = z
.object({
+ eventType: z
+ .enum(["clicks", "leads", "sales", "composite"], {
+ errorMap: (_issue, _ctx) => {
+ return {
+ message:
+ "Invalid event type. Valid event types are: clicks, leads, sales",
+ };
+ },
+ })
+ .optional()
+ .describe(
+ "The type of event to retrieve analytics for. Defaults to composite.",
+ ),
workspaceId: z
.string()
.optional()
diff --git a/apps/web/lib/zod/schemas/composite-analytics.ts b/apps/web/lib/zod/schemas/composite-analytics.ts
new file mode 100644
index 0000000000..2218791905
--- /dev/null
+++ b/apps/web/lib/zod/schemas/composite-analytics.ts
@@ -0,0 +1,96 @@
+import z from "@/lib/zod";
+import { COUNTRY_CODES } from "@dub/utils";
+
+export const compositeAnalyticsResponse = {
+ count: z.object({
+ clicks: z.number().describe("The total number of clicks"),
+ leads: z.number().describe("The total number of leads"),
+ sales: z.number().describe("The total number of sales"),
+ amount: z.number().describe("The total amount of sales"),
+ }),
+
+ timeseries: z.object({
+ start: z.string().describe("The starting timestamp of the interval"),
+ clicks: z.number().describe("The number of clicks in the interval"),
+ leads: z.number().describe("The number of leads in the interval"),
+ sales: z.number().describe("The number of sales in the interval"),
+ amount: z.number().describe("The total amount of sales in the interval"),
+ }),
+
+ countries: z
+ .object({
+ country: z
+ .enum(COUNTRY_CODES)
+ .describe("The 2-letter country code: https://d.to/geo"),
+ clicks: z.number().describe("The number of clicks from this country"),
+ leads: z.number().describe("The number of leads from this country"),
+ sales: z.number().describe("The number of sales from this country"),
+ amount: z
+ .number()
+ .describe("The total amount of sales from this country"),
+ })
+ .openapi({ ref: "salesByCountry" }),
+
+ cities: z
+ .object({
+ city: z.string().describe("The name of the city"),
+ country: z
+ .enum(COUNTRY_CODES)
+ .describe("The 2-letter country code of the city: https://d.to/geo"),
+ clicks: z.number().describe("The number of clicks from this city"),
+ leads: z.number().describe("The number of leads from this city"),
+ sales: z.number().describe("The number of sales from this city"),
+ amount: z.number().describe("The total amount of sales from this city"),
+ })
+ .openapi({ ref: "salesByCities" }),
+
+ devices: z.object({
+ device: z.string().describe("The name of the device"),
+ clicks: z.number().describe("The number of clicks from this device"),
+ leads: z.number().describe("The number of leads from this device"),
+ sales: z.number().describe("The number of sales from this device"),
+ amount: z.number().describe("The total amount of sales from this device"),
+ }),
+
+ browsers: z.object({
+ browser: z.string().describe("The name of the browser"),
+ clicks: z.number().describe("The number of clicks from this browser"),
+ leads: z.number().describe("The number of leads from this browser"),
+ sales: z.number().describe("The number of sales from this browser"),
+ amount: z.number().describe("The total amount of sales from this browser"),
+ }),
+
+ os: z.object({
+ os: z.string().describe("The name of the OS"),
+ clicks: z.number().describe("The number of clicks from this OS"),
+ leads: z.number().describe("The number of leads from this OS"),
+ sales: z.number().describe("The number of sales from this OS"),
+ amount: z.number().describe("The total amount of sales from this OS"),
+ }),
+
+ referers: z.object({
+ referer: z
+ .string()
+ .describe("The name of the referer. If unknown, this will be `(direct)`"),
+ clicks: z.number().describe("The number of clicks from this referer"),
+ leads: z.number().describe("The number of leads from this referer"),
+ sales: z.number().describe("The number of sales from this referer"),
+ amount: z.number().describe("The total amount of sales from this referer"),
+ }),
+
+ top_links: z.object({
+ link: z.string().describe("The unique ID of the short link"),
+ clicks: z.number().describe("The number of clicks from this link"),
+ leads: z.number().describe("The number of leads from this link"),
+ sales: z.number().describe("The number of sales from this link"),
+ amount: z.number().describe("The total amount of sales from this link"),
+ }),
+
+ top_urls: z.object({
+ url: z.string().describe("The destination URL"),
+ clicks: z.number().describe("The number of clicks from this URL"),
+ leads: z.number().describe("The number of leads from this URL"),
+ sales: z.number().describe("The number of sales from this URL"),
+ amount: z.number().describe("The total amount of sales from this URL"),
+ }),
+} as const;
diff --git a/apps/web/ui/analytics/main.tsx b/apps/web/ui/analytics/main.tsx
index 3628aad6e2..09a357f81e 100644
--- a/apps/web/ui/analytics/main.tsx
+++ b/apps/web/ui/analytics/main.tsx
@@ -83,38 +83,23 @@ export default function Main() {
}
: tabs[0];
- const { data: totalClicks } = useSWR<number>(
- `${baseApiPathGeneric}/clicks/count?${queryString}`,
- fetcher,
- {
- shouldRetryOnError: !requiresUpgrade,
- },
- );
-
- const { data: totalLeads } = useSWR<number>(
- `${baseApiPathGeneric}/leads/count?${queryString}`,
- fetcher,
- {
- shouldRetryOnError: !requiresUpgrade,
- },
- );
-
- const { data: { amount: totalSales } = {} } = useSWR<{
+ const { data } = useSWR<{
+ clicks: number;
+ leads: number;
+ sale: number;
amount: number;
- }>(`${baseApiPathGeneric}/sales/count?${queryString}`, fetcher, {
+ }>(`${baseApiPathGeneric}/composite/count?${queryString}`, fetcher, {
shouldRetryOnError: !requiresUpgrade,
});
- console.log({ totalClicks, totalLeads, totalSales });
-
return (
<div className="w-full overflow-hidden border border-gray-200 bg-white sm:rounded-xl">
<div className="scrollbar-hide mb-5 flex w-full divide-x overflow-y-hidden overflow-x-scroll border-b border-gray-200">
{tabs.map(({ id, label, colorClassName }, idx) => {
const total = {
- clicks: totalClicks,
- leads: totalLeads,
- sales: totalSales ? totalSales / 100 : undefined,
+ clicks: data?.clicks,
+ leads: data?.leads,
+ sales: data ? data.amount / 100 : undefined,
}[id];
return (