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 schema

Steven Tey347146a4c4b6
diff --git a/apps/web/app/api/track/click/route.ts b/apps/web/app/api/track/click/route.ts
index c85bdcfa98..4b73cf3e22 100644
--- a/apps/web/app/api/track/click/route.ts
+++ b/apps/web/app/api/track/click/route.ts
@@ -26,7 +26,7 @@ export const POST = async (req: Request) => {
});
}
- const redisKey = `trackClick:${link.id}:${ipAddress(req)}`;
+ const redisKey = `recordClick:${link.id}:${ipAddress(req)}`;
let clickId = await redis.get<string>(redisKey);
if (!clickId) {
clickId = nanoid(16);
diff --git a/apps/web/lib/tinybird/record-click.ts b/apps/web/lib/tinybird/record-click.ts
index aeff3aeaa9..233d2f9193 100644
--- a/apps/web/lib/tinybird/record-click.ts
+++ b/apps/web/lib/tinybird/record-click.ts
@@ -16,7 +16,7 @@ import {
getIdentityHash,
} from "../middleware/utils";
import { conn } from "../planetscale";
-import { ratelimit } from "../upstash";
+import { redis } from "../upstash";
import { webhookCache } from "../webhook/cache";
import { sendWebhooks } from "../webhook/qstash";
import { transformClickEventData } from "../webhook/transform";
@@ -58,13 +58,12 @@ export async function recordClick({
const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
+ const cacheKey = `recordClick:${linkId}:${ip}`;
+
if (!skipRatelimit) {
// deduplicate clicks from the same IP address + link ID – only record 1 click per hour
- const { success } = await ratelimit(1, "1 h").limit(
- `recordClick:${linkId}:${ip}`,
- );
-
- if (!success) {
+ const cachedClickId = await redis.get<string>(cacheKey);
+ if (cachedClickId) {
return null;
}
}
@@ -98,11 +97,11 @@ export async function recordClick({
// only record IP if it's a valid IP and not from a EU country
typeof ip === "string" && ip.trim().length > 0 && !isEuCountry ? ip : "",
continent: continent || "",
- country: geo?.country || "Unknown",
- city: geo?.city || "Unknown",
- region: geo?.region || "Unknown",
- latitude: geo?.latitude || "Unknown",
- longitude: geo?.longitude || "Unknown",
+ country: geo.country || "Unknown",
+ city: geo.city || "Unknown",
+ region: geo.region || "Unknown",
+ latitude: geo.latitude || "Unknown",
+ longitude: geo.longitude || "Unknown",
device: capitalize(ua.device.type) || "Desktop",
device_vendor: ua.device.vendor || "Unknown",
device_model: ua.device.model || "Unknown",
@@ -132,6 +131,11 @@ export async function recordClick({
},
).then((res) => res.json()),
+ // cache the click ID in Redis for 1 hour
+ redis.set(cacheKey, clickId, {
+ ex: 60 * 60,
+ }),
+
// increment the click count for the link (based on their ID)
// we have to use planetscale connection directly (not prismaEdge) because of connection pooling
conn.execute(
diff --git a/apps/web/prisma/schema/link.prisma b/apps/web/prisma/schema/link.prisma
index fd921135f6..3d6f5b3595 100644
--- a/apps/web/prisma/schema/link.prisma
+++ b/apps/web/prisma/schema/link.prisma
@@ -8,7 +8,7 @@ model Link {
expiredUrl String? @db.LongText // URL to redirect the user to when the link is expired
password String? // password to access the link
externalId String?
-
+ identifier String?
trackConversion Boolean @default(false) // whether to track conversions or not
proxy Boolean @default(false) // Proxy to use custom OG tags (stored on redis) – if false, will use OG tags from target url
@@ -64,6 +64,7 @@ model Link {
@@unique([domain, key])
@@unique([projectId, externalId])
+ @@unique([projectId, identifier])
@@index(projectId)
@@index(domain)
@@index(trackConversion)
diff --git a/apps/web/prisma/schema/workspace.prisma b/apps/web/prisma/schema/workspace.prisma
index 3365278031..9ef99f2298 100644
--- a/apps/web/prisma/schema/workspace.prisma
+++ b/apps/web/prisma/schema/workspace.prisma
@@ -34,6 +34,7 @@ model Project {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
usageLastChecked DateTime @default(now())
+ publishableKey String?
users ProjectUsers[]
invites ProjectInvite[]