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 analytics endpoints to openapi

Steven Tey42f577c4e752
diff --git a/apps/docs/mint.json b/apps/docs/mint.json
index d63bd8ee05..56db7053cd 100644
--- a/apps/docs/mint.json
+++ b/apps/docs/mint.json
@@ -29,7 +29,7 @@
"name": "Dashboard",
"url": "https://app.dub.co"
},
- "openapi": "https://api-staging.dub.co",
+ "openapi": "https://api.dub.co",
"tabs": [
{
"name": "API Reference",
diff --git a/apps/web/lib/openapi/analytics/browser.ts b/apps/web/lib/openapi/analytics/browser.ts
new file mode 100644
index 0000000000..ec7fc2f3e8
--- /dev/null
+++ b/apps/web/lib/openapi/analytics/browser.ts
@@ -0,0 +1,34 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import z from "@/lib/zod";
+import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+
+export const getBrowserAnalytics: ZodOpenApiOperationObject = {
+ operationId: "getBrowserAnalytics",
+ summary: "Retrieve browser analytics",
+ description:
+ "Retrieve the top browsers by number of clicks for a link, a domain, or the authenticated project.",
+ requestParams: {
+ query: getAnalyticsQuerySchema,
+ },
+ responses: {
+ "200": {
+ description: "The top browsers by number of clicks",
+ content: {
+ "application/json": {
+ schema: z.array(
+ z.object({
+ browser: z.string().describe("The name of the browser"),
+ clicks: z
+ .number()
+ .describe("The number of clicks from this browser"),
+ }),
+ ),
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Analytics"],
+ security: [{ bearerToken: [] }],
+};
diff --git a/apps/web/lib/openapi/analytics/city.ts b/apps/web/lib/openapi/analytics/city.ts
new file mode 100644
index 0000000000..6d17db4734
--- /dev/null
+++ b/apps/web/lib/openapi/analytics/city.ts
@@ -0,0 +1,40 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import z from "@/lib/zod";
+import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { COUNTRY_CODES } from "@dub/utils";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+
+export const getCityAnalytics: ZodOpenApiOperationObject = {
+ operationId: "getCityAnalytics",
+ summary: "Retrieve city analytics",
+ description:
+ "Retrieve the top countries by number of clicks for a link, a domain, or the authenticated project.",
+ requestParams: {
+ query: getAnalyticsQuerySchema,
+ },
+ responses: {
+ "200": {
+ description: "The top cities by number of clicks",
+ content: {
+ "application/json": {
+ schema: z.array(
+ 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"),
+ }),
+ ),
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Analytics"],
+ security: [{ bearerToken: [] }],
+};
diff --git a/apps/web/lib/openapi/analytics/clicks.ts b/apps/web/lib/openapi/analytics/clicks.ts
index 2e454b3d5e..45146175a1 100644
--- a/apps/web/lib/openapi/analytics/clicks.ts
+++ b/apps/web/lib/openapi/analytics/clicks.ts
@@ -2,7 +2,7 @@ import { openApiErrorResponses } from "@/lib/openapi/responses";
import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
import { ZodOpenApiOperationObject } from "zod-openapi";
-export const getClickAnalytics: ZodOpenApiOperationObject = {
+export const getClicksAnalytics: ZodOpenApiOperationObject = {
operationId: "getClicksAnalytics",
summary: "Retrieve clicks analytics",
description:
diff --git a/apps/web/lib/openapi/analytics/country.ts b/apps/web/lib/openapi/analytics/country.ts
index e69de29bb2..9f6a09a718 100644
--- a/apps/web/lib/openapi/analytics/country.ts
+++ b/apps/web/lib/openapi/analytics/country.ts
@@ -0,0 +1,37 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import z from "@/lib/zod";
+import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { COUNTRY_CODES } from "@dub/utils";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+
+export const getCountryAnalytics: ZodOpenApiOperationObject = {
+ operationId: "getCountryAnalytics",
+ summary: "Retrieve country analytics",
+ description:
+ "Retrieve the top countries by number of clicks for a link, a domain, or the authenticated project.",
+ requestParams: {
+ query: getAnalyticsQuerySchema,
+ },
+ responses: {
+ "200": {
+ description: "The top countries by number of clicks",
+ content: {
+ "application/json": {
+ schema: z.array(
+ z.object({
+ country: z
+ .enum(COUNTRY_CODES)
+ .describe("The 2-letter country code: https://d.to/geo"),
+ clicks: z
+ .number()
+ .describe("The number of clicks from this country"),
+ }),
+ ),
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Analytics"],
+ security: [{ bearerToken: [] }],
+};
diff --git a/apps/web/lib/openapi/analytics/device.ts b/apps/web/lib/openapi/analytics/device.ts
new file mode 100644
index 0000000000..65dc0835a9
--- /dev/null
+++ b/apps/web/lib/openapi/analytics/device.ts
@@ -0,0 +1,34 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import z from "@/lib/zod";
+import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+
+export const getDeviceAnalytics: ZodOpenApiOperationObject = {
+ operationId: "getDeviceAnalytics",
+ summary: "Retrieve device analytics",
+ description:
+ "Retrieve the top devices by number of clicks for a link, a domain, or the authenticated project.",
+ requestParams: {
+ query: getAnalyticsQuerySchema,
+ },
+ responses: {
+ "200": {
+ description: "The top devices by number of clicks",
+ content: {
+ "application/json": {
+ schema: z.array(
+ z.object({
+ device: z.string().describe("The name of the device"),
+ clicks: z
+ .number()
+ .describe("The number of clicks from this device"),
+ }),
+ ),
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Analytics"],
+ security: [{ bearerToken: [] }],
+};
diff --git a/apps/web/lib/openapi/analytics/index.ts b/apps/web/lib/openapi/analytics/index.ts
index 27c5c7b203..fa1b75d8eb 100644
--- a/apps/web/lib/openapi/analytics/index.ts
+++ b/apps/web/lib/openapi/analytics/index.ts
@@ -1,13 +1,45 @@
import { ZodOpenApiPathsObject } from "zod-openapi";
-import { getClickAnalytics } from "./clicks";
+import { getBrowserAnalytics } from "./browser";
+import { getCityAnalytics } from "./city";
+import { getClicksAnalytics } from "./clicks";
+import { getCountryAnalytics } from "./country";
+import { getDeviceAnalytics } from "./device";
+import { getOSAnalytics } from "./os";
+import { getRefererAnalytics } from "./referer";
import { getTimeseriesAnalytics } from "./timeseries";
+import { getTopLinks } from "./top-links";
+import { getTopURLs } from "./top-urls";
export const analyticsPaths: ZodOpenApiPathsObject = {
"/analytics/clicks": {
- get: getClickAnalytics,
+ get: getClicksAnalytics,
},
"/analytics/timeseries": {
get: getTimeseriesAnalytics,
},
+ "/analytics/country": {
+ get: getCountryAnalytics,
+ },
+ "/analytics/city": {
+ get: getCityAnalytics,
+ },
+ "/analytics/device": {
+ get: getDeviceAnalytics,
+ },
+ "/analytics/browser": {
+ get: getBrowserAnalytics,
+ },
+ "/analytics/os": {
+ get: getOSAnalytics,
+ },
+ "/analytics/referer": {
+ get: getRefererAnalytics,
+ },
+ "/analytics/top_links": {
+ get: getTopLinks,
+ },
+ "/analytics/top_urls": {
+ get: getTopURLs,
+ },
};
diff --git a/apps/web/lib/openapi/analytics/os.ts b/apps/web/lib/openapi/analytics/os.ts
new file mode 100644
index 0000000000..fcc381fd66
--- /dev/null
+++ b/apps/web/lib/openapi/analytics/os.ts
@@ -0,0 +1,32 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import z from "@/lib/zod";
+import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+
+export const getOSAnalytics: ZodOpenApiOperationObject = {
+ operationId: "getOSAnalytics",
+ summary: "Retrieve OS analytics",
+ description:
+ "Retrieve the top OS by number of clicks for a link, a domain, or the authenticated project.",
+ requestParams: {
+ query: getAnalyticsQuerySchema,
+ },
+ responses: {
+ "200": {
+ description: "The top OS by number of clicks",
+ content: {
+ "application/json": {
+ schema: z.array(
+ z.object({
+ os: z.string().describe("The name of the OS"),
+ clicks: z.number().describe("The number of clicks from this OS"),
+ }),
+ ),
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Analytics"],
+ security: [{ bearerToken: [] }],
+};
diff --git a/apps/web/lib/openapi/analytics/referer.ts b/apps/web/lib/openapi/analytics/referer.ts
new file mode 100644
index 0000000000..df288f38a3
--- /dev/null
+++ b/apps/web/lib/openapi/analytics/referer.ts
@@ -0,0 +1,38 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import z from "@/lib/zod";
+import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+
+export const getRefererAnalytics: ZodOpenApiOperationObject = {
+ operationId: "getRefererAnalytics",
+ summary: "Retrieve referer analytics",
+ description:
+ "Retrieve the top referers by number of clicks for a link, a domain, or the authenticated project.",
+ requestParams: {
+ query: getAnalyticsQuerySchema,
+ },
+ responses: {
+ "200": {
+ description: "The top referers by number of clicks",
+ content: {
+ "application/json": {
+ schema: z.array(
+ 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"),
+ }),
+ ),
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Analytics"],
+ security: [{ bearerToken: [] }],
+};
diff --git a/apps/web/lib/openapi/analytics/top-links.ts b/apps/web/lib/openapi/analytics/top-links.ts
new file mode 100644
index 0000000000..dc3559969c
--- /dev/null
+++ b/apps/web/lib/openapi/analytics/top-links.ts
@@ -0,0 +1,34 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import z from "@/lib/zod";
+import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+
+export const getTopLinks: ZodOpenApiOperationObject = {
+ operationId: "getTopLinks",
+ summary: "Retrieve top links",
+ description:
+ "Retrieve the top links by number of clicks for a domain or the authenticated project.",
+ requestParams: {
+ query: getAnalyticsQuerySchema,
+ },
+ responses: {
+ "200": {
+ description: "The top links by number of clicks",
+ content: {
+ "application/json": {
+ schema: z.array(
+ z.object({
+ link: z.string().describe("The unique ID of the short link"),
+ clicks: z
+ .number()
+ .describe("The number of clicks from this link"),
+ }),
+ ),
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Analytics"],
+ security: [{ bearerToken: [] }],
+};
diff --git a/apps/web/lib/openapi/analytics/top-urls.ts b/apps/web/lib/openapi/analytics/top-urls.ts
new file mode 100644
index 0000000000..1d54467903
--- /dev/null
+++ b/apps/web/lib/openapi/analytics/top-urls.ts
@@ -0,0 +1,32 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import z from "@/lib/zod";
+import { getAnalyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+
+export const getTopURLs: ZodOpenApiOperationObject = {
+ operationId: "getTopURLs",
+ summary: "Retrieve top URLs",
+ description:
+ "Retrieve the top URLs by number of clicks for a given short link.",
+ requestParams: {
+ query: getAnalyticsQuerySchema,
+ },
+ responses: {
+ "200": {
+ description: "The top URLs by number of clicks",
+ content: {
+ "application/json": {
+ schema: z.array(
+ z.object({
+ url: z.string().describe("The destination URL"),
+ clicks: z.number().describe("The number of clicks from this URL"),
+ }),
+ ),
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Analytics"],
+ security: [{ bearerToken: [] }],
+};
diff --git a/apps/web/lib/openapi/index.ts b/apps/web/lib/openapi/index.ts
index 176e78f51b..23506ae40d 100644
--- a/apps/web/lib/openapi/index.ts
+++ b/apps/web/lib/openapi/index.ts
@@ -34,8 +34,8 @@ export const openApiObject: ZodOpenApiObject = {
},
],
paths: {
- ...analyticsPaths,
...linksPaths,
+ ...analyticsPaths,
...qrCodePaths,
...projectsPaths,
...tagsPaths,
diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.ts
index ff074ae347..e36ed9af40 100644
--- a/apps/web/lib/zod/schemas/analytics.ts
+++ b/apps/web/lib/zod/schemas/analytics.ts
@@ -1,9 +1,7 @@
import { intervals, VALID_TINYBIRD_ENDPOINTS } from "@/lib/analytics";
import z from "@/lib/zod";
+import { COUNTRY_CODES } from "@dub/utils";
import { booleanQuerySchema } from ".";
-import { COUNTRIES } from "@dub/utils";
-
-const countryCodes = Object.keys(COUNTRIES) as [string, ...string[]];
export const getAnalyticsQuerySchema = z.object({
projectSlug: z
@@ -19,7 +17,7 @@ export const getAnalyticsQuerySchema = z.object({
.optional()
.describe("The interval to retrieve analytics for."),
country: z
- .enum(countryCodes)
+ .enum(COUNTRY_CODES)
.optional()
.describe("The country to retrieve analytics for."),
city: z.string().optional().describe("The city to retrieve analytics for."),
diff --git a/packages/utils/src/constants/countries.ts b/packages/utils/src/constants/countries.ts
index 73ec955ee8..b9921ed091 100644
--- a/packages/utils/src/constants/countries.ts
+++ b/packages/utils/src/constants/countries.ts
@@ -250,3 +250,5 @@ export const COUNTRIES: { [key: string]: string } = {
SS: "South Sudan",
XK: "Kosovo",
};
+
+export const COUNTRY_CODES = Object.keys(COUNTRIES) as [string, ...string[]];