The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
update recordClick
Steven Tey841d3edf6a4c
diff --git a/apps/web/lib/tinybird.ts b/apps/web/lib/tinybird.tsindex e6037681ce..231e0745e8 100644--- a/apps/web/lib/tinybird.ts+++ b/apps/web/lib/tinybird.ts@@ -1,5 +1,7 @@import {+ EU_COUNTRY_CODES,LOCALHOST_GEO_DATA,+ LOCALHOST_IP,capitalize,getDomainWithoutWWW,nanoid,@@ -10,6 +12,7 @@ import { detectBot } from "./middleware/utils";import { conn } from "./planetscale";import { LinkProps } from "./types";import { ratelimit } from "./upstash";+import { ipAddress } from "@vercel/edge";/*** Recording clicks with geo, ua, referer and timestamp data@@ -32,6 +35,7 @@ export async function recordClick({const geo = process.env.VERCEL === "1" ? req.geo : LOCALHOST_GEO_DATA;const ua = userAgent(req);const referer = req.headers.get("referer");+ const ip = ipAddress(req) || LOCALHOST_IP;const identity_hash = await getIdentityHash(req);// if in production / preview env, deduplicate clicks from the same IP & link ID – only record 1 click per hourif (process.env.VERCEL === "1") {@@ -58,6 +62,8 @@ export async function recordClick({link_id: id,alias_link_id: "",url: url || "",+ // only store IP Address if not EU country+ ip: geo?.country && EU_COUNTRY_CODES.includes(geo.country) ? "" : ip,country: geo?.country || "Unknown",city: geo?.city || "Unknown",region: geo?.region || "Unknown",diff --git a/packages/utils/src/constants/countries.ts b/packages/utils/src/constants/countries.tsindex b9921ed091..ecf76fb154 100644--- a/packages/utils/src/constants/countries.ts+++ b/packages/utils/src/constants/countries.ts@@ -252,3 +252,33 @@ export const COUNTRIES: { [key: string]: string } = {};export const COUNTRY_CODES = Object.keys(COUNTRIES) as [string, ...string[]];++export const EU_COUNTRY_CODES = [+ "AT",+ "BE",+ "BG",+ "CY",+ "CZ",+ "DE",+ "DK",+ "EE",+ "ES",+ "FI",+ "FR",+ "GR",+ "HR",+ "HU",+ "IE",+ "IT",+ "LT",+ "LU",+ "LV",+ "MT",+ "NL",+ "PL",+ "PT",+ "RO",+ "SE",+ "SI",+ "SK",+];