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.

finalize implementation

Steven Tey32d146ae7cea
diff --git a/apps/web/lib/tinybird/record-click.ts b/apps/web/lib/tinybird/record-click.ts
index c3e5a1b800..e87bba2f26 100644
--- a/apps/web/lib/tinybird/record-click.ts
+++ b/apps/web/lib/tinybird/record-click.ts
@@ -1,5 +1,4 @@
import {
- EU_COUNTRY_CODES,
LOCALHOST_GEO_DATA,
LOCALHOST_IP,
capitalize,
@@ -31,16 +30,20 @@ export async function recordClick({
return null; // don't record clicks from bots
}
const isQr = detectQr(req);
+
+ // get continent & geolocation data
+ const continent =
+ process.env.VERCEL === "1"
+ ? req.headers.get("x-vercel-ip-continent")
+ : LOCALHOST_GEO_DATA.continent;
const geo = process.env.VERCEL === "1" ? req.geo : LOCALHOST_GEO_DATA;
+
const ua = userAgent(req);
const referer = req.headers.get("referer");
- const continent = req.headers.get("x-vercel-ip-continent");
- console.log({ continent });
-
+ // deduplicate clicks from the same IP address + link ID – only record 1 click per hour
const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
const identity_hash = await getIdentityHash(req);
- // deduplicate clicks from the same IP address + link ID – only record 1 click per hour
const { success } = await ratelimit(1, "1 h").limit(
`recordClick:${ip}:${linkId}`,
);
@@ -65,12 +68,10 @@ export async function recordClick({
url: url || "",
ip:
// only record IP if it's a valid IP and not from EU
- typeof ip === "string" &&
- ip.trim().length > 0 &&
- (!geo?.country ||
- (geo?.country && !EU_COUNTRY_CODES.includes(geo.country)))
+ typeof ip === "string" && ip.trim().length > 0 && continent !== "EU"
? ip
: "",
+ continent: continent || "",
country: geo?.country || "Unknown",
city: geo?.city || "Unknown",
region: geo?.region || "Unknown",
diff --git a/packages/utils/src/constants/localhost.ts b/packages/utils/src/constants/localhost.ts
index 3504c7b465..1830bfb444 100644
--- a/packages/utils/src/constants/localhost.ts
+++ b/packages/utils/src/constants/localhost.ts
@@ -1,7 +1,8 @@
export const LOCALHOST_GEO_DATA = {
+ continent: "NA",
+ country: "US",
city: "San Francisco",
region: "CA",
- country: "US",
latitude: "37.7695",
longitude: "-122.385",
};