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 generic method to fetch analytics

Kiran K22b07a749dcf
diff --git a/apps/web/app/api/analytics/admin/clicks/[endpoint]/route.ts b/apps/web/app/api/analytics/admin/clicks/[endpoint]/route.ts
index 8ed4fd1fb9..3c2c775b05 100644
--- a/apps/web/app/api/analytics/admin/clicks/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/admin/clicks/[endpoint]/route.ts
@@ -1,7 +1,9 @@
import { getClicks } from "@/lib/analytics/get-clicks";
import { withAdmin } from "@/lib/auth";
-import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
-import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
+import {
+ analyticsEndpointSchema,
+ analyticsQuerySchema,
+} from "@/lib/zod/schemas/analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/admin/clicks/[endpoint] – get click analytics for admin
diff --git a/apps/web/app/api/analytics/clicks/[endpoint]/route.ts b/apps/web/app/api/analytics/clicks/[endpoint]/route.ts
index c55ec505f5..4d6d4ef7d3 100644
--- a/apps/web/app/api/analytics/clicks/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/clicks/[endpoint]/route.ts
@@ -2,8 +2,10 @@ import { getClicks } from "@/lib/analytics/get-clicks";
import { validDateRangeForPlan } from "@/lib/analytics/utils";
import { withWorkspace } from "@/lib/auth";
import { getDomainViaEdge } from "@/lib/planetscale";
-import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
-import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
+import {
+ analyticsEndpointSchema,
+ analyticsQuerySchema,
+} from "@/lib/zod/schemas/analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/clicks/[endpoint] – get click analytics
diff --git a/apps/web/app/api/analytics/demo/clicks/[endpoint]/route.ts b/apps/web/app/api/analytics/demo/clicks/[endpoint]/route.ts
index 0ea7ebe52e..54da773a47 100644
--- a/apps/web/app/api/analytics/demo/clicks/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/demo/clicks/[endpoint]/route.ts
@@ -1,7 +1,9 @@
import { getClicks } from "@/lib/analytics/get-clicks";
import { withAdmin } from "@/lib/auth";
-import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
-import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
+import {
+ analyticsEndpointSchema,
+ analyticsQuerySchema,
+} from "@/lib/zod/schemas/analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/demo/clicks/[endpoint] – get click analytics for admin
diff --git a/apps/web/app/api/analytics/demo/leads/[endpoint]/route.ts b/apps/web/app/api/analytics/demo/leads/[endpoint]/route.ts
index b460c93976..8bc3b854a9 100644
--- a/apps/web/app/api/analytics/demo/leads/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/demo/leads/[endpoint]/route.ts
@@ -1,7 +1,9 @@
import { getLeads } from "@/lib/analytics/get-leads";
import { withAdmin } from "@/lib/auth";
-import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
-import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
+import {
+ analyticsEndpointSchema,
+ analyticsQuerySchema,
+} from "@/lib/zod/schemas/analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/demo/leads/[endpoint] – get click analytics for admin
diff --git a/apps/web/app/api/analytics/demo/sales/[endpoint]/route.ts b/apps/web/app/api/analytics/demo/sales/[endpoint]/route.ts
index 24b267f679..d5ee802f7c 100644
--- a/apps/web/app/api/analytics/demo/sales/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/demo/sales/[endpoint]/route.ts
@@ -1,7 +1,9 @@
import { getSales } from "@/lib/analytics/get-sales";
import { withAdmin } from "@/lib/auth";
-import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
-import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
+import {
+ analyticsEndpointSchema,
+ analyticsQuerySchema,
+} from "@/lib/zod/schemas/analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/demo/sales/[endpoint] – get click analytics for admin
@@ -9,7 +11,7 @@ export const GET = withAdmin(async ({ params, searchParams }) => {
const { endpoint } = analyticsEndpointSchema.parse(params);
const parsedParams = analyticsQuerySchema.parse(searchParams);
- const response = await getSales({
+ const response = getSales({
...parsedParams,
endpoint,
isDemo: true,
diff --git a/apps/web/app/api/analytics/edge/clicks/[endpoint]/route.ts b/apps/web/app/api/analytics/edge/clicks/[endpoint]/route.ts
index 98840dc5ea..db581594ba 100644
--- a/apps/web/app/api/analytics/edge/clicks/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/edge/clicks/[endpoint]/route.ts
@@ -7,8 +7,10 @@ import {
} from "@/lib/api/errors";
import { getDomainOrLink, getWorkspaceViaEdge } from "@/lib/planetscale";
import { ratelimit } from "@/lib/upstash";
-import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
-import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
+import {
+ analyticsEndpointSchema,
+ analyticsQuerySchema,
+} from "@/lib/zod/schemas/analytics";
import { DUB_DEMO_LINKS, DUB_WORKSPACE_ID, getSearchParams } from "@dub/utils";
import { ipAddress } from "@vercel/edge";
import { NextResponse, type NextRequest } from "next/server";
diff --git a/apps/web/app/api/analytics/leads/[endpoint]/route.ts b/apps/web/app/api/analytics/leads/[endpoint]/route.ts
index fe55d556ce..8980638cfc 100644
--- a/apps/web/app/api/analytics/leads/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/leads/[endpoint]/route.ts
@@ -2,8 +2,10 @@ import { getLeads } from "@/lib/analytics/get-leads";
import { validDateRangeForPlan } from "@/lib/analytics/utils";
import { withWorkspace } from "@/lib/auth";
import { getDomainViaEdge } from "@/lib/planetscale";
-import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
-import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
+import {
+ analyticsEndpointSchema,
+ analyticsQuerySchema,
+} from "@/lib/zod/schemas/analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/leads/[endpoint] – get leads analytics
@@ -28,7 +30,7 @@ export const GET = withWorkspace(
? await getDomainViaEdge(domain).then((d) => d?.id)
: null;
- const response = await getLeads({
+ const response = getLeads({
...parsedParams,
endpoint,
...(linkId && { linkId }),
diff --git a/apps/web/app/api/analytics/sales/[endpoint]/route.ts b/apps/web/app/api/analytics/sales/[endpoint]/route.ts
index 3d6e1872bc..38fb467c11 100644
--- a/apps/web/app/api/analytics/sales/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/sales/[endpoint]/route.ts
@@ -2,8 +2,10 @@ import { getSales } from "@/lib/analytics/get-sales";
import { validDateRangeForPlan } from "@/lib/analytics/utils";
import { withWorkspace } from "@/lib/auth";
import { getDomainViaEdge } from "@/lib/planetscale";
-import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
-import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
+import {
+ analyticsEndpointSchema,
+ analyticsQuerySchema,
+} from "@/lib/zod/schemas/analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/sales/[endpoint] – get sales analytics
diff --git a/apps/web/lib/analytics/get-analytics.ts b/apps/web/lib/analytics/get-analytics.ts
new file mode 100644
index 0000000000..08d3fded4e
--- /dev/null
+++ b/apps/web/lib/analytics/get-analytics.ts
@@ -0,0 +1,86 @@
+import { tb } from "@/lib/tinybird";
+import z from "@/lib/zod";
+import { getDaysDifference } from "@dub/utils";
+import { DubApiError } from "../api/errors";
+import { tbDemo } from "../tinybird/demo-client";
+import {
+ analyticsFilterTB,
+ analyticsQuerySchema,
+} from "../zod/schemas/analytics";
+import { getClickAnalyticsResponse } from "../zod/schemas/clicks-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";
+
+export type AnalyticsFilters = z.infer<typeof analyticsQuerySchema> & {
+ endpoint?: AnalyticsEndpoints;
+ workspaceId?: string;
+ isDemo?: boolean;
+};
+
+const responseSchema = {
+ clicks: getClickAnalyticsResponse,
+ leads: leadAnalyticsResponse,
+ sales: saleAnalyticsResponse,
+};
+
+// Fetch data for `/api/analytics/(clicks|leads|sales)/[endpoint]`
+export const getAnalytics = async (
+ analyticsType: AnalyticsEventType,
+ filters: AnalyticsFilters,
+) => {
+ let { workspaceId, endpoint, interval, start, end, isDemo } = filters;
+ let granularity: "minute" | "hour" | "day" | "month" = "day";
+
+ if (start) {
+ start = new Date(start);
+ end = end ? new Date(end) : new Date(Date.now());
+
+ const daysDifference = getDaysDifference(start, end);
+
+ if (daysDifference <= 2) {
+ granularity = "hour";
+ } else if (daysDifference > 180) {
+ granularity = "month";
+ }
+
+ // Swap start and end if start is greater than end
+ if (start > end) {
+ [start, end] = [end, start];
+ }
+ } else {
+ interval = interval ?? "24h";
+ start = INTERVAL_DATA[interval].startDate;
+ end = new Date(Date.now());
+ granularity = INTERVAL_DATA[interval].granularity;
+ }
+
+ if (!endpoint) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "An endpoint is required",
+ });
+ }
+
+ // Create a Tinybird pipe
+ const pipe = (isDemo ? tbDemo : tb).buildPipe({
+ pipe: `${analyticsType}_${endpoint}`,
+ parameters: analyticsFilterTB,
+ data: responseSchema[analyticsType][endpoint],
+ });
+
+ const response = await pipe({
+ ...filters,
+ workspaceId,
+ start: start.toISOString().replace("T", " ").replace("Z", ""),
+ end: end.toISOString().replace("T", " ").replace("Z", ""),
+ granularity,
+ });
+
+ // for total clicks|leads|sales, we return just the value;
+ // everything else we return an array of values
+ return endpoint === "count" ? response.data[0][analyticsType] : response.data;
+};
diff --git a/apps/web/lib/analytics/get-clicks.ts b/apps/web/lib/analytics/get-clicks.ts
index b82fb60601..aa0ea9647c 100644
--- a/apps/web/lib/analytics/get-clicks.ts
+++ b/apps/web/lib/analytics/get-clicks.ts
@@ -1,25 +1,9 @@
import { conn } from "@/lib/planetscale";
-import { tb } from "@/lib/tinybird";
-import z from "@/lib/zod";
-import { getDaysDifference } from "@dub/utils";
import { headers } from "next/headers";
-import { tbDemo } from "../tinybird/demo-client";
-import { analyticsQuerySchema } from "../zod/schemas/analytics";
-import {
- getClickAnalytics,
- getClickAnalyticsResponse,
-} from "../zod/schemas/clicks-analytics";
-import { INTERVAL_DATA } from "./constants";
-import { AnalyticsEndpoints } from "./types";
+import { AnalyticsFilters, getAnalytics } from "./get-analytics";
-export const getClicks = async (
- props: z.infer<typeof analyticsQuerySchema> & {
- workspaceId?: string;
- endpoint?: AnalyticsEndpoints;
- isDemo?: boolean;
- },
-) => {
- let { workspaceId, endpoint, linkId, interval, start, end } = props;
+export const getClicks = async (filters: AnalyticsFilters) => {
+ let { endpoint, linkId, interval } = filters;
// get all-time clicks count if:
// 1. linkId is defined
@@ -49,44 +33,5 @@ export const getClicks = async (
return response.rows[0]["clicks"];
}
- const pipe = (props.isDemo ? tbDemo : tb).buildPipe({
- pipe: `clicks_${endpoint || "count"}`,
- parameters: getClickAnalytics,
- data: getClickAnalyticsResponse[endpoint || "count"],
- });
-
- let granularity: "minute" | "hour" | "day" | "month" = "day";
-
- if (start) {
- start = new Date(start!);
- end = end ? new Date(end) : new Date(Date.now());
-
- const daysDifference = getDaysDifference(start, end);
- if (daysDifference <= 2) granularity = "hour";
- else if (daysDifference > 180) granularity = "month";
-
- // swap start and end if start is greater than end
- if (start > end) {
- [start, end] = [end, start];
- }
- } else {
- interval = interval ?? "24h";
- start = INTERVAL_DATA[interval].startDate;
- end = new Date(Date.now());
- granularity = INTERVAL_DATA[interval].granularity;
- }
-
- const res = await pipe(
- getClickAnalytics.parse({
- ...props,
- workspaceId,
- start: start.toISOString().replace("T", " ").replace("Z", ""),
- end: end.toISOString().replace("T", " ").replace("Z", ""),
- granularity,
- }),
- );
-
- // for total clicks, we return just the value;
- // everything else we return an array of values
- return endpoint === "count" ? res.data[0].clicks : res.data;
+ return getAnalytics("clicks", filters);
};
diff --git a/apps/web/lib/analytics/get-leads.ts b/apps/web/lib/analytics/get-leads.ts
index 86b0848476..25b048dcaa 100644
--- a/apps/web/lib/analytics/get-leads.ts
+++ b/apps/web/lib/analytics/get-leads.ts
@@ -1,86 +1,5 @@
-import { tb } from "@/lib/tinybird";
-import z from "@/lib/zod";
-import { getDaysDifference } from "@dub/utils";
-import { tbDemo } from "../tinybird/demo-client";
-import { analyticsQuerySchema } from "../zod/schemas/analytics";
-import { getClickAnalytics } from "../zod/schemas/clicks-analytics";
-import { leadAnalyticsResponse } from "../zod/schemas/leads-analytics";
-import { INTERVAL_DATA } from "./constants";
-import { AnalyticsEndpoints } from "./types";
+import { AnalyticsFilters, getAnalytics } from "./get-analytics";
-export const getLeads = async (
- props: z.infer<typeof analyticsQuerySchema> & {
- workspaceId?: string;
- endpoint: AnalyticsEndpoints;
- isDemo?: boolean;
- },
-) => {
- let { workspaceId, endpoint, interval, start, end } = props;
-
- // get all-time clicks count if:
- // 1. linkId is defined
- // 2. endpoint is not defined
- // 3. interval is all time
- // 4. call is made from dashboard
- // if (
- // linkId &&
- // endpoint === "count" &&
- // interval === "all" &&
- // headers()?.get("Request-Source") === "app.dub.co"
- // ) {
- // let response = await conn.execute(
- // "SELECT clicks FROM Link WHERE `id` = ?",
- // [linkId],
- // );
-
- // if (response.rows.length === 0) {
- // response = await conn.execute(
- // "SELECT clicks FROM Domain WHERE `id` = ?",
- // [linkId],
- // );
- // if (response.rows.length === 0) {
- // return 0;
- // }
- // }
- // return response.rows[0]["clicks"];
- // }
-
- const pipe = (props.isDemo ? tbDemo : tb).buildPipe({
- pipe: `leads_${endpoint}`,
- parameters: getClickAnalytics,
- data: leadAnalyticsResponse[endpoint],
- });
-
- let granularity: "minute" | "hour" | "day" | "month" = "day";
-
- if (start) {
- start = new Date(start!);
- end = end ? new Date(end) : new Date(Date.now());
-
- const daysDifference = getDaysDifference(start, end);
- if (daysDifference <= 2) granularity = "hour";
- else if (daysDifference > 180) granularity = "month";
-
- // swap start and end if start is greater than end
- if (start > end) {
- [start, end] = [end, start];
- }
- } else {
- interval = interval ?? "24h";
- start = INTERVAL_DATA[interval].startDate;
- end = new Date(Date.now());
- granularity = INTERVAL_DATA[interval].granularity;
- }
-
- const response = await pipe({
- ...props,
- workspaceId,
- start: start.toISOString().replace("T", " ").replace("Z", ""),
- end: end.toISOString().replace("T", " ").replace("Z", ""),
- granularity,
- });
-
- // for total leads, we return just the value;
- // everything else we return an array of values
- return endpoint === "count" ? response.data[0].leads : response.data;
+export const getLeads = (filters: AnalyticsFilters) => {
+ return getAnalytics("leads", filters);
};
diff --git a/apps/web/lib/analytics/get-sales.ts b/apps/web/lib/analytics/get-sales.ts
index 2545f2254a..3ebf56ed8f 100644
--- a/apps/web/lib/analytics/get-sales.ts
+++ b/apps/web/lib/analytics/get-sales.ts
@@ -1,86 +1,5 @@
-import { tb } from "@/lib/tinybird";
-import z from "@/lib/zod";
-import { getDaysDifference } from "@dub/utils";
-import { tbDemo } from "../tinybird/demo-client";
-import { analyticsQuerySchema } from "../zod/schemas/analytics";
-import { getClickAnalytics } from "../zod/schemas/clicks-analytics";
-import { saleAnalyticsResponse } from "../zod/schemas/sales-analytics";
-import { INTERVAL_DATA } from "./constants";
-import { AnalyticsEndpoints } from "./types";
+import { AnalyticsFilters, getAnalytics } from "./get-analytics";
-export const getSales = async (
- props: z.infer<typeof analyticsQuerySchema> & {
- workspaceId?: string;
- endpoint: AnalyticsEndpoints;
- isDemo?: boolean;
- },
-) => {
- let { workspaceId, endpoint, interval, start, end } = props;
-
- // get all-time clicks count if:
- // 1. linkId is defined
- // 2. endpoint is not defined
- // 3. interval is all time
- // 4. call is made from dashboard
- // if (
- // linkId &&
- // endpoint === "count" &&
- // interval === "all" &&
- // headers()?.get("Request-Source") === "app.dub.co"
- // ) {
- // let response = await conn.execute(
- // "SELECT clicks FROM Link WHERE `id` = ?",
- // [linkId],
- // );
-
- // if (response.rows.length === 0) {
- // response = await conn.execute(
- // "SELECT clicks FROM Domain WHERE `id` = ?",
- // [linkId],
- // );
- // if (response.rows.length === 0) {
- // return 0;
- // }
- // }
- // return response.rows[0]["clicks"];
- // }
-
- const pipe = (props.isDemo ? tbDemo : tb).buildPipe({
- pipe: `sales_${endpoint}`,
- parameters: getClickAnalytics,
- data: saleAnalyticsResponse[endpoint],
- });
-
- let granularity: "minute" | "hour" | "day" | "month" = "day";
-
- if (start) {
- start = new Date(start!);
- end = end ? new Date(end) : new Date(Date.now());
-
- const daysDifference = getDaysDifference(start, end);
- if (daysDifference <= 2) granularity = "hour";
- else if (daysDifference > 180) granularity = "month";
-
- // swap start and end if start is greater than end
- if (start > end) {
- [start, end] = [end, start];
- }
- } else {
- interval = interval ?? "24h";
- start = INTERVAL_DATA[interval].startDate;
- end = new Date(Date.now());
- granularity = INTERVAL_DATA[interval].granularity;
- }
-
- const response = await pipe({
- ...props,
- workspaceId,
- start: start.toISOString().replace("T", " ").replace("Z", ""),
- end: end.toISOString().replace("T", " ").replace("Z", ""),
- granularity,
- });
-
- // for total sales, we return just the value;
- // everything else we return an array of values
- return endpoint === "count" ? response.data[0].sales : response.data;
+export const getSales = (filters: AnalyticsFilters) => {
+ return getAnalytics("sales", filters);
};
diff --git a/apps/web/lib/api/errors.ts b/apps/web/lib/api/errors.ts
index 9b37dc4677..750e6c9985 100644
--- a/apps/web/lib/api/errors.ts
+++ b/apps/web/lib/api/errors.ts
@@ -118,8 +118,7 @@ export function fromZodError(error: ZodError): ErrorResponse {
}
export function handleApiError(error: any): ErrorResponse & { status: number } {
- console.error("API error occurred", error);
- // console.error("API error occurred", JSON.stringify(error, null, 2));
+ //console.error("API error occurred", error);
// Zod errors
if (error instanceof ZodError) {
diff --git a/apps/web/lib/edge-config/is-beta-tester.ts b/apps/web/lib/edge-config/is-beta-tester.ts
index 6b07e6c4c4..2481cb1ebe 100644
--- a/apps/web/lib/edge-config/is-beta-tester.ts
+++ b/apps/web/lib/edge-config/is-beta-tester.ts
@@ -1,6 +1,7 @@
import { get } from "@vercel/edge-config";
export const isBetaTester = async (workspaceId: string) => {
+ // return true
workspaceId = workspaceId.startsWith("ws_")
? workspaceId
: `ws_${workspaceId}`;
diff --git a/apps/web/lib/swr/use-workspace.ts b/apps/web/lib/swr/use-workspace.ts
index cb8dcb4216..cbc067c71a 100644
--- a/apps/web/lib/swr/use-workspace.ts
+++ b/apps/web/lib/swr/use-workspace.ts
@@ -30,5 +30,6 @@ export default function useWorkspace() {
error,
mutate,
loading: slug && !workspace && !error ? true : false,
+ betaTester: true,
};
}
diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.ts
index 25bae0a013..e4c255beab 100644
--- a/apps/web/lib/zod/schemas/analytics.ts
+++ b/apps/web/lib/zod/schemas/analytics.ts
@@ -1,9 +1,29 @@
-import { intervals } from "@/lib/analytics/constants";
+import {
+ VALID_ANALYTICS_ENDPOINTS,
+ intervals,
+} from "@/lib/analytics/constants";
+import { formatAnalyticsEndpoint } from "@/lib/analytics/utils";
import z from "@/lib/zod";
import { COUNTRY_CODES } from "@dub/utils";
import { booleanQuerySchema } from "./misc";
import { parseDateSchema } from "./utils";
+export const analyticsEndpointSchema = z.object({
+ endpoint: z
+ .enum(VALID_ANALYTICS_ENDPOINTS, {
+ errorMap: (_issue, _ctx) => {
+ return {
+ message: `Invalid endpoint value. Valid endpoints are: ${VALID_ANALYTICS_ENDPOINTS.join(", ")}`,
+ };
+ },
+ })
+ .transform((v) => formatAnalyticsEndpoint(v, "plural"))
+ .optional()
+ .describe(
+ "The field to group the analytics by. If undefined, returns the total click count.",
+ ),
+});
+
// Query schema for `/api/analytics/(clicks|leads|sales)/[endpoint]`
export const analyticsQuerySchema = z.object({
domain: z.string().optional().describe("The domain to filter analytics for."),
@@ -85,3 +105,37 @@ export const analyticsQuerySchema = z.object({
"Filter for root domains. If true, filter for domains only. If false, filter for links only. If undefined, return both.",
),
});
+
+// Analytics filter params for Tinybird endpoints
+export const analyticsFilterTB = z
+ .object({
+ workspaceId: z
+ .string()
+ .optional()
+ .transform((v) => {
+ if (v && !v.startsWith("ws_")) {
+ return `ws_${v}`;
+ } else {
+ return v;
+ }
+ }),
+ root: z.boolean().optional(),
+ qr: z.boolean().optional(),
+ start: z.string(),
+ end: z.string(),
+ granularity: z.enum(["minute", "hour", "day", "month"]).optional(),
+ })
+ .merge(
+ analyticsQuerySchema.pick({
+ browser: true,
+ city: true,
+ country: true,
+ device: true,
+ domain: true,
+ linkId: true,
+ os: true,
+ referer: true,
+ tagId: true,
+ url: true,
+ }),
+ );
diff --git a/apps/web/lib/zod/schemas/clicks-analytics.ts b/apps/web/lib/zod/schemas/clicks-analytics.ts
index 49633eace5..d23935a353 100644
--- a/apps/web/lib/zod/schemas/clicks-analytics.ts
+++ b/apps/web/lib/zod/schemas/clicks-analytics.ts
@@ -1,53 +1,5 @@
-import { VALID_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
-import { formatAnalyticsEndpoint } from "@/lib/analytics/utils";
import z from "@/lib/zod";
import { COUNTRY_CODES } from "@dub/utils";
-import { analyticsQuerySchema } from "./analytics";
-
-export const analyticsEndpointSchema = z.object({
- endpoint: z
- .enum(VALID_ANALYTICS_ENDPOINTS, {
- errorMap: (_issue, _ctx) => {
- return {
- message: `Invalid endpoint value. Valid endpoints are: ${VALID_ANALYTICS_ENDPOINTS.join(", ")}`,
- };
- },
- })
- .transform((v) => formatAnalyticsEndpoint(v, "plural"))
- .optional()
- .describe(
- "The field to group the analytics by. If undefined, returns the total click count.",
- ),
-});
-
-export const getClickAnalytics = analyticsQuerySchema
- .omit({
- groupBy: true,
- interval: true,
- externalId: true,
- key: true,
- start: true,
- end: true,
- root: true,
- qr: true,
- })
- .extend({
- workspaceId: z
- .string()
- .optional()
- .transform((v) => {
- if (v && !v.startsWith("ws_")) {
- return `ws_${v}`;
- } else {
- return v;
- }
- }),
- root: z.boolean().optional(),
- qr: z.boolean().optional(),
- start: z.string(),
- end: z.string(),
- granularity: z.enum(["minute", "hour", "day", "month"]).optional(),
- });
// Analytics response schemas
export const getClickAnalyticsResponse = {
diff --git a/apps/web/ui/analytics/main.tsx b/apps/web/ui/analytics/main.tsx
index 4a5afc23e3..340400e70b 100644
--- a/apps/web/ui/analytics/main.tsx
+++ b/apps/web/ui/analytics/main.tsx
@@ -45,6 +45,7 @@ export default function Main() {
// Root domain related
const root = searchParams.get("root");
+ console.log({ betaTester });
const tabs = useMemo(
() =>