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.

support backward compatibility for analytics API

Kiran K1bfa7ebe9c44
diff --git a/apps/web/app/api/analytics/[eventType]/route.ts b/apps/web/app/api/analytics/[eventType]/route.ts
index 756a27cb54..baefbb6671 100644
--- a/apps/web/app/api/analytics/[eventType]/route.ts
+++ b/apps/web/app/api/analytics/[eventType]/route.ts
@@ -1 +1,67 @@
-export * from "../route";
+import { DEPRECATED_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
+import { getAnalytics } from "@/lib/analytics/get-analytics";
+import { validDateRangeForPlan } from "@/lib/analytics/utils";
+import { withWorkspace } from "@/lib/auth";
+import { getDomainViaEdge } from "@/lib/planetscale";
+import z from "@/lib/zod";
+import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
+import { NextResponse } from "next/server";
+
+const schema = z.object({
+ eventType: z
+ .enum(DEPRECATED_ANALYTICS_ENDPOINTS, {
+ errorMap: (_issue, _ctx) => {
+ return {
+ message: `Invalid endpoint value. Valid values are: ${DEPRECATED_ANALYTICS_ENDPOINTS.join(", ")}`,
+ };
+ },
+ })
+ .describe("The parameter to group the analytics data points by."),
+});
+
+// GET /api/analytics/{endpoint} – get analytics (Deprecated)
+export const GET = withWorkspace(
+ async ({ params, searchParams, workspace, link }) => {
+ let { eventType: endpoint } = schema.parse(params);
+ const parsedParams = analyticsQuerySchema.parse(searchParams);
+
+ const { domain, key, interval, start, end } = parsedParams;
+
+ validDateRangeForPlan({
+ plan: workspace.plan,
+ interval,
+ start,
+ end,
+ throwError: true,
+ });
+
+ const linkId = link
+ ? link.id
+ : domain && key === "_root"
+ ? await getDomainViaEdge(domain).then((d) => d?.id)
+ : null;
+
+ const oldSingularEndpoints = {
+ country: "countries",
+ city: "cities",
+ device: "devices",
+ browser: "browsers",
+ referer: "referers",
+ clicks: "count",
+ };
+
+ const response = await getAnalytics({
+ ...parsedParams,
+ ...(linkId && { linkId }),
+ event: "clicks",
+ groupBy: oldSingularEndpoints[endpoint] || endpoint,
+ workspaceId: workspace.id,
+ isDeprecatedEndpoint: true,
+ });
+
+ return NextResponse.json(response);
+ },
+ {
+ needNotExceededClicks: true,
+ },
+);
diff --git a/apps/web/lib/analytics/constants.ts b/apps/web/lib/analytics/constants.ts
index 7ec9f9a9ea..ee13569ede 100644
--- a/apps/web/lib/analytics/constants.ts
+++ b/apps/web/lib/analytics/constants.ts
@@ -120,3 +120,29 @@ export const VALID_ANALYTICS_FILTERS = [
];
export const EVENT_TYPES = ["clicks", "leads", "sales", "composite"] as const;
+
+export const DEPRECATED_ANALYTICS_ENDPOINTS = [
+ "clicks",
+ "timeseries",
+ "country",
+ "city",
+ "device",
+ "browser",
+ "os",
+ "referer",
+ "top_links",
+ "top_urls",
+] as const;
+
+export const OLD_TO_NEW_ANALYTICS_ENDPOINTS = {
+ clicks: "count",
+ timeseries: "timeseries",
+ country: "countries",
+ city: "cities",
+ device: "devices",
+ browser: "browsers",
+ os: "os",
+ referer: "referers",
+ top_links: "top_links",
+ top_urls: "top_urls",
+};
diff --git a/apps/web/tests/analytics/clicks.test.ts b/apps/web/tests/analytics/clicks.test.ts
index d3c031af3a..b7f37caed0 100644
--- a/apps/web/tests/analytics/clicks.test.ts
+++ b/apps/web/tests/analytics/clicks.test.ts
@@ -1,4 +1,8 @@
-import { VALID_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
+import {
+ DEPRECATED_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";
@@ -71,3 +75,36 @@ describe.runIf(env.CI).sequential("GET /analytics/clicks", async () => {
expect(clicks).toBeGreaterThanOrEqual(0);
});
});
+
+// 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;
+
+ DEPRECATED_ANALYTICS_ENDPOINTS.map((endpoint) => {
+ test(`/analytics/${endpoint}`, async () => {
+ const { status, data } = await http.get<any[]>({
+ path: `/analytics/${endpoint}`,
+ query: { workspaceId, ...filter },
+ });
+
+ const analyticsEndpoint =
+ OLD_TO_NEW_ANALYTICS_ENDPOINTS[endpoint] || endpoint;
+
+ expect(status).toEqual(200);
+
+ if (analyticsEndpoint === "count") {
+ expect(data).toEqual(expect.any(Number));
+ expect(data).toBeGreaterThanOrEqual(0);
+ } else {
+ const parsed = z
+ .array(clickAnalyticsResponse[analyticsEndpoint].strict())
+ .safeParse(data);
+
+ expect(data.length).toBeGreaterThanOrEqual(0);
+ expect(parsed.success).toBeTruthy();
+ }
+ });
+ });
+});