The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add identity_hash
Steven Tey6220b8d3230f
diff --git a/apps/web/lib/tinybird.ts b/apps/web/lib/tinybird.tsindex aab69d2ef9..0dc4cd523c 100644--- a/apps/web/lib/tinybird.ts+++ b/apps/web/lib/tinybird.ts@@ -3,6 +3,7 @@ import {LOCALHOST_IP,capitalize,getDomainWithoutWWW,+ hashStringSHA256,nanoid,} from "@dub/utils";import { ipAddress } from "@vercel/edge";@@ -34,7 +35,7 @@ export async function recordClick({const ua = userAgent(req);const referer = req.headers.get("referer");const ip = ipAddress(req) || LOCALHOST_IP;- // if in production / preview env, deduplicate clicks from the same IP & link ID – only record 1 click per hour+ // if in production / preview env, deduplicate clicks from the same IP & link ID – only record 1 click per hourif (process.env.VERCEL === "1") {const { success } = await ratelimit(2, "1 h").limit(`recordClick:${ip}:${id}`,@@ -43,6 +44,8 @@ export async function recordClick({return null;}}+ // combine IP + UA to create a unique identifier for the user (for deduplication)+ const identity_hash = await hashStringSHA256(`${ip}-${ua.ua}`);return await Promise.allSettled([fetch(@@ -54,6 +57,7 @@ export async function recordClick({},body: JSON.stringify({timestamp: new Date(Date.now()).toISOString(),+ identity_hash,click_id: nanoid(16),link_id: id,alias_link_id: "",diff --git a/packages/utils/src/functions/hash.ts b/packages/utils/src/functions/hash.tsindex 8e7ed92eec..0d4203401f 100644--- a/packages/utils/src/functions/hash.ts+++ b/packages/utils/src/functions/hash.ts@@ -14,3 +14,20 @@ export async function generateMD5Hash(message: string) {return hashHex;}++export async function hashStringSHA256(str: string) {+ // Encode the string into bytes+ const encoder = new TextEncoder();+ const data = encoder.encode(str);++ // Hash the data with SHA-256+ const hashBuffer = await crypto.subtle.digest("SHA-256", data);++ // Convert the buffer to a hexadecimal string+ const hashArray = Array.from(new Uint8Array(hashBuffer));+ const hashHex = hashArray+ .map((b) => b.toString(16).padStart(2, "0"))+ .join("");++ return hashHex;+}