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.

stashed changes

Steven Tey0dc3590a6d00
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 36d3a2ba3f..407039128c 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -10,8 +10,8 @@ import {
} from "@dub/utils";
import { type Link as LinkProps } from "@prisma/client";
import cloudinary from "cloudinary";
-import { NextApiRequest } from "next";
import { isIframeable } from "../middleware/utils";
+import { deleteClickData } from "../tinybird";
export async function getLinksForProject({
projectId,
@@ -409,6 +409,10 @@ export async function deleteLink({
cloudinary.v2.uploader.destroy(`${domain}/${key}`, {
invalidate: true,
}),
+ deleteClickData({
+ domain,
+ key,
+ }),
redis.del(`${domain}:${key}`),
]);
}
diff --git a/apps/web/lib/tinybird.ts b/apps/web/lib/tinybird.ts
index 84ed6f976d..d0b6e0e7b1 100644
--- a/apps/web/lib/tinybird.ts
+++ b/apps/web/lib/tinybird.ts
@@ -44,6 +44,9 @@ export async function recordClick({
"https://api.us-east.tinybird.co/v0/events?name=click_events&wait=true",
{
method: "POST",
+ headers: {
+ Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
+ },
body: JSON.stringify({
timestamp: new Date(Date.now()).toISOString(),
domain,
@@ -68,9 +71,6 @@ export async function recordClick({
referer: referer ? getDomainWithoutWWW(referer) : "(direct)",
referer_url: referer || "(direct)",
}),
- headers: {
- Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
- },
},
).then((res) => res.json()),
// increment the click count for the link if key is specified (not root click)
@@ -96,6 +96,33 @@ export async function recordClick({
]);
}
+export async function deleteClickData({
+ domain,
+ key,
+}: {
+ domain: string;
+ key: string;
+}) {
+ if (!domain || !key) {
+ return null;
+ }
+ // const deleteCondition = `domain='${domain}' AND key='${key}'`;
+ const deleteCondition = `domain='pris.ly' AND key='accelerate-blog'`;
+ const response = await fetch(
+ "https://api.tinybird.co/v0/datasources/monitoring_events/delete",
+ {
+ method: "POST",
+ headers: {
+ Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
+ "Content-Type": "application/x-www-form-urlencoded",
+ },
+ body: `delete_condition=${encodeURIComponent(deleteCondition)}`,
+ },
+ ).then((res) => res.json());
+ console.log({ response });
+ return response;
+}
+
export async function getTopLinks(domains: string[]) {
return await fetch(
`https://api.us-east.tinybird.co/v0/pipes/top_links.json?domains=${domains.join(