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 UTM parameter handling in link updates.

Kiran K273d73b42c49
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 3b97169cd9..5010599502 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -114,15 +114,10 @@ export const PATCH = withWorkspace(
? link.expiresAt.toISOString()
: link.expiresAt,
geo: link.geo as NewLinkProps["geo"],
-
...body,
- // for UTM tags, we only pass them to processLink if they have changed from their previous value
- // or else they will override any changes to the UTM params in the destination URL
+ // Only pass UTM tags to processLink when explicitly provided in body (preserves existing values otherwise)
...Object.fromEntries(
- UTMTags.map((tag) => [
- tag,
- body[tag] === link[tag] ? undefined : body[tag],
- ]),
+ UTMTags.filter((tag) => tag in body).map((tag) => [tag, body[tag]]),
),
// When root domain
diff --git a/apps/web/lib/api/links/process-link.ts b/apps/web/lib/api/links/process-link.ts
index 4ef5892cb5..9b77264786 100644
--- a/apps/web/lib/api/links/process-link.ts
+++ b/apps/web/lib/api/links/process-link.ts
@@ -91,9 +91,11 @@ export async function processLink<T extends Record<string, any>>({
code: "unprocessable_entity",
};
}
- if (UTMTags.some((tag) => payload[tag])) {
+
+ // Process UTM params only if the key exists, allowing null/empty to clear them.
+ if (UTMTags.some((tag) => tag in payload)) {
const utmParams = UTMTags.reduce((acc, tag) => {
- if (payload[tag]) {
+ if (tag in payload) {
acc[tag] = payload[tag];
}
return acc;
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 8ce81d2210..cfeaf1406b 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -471,36 +471,42 @@ export const createLinkBodySchema = z.object({
),
utm_source: z
.string()
+ .transform((v) => (v === "" ? null : v))
.nullish()
.describe(
"The UTM source of the short link. If set, this will populate or override the UTM source in the destination URL.",
),
utm_medium: z
.string()
+ .transform((v) => (v === "" ? null : v))
.nullish()
.describe(
"The UTM medium of the short link. If set, this will populate or override the UTM medium in the destination URL.",
),
utm_campaign: z
.string()
+ .transform((v) => (v === "" ? null : v))
.nullish()
.describe(
"The UTM campaign of the short link. If set, this will populate or override the UTM campaign in the destination URL.",
),
utm_term: z
.string()
+ .transform((v) => (v === "" ? null : v))
.nullish()
.describe(
"The UTM term of the short link. If set, this will populate or override the UTM term in the destination URL.",
),
utm_content: z
.string()
+ .transform((v) => (v === "" ? null : v))
.nullish()
.describe(
"The UTM content of the short link. If set, this will populate or override the UTM content in the destination URL.",
),
ref: z
.string()
+ .transform((v) => (v === "" ? null : v))
.nullish()
.describe(
"The referral tag of the short link. If set, this will populate or override the `ref` query parameter in the destination URL.",
diff --git a/apps/web/tests/links/update-link.test.ts b/apps/web/tests/links/update-link.test.ts
index 15e81760bc..1f04183c0e 100644
--- a/apps/web/tests/links/update-link.test.ts
+++ b/apps/web/tests/links/update-link.test.ts
@@ -1,6 +1,6 @@
import { normalizeWorkspaceId } from "@/lib/api/workspaces/workspace-id";
import { Link } from "@dub/prisma/client";
-import { afterAll, describe, expect, test } from "vitest";
+import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { randomId } from "../utils/helpers";
import { IntegrationHarness } from "../utils/integration";
import { E2E_LINK } from "../utils/resource";
@@ -181,6 +181,110 @@ describe.sequential("PATCH /links/{linkId}", async () => {
});
});
+describe.sequential("PATCH /links/{linkId} - UTM parameters", async () => {
+ const h = new IntegrationHarness();
+ const { http } = await h.init();
+
+ let link: Link;
+
+ beforeAll(async () => {
+ const { data } = await http.post<Link>({
+ path: "/links",
+ body: {
+ url,
+ domain,
+ },
+ });
+ link = data;
+ });
+
+ afterAll(async () => {
+ await h.deleteLink(link.id);
+ });
+
+ test("update link with URL and UTM params", async () => {
+ const { data: updated } = await http.patch<Link>({
+ path: `/links/${link.id}`,
+ body: {
+ url: "https://example.com",
+ utm_source: "test_source",
+ utm_medium: "email",
+ },
+ });
+
+ expect(updated.url).toBe(
+ "https://example.com/?utm_source=test_source&utm_medium=email",
+ );
+ expect(updated.utm_source).toBe("test_source");
+ expect(updated.utm_medium).toBe("email");
+ });
+
+ test("update UTM params only (no URL change)", async () => {
+ const { data: updated } = await http.patch<Link>({
+ path: `/links/${link.id}`,
+ body: {
+ utm_source: "new_source",
+ utm_campaign: "spring_sale",
+ },
+ });
+
+ expect(updated.url).toBe(
+ "https://example.com/?utm_source=new_source&utm_medium=email&utm_campaign=spring_sale",
+ );
+ expect(updated.utm_source).toBe("new_source");
+ expect(updated.utm_medium).toBe("email");
+ expect(updated.utm_campaign).toBe("spring_sale");
+ });
+
+ test("update with same UTM value", async () => {
+ const { data: updated } = await http.patch<Link>({
+ path: `/links/${link.id}`,
+ body: {
+ utm_source: "new_source",
+ },
+ });
+
+ expect(updated.url).toBe(
+ "https://example.com/?utm_source=new_source&utm_medium=email&utm_campaign=spring_sale",
+ );
+ expect(updated.utm_source).toBe("new_source");
+ expect(updated.utm_medium).toBe("email");
+ expect(updated.utm_campaign).toBe("spring_sale");
+ });
+
+ test("update URL only - preserves existing UTM params", async () => {
+ const { data: updated } = await http.patch<Link>({
+ path: `/links/${link.id}`,
+ body: {
+ url: "https://newdomain.com/path",
+ },
+ });
+
+ expect(updated.url).toBe(
+ "https://newdomain.com/path?utm_source=new_source&utm_medium=email&utm_campaign=spring_sale",
+ );
+ expect(updated.utm_source).toBe("new_source");
+ expect(updated.utm_medium).toBe("email");
+ expect(updated.utm_campaign).toBe("spring_sale");
+ });
+
+ test("clear single UTM param with empty string", async () => {
+ const { data: updated } = await http.patch<Link>({
+ path: `/links/${link.id}`,
+ body: {
+ utm_campaign: "",
+ },
+ });
+
+ expect(updated.url).toBe(
+ "https://newdomain.com/path?utm_source=new_source&utm_medium=email",
+ );
+ expect(updated.utm_source).toBe("new_source");
+ expect(updated.utm_medium).toBe("email");
+ expect(updated.utm_campaign).toBe(null);
+ });
+});
+
describe.sequential(
"PUT /links/{linkId} (backwards compatibility)",
async () => {