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 track/open to openapi

Steven Tey3408b42c1cdd
diff --git a/apps/web/app/(ee)/api/track/open/route.ts b/apps/web/app/(ee)/api/track/open/route.ts
index ad4fa1f884..0f27ad1093 100644
--- a/apps/web/app/(ee)/api/track/open/route.ts
+++ b/apps/web/app/(ee)/api/track/open/route.ts
@@ -7,12 +7,14 @@ import { getLinkViaEdge } from "@/lib/planetscale";
import { recordClick } from "@/lib/tinybird";
import { RedisLinkProps } from "@/lib/types";
import { formatRedisLink, redis } from "@/lib/upstash";
-import { parseUrlSchema } from "@/lib/zod/schemas/utils";
+import {
+ trackOpenRequestSchema,
+ trackOpenResponseSchema,
+} from "@/lib/zod/schemas/opens";
import { LOCALHOST_IP, nanoid } from "@dub/utils";
import { ipAddress, waitUntil } from "@vercel/functions";
import { AxiomRequest, withAxiom } from "next-axiom";
import { NextResponse } from "next/server";
-import { z } from "zod";
const CORS_HEADERS = {
"Access-Control-Allow-Origin": "*",
@@ -20,33 +22,6 @@ const CORS_HEADERS = {
"Access-Control-Allow-Headers": "Content-Type, Authorization",
};
-const trackOpenRequestSchema = z
- .object({
- deepLink: parseUrlSchema.optional(),
- dubDomain: z.string().optional(),
- })
- .superRefine((data, ctx) => {
- if (!data.deepLink && !data.dubDomain) {
- ctx.addIssue({
- code: z.ZodIssueCode.custom,
- message:
- "You need to provide either `deepLink` or `dubDomain` for deferred deep linking.",
- });
- }
- });
-
-const trackOpenResponseSchema = z.object({
- clickId: z.string().nullable(),
- link: z
- .object({
- id: z.string(),
- domain: z.string(),
- key: z.string(),
- url: z.string(),
- })
- .nullable(),
-});
-
// POST /api/track/open – Track an open event for deep link
export const POST = withAxiom(async (req: AxiomRequest) => {
try {
diff --git a/apps/web/lib/openapi/track/index.ts b/apps/web/lib/openapi/track/index.ts
index 7b144c760a..2c3c1aa829 100644
--- a/apps/web/lib/openapi/track/index.ts
+++ b/apps/web/lib/openapi/track/index.ts
@@ -1,5 +1,6 @@
import { ZodOpenApiPathsObject } from "zod-openapi";
import { trackLead } from "./lead";
+import { trackOpen } from "./open";
import { trackSale } from "./sale";
export const trackPaths: ZodOpenApiPathsObject = {
@@ -9,4 +10,7 @@ export const trackPaths: ZodOpenApiPathsObject = {
"/track/sale": {
post: trackSale,
},
+ "/track/open": {
+ post: trackOpen,
+ },
};
diff --git a/apps/web/lib/openapi/track/open.ts b/apps/web/lib/openapi/track/open.ts
new file mode 100644
index 0000000000..74c29ce69e
--- /dev/null
+++ b/apps/web/lib/openapi/track/open.ts
@@ -0,0 +1,33 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import {
+ trackOpenRequestSchema,
+ trackOpenResponseSchema,
+} from "@/lib/zod/schemas/opens";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+
+export const trackOpen: ZodOpenApiOperationObject = {
+ operationId: "trackOpen",
+ "x-speakeasy-ignore": true,
+ summary: "Track a deep link open event",
+ description:
+ "This endpoint is used to track when a user opens your app via a Dub-powered deep link (for both iOS and Android).",
+ requestBody: {
+ content: {
+ "application/json": {
+ schema: trackOpenRequestSchema,
+ },
+ },
+ },
+ responses: {
+ "200": {
+ description: "The response from the tracked open event.",
+ content: {
+ "application/json": {
+ schema: trackOpenResponseSchema,
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Track"],
+};
diff --git a/apps/web/lib/zod/schemas/opens.ts b/apps/web/lib/zod/schemas/opens.ts
new file mode 100644
index 0000000000..366548aa71
--- /dev/null
+++ b/apps/web/lib/zod/schemas/opens.ts
@@ -0,0 +1,54 @@
+import { parseUrlSchema } from "@/lib/zod/schemas/utils";
+import { z } from "zod";
+
+export const trackOpenRequestSchema = z
+ .object({
+ deepLink: parseUrlSchema
+ .optional()
+ .describe(
+ "The deep link that brought the user to the app. If left blank, Dub will fallback to probabilistic tracking by using the `dubDomain` parameter to check if there is an associated click event for the user's IP address. Learn more: https://d.to/ddl",
+ ),
+ dubDomain: z
+ .string()
+ .optional()
+ .describe(
+ "Your deep link custom domain on Dub (e.g. `acme.link`). This is used in probabilistic tracking to check if there is an associated click event for the user's IP address. Learn more: https://d.to/ddl",
+ ),
+ })
+ .superRefine((data, ctx) => {
+ if (!data.deepLink && !data.dubDomain) {
+ ctx.addIssue({
+ code: z.ZodIssueCode.custom,
+ message:
+ "You need to provide either `deepLink` or `dubDomain` for deferred deep linking.",
+ });
+ }
+ });
+
+export const trackOpenResponseSchema = z.object({
+ clickId: z
+ .string()
+ .nullable()
+ .describe(
+ "The click ID of the associated open event (or the prior click that led the user to the app store for probabilistic tracking). This will be `null` if the open event was not associated with a link (e.g. a direct download from the app store). Learn more: https://d.to/ddl",
+ ),
+ link: z
+ .object({
+ id: z.string().describe("The ID of the deep link.").openapi({
+ example: "link_xxx",
+ }),
+ domain: z.string().describe("The domain of the deep link.").openapi({
+ example: "acme.link",
+ }),
+ key: z.string().describe("The key of the deep link.").openapi({
+ example: "fb-promo",
+ }),
+ url: z.string().describe("The URL of the deep link.").openapi({
+ example: "https://acme.com/product/123",
+ }),
+ })
+ .nullable()
+ .describe(
+ "The deep link that brought the user to the app. This will be `null` if the open event was not associated with a link (e.g. a direct download from the app store). Learn more: https://d.to/ddl",
+ ),
+});