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.

use analytics/clicks/[endpoint] instead

Steven Tey0f3422324c8b
diff --git a/apps/docs/snippets/analytics-arguments.mdx b/apps/docs/snippets/analytics-arguments.mdx
index 604b7c8511..a11cbcc14b 100644
--- a/apps/docs/snippets/analytics-arguments.mdx
+++ b/apps/docs/snippets/analytics-arguments.mdx
@@ -1,12 +1,5 @@
## Arguments
-<ParamField body="groupBy" type="enum<string>">
- The field to group the analytics by. If undefined, returns the total click count.
-
-Available options are `timeseries`, `country`, `city`, `device`, `browser`, `os`, `referer`, `top_links`, and `top_urls`.
-
-</ParamField>
-
<ParamField body="domain" type="string">
The domain of the short link.
</ParamField>
diff --git a/apps/web/app/api/admin/analytics/clicks/[endpoint]/route.ts b/apps/web/app/api/admin/analytics/clicks/[endpoint]/route.ts
new file mode 100644
index 0000000000..756a27cb54
--- /dev/null
+++ b/apps/web/app/api/admin/analytics/clicks/[endpoint]/route.ts
@@ -0,0 +1 @@
+export * from "../route";
diff --git a/apps/web/app/api/admin/analytics/clicks/route.ts b/apps/web/app/api/admin/analytics/clicks/route.ts
index 3097edf7e4..c18f1f3585 100644
--- a/apps/web/app/api/admin/analytics/clicks/route.ts
+++ b/apps/web/app/api/admin/analytics/clicks/route.ts
@@ -1,13 +1,20 @@
import { getClicks } from "@/lib/analytics";
import { withAdmin } from "@/lib/auth";
-import { clickAnalyticsQuerySchema } from "@/lib/zod/schemas";
+import {
+ analyticsEndpointSchema,
+ clickAnalyticsQuerySchema,
+} from "@/lib/zod/schemas";
import { NextResponse } from "next/server";
// GET /api/admin/analytics/clicks – get click analytics for admin
-export const GET = withAdmin(async ({ searchParams }) => {
+export const GET = withAdmin(async ({ params, searchParams }) => {
+ const { endpoint } = analyticsEndpointSchema.parse(params);
const parsedParams = clickAnalyticsQuerySchema.parse(searchParams);
- const response = await getClicks(parsedParams);
+ const response = await getClicks({
+ ...parsedParams,
+ endpoint,
+ });
return NextResponse.json(response);
});
diff --git a/apps/web/app/api/analytics/[groupBy]/route.ts b/apps/web/app/api/analytics/[endpoint]/route.ts
similarity index 100%
rename from apps/web/app/api/analytics/[groupBy]/route.ts
rename to apps/web/app/api/analytics/[endpoint]/route.ts
diff --git a/apps/web/app/api/analytics/clicks/[endpoint]/route.ts b/apps/web/app/api/analytics/clicks/[endpoint]/route.ts
new file mode 100644
index 0000000000..756a27cb54
--- /dev/null
+++ b/apps/web/app/api/analytics/clicks/[endpoint]/route.ts
@@ -0,0 +1 @@
+export * from "../route";
diff --git a/apps/web/app/api/analytics/clicks/route.ts b/apps/web/app/api/analytics/clicks/route.ts
index 88f320e405..d2ba09ca3d 100644
--- a/apps/web/app/api/analytics/clicks/route.ts
+++ b/apps/web/app/api/analytics/clicks/route.ts
@@ -1,17 +1,19 @@
import { getClicks, validDateRangeForPlan } from "@/lib/analytics";
import { withWorkspace } from "@/lib/auth";
import { getDomainViaEdge } from "@/lib/planetscale";
-import { clickAnalyticsQuerySchema } from "@/lib/zod/schemas";
+import {
+ analyticsEndpointSchema,
+ clickAnalyticsQuerySchema,
+} from "@/lib/zod/schemas";
import { NextResponse } from "next/server";
// GET /api/analytics/clicks – get click analytics
export const GET = withWorkspace(
async ({ params, searchParams, workspace, link }) => {
- let { groupBy: oldGroupBy } = clickAnalyticsQuerySchema.parse(params);
+ let { endpoint } = analyticsEndpointSchema.parse(params);
const parsedParams = clickAnalyticsQuerySchema.parse(searchParams);
-
- let { groupBy, domain, key, interval, start, end } = parsedParams;
+ let { domain, key, interval, start, end } = parsedParams;
validDateRangeForPlan({
plan: workspace.plan,
@@ -29,7 +31,7 @@ export const GET = withWorkspace(
const response = await getClicks({
...parsedParams,
- groupBy: groupBy || oldGroupBy,
+ endpoint,
...(linkId && { linkId }),
workspaceId: workspace.id,
});
diff --git a/apps/web/lib/analytics/clicks.ts b/apps/web/lib/analytics/clicks.ts
index cbc57bae23..81b4372388 100644
--- a/apps/web/lib/analytics/clicks.ts
+++ b/apps/web/lib/analytics/clicks.ts
@@ -2,7 +2,7 @@ import { conn } from "@/lib/planetscale";
import { tb } from "@/lib/tinybird";
import z from "@/lib/zod";
import { getDaysDifference } from "@dub/utils";
-import { intervalData } from ".";
+import { AnalyticsEndpoints, intervalData } from ".";
import {
clickAnalyticsQuerySchema,
getClickAnalytics,
@@ -12,15 +12,16 @@ import {
export const getClicks = async (
props: z.infer<typeof clickAnalyticsQuerySchema> & {
workspaceId?: string;
+ endpoint: AnalyticsEndpoints;
},
) => {
- let { workspaceId, groupBy, linkId, interval, start, end } = props;
+ let { workspaceId, endpoint, linkId, interval, start, end } = props;
// get all-time clicks count if:
// 1. linkId is defined
- // 2. groupBy is not defined
+ // 2. endpoint is not defined
// 3. interval is all time
- if (linkId && !groupBy && interval === "all") {
+ if (linkId && !endpoint && interval === "all") {
let response = await conn.execute(
"SELECT clicks FROM Link WHERE `id` = ?",
[linkId],
@@ -39,9 +40,9 @@ export const getClicks = async (
}
const pipe = tb.buildPipe({
- pipe: groupBy || "clicks",
+ pipe: endpoint || "clicks",
parameters: getClickAnalytics,
- data: getClickAnalyticsResponse[groupBy || "clicks"],
+ data: getClickAnalyticsResponse[endpoint || "clicks"],
});
let granularity: "minute" | "hour" | "day" | "month" = "day";
@@ -73,7 +74,7 @@ export const getClicks = async (
}),
);
- // for total clicks (groupBy undefined), we return just the value;
+ // for total clicks (endpoint undefined), we return just the value;
// everything else we return an array of values
- return groupBy ? res.data : res.data[0];
+ return endpoint ? res.data : res.data[0];
};
diff --git a/apps/web/lib/analytics/types.ts b/apps/web/lib/analytics/types.ts
index a202321f90..b1d12796b1 100644
--- a/apps/web/lib/analytics/types.ts
+++ b/apps/web/lib/analytics/types.ts
@@ -1,6 +1,17 @@
import { intervals } from "./constants";
export type IntervalProps = (typeof intervals)[number];
+export type AnalyticsEndpoints =
+ | undefined
+ | "timeseries"
+ | "country"
+ | "city"
+ | "device"
+ | "browser"
+ | "os"
+ | "referer"
+ | "top_links"
+ | "top_urls";
export type LocationTabs = "country" | "city";
export type TopLinksTabs = "link" | "url";
export type DeviceTabs = "device" | "browser" | "os" | "ua";
diff --git a/apps/web/lib/openapi/analytics/browser.ts b/apps/web/lib/openapi/analytics/browser.ts
index be60f9badc..b3e9abc45b 100644
--- a/apps/web/lib/openapi/analytics/browser.ts
+++ b/apps/web/lib/openapi/analytics/browser.ts
@@ -11,13 +11,10 @@ export const getBrowserAnalytics: ZodOpenApiOperationObject = {
operationId: "getBrowserAnalytics",
"x-speakeasy-name-override": "browsers",
summary: "Retrieve browser analytics",
- deprecated: true,
description:
- "Deprecated: Use dub.anlaytics.clicks({ groupBy: 'browser' }) instead. Retrieve the top browsers by number of clicks for a link, a domain, or the authenticated workspace.",
+ "Retrieve the top browsers by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceParamsSchema.merge(
- clickAnalyticsQuerySchema.omit({ groupBy: true }),
- ),
+ query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/city.ts b/apps/web/lib/openapi/analytics/city.ts
index 9e87bd82c2..f1697f5dec 100644
--- a/apps/web/lib/openapi/analytics/city.ts
+++ b/apps/web/lib/openapi/analytics/city.ts
@@ -11,13 +11,10 @@ export const getCityAnalytics: ZodOpenApiOperationObject = {
operationId: "getCityAnalytics",
"x-speakeasy-name-override": "cities",
summary: "Retrieve city analytics",
- deprecated: true,
description:
- "Deprecated: Use dub.anlaytics.clicks({ groupBy: 'city' }) instead. Retrieve the top countries by number of clicks for a link, a domain, or the authenticated workspace.",
+ "Retrieve the top countries by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceParamsSchema.merge(
- clickAnalyticsQuerySchema.omit({ groupBy: true }),
- ),
+ query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/country.ts b/apps/web/lib/openapi/analytics/country.ts
index 9ca8c8123f..20790f66b8 100644
--- a/apps/web/lib/openapi/analytics/country.ts
+++ b/apps/web/lib/openapi/analytics/country.ts
@@ -11,13 +11,10 @@ export const getCountryAnalytics: ZodOpenApiOperationObject = {
operationId: "getCountryAnalytics",
"x-speakeasy-name-override": "countries",
summary: "Retrieve country analytics",
- deprecated: true,
description:
- "Deprecated: Use dub.anlaytics.clicks({ groupBy: 'country' }) instead. Retrieve the top countries by number of clicks for a link, a domain, or the authenticated workspace.",
+ "Retrieve the top countries by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceParamsSchema.merge(
- clickAnalyticsQuerySchema.omit({ groupBy: true }),
- ),
+ query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/device.ts b/apps/web/lib/openapi/analytics/device.ts
index b8b334ed35..666db076dc 100644
--- a/apps/web/lib/openapi/analytics/device.ts
+++ b/apps/web/lib/openapi/analytics/device.ts
@@ -11,13 +11,10 @@ export const getDeviceAnalytics: ZodOpenApiOperationObject = {
operationId: "getDeviceAnalytics",
"x-speakeasy-name-override": "devices",
summary: "Retrieve device analytics",
- deprecated: true,
description:
- "Deprecated: Use dub.anlaytics.clicks({ groupBy: 'device' }) instead. Retrieve the top devices by number of clicks for a link, a domain, or the authenticated workspace.",
+ "Retrieve the top devices by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceParamsSchema.merge(
- clickAnalyticsQuerySchema.omit({ groupBy: true }),
- ),
+ query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/index.ts b/apps/web/lib/openapi/analytics/index.ts
index 2f1a516701..b3651d023a 100644
--- a/apps/web/lib/openapi/analytics/index.ts
+++ b/apps/web/lib/openapi/analytics/index.ts
@@ -14,31 +14,31 @@ export const analyticsPaths: ZodOpenApiPathsObject = {
"/analytics/clicks": {
get: getClicksAnalytics,
},
- "/analytics/timeseries": {
+ "/analytics/clicks/timeseries": {
get: getTimeseriesAnalytics,
},
- "/analytics/country": {
+ "/analytics/clicks/country": {
get: getCountryAnalytics,
},
- "/analytics/city": {
+ "/analytics/clicks/city": {
get: getCityAnalytics,
},
- "/analytics/device": {
+ "/analytics/clicks/device": {
get: getDeviceAnalytics,
},
- "/analytics/browser": {
+ "/analytics/clicks/browser": {
get: getBrowserAnalytics,
},
- "/analytics/os": {
+ "/analytics/clicks/os": {
get: getOSAnalytics,
},
- "/analytics/referer": {
+ "/analytics/clicks/referer": {
get: getRefererAnalytics,
},
- "/analytics/top_links": {
+ "/analytics/clicks/top_links": {
get: getTopLinks,
},
- "/analytics/top_urls": {
+ "/analytics/clicks/top_urls": {
get: getTopURLs,
},
};
diff --git a/apps/web/lib/openapi/analytics/os.ts b/apps/web/lib/openapi/analytics/os.ts
index a7c05c5c4f..4b723acae3 100644
--- a/apps/web/lib/openapi/analytics/os.ts
+++ b/apps/web/lib/openapi/analytics/os.ts
@@ -11,13 +11,10 @@ export const getOSAnalytics: ZodOpenApiOperationObject = {
operationId: "getOSAnalytics",
"x-speakeasy-name-override": "os",
summary: "Retrieve OS analytics",
- deprecated: true,
description:
- "Deprecated: Use dub.anlaytics.clicks({ groupBy: 'os' }) instead. Retrieve the top OS by number of clicks for a link, a domain, or the authenticated workspace.",
+ "Retrieve the top OS by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceParamsSchema.merge(
- clickAnalyticsQuerySchema.omit({ groupBy: true }),
- ),
+ query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/referer.ts b/apps/web/lib/openapi/analytics/referer.ts
index b05bbb162b..3773734f40 100644
--- a/apps/web/lib/openapi/analytics/referer.ts
+++ b/apps/web/lib/openapi/analytics/referer.ts
@@ -11,13 +11,10 @@ export const getRefererAnalytics: ZodOpenApiOperationObject = {
operationId: "getRefererAnalytics",
"x-speakeasy-name-override": "referers",
summary: "Retrieve referer analytics",
- deprecated: true,
description:
- "Deprecated: Use dub.anlaytics.clicks({ groupBy: 'referer' }) instead. Retrieve the top referers by number of clicks for a link, a domain, or the authenticated workspace.",
+ "Retrieve the top referers by number of clicks for a link, a domain, or the authenticated workspace.",
requestParams: {
- query: workspaceParamsSchema.merge(
- clickAnalyticsQuerySchema.omit({ groupBy: true }),
- ),
+ query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/timeseries.ts b/apps/web/lib/openapi/analytics/timeseries.ts
index 53dda6023b..bd9071207e 100644
--- a/apps/web/lib/openapi/analytics/timeseries.ts
+++ b/apps/web/lib/openapi/analytics/timeseries.ts
@@ -11,13 +11,10 @@ export const getTimeseriesAnalytics: ZodOpenApiOperationObject = {
operationId: "getTimeseriesAnalytics",
"x-speakeasy-name-override": "timeseries",
summary: "Retrieve timeseries analytics",
- deprecated: true,
description:
- "Deprecated: Use dub.anlaytics.clicks({ groupBy: 'timeseries' }) instead. Retrieve the number of clicks for a link, a domain, or the authenticated workspace over a period of time.",
+ "Retrieve the number of clicks for a link, a domain, or the authenticated workspace over a period of time.",
requestParams: {
- query: workspaceParamsSchema.merge(
- clickAnalyticsQuerySchema.omit({ groupBy: true }),
- ),
+ query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/top-links.ts b/apps/web/lib/openapi/analytics/top-links.ts
index 80e5c9f5c8..9d90f13aac 100644
--- a/apps/web/lib/openapi/analytics/top-links.ts
+++ b/apps/web/lib/openapi/analytics/top-links.ts
@@ -11,13 +11,10 @@ export const getTopLinks: ZodOpenApiOperationObject = {
operationId: "getTopLinks",
"x-speakeasy-name-override": "topLinks",
summary: "Retrieve top links",
- deprecated: true,
description:
- "Deprecated: Use dub.anlaytics.clicks({ groupBy: 'top_links' }) instead. Retrieve the top links by number of clicks for a domain or the authenticated workspace.",
+ "Retrieve the top links by number of clicks for a domain or the authenticated workspace.",
requestParams: {
- query: workspaceParamsSchema.merge(
- clickAnalyticsQuerySchema.omit({ groupBy: true }),
- ),
+ query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/openapi/analytics/top-urls.ts b/apps/web/lib/openapi/analytics/top-urls.ts
index d78ecd5459..5b4d949571 100644
--- a/apps/web/lib/openapi/analytics/top-urls.ts
+++ b/apps/web/lib/openapi/analytics/top-urls.ts
@@ -11,13 +11,10 @@ export const getTopURLs: ZodOpenApiOperationObject = {
operationId: "getTopURLs",
"x-speakeasy-name-override": "topUrls",
summary: "Retrieve top URLs",
- deprecated: true,
description:
- "Deprecated: Use dub.anlaytics.clicks({ groupBy: 'top_urls' }) instead. Retrieve the top URLs by number of clicks for a given short link.",
+ "Retrieve the top URLs by number of clicks for a given short link.",
requestParams: {
- query: workspaceParamsSchema.merge(
- clickAnalyticsQuerySchema.omit({ groupBy: true }),
- ),
+ query: workspaceParamsSchema.merge(clickAnalyticsQuerySchema),
},
responses: {
"200": {
diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.ts
index c1ff720522..db6c1f51ad 100644
--- a/apps/web/lib/zod/schemas/analytics.ts
+++ b/apps/web/lib/zod/schemas/analytics.ts
@@ -4,12 +4,12 @@ import { COUNTRY_CODES } from "@dub/utils";
import { booleanQuerySchema } from "./misc";
import { parseDateSchema } from "./utils";
-export const clickAnalyticsQuerySchema = z.object({
- groupBy: z
+export const analyticsEndpointSchema = z.object({
+ endpoint: z
.enum(VALID_TINYBIRD_ENDPOINTS, {
errorMap: (_issue, _ctx) => {
return {
- message: `Invalid groupBy value. Valid endpoints are: ${VALID_TINYBIRD_ENDPOINTS.join(", ")}`,
+ message: `Invalid endpoint value. Valid endpoints are: ${VALID_TINYBIRD_ENDPOINTS.join(", ")}`,
};
},
})
@@ -17,6 +17,9 @@ export const clickAnalyticsQuerySchema = z.object({
.describe(
"The field to group the analytics by. If undefined, returns the total click count.",
),
+});
+
+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
diff --git a/apps/web/ui/analytics/clicks-chart.tsx b/apps/web/ui/analytics/clicks-chart.tsx
index d760c39ce4..c713232640 100644
--- a/apps/web/ui/analytics/clicks-chart.tsx
+++ b/apps/web/ui/analytics/clicks-chart.tsx
@@ -12,7 +12,7 @@ export default function ClicksChart() {
const { baseApiPath, queryString, interval } = useContext(AnalyticsContext);
const { data } = useSWR<{ start: Date; clicks: number }[]>(
- `${baseApiPath}?${queryString}&groupBy=timeseries`,
+ `${baseApiPath}/timeseries?${queryString}`,
fetcher,
);
diff --git a/apps/web/ui/analytics/devices.tsx b/apps/web/ui/analytics/devices.tsx
index a85911d839..374894edb6 100644
--- a/apps/web/ui/analytics/devices.tsx
+++ b/apps/web/ui/analytics/devices.tsx
@@ -17,7 +17,7 @@ export default function Devices() {
({
[key in DeviceTabs]: string;
} & { clicks: number })[]
- >(`${baseApiPath}?${queryString}&groupBy=${tab}`, fetcher);
+ >(`${baseApiPath}/${tab}?${queryString}`, fetcher);
const { queryParams } = useRouterStuff();
const [showModal, setShowModal] = useState(false);
diff --git a/apps/web/ui/analytics/locations.tsx b/apps/web/ui/analytics/locations.tsx
index 7aad6a1bea..c2e2607e17 100644
--- a/apps/web/ui/analytics/locations.tsx
+++ b/apps/web/ui/analytics/locations.tsx
@@ -13,7 +13,7 @@ export default function Locations() {
const { baseApiPath, queryString } = useContext(AnalyticsContext);
const { data } = useSWR<{ country: string; city: string; clicks: number }[]>(
- `${baseApiPath}?${queryString}&groupBy=${tab}`,
+ `${baseApiPath}/${tab}?${queryString}`,
fetcher,
);
diff --git a/apps/web/ui/analytics/referer.tsx b/apps/web/ui/analytics/referer.tsx
index 8ef4284691..53a4ef7ad9 100644
--- a/apps/web/ui/analytics/referer.tsx
+++ b/apps/web/ui/analytics/referer.tsx
@@ -10,7 +10,7 @@ export default function Referer() {
const { baseApiPath, queryString } = useContext(AnalyticsContext);
const { data } = useSWR<{ referer: string; clicks: number }[]>(
- `${baseApiPath}?${queryString}&groupBy=referer`,
+ `${baseApiPath}/referer?${queryString}`,
fetcher,
);
diff --git a/apps/web/ui/analytics/top-links.tsx b/apps/web/ui/analytics/top-links.tsx
index 624ac278cd..5a6244c57b 100644
--- a/apps/web/ui/analytics/top-links.tsx
+++ b/apps/web/ui/analytics/top-links.tsx
@@ -27,7 +27,7 @@ export default function TopLinks() {
({ domain: string; key: string } & {
[key in TopLinksTabs]: string;
} & { clicks: number })[]
- >(`${baseApiPath}?${queryString}&groupBy=top_${tab}s`, fetcher);
+ >(`${baseApiPath}/top_${tab}s?${queryString}`, fetcher);
const { queryParams } = useRouterStuff();
const searchParams = useSearchParams();