Skip to content

Commit

Permalink
Merge pull request #1817 from dubinc/track-click-url
Browse files Browse the repository at this point in the history
Accept custom URL in /track/click
  • Loading branch information
steven-tey authored Dec 20, 2024
2 parents e496ee8 + e378e04 commit 928a050
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions apps/web/app/api/track/click/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { parseRequestBody } from "@/lib/api/utils";
import { getLinkViaEdge } from "@/lib/planetscale";
import { recordClick } from "@/lib/tinybird";
import { ratelimit, redis } from "@/lib/upstash";
import { LOCALHOST_IP, nanoid } from "@dub/utils";
import { isValidUrl, LOCALHOST_IP, nanoid } from "@dub/utils";
import { ipAddress, waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";

Expand All @@ -18,7 +18,7 @@ const CORS_HEADERS = {
// POST /api/track/click – Track a click event from client side
export const POST = async (req: Request) => {
try {
const { domain, key } = await parseRequestBody(req);
const { domain, key, url } = await parseRequestBody(req);

if (!domain || !key) {
throw new DubApiError({
Expand Down Expand Up @@ -48,6 +48,8 @@ export const POST = async (req: Request) => {
});
}

const finalUrl = isValidUrl(url) ? url : link.url;

const cacheKey = `recordClick:${link.id}:${ip}`;

let clickId = await redis.get<string>(cacheKey);
Expand All @@ -60,7 +62,7 @@ export const POST = async (req: Request) => {
req,
clickId,
linkId: link.id,
url: link.url,
url: finalUrl,
skipRatelimit: true,
}),
);
Expand Down

0 comments on commit 928a050

Please sign in to comment.