-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve /track/click and /track/visit
- Loading branch information
1 parent
d28f05e
commit 6a98e09
Showing
5 changed files
with
149 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { verifyAnalyticsAllowedHostnames } from "@/lib/analytics/verify-analytics-allowed-hostnames"; | ||
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors"; | ||
import { parseRequestBody } from "@/lib/api/utils"; | ||
import { getLinkWithAllowedHostnames } from "@/lib/planetscale/get-link-with-allowed-hostnames"; | ||
import { recordClick } from "@/lib/tinybird"; | ||
import { redis } from "@/lib/upstash"; | ||
import { isValidUrl, LOCALHOST_IP, nanoid } from "@dub/utils"; | ||
import { ipAddress, waitUntil } from "@vercel/functions"; | ||
import { AxiomRequest, withAxiom } from "next-axiom"; | ||
import { NextResponse } from "next/server"; | ||
|
||
export const runtime = "edge"; | ||
|
||
const CORS_HEADERS = { | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Allow-Methods": "POST, OPTIONS", | ||
"Access-Control-Allow-Headers": "Content-Type, Authorization", | ||
}; | ||
|
||
// POST /api/track/visit – Track a visit event from the client-side | ||
export const POST = withAxiom( | ||
async (req: AxiomRequest) => { | ||
try { | ||
const { domain, url, referrer } = await parseRequestBody(req); | ||
|
||
if (!domain || !url) { | ||
throw new DubApiError({ | ||
code: "bad_request", | ||
message: "Missing domain or url", | ||
}); | ||
} | ||
|
||
const urlObj = new URL(url); | ||
|
||
let linkKey = urlObj.pathname.slice(1); | ||
if (linkKey === "") { | ||
linkKey = "_root"; | ||
} | ||
|
||
const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP; | ||
const cacheKey = `recordClick:${domain}:${linkKey}:${ip}`; | ||
|
||
let clickId = await redis.get<string>(cacheKey); | ||
|
||
// only generate + record a new click ID if it's not already cached in Redis | ||
if (!clickId) { | ||
clickId = nanoid(16); | ||
|
||
let link = await getLinkWithAllowedHostnames(domain, linkKey); | ||
|
||
if (!link) { | ||
return NextResponse.json( | ||
{ | ||
clickId: null, | ||
}, | ||
{ | ||
headers: CORS_HEADERS, | ||
}, | ||
); | ||
} | ||
|
||
const allowedHostnames = link.allowedHostnames; | ||
verifyAnalyticsAllowedHostnames({ allowedHostnames, req }); | ||
|
||
const finalUrl = isValidUrl(url) ? url : link.url; | ||
|
||
waitUntil( | ||
recordClick({ | ||
req, | ||
clickId, | ||
linkId: link.id, | ||
url: finalUrl, | ||
workspaceId: link.projectId, | ||
...(referrer && { referrer }), | ||
}), | ||
); | ||
} | ||
|
||
return NextResponse.json( | ||
{ | ||
clickId, | ||
}, | ||
{ | ||
headers: CORS_HEADERS, | ||
}, | ||
); | ||
} catch (error) { | ||
return handleAndReturnErrorResponse(error, CORS_HEADERS); | ||
} | ||
}, | ||
{ | ||
logRequestDetails: ["body", "nextUrl"], | ||
}, | ||
); | ||
|
||
export const OPTIONS = () => { | ||
return new Response(null, { | ||
status: 204, | ||
headers: CORS_HEADERS, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters