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 analyticsQuerySchema

Kiran K852ec96e9f83
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 82351dc8da..8ed4fd1fb9 100644
--- a/apps/web/app/api/analytics/admin/clicks/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/admin/clicks/[endpoint]/route.ts
@@ -1,15 +1,13 @@
import { getClicks } from "@/lib/analytics/get-clicks";
import { withAdmin } from "@/lib/auth";
-import {
- analyticsEndpointSchema,
- clickAnalyticsQuerySchema,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/admin/clicks/[endpoint] – get click analytics for admin
export const GET = withAdmin(async ({ params, searchParams }) => {
const { endpoint } = analyticsEndpointSchema.parse(params);
- const parsedParams = clickAnalyticsQuerySchema.parse(searchParams);
+ const parsedParams = analyticsQuerySchema.parse(searchParams);
const response = await getClicks({
...parsedParams,
diff --git a/apps/web/app/api/analytics/clicks/[endpoint]/route.ts b/apps/web/app/api/analytics/clicks/[endpoint]/route.ts
index 9c76214650..c55ec505f5 100644
--- a/apps/web/app/api/analytics/clicks/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/clicks/[endpoint]/route.ts
@@ -2,10 +2,8 @@ import { getClicks } from "@/lib/analytics/get-clicks";
import { validDateRangeForPlan } from "@/lib/analytics/utils";
import { withWorkspace } from "@/lib/auth";
import { getDomainViaEdge } from "@/lib/planetscale";
-import {
- analyticsEndpointSchema,
- clickAnalyticsQuerySchema,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/clicks/[endpoint] – get click analytics
@@ -13,7 +11,7 @@ export const GET = withWorkspace(
async ({ params, searchParams, workspace, link }) => {
const { endpoint = "count" } = analyticsEndpointSchema.parse(params);
- const parsedParams = clickAnalyticsQuerySchema.parse(searchParams);
+ const parsedParams = analyticsQuerySchema.parse(searchParams);
const { domain, key, interval, start, end } = parsedParams;
validDateRangeForPlan({
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 37410c8ec6..0ea7ebe52e 100644
--- a/apps/web/app/api/analytics/demo/clicks/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/demo/clicks/[endpoint]/route.ts
@@ -1,15 +1,13 @@
import { getClicks } from "@/lib/analytics/get-clicks";
import { withAdmin } from "@/lib/auth";
-import {
- analyticsEndpointSchema,
- clickAnalyticsQuerySchema,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/demo/clicks/[endpoint] – get click analytics for admin
export const GET = withAdmin(async ({ params, searchParams }) => {
const { endpoint } = analyticsEndpointSchema.parse(params);
- const parsedParams = clickAnalyticsQuerySchema.parse(searchParams);
+ const parsedParams = analyticsQuerySchema.parse(searchParams);
const response = await getClicks({
...parsedParams,
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 6446daaa07..b460c93976 100644
--- a/apps/web/app/api/analytics/demo/leads/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/demo/leads/[endpoint]/route.ts
@@ -1,15 +1,13 @@
import { getLeads } from "@/lib/analytics/get-leads";
import { withAdmin } from "@/lib/auth";
-import {
- analyticsEndpointSchema,
- clickAnalyticsQuerySchema,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/demo/leads/[endpoint] – get click analytics for admin
export const GET = withAdmin(async ({ params, searchParams }) => {
const { endpoint } = analyticsEndpointSchema.parse(params);
- const parsedParams = clickAnalyticsQuerySchema.parse(searchParams);
+ const parsedParams = analyticsQuerySchema.parse(searchParams);
const response = await getLeads({
...parsedParams,
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 c50ffb570c..24b267f679 100644
--- a/apps/web/app/api/analytics/demo/sales/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/demo/sales/[endpoint]/route.ts
@@ -1,15 +1,13 @@
import { getSales } from "@/lib/analytics/get-sales";
import { withAdmin } from "@/lib/auth";
-import {
- analyticsEndpointSchema,
- clickAnalyticsQuerySchema,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/demo/sales/[endpoint] – get click analytics for admin
export const GET = withAdmin(async ({ params, searchParams }) => {
const { endpoint } = analyticsEndpointSchema.parse(params);
- const parsedParams = clickAnalyticsQuerySchema.parse(searchParams);
+ const parsedParams = analyticsQuerySchema.parse(searchParams);
const response = await getSales({
...parsedParams,
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 7237e7a900..98840dc5ea 100644
--- a/apps/web/app/api/analytics/edge/clicks/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/edge/clicks/[endpoint]/route.ts
@@ -7,10 +7,8 @@ import {
} from "@/lib/api/errors";
import { getDomainOrLink, getWorkspaceViaEdge } from "@/lib/planetscale";
import { ratelimit } from "@/lib/upstash";
-import {
- analyticsEndpointSchema,
- clickAnalyticsQuerySchema,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
import { DUB_DEMO_LINKS, DUB_WORKSPACE_ID, getSearchParams } from "@dub/utils";
import { ipAddress } from "@vercel/edge";
import { NextResponse, type NextRequest } from "next/server";
@@ -25,7 +23,7 @@ export const GET = async (
const { endpoint } = analyticsEndpointSchema.parse(params);
const searchParams = getSearchParams(req.url);
- const parsedParams = clickAnalyticsQuerySchema.parse(searchParams);
+ const parsedParams = analyticsQuerySchema.parse(searchParams);
const { domain, key, interval, start, end } = parsedParams;
diff --git a/apps/web/app/api/analytics/export/route.ts b/apps/web/app/api/analytics/export/route.ts
index eee00c28b5..5ecd3eb91c 100644
--- a/apps/web/app/api/analytics/export/route.ts
+++ b/apps/web/app/api/analytics/export/route.ts
@@ -8,7 +8,7 @@ import { validDateRangeForPlan } from "@/lib/analytics/utils";
import { withWorkspace } from "@/lib/auth";
import { getDomainViaEdge } from "@/lib/planetscale";
import { prisma } from "@/lib/prisma";
-import { clickAnalyticsQuerySchema } from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { linkConstructor } from "@dub/utils";
import { json2csv } from "json-2-csv";
import JSZip from "jszip";
@@ -28,7 +28,7 @@ const convertToCSV = (data: object[]) => {
// GET /api/analytics/[endpoint]/export – get export data for analytics
export const GET = withWorkspace(
async ({ searchParams, workspace, link }) => {
- const parsedParams = clickAnalyticsQuerySchema.parse(searchParams);
+ const parsedParams = analyticsQuerySchema.parse(searchParams);
const { domain, key, interval, start, end } = parsedParams;
validDateRangeForPlan({
diff --git a/apps/web/app/api/analytics/leads/[endpoint]/route.ts b/apps/web/app/api/analytics/leads/[endpoint]/route.ts
index 39de7743a9..fe55d556ce 100644
--- a/apps/web/app/api/analytics/leads/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/leads/[endpoint]/route.ts
@@ -2,17 +2,15 @@ import { getLeads } from "@/lib/analytics/get-leads";
import { validDateRangeForPlan } from "@/lib/analytics/utils";
import { withWorkspace } from "@/lib/auth";
import { getDomainViaEdge } from "@/lib/planetscale";
-import {
- analyticsEndpointSchema,
- clickAnalyticsQuerySchema,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/leads/[endpoint] – get leads analytics
export const GET = withWorkspace(
async ({ params, searchParams, workspace, link }) => {
const { endpoint } = analyticsEndpointSchema.parse(params);
- const parsedParams = clickAnalyticsQuerySchema.parse(searchParams);
+ const parsedParams = analyticsQuerySchema.parse(searchParams);
const { domain, key, interval, start, end } = parsedParams;
diff --git a/apps/web/app/api/analytics/sales/[endpoint]/route.ts b/apps/web/app/api/analytics/sales/[endpoint]/route.ts
index 8f3d66794a..3d6e1872bc 100644
--- a/apps/web/app/api/analytics/sales/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/sales/[endpoint]/route.ts
@@ -2,17 +2,15 @@ import { getSales } from "@/lib/analytics/get-sales";
import { validDateRangeForPlan } from "@/lib/analytics/utils";
import { withWorkspace } from "@/lib/auth";
import { getDomainViaEdge } from "@/lib/planetscale";
-import {
- analyticsEndpointSchema,
- clickAnalyticsQuerySchema,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { analyticsEndpointSchema } from "@/lib/zod/schemas/clicks-analytics";
import { NextResponse } from "next/server";
// GET /api/analytics/sales/[endpoint] – get sales analytics
export const GET = withWorkspace(
async ({ params, searchParams, workspace, link }) => {
const { endpoint } = analyticsEndpointSchema.parse(params);
- const parsedParams = clickAnalyticsQuerySchema.parse(searchParams);
+ const parsedParams = analyticsQuerySchema.parse(searchParams);
const { domain, key, interval, start, end } = parsedParams;
diff --git a/apps/web/lib/analytics/get-clicks.ts b/apps/web/lib/analytics/get-clicks.ts
index 77a07f742b..b82fb60601 100644
--- a/apps/web/lib/analytics/get-clicks.ts
+++ b/apps/web/lib/analytics/get-clicks.ts
@@ -4,8 +4,8 @@ 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 {
- clickAnalyticsQuerySchema,
getClickAnalytics,
getClickAnalyticsResponse,
} from "../zod/schemas/clicks-analytics";
@@ -13,7 +13,7 @@ import { INTERVAL_DATA } from "./constants";
import { AnalyticsEndpoints } from "./types";
export const getClicks = async (
- props: z.infer<typeof clickAnalyticsQuerySchema> & {
+ props: z.infer<typeof analyticsQuerySchema> & {
workspaceId?: string;
endpoint?: AnalyticsEndpoints;
isDemo?: boolean;
diff --git a/apps/web/lib/analytics/get-leads.ts b/apps/web/lib/analytics/get-leads.ts
index 7b990a4d91..86b0848476 100644
--- a/apps/web/lib/analytics/get-leads.ts
+++ b/apps/web/lib/analytics/get-leads.ts
@@ -2,16 +2,14 @@ import { tb } from "@/lib/tinybird";
import z from "@/lib/zod";
import { getDaysDifference } from "@dub/utils";
import { tbDemo } from "../tinybird/demo-client";
-import {
- clickAnalyticsQuerySchema,
- getClickAnalytics,
-} from "../zod/schemas/clicks-analytics";
+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";
export const getLeads = async (
- props: z.infer<typeof clickAnalyticsQuerySchema> & {
+ props: z.infer<typeof analyticsQuerySchema> & {
workspaceId?: string;
endpoint: AnalyticsEndpoints;
isDemo?: boolean;
diff --git a/apps/web/lib/analytics/get-sales.ts b/apps/web/lib/analytics/get-sales.ts
index da93037f67..2545f2254a 100644
--- a/apps/web/lib/analytics/get-sales.ts
+++ b/apps/web/lib/analytics/get-sales.ts
@@ -2,16 +2,14 @@ import { tb } from "@/lib/tinybird";
import z from "@/lib/zod";
import { getDaysDifference } from "@dub/utils";
import { tbDemo } from "../tinybird/demo-client";
-import {
- clickAnalyticsQuerySchema,
- getClickAnalytics,
-} from "../zod/schemas/clicks-analytics";
+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";
export const getSales = async (
- props: z.infer<typeof clickAnalyticsQuerySchema> & {
+ props: z.infer<typeof analyticsQuerySchema> & {
workspaceId?: string;
endpoint: AnalyticsEndpoints;
isDemo?: boolean;
diff --git a/apps/web/lib/openapi/analytics/clicks/browsers.ts b/apps/web/lib/openapi/analytics/clicks/browsers.ts
index 8b39eefad4..0180fc7525 100644
--- a/apps/web/lib/openapi/analytics/clicks/browsers.ts
+++ b/apps/web/lib/openapi/analytics/clicks/browsers.ts
@@ -1,9 +1,7 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
import z from "@/lib/zod";
-import {
- clickAnalyticsQuerySchema,
- getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { getClickAnalyticsResponse } from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
@@ -14,7 +12,7 @@ export const getBrowsersByClicks: ZodOpenApiOperationObject = {
description:
"Retrieve the top browsers by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
+ query: workspaceParamsSchema.merge(analyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/clicks/cities.ts b/apps/web/lib/openapi/analytics/clicks/cities.ts
index 4c4f568589..6f213f5d48 100644
--- a/apps/web/lib/openapi/analytics/clicks/cities.ts
+++ b/apps/web/lib/openapi/analytics/clicks/cities.ts
@@ -1,9 +1,7 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
import z from "@/lib/zod";
-import {
- clickAnalyticsQuerySchema,
- getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { getClickAnalyticsResponse } from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
@@ -14,7 +12,7 @@ export const getCitiesByClicks: ZodOpenApiOperationObject = {
description:
"Retrieve the top countries by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
+ query: workspaceParamsSchema.merge(analyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/clicks/count.ts b/apps/web/lib/openapi/analytics/clicks/count.ts
index 50198dca6b..d49988f9fe 100644
--- a/apps/web/lib/openapi/analytics/clicks/count.ts
+++ b/apps/web/lib/openapi/analytics/clicks/count.ts
@@ -1,5 +1,5 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
-import { clickAnalyticsQuerySchema } from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
@@ -10,7 +10,7 @@ export const getClicksCount: ZodOpenApiOperationObject = {
description:
"Retrieve the total number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
+ query: workspaceParamsSchema.merge(analyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/clicks/countries.ts b/apps/web/lib/openapi/analytics/clicks/countries.ts
index b0d4eca2df..f0a668ad13 100644
--- a/apps/web/lib/openapi/analytics/clicks/countries.ts
+++ b/apps/web/lib/openapi/analytics/clicks/countries.ts
@@ -1,9 +1,7 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
import z from "@/lib/zod";
-import {
- clickAnalyticsQuerySchema,
- getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { getClickAnalyticsResponse } from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
@@ -14,7 +12,7 @@ export const getCountriesByClicks: ZodOpenApiOperationObject = {
description:
"Retrieve the top countries by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
+ query: workspaceParamsSchema.merge(analyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/clicks/devices.ts b/apps/web/lib/openapi/analytics/clicks/devices.ts
index 2072b71fec..a66019a0d2 100644
--- a/apps/web/lib/openapi/analytics/clicks/devices.ts
+++ b/apps/web/lib/openapi/analytics/clicks/devices.ts
@@ -1,9 +1,7 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
import z from "@/lib/zod";
-import {
- clickAnalyticsQuerySchema,
- getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { getClickAnalyticsResponse } from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
@@ -14,7 +12,7 @@ export const getDevicesByClicks: ZodOpenApiOperationObject = {
description:
"Retrieve the top devices by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
+ query: workspaceParamsSchema.merge(analyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/clicks/os.ts b/apps/web/lib/openapi/analytics/clicks/os.ts
index 641bd17829..42d0e4dc68 100644
--- a/apps/web/lib/openapi/analytics/clicks/os.ts
+++ b/apps/web/lib/openapi/analytics/clicks/os.ts
@@ -1,9 +1,7 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
import z from "@/lib/zod";
-import {
- clickAnalyticsQuerySchema,
- getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { getClickAnalyticsResponse } from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
@@ -14,7 +12,7 @@ export const getOSByClicks: ZodOpenApiOperationObject = {
description:
"Retrieve the top OS by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
+ query: workspaceParamsSchema.merge(analyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/clicks/referers.ts b/apps/web/lib/openapi/analytics/clicks/referers.ts
index eeba82421a..06f82892d8 100644
--- a/apps/web/lib/openapi/analytics/clicks/referers.ts
+++ b/apps/web/lib/openapi/analytics/clicks/referers.ts
@@ -1,9 +1,7 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
import z from "@/lib/zod";
-import {
- clickAnalyticsQuerySchema,
- getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { getClickAnalyticsResponse } from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
@@ -14,7 +12,7 @@ export const getReferersByClicks: ZodOpenApiOperationObject = {
description:
"Retrieve the top referers by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
+ query: workspaceParamsSchema.merge(analyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/clicks/timeseries.ts b/apps/web/lib/openapi/analytics/clicks/timeseries.ts
index e16ffccc4c..f671b8c3aa 100644
--- a/apps/web/lib/openapi/analytics/clicks/timeseries.ts
+++ b/apps/web/lib/openapi/analytics/clicks/timeseries.ts
@@ -1,9 +1,7 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
import z from "@/lib/zod";
-import {
- clickAnalyticsQuerySchema,
- getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { getClickAnalyticsResponse } from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
@@ -14,7 +12,7 @@ export const getTimeseriesByClicks: ZodOpenApiOperationObject = {
description:
"Retrieve timeseries click analytics for a link, a domain, or the authenticated workspace over a period of time.",
requestParams: {
- query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
+ query: workspaceParamsSchema.merge(analyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/clicks/top-links.ts b/apps/web/lib/openapi/analytics/clicks/top-links.ts
index 41e7a4fc8f..3614fea4c4 100644
--- a/apps/web/lib/openapi/analytics/clicks/top-links.ts
+++ b/apps/web/lib/openapi/analytics/clicks/top-links.ts
@@ -1,9 +1,7 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
import z from "@/lib/zod";
-import {
- clickAnalyticsQuerySchema,
- getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { getClickAnalyticsResponse } from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
@@ -14,7 +12,7 @@ export const getTopLinksByClicks: ZodOpenApiOperationObject = {
description:
"Retrieve the top links by number of clicks for a domain or the authenticated workspace.",
requestParams: {
- query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
+ query: workspaceParamsSchema.merge(analyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/clicks/top-urls.ts b/apps/web/lib/openapi/analytics/clicks/top-urls.ts
index bf97dd9d70..fafdda36ce 100644
--- a/apps/web/lib/openapi/analytics/clicks/top-urls.ts
+++ b/apps/web/lib/openapi/analytics/clicks/top-urls.ts
@@ -1,9 +1,7 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
import z from "@/lib/zod";
-import {
- clickAnalyticsQuerySchema,
- getClickAnalyticsResponse,
-} from "@/lib/zod/schemas/clicks-analytics";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { getClickAnalyticsResponse } from "@/lib/zod/schemas/clicks-analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../../request";
@@ -14,7 +12,7 @@ export const getTopURLsByClicks: ZodOpenApiOperationObject = {
description:
"Retrieve the top URLs by number of clicks for a given short link.",
requestParams: {
- query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
+ query: workspaceParamsSchema.merge(analyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.ts
new file mode 100644
index 0000000000..25bae0a013
--- /dev/null
+++ b/apps/web/lib/zod/schemas/analytics.ts
@@ -0,0 +1,87 @@
+import { intervals } from "@/lib/analytics/constants";
+import z from "@/lib/zod";
+import { COUNTRY_CODES } from "@dub/utils";
+import { booleanQuerySchema } from "./misc";
+import { parseDateSchema } from "./utils";
+
+// 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."),
+ key: z.string().optional().describe("The short link slug."),
+ linkId: z
+ .string()
+ .optional()
+ .describe("The unique ID of the short link on Dub."),
+ externalId: z
+ .string()
+ .optional()
+ .describe(
+ "This is the ID of the link in the your database. Must be prefixed with 'ext_' when passed as a query parameter.",
+ ),
+ interval: z
+ .enum(intervals)
+ .optional()
+ .describe(
+ "The interval to retrieve analytics for. Takes precedence over start and end. If undefined, defaults to 24h.",
+ ),
+ start: parseDateSchema
+ .refine(
+ (value: Date) => {
+ const foundingDate = new Date("2022-09-22T00:00:00.000Z"); // Dub.co founding date
+ return value >= foundingDate;
+ },
+ {
+ message: "The start date cannot be earlier than September 22, 2022.",
+ },
+ )
+ .optional()
+ .describe("The start date and time when to retrieve analytics from."),
+ end: parseDateSchema
+ .refine(
+ (value: Date) => {
+ const todaysDate = new Date();
+ return value <= todaysDate;
+ },
+ {
+ message: "The end date cannot be in future.",
+ },
+ )
+ .optional()
+ .describe(
+ "The end date and time when to retrieve analytics from. If not provided, defaults to the current date.",
+ ),
+ country: z
+ .enum(COUNTRY_CODES)
+ .optional()
+ .describe("The country to retrieve analytics for.")
+ .openapi({ ref: "countryCode" }),
+ city: z.string().optional().describe("The city to retrieve analytics for."),
+ device: z
+ .string()
+ .optional()
+ .describe("The device to retrieve analytics for."),
+ browser: z
+ .string()
+ .optional()
+ .describe("The browser to retrieve analytics for."),
+ os: z.string().optional().describe("The OS to retrieve analytics for."),
+ referer: z
+ .string()
+ .optional()
+ .describe("The referer to retrieve analytics for."),
+ url: z.string().optional().describe("The URL to retrieve analytics for."),
+ tagId: z
+ .string()
+ .optional()
+ .describe("The tag ID to retrieve analytics for."),
+ qr: booleanQuerySchema
+ .optional()
+ .describe(
+ "Filter for QR code scans. If true, filter for QR codes only. If false, filter for links only. If undefined, return both.",
+ ),
+ root: booleanQuerySchema
+ .optional()
+ .describe(
+ "Filter for root domains. If true, filter for domains only. If false, filter for links only. If undefined, return both.",
+ ),
+});
diff --git a/apps/web/lib/zod/schemas/clicks-analytics.ts b/apps/web/lib/zod/schemas/clicks-analytics.ts
index ef0fa1816b..49633eace5 100644
--- a/apps/web/lib/zod/schemas/clicks-analytics.ts
+++ b/apps/web/lib/zod/schemas/clicks-analytics.ts
@@ -1,12 +1,8 @@
-import {
- intervals,
- VALID_ANALYTICS_ENDPOINTS,
-} from "@/lib/analytics/constants";
+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 { booleanQuerySchema } from "./misc";
-import { parseDateSchema } from "./utils";
+import { analyticsQuerySchema } from "./analytics";
export const analyticsEndpointSchema = z.object({
endpoint: z
@@ -24,88 +20,7 @@ export const analyticsEndpointSchema = z.object({
),
});
-export const clickAnalyticsQuerySchema = z.object({
- domain: z.string().optional().describe("The domain to filter analytics for."),
- key: z.string().optional().describe("The short link slug."),
- linkId: z
- .string()
- .optional()
- .describe("The unique ID of the short link on Dub."),
- externalId: z
- .string()
- .optional()
- .describe(
- "This is the ID of the link in the your database. Must be prefixed with 'ext_' when passed as a query parameter.",
- ),
- interval: z
- .enum(intervals)
- .optional()
- .describe(
- "The interval to retrieve analytics for. Takes precedence over start and end. If undefined, defaults to 24h.",
- ),
- start: parseDateSchema
- .refine(
- (value: Date) => {
- const foundingDate = new Date("2022-09-22T00:00:00.000Z"); // Dub.co founding date
- return value >= foundingDate;
- },
- {
- message: "The start date cannot be earlier than September 22, 2022.",
- },
- )
- .optional()
- .describe("The start date and time when to retrieve analytics from."),
- end: parseDateSchema
- .refine(
- (value: Date) => {
- const todaysDate = new Date();
- return value <= todaysDate;
- },
- {
- message: "The end date cannot be in future.",
- },
- )
- .optional()
- .describe(
- "The end date and time when to retrieve analytics from. If not provided, defaults to the current date.",
- ),
- country: z
- .enum(COUNTRY_CODES)
- .optional()
- .describe("The country to retrieve analytics for.")
- .openapi({ ref: "countryCode" }),
- city: z.string().optional().describe("The city to retrieve analytics for."),
- device: z
- .string()
- .optional()
- .describe("The device to retrieve analytics for."),
- browser: z
- .string()
- .optional()
- .describe("The browser to retrieve analytics for."),
- os: z.string().optional().describe("The OS to retrieve analytics for."),
- referer: z
- .string()
- .optional()
- .describe("The referer to retrieve analytics for."),
- url: z.string().optional().describe("The URL to retrieve analytics for."),
- tagId: z
- .string()
- .optional()
- .describe("The tag ID to retrieve analytics for."),
- qr: booleanQuerySchema
- .optional()
- .describe(
- "Filter for QR code scans. If true, filter for QR codes only. If false, filter for links only. If undefined, return both.",
- ),
- root: booleanQuerySchema
- .optional()
- .describe(
- "Filter for root domains. If true, filter for domains only. If false, filter for links only. If undefined, return both.",
- ),
-});
-
-export const getClickAnalytics = clickAnalyticsQuerySchema
+export const getClickAnalytics = analyticsQuerySchema
.omit({
groupBy: true,
interval: true,