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.

Update `shouldPassClickId` implementation

Steven Tey48c5b397eeb3
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 6e1b4ffbf4..d31b999e75 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -167,6 +167,11 @@ export default async function LinkMiddleware(
const url = testUrl || cachedLink.url;
+ // we only pass the clickId if:
+ // - trackConversion is enabled
+ // - it's a partner link
+ const shouldPassClickId = trackConversion || isPartnerLink;
+
// by default, we only index default dub domain links (e.g. dub.sh)
// everything else is not indexed by default, unless the user has explicitly set it to be indexed
const shouldIndex = isDubDomain(domain) || doIndex === true;
@@ -244,8 +249,8 @@ export default async function LinkMiddleware(
const cookieStore = cookies();
let clickId = cookieStore.get(dubIdCookieName)?.value;
if (!clickId) {
- // if trackConversion is enabled, check if clickId is cached in Redis
- if (trackConversion) {
+ // if we need to pass the clickId, check if clickId is cached in Redis
+ if (shouldPassClickId) {
const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
clickId = (await clickCache.get({ domain, key, ip })) || undefined;
@@ -276,7 +281,7 @@ export default async function LinkMiddleware(
url,
webhookIds,
workspaceId,
- trackConversion,
+ shouldPassClickId,
}),
);
@@ -297,12 +302,6 @@ export default async function LinkMiddleware(
const { country } =
process.env.VERCEL === "1" && req.geo ? req.geo : LOCALHOST_GEO_DATA;
- // we only pass the clickId if:
- // - trackConversion is enabled
- // - not a partner link (TODO: add this later) !isPartnerLink
- // - there is a clickId
- const shouldPassClickId = trackConversion && clickId;
-
// rewrite to proxy page (/proxy/[domain]/[key]) if it's a bot and proxy is enabled
if (isBot && proxy) {
return createResponseWithCookies(
@@ -330,7 +329,7 @@ export default async function LinkMiddleware(
url,
webhookIds,
workspaceId,
- trackConversion,
+ shouldPassClickId,
}),
);
@@ -368,7 +367,7 @@ export default async function LinkMiddleware(
url,
webhookIds,
workspaceId,
- trackConversion,
+ shouldPassClickId,
}),
);
@@ -408,7 +407,7 @@ export default async function LinkMiddleware(
url: ios,
webhookIds,
workspaceId,
- trackConversion,
+ shouldPassClickId,
}),
);
@@ -442,7 +441,7 @@ export default async function LinkMiddleware(
url: android,
webhookIds,
workspaceId,
- trackConversion,
+ shouldPassClickId,
}),
);
@@ -476,7 +475,7 @@ export default async function LinkMiddleware(
url: geo[country],
webhookIds,
workspaceId,
- trackConversion,
+ shouldPassClickId,
}),
);
@@ -510,7 +509,7 @@ export default async function LinkMiddleware(
url,
webhookIds,
workspaceId,
- trackConversion,
+ shouldPassClickId,
}),
);
@@ -521,7 +520,7 @@ export default async function LinkMiddleware(
headers: new Headers({
destination: getFinalUrl(url, {
req,
- clickId: trackConversion ? clickId : undefined,
+ clickId: shouldPassClickId ? clickId : undefined,
}),
}),
},
diff --git a/apps/web/lib/tinybird/record-click.ts b/apps/web/lib/tinybird/record-click.ts
index 9c3de151bd..109be2de1b 100644
--- a/apps/web/lib/tinybird/record-click.ts
+++ b/apps/web/lib/tinybird/record-click.ts
@@ -37,7 +37,7 @@ export async function recordClick({
skipRatelimit,
timestamp,
referrer,
- trackConversion,
+ shouldPassClickId,
}: {
req: Request;
clickId?: string;
@@ -50,7 +50,7 @@ export async function recordClick({
skipRatelimit?: boolean;
timestamp?: string;
referrer?: string;
- trackConversion?: boolean;
+ shouldPassClickId?: boolean;
}) {
if (!clickId) {
return null;
@@ -165,7 +165,7 @@ export async function recordClick({
// cache the click data for 5 mins
// we're doing this because ingested click events are not available immediately in Tinybird
- trackConversion &&
+ shouldPassClickId &&
redis.set(`clickCache:${clickId}`, clickData, {
ex: 60 * 5,
}),