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 `dub.partners.analytics` method

Steven Tey0287dee07bd1
diff --git a/apps/web/app/api/partners/analytics/route.ts b/apps/web/app/api/partners/analytics/route.ts
new file mode 100644
index 0000000000..da3870b1d2
--- /dev/null
+++ b/apps/web/app/api/partners/analytics/route.ts
@@ -0,0 +1,65 @@
+import { getAnalytics } from "@/lib/analytics/get-analytics";
+import { DubApiError } from "@/lib/api/errors";
+import { withWorkspace } from "@/lib/auth";
+import { getPartnerAnalyticsSchema } from "@/lib/zod/schemas/partners";
+import { prisma } from "@dub/prisma";
+import { NextResponse } from "next/server";
+
+// GET /api/partners/analytics – get analytics for a partner
+export const GET = withWorkspace(async ({ workspace, searchParams }) => {
+ const {
+ groupBy,
+ event,
+ programId,
+ partnerId,
+ tenantId,
+ interval,
+ start,
+ end,
+ timezone,
+ } = getPartnerAnalyticsSchema.parse(searchParams);
+
+ if (!programId) {
+ throw new DubApiError({
+ code: "bad_request",
+ message:
+ "Program ID not found. Did you forget to include a `programId` query parameter?",
+ });
+ }
+
+ if (!partnerId && !tenantId) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "You must provide a partnerId or tenantId.",
+ });
+ }
+
+ const programEnrollment = await prisma.programEnrollment.findUniqueOrThrow({
+ where: partnerId
+ ? { partnerId_programId: { partnerId, programId } }
+ : { tenantId_programId: { tenantId: tenantId!, programId } },
+ include: {
+ program: true,
+ },
+ });
+
+ if (programEnrollment.program.workspaceId !== workspace.id) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "Program not found.",
+ });
+ }
+
+ const analytics = await getAnalytics({
+ groupBy,
+ event,
+ programId,
+ partnerId,
+ interval,
+ start,
+ end,
+ timezone,
+ });
+
+ return NextResponse.json(analytics);
+});
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index d05a20cbfb..e192bc08b8 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -6,6 +6,7 @@ import {
} from "@dub/prisma/client";
import { COUNTRY_CODES } from "@dub/utils";
import { z } from "zod";
+import { analyticsQuerySchema } from "./analytics";
import { CustomerSchema } from "./customers";
import { createLinkBodySchema } from "./links";
import { getPaginationQuerySchema } from "./misc";
@@ -273,3 +274,25 @@ export const createPartnerLinkSchema = z
linkProps: true,
}),
);
+
+export const getPartnerAnalyticsSchema = analyticsQuerySchema
+ .pick({
+ event: true,
+ programId: true,
+ partnerId: true,
+ tenantId: true,
+ interval: true,
+ start: true,
+ end: true,
+ timezone: true,
+ })
+ .merge(
+ z.object({
+ groupBy: z
+ .enum(["top_links", "timeseries", "count"])
+ .default("count")
+ .describe(
+ "The parameter to group the analytics data points by. Defaults to `count` if undefined.",
+ ),
+ }),
+ );