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.

Improve /track/visit with `mget`

Steven Teycce02e939cf6
diff --git a/apps/web/app/api/track/visit/route.ts b/apps/web/app/api/track/visit/route.ts
index 3313b49e56..2fdfd8f43c 100644
--- a/apps/web/app/api/track/visit/route.ts
+++ b/apps/web/app/api/track/visit/route.ts
@@ -5,7 +5,8 @@ import { clickCache } from "@/lib/api/links/click-cache";
import { parseRequestBody } from "@/lib/api/utils";
import { getLinkViaEdge, getWorkspaceViaEdge } from "@/lib/planetscale";
import { recordClick } from "@/lib/tinybird";
-import { formatRedisLink } from "@/lib/upstash";
+import { RedisLinkProps } from "@/lib/types";
+import { formatRedisLink, redis } from "@/lib/upstash";
import { isValidUrl, LOCALHOST_IP, nanoid } from "@dub/utils";
import { ipAddress, waitUntil } from "@vercel/functions";
import { AxiomRequest, withAxiom } from "next-axiom";
@@ -40,24 +41,16 @@ export const POST = withAxiom(async (req: AxiomRequest) => {
const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
- let clickId = await clickCache.get({
- domain,
- key,
- ip,
- });
+ let [cachedClickId, cachedLink] = await redis.mget<
+ [string, RedisLinkProps]
+ >([
+ clickCache._createKey({ domain, key, ip }),
+ linkCache._createKey({ domain, key }),
+ ]);
- // if the clickId is already cached in Redis, return it
- if (clickId) {
- return NextResponse.json({ clickId }, { headers: CORS_HEADERS });
- }
-
- // Otherwise, track the visit event
- clickId = nanoid(16);
-
- let cachedLink = await linkCache.get({
- domain,
- key,
- });
+ // assign a new clickId if there's no cached clickId
+ // else, reuse the cached clickId
+ const clickId = cachedClickId ?? nanoid(16);
if (!cachedLink) {
const link = await getLinkViaEdge({
diff --git a/apps/web/lib/auth/track-lead.ts b/apps/web/lib/auth/track-lead.ts
index f4fdda4447..4299007a75 100644
--- a/apps/web/lib/auth/track-lead.ts
+++ b/apps/web/lib/auth/track-lead.ts
@@ -3,17 +3,16 @@ import { User } from "next-auth";
import { cookies } from "next/headers";
export const trackLead = async (user: User) => {
- const clickId =
- cookies().get("dub_id")?.value || cookies().get("dclid")?.value;
+ const dubId = cookies().get("dub_id")?.value;
- if (!clickId) {
- console.log("No clickId cookie found, skipping lead tracking...");
+ if (!dubId) {
+ console.log("No dub_id cookie found, skipping lead tracking...");
return;
}
// send the lead event to Dub
await dub.track.lead({
- clickId,
+ clickId: dubId,
eventName: "Sign Up",
externalId: user.id,
customerName: user.name,
@@ -21,7 +20,7 @@ export const trackLead = async (user: User) => {
customerAvatar: user.image,
});
- // delete the clickId cookie
- cookies().delete("dclid");
+ // delete the cookies
cookies().delete("dub_id");
+ cookies().delete("dub_partner_data");
};