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.

add tb scripts

Steven Tey58dc3d01d726
diff --git a/apps/web/scripts/tinybird/update-click-event.ts b/apps/web/scripts/tinybird/update-click-event.ts
new file mode 100644
index 0000000000..773d6613f2
--- /dev/null
+++ b/apps/web/scripts/tinybird/update-click-event.ts
@@ -0,0 +1,78 @@
+import "dotenv-flow/config";
+import { getClickEvent } from "../../lib/tinybird/get-click-event";
+
+// update tinybird sale event
+async function main() {
+ const clickId = "liE74JEvfIBXKNmR";
+ const columnName = "link_id";
+ const columnValue = "link_1JRVCZWHWACS7R2KZB9ME6CJR";
+
+ const { data } = await getClickEvent({ clickId });
+ const oldData = data[0];
+ if (!oldData) {
+ console.log("No data found");
+ return;
+ }
+ const updatedData = {
+ ...oldData,
+ alias_link_id: "",
+ vercel_region: "iad1",
+ [columnName]: columnValue,
+ };
+ console.log(updatedData);
+
+ const res = await fetch(
+ `${process.env.TINYBIRD_API_URL}/v0/events?name=dub_click_events&wait=true`,
+ {
+ method: "POST",
+ headers: {
+ Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
+ },
+ body: JSON.stringify(updatedData),
+ },
+ ).then((res) => res.json());
+ console.log(res);
+
+ // delete data from tinybird
+ const deleteRes = await Promise.allSettled([
+ deleteData({
+ dataSource: "dub_click_events_mv",
+ clickId,
+ columnName,
+ oldValue: oldData[columnName],
+ }),
+ deleteData({
+ dataSource: "dub_click_events_id",
+ clickId,
+ columnName,
+ oldValue: oldData[columnName],
+ }),
+ ]);
+ console.log(deleteRes);
+}
+
+const deleteData = async ({
+ dataSource,
+ clickId,
+ columnName,
+ oldValue,
+}: {
+ dataSource: string;
+ clickId: string;
+ columnName: string;
+ oldValue: string;
+}) => {
+ return fetch(
+ `https://api.us-east.tinybird.co/v0/datasources/${dataSource}/delete`,
+ {
+ method: "POST",
+ headers: {
+ Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
+ "Content-Type": "application/x-www-form-urlencoded",
+ },
+ body: `delete_condition=click_id='${clickId}' and ${columnName}='${oldValue}'`,
+ },
+ ).then((res) => res.json());
+};
+
+main();
diff --git a/apps/web/scripts/tinybird/update-lead-event.ts b/apps/web/scripts/tinybird/update-lead-event.ts
new file mode 100644
index 0000000000..c109a8b683
--- /dev/null
+++ b/apps/web/scripts/tinybird/update-lead-event.ts
@@ -0,0 +1,77 @@
+import z from "@/lib/zod";
+import "dotenv-flow/config";
+import { tb } from "../../lib/tinybird/client";
+import { recordLeadWithTimestamp } from "../../lib/tinybird/record-lead";
+
+const getLeadEvent = tb.buildPipe({
+ pipe: "get_lead_event_by_id",
+ parameters: z.object({
+ eventId: z.string(),
+ }),
+ data: z.any(),
+});
+
+// update tinybird lead event
+async function main() {
+ const eventId = "nLvob3FdmsYuI1BI";
+ const columnName = "link_id";
+ const columnValue = "link_1JRVCZWHWACS7R2KZB9ME6CJR";
+
+ const { data } = await getLeadEvent({ eventId });
+ const oldData = data[0];
+ if (!oldData) {
+ console.log("No data found");
+ return;
+ }
+ const updatedData = {
+ ...oldData,
+ [columnName]: columnValue,
+ };
+ console.log(updatedData);
+
+ const res = await recordLeadWithTimestamp(updatedData);
+ console.log(res);
+
+ // delete data from tinybird
+ const deleteRes = await Promise.allSettled([
+ deleteData({
+ dataSource: "dub_lead_events",
+ eventId,
+ columnName,
+ oldValue: oldData[columnName],
+ }),
+ deleteData({
+ dataSource: "dub_lead_events_mv",
+ eventId,
+ columnName,
+ oldValue: oldData[columnName],
+ }),
+ ]);
+ console.log(deleteRes);
+}
+
+const deleteData = async ({
+ dataSource,
+ eventId,
+ columnName,
+ oldValue,
+}: {
+ dataSource: string;
+ eventId: string;
+ columnName: string;
+ oldValue: string;
+}) => {
+ return fetch(
+ `https://api.us-east.tinybird.co/v0/datasources/${dataSource}/delete`,
+ {
+ method: "POST",
+ headers: {
+ Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
+ "Content-Type": "application/x-www-form-urlencoded",
+ },
+ body: `delete_condition=event_id='${eventId}' and ${columnName}='${oldValue}'`,
+ },
+ ).then((res) => res.json());
+};
+
+main();
diff --git a/apps/web/scripts/tinybird/update-sale-event.ts b/apps/web/scripts/tinybird/update-sale-event.ts
index 7c2a8e6891..db0dcda7e6 100644
--- a/apps/web/scripts/tinybird/update-sale-event.ts
+++ b/apps/web/scripts/tinybird/update-sale-event.ts
@@ -3,7 +3,7 @@ import "dotenv-flow/config";
import { tb } from "../../lib/tinybird/client";
import { recordSaleWithTimestamp } from "../../lib/tinybird/record-sale";
-export const getSaleEvent = tb.buildPipe({
+const getSaleEvent = tb.buildPipe({
pipe: "get_sale_event",
parameters: z.object({
eventId: z.string(),
@@ -46,6 +46,12 @@ async function main() {
columnName,
oldValue: oldData[columnName],
}),
+ deleteData({
+ dataSource: "dub_sale_events_id",
+ eventId,
+ columnName,
+ oldValue: oldData[columnName],
+ }),
]);
console.log(deleteRes);
}