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.

use dub-no-track searchParams instead of headers

Steven Teyb7300d7131bf
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 086e2a25a3..8adf9307ab 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -54,11 +54,10 @@ export default async function LinkMiddleware(
// we don't support .php links (too much bot traffic)
// hence we redirect to the root domain and add `dub-no-track` header to avoid tracking bot traffic
if (key.endsWith(".php")) {
- return NextResponse.redirect(new URL("/", req.url), {
+ return NextResponse.redirect(new URL("/?dub-no-track=1", req.url), {
headers: {
...DUB_HEADERS,
"X-Robots-Tag": "googlebot: noindex",
- "dub-no-track": "true",
},
status: 302,
});
diff --git a/apps/web/lib/middleware/utils/get-final-url.ts b/apps/web/lib/middleware/utils/get-final-url.ts
index d10d685b0a..4267fa32a2 100644
--- a/apps/web/lib/middleware/utils/get-final-url.ts
+++ b/apps/web/lib/middleware/utils/get-final-url.ts
@@ -1,15 +1,5 @@
import { NextRequest } from "next/server";
-// Only add query params to the final URL if they are in this list
-const allowedQueryParams = [
- "utm_source",
- "utm_medium",
- "utm_campaign",
- "utm_term",
- "utm_content",
- "ref",
-];
-
export const getFinalUrl = (
url: string,
{ req, clickId }: { req: NextRequest; clickId?: string },
@@ -20,7 +10,12 @@ export const getFinalUrl = (
// get the query params of the target url
const urlObj = new URL(url);
- if (clickId) {
+ // remove dub-no-track param from the final url if it exists (used to skip tracking)
+ if (urlObj.searchParams.has("dub-no-track")) {
+ urlObj.searchParams.delete("dub-no-track");
+ // if there's no dub-no-track param, then add clickId to the final url if it exists
+ // reasoning: if you're skipping tracking, there's no point in passing the clickId anyway
+ } else if (clickId) {
// add clickId to the final url if it exists
urlObj.searchParams.set("dub_id", clickId);
}
@@ -34,14 +29,24 @@ export const getFinalUrl = (
urlObj.searchParams.set(key, value);
}
+ // remove qr param from the final url if the value is "1" (only used for detectQr function)
if (urlObj.searchParams.get("qr") === "1") {
- // remove qr param from the final url if the value is "1" (only used for detectQr function)
urlObj.searchParams.delete("qr");
}
return urlObj.toString();
};
+// Only add query params to the final URL if they are in this list
+const allowedQueryParams = [
+ "utm_source",
+ "utm_medium",
+ "utm_campaign",
+ "utm_term",
+ "utm_content",
+ "ref",
+];
+
// Get final cleaned url for storing in TB
export const getFinalUrlForRecordClick = ({
req,
diff --git a/apps/web/lib/tinybird/record-click.ts b/apps/web/lib/tinybird/record-click.ts
index 7a81e6e45a..8562e5bbf8 100644
--- a/apps/web/lib/tinybird/record-click.ts
+++ b/apps/web/lib/tinybird/record-click.ts
@@ -41,10 +41,7 @@ export async function recordClick({
const searchParams = new URL(req.url).searchParams;
// only track the click when there is no `dub-no-track` header or query param
- if (
- req.headers.has("dub-no-track") ||
- searchParams.get("dub-no-track") === "1"
- ) {
+ if (req.headers.has("dub-no-track") || searchParams.has("dub-no-track")) {
return null;
}
diff --git a/apps/web/tests/redirects/index.test.ts b/apps/web/tests/redirects/index.test.ts
index b82723b81c..dd449a2fc5 100644
--- a/apps/web/tests/redirects/index.test.ts
+++ b/apps/web/tests/redirects/index.test.ts
@@ -7,7 +7,7 @@ const fetchOptions: RequestInit = {
cache: "no-store",
redirect: "manual",
headers: {
- "dub-no-track": "true",
+ "dub-no-track": "1",
},
};