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.

fix analytics API

Steven Teyc78f89a9866c
diff --git a/apps/docs/api-reference/endpoint/retrieve-clicks-analytics.mdx b/apps/docs/api-reference/endpoint/retrieve-clicks-analytics.mdx
new file mode 100644
index 0000000000..07c0507a67
--- /dev/null
+++ b/apps/docs/api-reference/endpoint/retrieve-clicks-analytics.mdx
@@ -0,0 +1,9 @@
+---
+title: Retrieve Clicks
+openapi: get /analytics/clicks
+---
+
+<Note>
+ Analytics endpoints require a [Pro plan](/help/article/pro-plan) subscription
+ or higher.
+</Note>
diff --git a/apps/docs/api-reference/endpoint/retrieve-timeseries-analytics.mdx b/apps/docs/api-reference/endpoint/retrieve-timeseries-analytics.mdx
new file mode 100644
index 0000000000..3c2d88cf45
--- /dev/null
+++ b/apps/docs/api-reference/endpoint/retrieve-timeseries-analytics.mdx
@@ -0,0 +1,9 @@
+---
+title: Retrieve Timeseries
+openapi: get /analytics/timeseries
+---
+
+<Note>
+ Analytics endpoints require a [Pro plan](/help/article/pro-plan) subscription
+ or higher.
+</Note>
diff --git a/apps/web/app/api/analytics/[endpoint]/route.ts b/apps/web/app/api/analytics/[endpoint]/route.ts
index 723351143d..46dd0fc3c0 100644
--- a/apps/web/app/api/analytics/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/[endpoint]/route.ts
@@ -1,10 +1,10 @@
import { getAnalytics } from "@/lib/analytics";
-import { withAuth } from "@/lib/auth";
import { DubApiError } from "@/lib/api/errors";
+import { withAuth } from "@/lib/auth";
import { getDomainViaEdge } from "@/lib/planetscale";
import {
- getAnalyticsQuerySchema,
analyticsEndpointSchema,
+ getAnalyticsQuerySchema,
} from "@/lib/zod/schemas/analytics";
import { NextResponse } from "next/server";
diff --git a/apps/web/lib/auth/index.ts b/apps/web/lib/auth/index.ts
index 8ce08ed82e..77d8683eff 100644
--- a/apps/web/lib/auth/index.ts
+++ b/apps/web/lib/auth/index.ts
@@ -1,3 +1,4 @@
+import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import prisma from "@/lib/prisma";
import { API_DOMAIN, getSearchParams, isDubDomain } from "@dub/utils";
import { Link as LinkProps } from "@prisma/client";
@@ -5,7 +6,6 @@ import { isAdmin } from "app/admin.dub.co/actions";
import { createHash } from "crypto";
import { getServerSession } from "next-auth/next";
import { exceededLimitError } from "../api/errors";
-import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import { PlanProps, ProjectProps } from "../types";
import { ratelimit } from "../upstash";
import { authOptions } from "./options";
@@ -138,15 +138,6 @@ export const withAuth = (
}
if (apiKey) {
- const url = new URL(req.url || "", API_DOMAIN);
-
- if (url.pathname.includes("/analytics")) {
- throw new DubApiError({
- code: "forbidden",
- message: "API access is not available for Analytics yet.",
- });
- }
-
const hashedKey = hashToken(apiKey, {
noSecret: true,
});
@@ -376,6 +367,19 @@ export const withAuth = (
});
}
+ // analytics API checks
+ const url = new URL(req.url || "", API_DOMAIN);
+ if (
+ project.plan === "free" &&
+ apiKey &&
+ url.pathname.includes("/analytics")
+ ) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: "Analytics API is only available on paid plans.",
+ });
+ }
+
// link checks (if linkId or domain and key are provided)
if ((linkId || (domain && key && key !== "_root")) && !skipLinkChecks) {
// special case for getting domain by ID