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.

make all analytics responses composite by default

Steven Tey54c424a615ec
diff --git a/apps/web/lib/analytics/get-analytics.ts b/apps/web/lib/analytics/get-analytics.ts
index 1c96ff915c..4261263c6f 100644
--- a/apps/web/lib/analytics/get-analytics.ts
+++ b/apps/web/lib/analytics/get-analytics.ts
@@ -5,20 +5,10 @@ import { prismaEdge } from "../prisma/edge";
import { tbDemo } from "../tinybird/demo-client";
import z from "../zod";
import { analyticsFilterTB } 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 { analyticsResponse } from "../zod/schemas/analytics-response";
import { INTERVAL_DATA } from "./constants";
import { AnalyticsFilters } from "./types";
-const responseSchema = {
- clicks: clickAnalyticsResponse,
- leads: leadAnalyticsResponse,
- sales: saleAnalyticsResponse,
- composite: compositeAnalyticsResponse,
-};
-
// Fetch data for /api/analytics
export const getAnalytics = async (params: AnalyticsFilters) => {
let {
@@ -84,7 +74,7 @@ export const getAnalytics = async (params: AnalyticsFilters) => {
const pipe = (isDemo ? tbDemo : tb).buildPipe({
pipe: `v1_${groupBy}`,
parameters: analyticsFilterTB,
- data: groupBy === "top_links" ? z.any() : responseSchema[event][groupBy],
+ data: groupBy === "top_links" ? z.any() : analyticsResponse[groupBy],
});
const response = await pipe({
@@ -139,10 +129,19 @@ export const getAnalytics = async (params: AnalyticsFilters) => {
createdAt: link.createdAt,
}));
- return topLinksData.map((d) => ({
- ...allLinks.find((l) => l.id === d.link),
- ...d,
- }));
+ return topLinksData.map((d) => {
+ const link = allLinks.find((l) => l.id === d.link);
+ if (!link) {
+ return null;
+ }
+ return {
+ ...analyticsResponse[groupBy].parse({
+ ...link,
+ createdAt: link.createdAt.toISOString(),
+ ...d,
+ }),
+ };
+ });
}
// Return array for other endpoints
diff --git a/apps/web/lib/analytics/types.ts b/apps/web/lib/analytics/types.ts
index 7d0cab80fd..312be44d32 100644
--- a/apps/web/lib/analytics/types.ts
+++ b/apps/web/lib/analytics/types.ts
@@ -11,13 +11,11 @@ import {
} from "./constants";
export type IntervalOptions = (typeof intervals)[number];
+
export type AnalyticsGroupByOptions =
(typeof VALID_ANALYTICS_ENDPOINTS)[number];
-export type CompositeAnalyticsResponseOptions =
- | "clicks"
- | "leads"
- | "sales"
- | "amount";
+
+export type AnalyticsResponseOptions = "clicks" | "leads" | "sales" | "amount";
export type EventType = (typeof EVENT_TYPES)[number];
diff --git a/apps/web/lib/openapi/analytics/index.ts b/apps/web/lib/openapi/analytics/index.ts
index b68a509530..c00556ee62 100644
--- a/apps/web/lib/openapi/analytics/index.ts
+++ b/apps/web/lib/openapi/analytics/index.ts
@@ -1,10 +1,7 @@
import { openApiErrorResponses } from "@/lib/openapi/responses";
import z from "@/lib/zod";
import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
-import { clickAnalyticsResponse } from "@/lib/zod/schemas/clicks-analytics";
-import { compositeAnalyticsResponse } from "@/lib/zod/schemas/composite-analytics";
-import { leadAnalyticsResponse } from "@/lib/zod/schemas/leads-analytics";
-import { saleAnalyticsResponse } from "@/lib/zod/schemas/sales-analytics";
+import { analyticsResponse } from "@/lib/zod/schemas/analytics-response";
import { ZodOpenApiOperationObject, ZodOpenApiPathsObject } from "zod-openapi";
const retrieveAnalytics: ZodOpenApiOperationObject = {
@@ -23,115 +20,32 @@ const retrieveAnalytics: ZodOpenApiOperationObject = {
content: {
"application/json": {
schema: z.union([
- compositeAnalyticsResponse.count,
+ analyticsResponse.count,
z
- .array(compositeAnalyticsResponse.timeseries)
- .openapi({ title: "CompositeTimeseries" }),
+ .array(analyticsResponse.timeseries)
+ .openapi({ title: "AnalyticsTimeseries" }),
z
- .array(compositeAnalyticsResponse.countries)
- .openapi({ title: "CompositeCountries" }),
+ .array(analyticsResponse.countries)
+ .openapi({ title: "AnalyticsCountries" }),
z
- .array(compositeAnalyticsResponse.cities)
- .openapi({ title: "CompositeCities" }),
+ .array(analyticsResponse.cities)
+ .openapi({ title: "AnalyticsCities" }),
z
- .array(compositeAnalyticsResponse.devices)
- .openapi({ title: "CompositeDevices" }),
+ .array(analyticsResponse.devices)
+ .openapi({ title: "AnalyticsDevices" }),
z
- .array(compositeAnalyticsResponse.browsers)
- .openapi({ title: "CompositeBrowsers" }),
+ .array(analyticsResponse.browsers)
+ .openapi({ title: "AnalyticsBrowsers" }),
+ z.array(analyticsResponse.os).openapi({ title: "AnalyticsOS" }),
z
- .array(compositeAnalyticsResponse.os)
- .openapi({ title: "CompositeOS" }),
+ .array(analyticsResponse.referers)
+ .openapi({ title: "AnalyticsReferers" }),
z
- .array(compositeAnalyticsResponse.referers)
- .openapi({ title: "CompositeReferers" }),
+ .array(analyticsResponse.top_links)
+ .openapi({ title: "AnalyticsTopLinks" }),
z
- .array(compositeAnalyticsResponse.top_links)
- .openapi({ title: "CompositeTopLinks" }),
- z
- .array(compositeAnalyticsResponse.top_urls)
- .openapi({ title: "CompositeTopUrls" }),
-
- clickAnalyticsResponse.count,
- z
- .array(clickAnalyticsResponse.timeseries)
- .openapi({ title: "ClickTimeseries" }),
- z
- .array(clickAnalyticsResponse.countries)
- .openapi({ title: "ClickCountries" }),
- z
- .array(clickAnalyticsResponse.cities)
- .openapi({ title: "ClickCities" }),
- z
- .array(clickAnalyticsResponse.devices)
- .openapi({ title: "ClickDevices" }),
- z
- .array(clickAnalyticsResponse.browsers)
- .openapi({ title: "ClickBrowsers" }),
- z.array(clickAnalyticsResponse.os).openapi({ title: "ClickOS" }),
- z
- .array(clickAnalyticsResponse.referers)
- .openapi({ title: "ClickReferers" }),
- z
- .array(clickAnalyticsResponse.top_links)
- .openapi({ title: "ClickTopLinks" }),
- z
- .array(clickAnalyticsResponse.top_urls)
- .openapi({ title: "ClickTopUrls" }),
-
- leadAnalyticsResponse.count,
- z
- .array(leadAnalyticsResponse.timeseries)
- .openapi({ title: "LeadsTimeseries" }),
- z
- .array(leadAnalyticsResponse.countries)
- .openapi({ title: "LeadsCountries" }),
- z
- .array(leadAnalyticsResponse.cities)
- .openapi({ title: "LeadsCities" }),
- z
- .array(leadAnalyticsResponse.devices)
- .openapi({ title: "LeadsDevices" }),
- z
- .array(leadAnalyticsResponse.browsers)
- .openapi({ title: "LeadsBrowsers" }),
- z.array(leadAnalyticsResponse.os).openapi({ title: "LeadsOS" }),
- z
- .array(leadAnalyticsResponse.referers)
- .openapi({ title: "LeadsReferers" }),
- z
- .array(leadAnalyticsResponse.top_links)
- .openapi({ title: "LeadsTopLinks" }),
- z
- .array(leadAnalyticsResponse.top_urls)
- .openapi({ title: "LeadsTopUrls" }),
-
- saleAnalyticsResponse.count,
- z
- .array(saleAnalyticsResponse.timeseries)
- .openapi({ title: "SalesTimeseries" }),
- z
- .array(saleAnalyticsResponse.countries)
- .openapi({ title: "SalesCountries" }),
- z
- .array(saleAnalyticsResponse.cities)
- .openapi({ title: "SalesCities" }),
- z
- .array(saleAnalyticsResponse.devices)
- .openapi({ title: "SalesDevices" }),
- z
- .array(saleAnalyticsResponse.browsers)
- .openapi({ title: "SalesBrowsers" }),
- z.array(saleAnalyticsResponse.os).openapi({ title: "SalesOS" }),
- z
- .array(saleAnalyticsResponse.referers)
- .openapi({ title: "SalesReferers" }),
- z
- .array(saleAnalyticsResponse.top_links)
- .openapi({ title: "SalesTopLinks" }),
- z
- .array(saleAnalyticsResponse.top_urls)
- .openapi({ title: "SalesTopUrls" }),
+ .array(analyticsResponse.top_urls)
+ .openapi({ title: "AnalyticsTopUrls" }),
]),
},
},
diff --git a/apps/web/lib/zod/schemas/analytics-response.ts b/apps/web/lib/zod/schemas/analytics-response.ts
new file mode 100644
index 0000000000..a7d0d8361a
--- /dev/null
+++ b/apps/web/lib/zod/schemas/analytics-response.ts
@@ -0,0 +1,276 @@
+import z from "@/lib/zod";
+import { CONTINENT_CODES, COUNTRY_CODES } from "@dub/utils";
+
+export const analyticsResponse = {
+ count: z
+ .object({
+ clicks: z.number().describe("The total number of clicks").default(0),
+ leads: z.number().describe("The total number of leads").default(0),
+ sales: z.number().describe("The total number of sales").default(0),
+ amount: z.number().describe("The total amount of sales").default(0),
+ })
+ .openapi({ ref: "AnalyticsCount", title: "AnalyticsCount" }),
+ timeseries: z
+ .object({
+ start: z.string().describe("The starting timestamp of the interval"),
+ clicks: z
+ .number()
+ .describe("The number of clicks in the interval")
+ .default(0),
+ leads: z
+ .number()
+ .describe("The number of leads in the interval")
+ .default(0),
+ sales: z
+ .number()
+ .describe("The number of sales in the interval")
+ .default(0),
+ amount: z
+ .number()
+ .describe("The total amount of sales in the interval")
+ .default(0),
+ })
+ .openapi({ ref: "AnalyticsTimeseries" }),
+
+ continents: z
+ .object({
+ continent: z
+ .enum(CONTINENT_CODES)
+ .describe(
+ "The 2-letter ISO 3166-1 code representing the continent associated with the location of the user.",
+ ),
+ clicks: z
+ .number()
+ .describe("The number of clicks from this continent")
+ .default(0),
+ leads: z
+ .number()
+ .describe("The number of leads from this continent")
+ .default(0),
+ sales: z
+ .number()
+ .describe("The number of sales from this continent")
+ .default(0),
+ amount: z
+ .number()
+ .describe("The total amount of sales from this continent")
+ .default(0),
+ })
+ .openapi({ ref: "AnalyticsContinents" }),
+
+ countries: z
+ .object({
+ country: z
+ .enum(COUNTRY_CODES)
+ .describe(
+ "The 2-letter ISO 3166-1 country code for the country associated with the location of the user. Learn more: https://d.to/geo",
+ ),
+ clicks: z
+ .number()
+ .describe("The number of clicks from this country")
+ .default(0),
+ leads: z
+ .number()
+ .describe("The number of leads from this country")
+ .default(0),
+ sales: z
+ .number()
+ .describe("The number of sales from this country")
+ .default(0),
+ amount: z
+ .number()
+ .describe("The total amount of sales from this country")
+ .default(0),
+ })
+ .openapi({ ref: "AnalyticsCountries" }),
+
+ 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")
+ .default(0),
+ leads: z
+ .number()
+ .describe("The number of leads from this city")
+ .default(0),
+ sales: z
+ .number()
+ .describe("The number of sales from this city")
+ .default(0),
+ amount: z
+ .number()
+ .describe("The total amount of sales from this city")
+ .default(0),
+ })
+ .openapi({ ref: "AnalyticsCities" }),
+
+ devices: z
+ .object({
+ device: z.string().describe("The name of the device"),
+ clicks: z
+ .number()
+ .describe("The number of clicks from this device")
+ .default(0),
+ leads: z
+ .number()
+ .describe("The number of leads from this device")
+ .default(0),
+ sales: z
+ .number()
+ .describe("The number of sales from this device")
+ .default(0),
+ amount: z
+ .number()
+ .describe("The total amount of sales from this device")
+ .default(0),
+ })
+ .openapi({ ref: "AnalyticsDevices" }),
+
+ browsers: z
+ .object({
+ browser: z.string().describe("The name of the browser"),
+ clicks: z
+ .number()
+ .describe("The number of clicks from this browser")
+ .default(0),
+ leads: z
+ .number()
+ .describe("The number of leads from this browser")
+ .default(0),
+ sales: z
+ .number()
+ .describe("The number of sales from this browser")
+ .default(0),
+ amount: z
+ .number()
+ .describe("The total amount of sales from this browser")
+ .default(0),
+ })
+ .openapi({ ref: "AnalyticsBrowsers" }),
+
+ os: z
+ .object({
+ os: z.string().describe("The name of the OS"),
+ clicks: z
+ .number()
+ .describe("The number of clicks from this OS")
+ .default(0),
+ leads: z.number().describe("The number of leads from this OS").default(0),
+ sales: z.number().describe("The number of sales from this OS").default(0),
+ amount: z
+ .number()
+ .describe("The total amount of sales from this OS")
+ .default(0),
+ })
+ .openapi({ ref: "AnalyticsOS" }),
+
+ 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")
+ .default(0),
+ leads: z
+ .number()
+ .describe("The number of leads from this referer")
+ .default(0),
+ sales: z
+ .number()
+ .describe("The number of sales from this referer")
+ .default(0),
+ amount: z
+ .number()
+ .describe("The total amount of sales from this referer")
+ .default(0),
+ })
+ .openapi({ ref: "AnalyticsReferers" }),
+
+ top_links: z
+ .object({
+ link: z
+ .string()
+ .describe("The unique ID of the short link")
+ .openapi({ deprecated: true }),
+ id: z.string().describe("The unique ID of the short link"),
+ domain: z.string().describe("The domain of the short link"),
+ key: z.string().describe("The key of the short link"),
+ shortLink: z.string().describe("The short link URL"),
+ url: z.string().describe("The destination URL of the short link"),
+ createdAt: z
+ .string()
+ .describe("The creation timestamp of the short link"),
+ clicks: z
+ .number()
+ .describe("The number of clicks from this link")
+ .default(0),
+ leads: z
+ .number()
+ .describe("The number of leads from this link")
+ .default(0),
+ sales: z
+ .number()
+ .describe("The number of sales from this link")
+ .default(0),
+ amount: z
+ .number()
+ .describe("The total amount of sales from this link")
+ .default(0),
+ })
+ .openapi({ ref: "AnalyticsTopLinks" }),
+
+ top_urls: z
+ .object({
+ url: z.string().describe("The destination URL"),
+ clicks: z
+ .number()
+ .describe("The number of clicks from this URL")
+ .default(0),
+ leads: z
+ .number()
+ .describe("The number of leads from this URL")
+ .default(0),
+ sales: z
+ .number()
+ .describe("The number of sales from this URL")
+ .default(0),
+ amount: z
+ .number()
+ .describe("The total amount of sales from this URL")
+ .default(0),
+ })
+ .openapi({ ref: "AnalyticsTopUrls" }),
+
+ trigger: z
+ .object({
+ trigger: z
+ .enum(["link", "qr"])
+ .describe("The type of trigger method: link click or QR scan"),
+ clicks: z
+ .number()
+ .describe("The number of clicks from this trigger method.")
+ .default(0),
+ leads: z
+ .number()
+ .describe("The number of leads from this trigger method.")
+ .default(0),
+ sales: z
+ .number()
+ .describe("The number of sales from this trigger method.")
+ .default(0),
+ amount: z
+ .number()
+ .describe("The total amount of sales from this trigger method.")
+ .default(0),
+ })
+ .openapi({ ref: "AnalyticsTrigger" }),
+} as const;
diff --git a/apps/web/lib/zod/schemas/clicks-analytics.ts b/apps/web/lib/zod/schemas/clicks-analytics.ts
deleted file mode 100644
index 047a510271..0000000000
--- a/apps/web/lib/zod/schemas/clicks-analytics.ts
+++ /dev/null
@@ -1,118 +0,0 @@
-import z from "@/lib/zod";
-import { CONTINENT_CODES, COUNTRY_CODES } from "@dub/utils";
-
-// Click analytics response schemas
-export const clickAnalyticsResponse = {
- count: z
- .object({
- clicks: z.number().describe("The total number of clicks"),
- })
- .openapi({ ref: "ClicksCount", title: "ClicksCount" }),
-
- timeseries: z
- .object({
- start: z.string().describe("The starting timestamp of the interval"),
- clicks: z.number().describe("The number of clicks in the interval"),
- })
- .openapi({ ref: "ClicksTimeseries" }),
-
- continents: z
- .object({
- continent: z
- .enum(CONTINENT_CODES)
- .describe(
- "The 2-letter ISO 3166-1 code representing the continent associated with the location of the user.",
- ),
- clicks: z.number().describe("The number of clicks from this continent"),
- })
- .openapi({ ref: "ClicksContinents" }),
-
- countries: z
- .object({
- country: z
- .enum(COUNTRY_CODES)
- .describe(
- "The 2-letter ISO 3166-1 country code for the country associated with the location of the user. Learn more: https://d.to/geo",
- ),
- clicks: z.number().describe("The number of clicks from this country"),
- })
- .openapi({ ref: "ClicksCountries" }),
-
- 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"),
- })
- .openapi({ ref: "ClicksCities" }),
-
- devices: z
- .object({
- device: z.string().describe("The name of the device"),
- clicks: z.number().describe("The number of clicks from this device"),
- })
- .openapi({ ref: "ClicksDevices" }),
-
- browsers: z
- .object({
- browser: z.string().describe("The name of the browser"),
- clicks: z.number().describe("The number of clicks from this browser"),
- })
- .openapi({ ref: "ClicksBrowsers" }),
-
- os: z
- .object({
- os: z.string().describe("The name of the OS"),
- clicks: z.number().describe("The number of clicks from this OS"),
- })
- .openapi({ ref: "ClicksOS" }),
-
- 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"),
- })
- .openapi({ ref: "ClicksReferers" }),
-
- top_links: z
- .object({
- link: z
- .string()
- .describe("The unique ID of the short link")
- .openapi({ deprecated: true }),
- id: z.string().describe("The unique ID of the short link"),
- domain: z.string().describe("The domain of the short link"),
- key: z.string().describe("The key of the short link"),
- shortLink: z.string().describe("The short link URL"),
- url: z.string().describe("The destination URL of the short link"),
- createdAt: z
- .string()
- .describe("The creation timestamp of the short link"),
- clicks: z.number().describe("The number of clicks from this link"),
- })
- .openapi({ ref: "ClicksTopLinks" }),
-
- top_urls: z
- .object({
- url: z.string().describe("The destination URL"),
- clicks: z.number().describe("The number of clicks from this URL"),
- })
- .openapi({ ref: "ClicksTopUrls" }),
-
- trigger: z
- .object({
- trigger: z
- .enum(["link", "qr"])
- .describe("The type of trigger method: link click or QR scan"),
- clicks: z
- .number()
- .describe("The number of clicks from this trigger method."),
- })
- .openapi({ ref: "ClicksTrigger" }),
-} as const;
diff --git a/apps/web/lib/zod/schemas/composite-analytics.ts b/apps/web/lib/zod/schemas/composite-analytics.ts
deleted file mode 100644
index 5303d4b9c5..0000000000
--- a/apps/web/lib/zod/schemas/composite-analytics.ts
+++ /dev/null
@@ -1,167 +0,0 @@
-import z from "@/lib/zod";
-import { CONTINENT_CODES, 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"),
- })
- .openapi({ ref: "CompositeCount", title: "CompositeCount" }),
-
- 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"),
- })
- .openapi({ ref: "CompositeTimeseries" }),
-
- continents: z
- .object({
- continent: z
- .enum(CONTINENT_CODES)
- .describe(
- "The 2-letter ISO 3166-1 code representing the continent associated with the location of the user.",
- ),
- clicks: z.number().describe("The number of clicks from this continent"),
- leads: z.number().describe("The number of leads from this continent"),
- sales: z.number().describe("The number of sales from this continent"),
- amount: z
- .number()
- .describe("The total amount of sales from this continent"),
- })
- .openapi({ ref: "CompositeContinents" }),
-
- countries: z
- .object({
- country: z
- .enum(COUNTRY_CODES)
- .describe(
- "The 2-letter ISO 3166-1 country code for the country associated with the location of the user. Learn more: 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: "CompositeCountries" }),
-
- 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: "CompositeCities" }),
-
- 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"),
- })
- .openapi({ ref: "CompositeDevices" }),
-
- 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"),
- })
- .openapi({ ref: "CompositeBrowsers" }),
-
- 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"),
- })
- .openapi({ ref: "CompositeOS" }),
-
- 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"),
- })
- .openapi({ ref: "CompositeReferers" }),
-
- top_links: z
- .object({
- link: z
- .string()
- .describe("The unique ID of the short link")
- .openapi({ deprecated: true }),
- id: z.string().describe("The unique ID of the short link"),
- domain: z.string().describe("The domain of the short link"),
- key: z.string().describe("The key of the short link"),
- shortLink: z.string().describe("The short link URL"),
- url: z.string().describe("The destination URL of the short link"),
- createdAt: z
- .string()
- .describe("The creation timestamp 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"),
- })
- .openapi({ ref: "CompositeTopLinks" }),
-
- 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"),
- })
- .openapi({ ref: "CompositeTopUrls" }),
-
- trigger: z
- .object({
- trigger: z
- .enum(["link", "qr"])
- .describe("The type of trigger method: link click or QR scan"),
- clicks: z
- .number()
- .describe("The number of clicks from this trigger method."),
- leads: z
- .number()
- .describe("The number of leads from this trigger method."),
- sales: z
- .number()
- .describe("The number of sales from this trigger method."),
- amount: z
- .number()
- .describe("The total amount of sales from this trigger method."),
- })
- .openapi({ ref: "CompositeTrigger" }),
-} as const;
diff --git a/apps/web/lib/zod/schemas/leads-analytics.ts b/apps/web/lib/zod/schemas/leads-analytics.ts
deleted file mode 100644
index 6100df7ec3..0000000000
--- a/apps/web/lib/zod/schemas/leads-analytics.ts
+++ /dev/null
@@ -1,117 +0,0 @@
-import z from "@/lib/zod";
-import { CONTINENT_CODES, COUNTRY_CODES } from "@dub/utils";
-
-export const leadAnalyticsResponse = {
- count: z
- .object({
- leads: z.number().describe("The total number of leads"),
- })
- .openapi({ ref: "LeadsCount", title: "LeadsCount" }),
-
- timeseries: z
- .object({
- start: z.string().describe("The starting timestamp of the interval"),
- leads: z.number().describe("The number of leads in the interval"),
- })
- .openapi({ ref: "LeadsTimeseries" }),
-
- continents: z
- .object({
- continent: z
- .enum(CONTINENT_CODES)
- .describe(
- "The 2-letter ISO 3166-1 code representing the continent associated with the location of the user.",
- ),
- leads: z.number().describe("The number of leads from this continent"),
- })
- .openapi({ ref: "LeadsContinents" }),
-
- countries: z
- .object({
- country: z
- .enum(COUNTRY_CODES)
- .describe(
- "The 2-letter ISO 3166-1 country code for the country associated with the location of the user. Learn more: https://d.to/geo",
- ),
- leads: z.number().describe("The number of leads from this country"),
- })
- .openapi({ ref: "LeadsCountries" }),
-
- 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"),
- leads: z.number().describe("The number of leads from this city"),
- })
- .openapi({ ref: "LeadsCities" }),
-
- devices: z
- .object({
- device: z.string().describe("The name of the device"),
- leads: z.number().describe("The number of leads from this device"),
- })
- .openapi({ ref: "LeadsDevices" }),
-
- browsers: z
- .object({
- browser: z.string().describe("The name of the browser"),
- leads: z.number().describe("The number of leads from this browser"),
- })
- .openapi({ ref: "LeadsBrowsers" }),
-
- os: z
- .object({
- os: z.string().describe("The name of the OS"),
- leads: z.number().describe("The number of leads from this OS"),
- })
- .openapi({ ref: "LeadsOS" }),
-
- referers: z
- .object({
- referer: z
- .string()
- .describe(
- "The name of the referer. If unknown, this will be `(direct)`",
- ),
- leads: z.number().describe("The number of leads from this referer"),
- })
- .openapi({ ref: "LeadsReferers" }),
-
- top_links: z
- .object({
- link: z
- .string()
- .describe("The unique ID of the short link")
- .openapi({ deprecated: true }),
- id: z.string().describe("The unique ID of the short link"),
- domain: z.string().describe("The domain of the short link"),
- key: z.string().describe("The key of the short link"),
- shortLink: z.string().describe("The short link URL"),
- url: z.string().describe("The destination URL of the short link"),
- createdAt: z
- .string()
- .describe("The creation timestamp of the short link"),
- leads: z.number().describe("The number of leads from this link"),
- })
- .openapi({ ref: "LeadsTopLinks" }),
-
- top_urls: z
- .object({
- url: z.string().describe("The destination URL"),
- leads: z.number().describe("The number of leads from this URL"),
- })
- .openapi({ ref: "LeadsTopUrls" }),
-
- trigger: z
- .object({
- trigger: z
- .enum(["link", "qr"])
- .describe("The type of trigger method: link click or QR scan"),
- leads: z
- .number()
- .describe("The number of leads from this trigger method."),
- })
- .openapi({ ref: "LeadsTrigger" }),
-} as const;
diff --git a/apps/web/lib/zod/schemas/sales-analytics.ts b/apps/web/lib/zod/schemas/sales-analytics.ts
deleted file mode 100644
index 4b6cecfa60..0000000000
--- a/apps/web/lib/zod/schemas/sales-analytics.ts
+++ /dev/null
@@ -1,136 +0,0 @@
-import z from "@/lib/zod";
-import { CONTINENT_CODES, COUNTRY_CODES } from "@dub/utils";
-
-export const saleAnalyticsResponse = {
- count: z
- .object({
- sales: z.number().describe("The total number of sales"),
- amount: z.number().describe("The total amount of sales"),
- })
- .openapi({ ref: "SalesCount", title: "SalesCount" }),
-
- timeseries: z
- .object({
- start: z.string().describe("The starting timestamp of 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"),
- })
- .openapi({ ref: "SalesTimeseries" }),
-
- continents: z
- .object({
- continent: z
- .enum(CONTINENT_CODES)
- .describe(
- "The 2-letter ISO 3166-1 code representing the continent associated with the location of the user.",
- ),
- sales: z.number().describe("The number of sales from this continent"),
- })
- .openapi({ ref: "SalesContinents" }),
-
- countries: z
- .object({
- country: z
- .enum(COUNTRY_CODES)
- .describe(
- "The 2-letter ISO 3166-1 country code for the country associated with the location of the user. Learn more: https://d.to/geo",
- ),
- 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: "SalesCountries" }),
-
- 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"),
- 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: "SalesCities" }),
-
- devices: z
- .object({
- device: z.string().describe("The name of the device"),
- sales: z.number().describe("The number of sales from this device"),
- amount: z.number().describe("The total amount of sales from this device"),
- })
- .openapi({ ref: "SalesDevices" }),
-
- browsers: z
- .object({
- browser: z.string().describe("The name of the browser"),
- sales: z.number().describe("The number of sales from this browser"),
- amount: z
- .number()
- .describe("The total amount of sales from this browser"),
- })
- .openapi({ ref: "SalesBrowsers" }),
-
- os: z
- .object({
- os: z.string().describe("The name of the OS"),
- sales: z.number().describe("The number of sales from this OS"),
- amount: z.number().describe("The total amount of sales from this OS"),
- })
- .openapi({ ref: "SalesOS" }),
-
- referers: z
- .object({
- referer: z
- .string()
- .describe(
- "The name of the referer. If unknown, this will be `(direct)`",
- ),
- sales: z.number().describe("The number of sales from this referer"),
- amount: z
- .number()
- .describe("The total amount of sales from this referer"),
- })
- .openapi({ ref: "SalesReferers" }),
-
- top_links: z
- .object({
- link: z
- .string()
- .describe("The unique ID of the short link")
- .openapi({ deprecated: true }),
- id: z.string().describe("The unique ID of the short link"),
- domain: z.string().describe("The domain of the short link"),
- key: z.string().describe("The key of the short link"),
- shortLink: z.string().describe("The short link URL"),
- url: z.string().describe("The destination URL of the short link"),
- createdAt: z
- .string()
- .describe("The creation timestamp of the short link"),
- sales: z.number().describe("The number of sales from this link"),
- amount: z.number().describe("The total amount of sales from this link"),
- })
- .openapi({ ref: "SalesTopLinks" }),
-
- top_urls: z
- .object({
- url: z.string().describe("The destination URL"),
- sales: z.number().describe("The number of sales from this URL"),
- amount: z.number().describe("The total amount of sales from this URL"),
- })
- .openapi({ ref: "SalesTopUrls" }),
-
- trigger: z
- .object({
- trigger: z
- .enum(["link", "qr"])
- .describe("The type of trigger method: link click or QR scan"),
- sales: z
- .number()
- .describe("The number of sales from this trigger method."),
- amount: z
- .number()
- .describe("The total amount of sales from this trigger method."),
- })
- .openapi({ ref: "SalesTrigger" }),
-} as const;
diff --git a/apps/web/tests/analytics/clicks.test.ts b/apps/web/tests/analytics/clicks.test.ts
deleted file mode 100644
index 4b86eb26c2..0000000000
--- a/apps/web/tests/analytics/clicks.test.ts
+++ /dev/null
@@ -1,104 +0,0 @@
-import {
- OLD_ANALYTICS_ENDPOINTS,
- OLD_TO_NEW_ANALYTICS_ENDPOINTS,
- VALID_ANALYTICS_ENDPOINTS,
-} from "@/lib/analytics/constants";
-import z from "@/lib/zod";
-import { clickAnalyticsResponse } from "@/lib/zod/schemas/clicks-analytics";
-import { describe, expect, test } from "vitest";
-import { env } from "../utils/env";
-import { IntegrationHarness } from "../utils/integration";
-import { filter } from "./utils";
-
-describe.runIf(env.CI).sequential("GET /analytics?event=clicks", async () => {
- const h = new IntegrationHarness();
- const { workspace, http } = await h.init();
- const workspaceId = workspace.id;
-
- VALID_ANALYTICS_ENDPOINTS.map((groupBy) => {
- test(`by ${groupBy}`, async () => {
- const { status, data } = await http.get<any[]>({
- path: `/analytics`,
- query: { event: "clicks", groupBy, workspaceId, ...filter },
- });
-
- const responseSchema =
- groupBy === "count"
- ? clickAnalyticsResponse[groupBy].strict()
- : z.array(clickAnalyticsResponse[groupBy].strict());
-
- const parsed = responseSchema.safeParse(data);
-
- expect(status).toEqual(200);
- expect(parsed.success).toBeTruthy();
- });
- });
-});
-
-// deprecated, backwards compatiblity
-describe.runIf(env.CI).sequential("GET /analytics/clicks", async () => {
- const h = new IntegrationHarness();
- const { workspace, http } = await h.init();
- const workspaceId = workspace.id;
-
- OLD_ANALYTICS_ENDPOINTS.slice(0, 5).map((endpoint) => {
- test(`deprecated: by ${endpoint}`, async () => {
- const { status, data } = await http.get<any[]>({
- path: `/analytics/clicks/${endpoint}`,
- query: { workspaceId, ...filter },
- });
-
- if (endpoint === "clicks" || endpoint === "count") {
- expect(status).toEqual(200);
- expect(data).toEqual(expect.any(Number));
- expect(data).toBeGreaterThanOrEqual(0);
- } else {
- const parsed = z
- .array(
- clickAnalyticsResponse[
- OLD_TO_NEW_ANALYTICS_ENDPOINTS[endpoint] || endpoint
- ].strict(),
- )
- .safeParse(data);
-
- expect(status).toEqual(200);
- expect(data.length).toBeGreaterThanOrEqual(0);
- expect(parsed.success).toBeTruthy();
- }
- });
- });
-});
-
-// deprecated, backwards compatiblity
-describe.runIf(env.CI).sequential("GET /analytics/{endpoint}", async () => {
- const h = new IntegrationHarness();
- const { workspace, http } = await h.init();
- const workspaceId = workspace.id;
-
- OLD_ANALYTICS_ENDPOINTS.slice(0, 5).map((endpoint) => {
- test(`/analytics/${endpoint}`, async () => {
- const { status, data } = await http.get<any[]>({
- path: `/analytics/${endpoint}`,
- query: { workspaceId, ...filter },
- });
-
- expect(status).toEqual(200);
-
- if (endpoint === "clicks" || endpoint === "count") {
- expect(data).toEqual(expect.any(Number));
- expect(data).toBeGreaterThanOrEqual(0);
- } else {
- const parsed = z
- .array(
- clickAnalyticsResponse[
- OLD_TO_NEW_ANALYTICS_ENDPOINTS[endpoint] || endpoint
- ].strict(),
- )
- .safeParse(data);
-
- expect(data.length).toBeGreaterThanOrEqual(0);
- expect(parsed.success).toBeTruthy();
- }
- });
- });
-});
diff --git a/apps/web/tests/analytics/leads.test.ts b/apps/web/tests/analytics/leads.test.ts
deleted file mode 100644
index d592723c56..0000000000
--- a/apps/web/tests/analytics/leads.test.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import { VALID_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
-import z from "@/lib/zod";
-import { leadAnalyticsResponse } from "@/lib/zod/schemas/leads-analytics";
-import { describe, expect, test } from "vitest";
-import { env } from "../utils/env";
-import { IntegrationHarness } from "../utils/integration";
-import { filter } from "./utils";
-
-describe.runIf(env.CI).sequential("GET /analytics?event=leads", async () => {
- const h = new IntegrationHarness();
- const { workspace, http } = await h.init();
- const workspaceId = workspace.id;
-
- VALID_ANALYTICS_ENDPOINTS.map((groupBy) => {
- test(`by ${groupBy}`, async () => {
- const { status, data } = await http.get<any[]>({
- path: `/analytics`,
- query: { event: "leads", groupBy, workspaceId, ...filter },
- });
-
- const responseSchema =
- groupBy === "count"
- ? leadAnalyticsResponse[groupBy].strict()
- : z.array(leadAnalyticsResponse[groupBy].strict());
-
- const parsed = responseSchema.safeParse(data);
-
- expect(status).toEqual(200);
- expect(parsed.success).toBeTruthy();
- });
- });
-});
diff --git a/apps/web/tests/analytics/composite.test.ts b/apps/web/tests/analytics/response.test.ts
similarity index 82%
rename from apps/web/tests/analytics/composite.test.ts
rename to apps/web/tests/analytics/response.test.ts
index 82563dd811..ce6bc3e618 100644
--- a/apps/web/tests/analytics/composite.test.ts
+++ b/apps/web/tests/analytics/response.test.ts
@@ -1,6 +1,6 @@
import { VALID_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
import z from "@/lib/zod";
-import { compositeAnalyticsResponse } from "@/lib/zod/schemas/composite-analytics";
+import { analyticsResponse } from "@/lib/zod/schemas/analytics-response";
import { describe, expect, test } from "vitest";
import { env } from "../utils/env";
import { IntegrationHarness } from "../utils/integration";
@@ -22,8 +22,8 @@ describe
const responseSchema =
groupBy === "count"
- ? compositeAnalyticsResponse[groupBy].strict()
- : z.array(compositeAnalyticsResponse[groupBy].strict());
+ ? analyticsResponse[groupBy].strict()
+ : z.array(analyticsResponse[groupBy].strict());
const parsed = responseSchema.safeParse(data);
diff --git a/apps/web/tests/analytics/sales.test.ts b/apps/web/tests/analytics/sales.test.ts
deleted file mode 100644
index 3a8e78c69d..0000000000
--- a/apps/web/tests/analytics/sales.test.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import { VALID_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
-import z from "@/lib/zod";
-import { saleAnalyticsResponse } from "@/lib/zod/schemas/sales-analytics";
-import { describe, expect, test } from "vitest";
-import { env } from "../utils/env";
-import { IntegrationHarness } from "../utils/integration";
-import { filter } from "./utils";
-
-describe.runIf(env.CI).sequential("GET /analytics?event=sales", async () => {
- const h = new IntegrationHarness();
- const { workspace, http } = await h.init();
- const workspaceId = workspace.id;
-
- VALID_ANALYTICS_ENDPOINTS.map((groupBy) => {
- test(`by ${groupBy}`, async () => {
- const { status, data } = await http.get<any[]>({
- path: `/analytics`,
- query: { event: "sales", groupBy, workspaceId, ...filter },
- });
-
- const responseSchema =
- groupBy === "count"
- ? saleAnalyticsResponse[groupBy].strict()
- : z.array(saleAnalyticsResponse[groupBy].strict());
-
- const parsed = responseSchema.safeParse(data);
-
- expect(status).toEqual(200);
- expect(parsed.success).toBeTruthy();
- });
- });
-});
diff --git a/apps/web/ui/analytics/analytics-provider.tsx b/apps/web/ui/analytics/analytics-provider.tsx
index e1a499ddf8..7311f0d71f 100644
--- a/apps/web/ui/analytics/analytics-provider.tsx
+++ b/apps/web/ui/analytics/analytics-provider.tsx
@@ -6,8 +6,8 @@ import {
VALID_ANALYTICS_FILTERS,
} from "@/lib/analytics/constants";
import {
+ AnalyticsResponseOptions,
AnalyticsView,
- CompositeAnalyticsResponseOptions,
EventType,
} from "@/lib/analytics/types";
import { editQueryString } from "@/lib/analytics/utils";
@@ -41,7 +41,7 @@ export const AnalyticsContext = createContext<{
interval?: string;
tagId?: string;
totalEvents?: {
- [key in CompositeAnalyticsResponseOptions]: number;
+ [key in AnalyticsResponseOptions]: number;
};
adminPage?: boolean;
demoPage?: boolean;
@@ -195,7 +195,7 @@ export default function AnalyticsProvider({
useEffect(() => setRequiresUpgrade(false), [queryString]);
const { data: totalEvents } = useSWR<{
- [key in CompositeAnalyticsResponseOptions]: number;
+ [key in AnalyticsResponseOptions]: number;
}>(
`${baseApiPath}?${editQueryString(queryString, {
event: demoPage || conversionEnabled ? "composite" : "clicks",
diff --git a/apps/web/ui/analytics/events/events-tabs.tsx b/apps/web/ui/analytics/events/events-tabs.tsx
index 7098db2593..ce00267911 100644
--- a/apps/web/ui/analytics/events/events-tabs.tsx
+++ b/apps/web/ui/analytics/events/events-tabs.tsx
@@ -1,4 +1,4 @@
-import { CompositeAnalyticsResponseOptions } from "@/lib/analytics/types";
+import { AnalyticsResponseOptions } from "@/lib/analytics/types";
import { editQueryString } from "@/lib/analytics/utils";
import useWorkspace from "@/lib/swr/use-workspace";
import { MiniAreaChart } from "@dub/blocks";
@@ -29,7 +29,7 @@ export default function EventsTabs() {
useContext(AnalyticsContext);
const { data: totalEvents, isLoading: isLoadingTotalEvents } = useSWR<{
- [key in CompositeAnalyticsResponseOptions]: number;
+ [key in AnalyticsResponseOptions]: number;
}>(
`${baseApiPath}?${editQueryString(queryString, {
event: demoPage || conversionEnabled ? "composite" : "clicks",