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 update-sale-event script

Steven Tey96ef1ac07eee
diff --git a/apps/web/lib/analytics/get-analytics.ts b/apps/web/lib/analytics/get-analytics.ts
index d5187dcfa5..72a2e6b6f3 100644
--- a/apps/web/lib/analytics/get-analytics.ts
+++ b/apps/web/lib/analytics/get-analytics.ts
@@ -4,7 +4,6 @@ import { UTM_TAGS_PLURAL_LIST } from "@/lib/zod/schemas/utm";
import { prismaEdge } from "@dub/prisma/edge";
import { linkConstructor, punyEncode } from "@dub/utils";
import { conn } from "../planetscale";
-import { tbDemo } from "../tinybird/demo-client";
import z from "../zod";
import { analyticsFilterTB } from "../zod/schemas/analytics";
import { analyticsResponse } from "../zod/schemas/analytics-response";
@@ -80,7 +79,7 @@ export const getAnalytics = async (params: AnalyticsFilters) => {
}
// Create a Tinybird pipe
- const pipe = (isDemo ? tbDemo : tb).buildPipe({
+ const pipe = tb.buildPipe({
pipe: `v2_${UTM_TAGS_PLURAL_LIST.includes(groupBy) ? "utms" : groupBy}`,
parameters: analyticsFilterTB,
data:
diff --git a/apps/web/lib/analytics/get-events.ts b/apps/web/lib/analytics/get-events.ts
index 068275ada6..ace304cc9c 100644
--- a/apps/web/lib/analytics/get-events.ts
+++ b/apps/web/lib/analytics/get-events.ts
@@ -3,7 +3,6 @@ import { prisma } from "@dub/prisma";
import { Link } from "@dub/prisma/client";
import { transformLink } from "../api/links";
import { generateRandomName } from "../names";
-import { tbDemo } from "../tinybird/demo-client";
import z from "../zod";
import { eventsFilterTB } from "../zod/schemas/analytics";
import {
@@ -67,7 +66,7 @@ export const getEvents = async (params: EventsFilters) => {
sortOrder = order;
}
- const pipe = (isDemo ? tbDemo : tb).buildPipe({
+ const pipe = tb.buildPipe({
pipe: "v2_events",
parameters: eventsFilterTB,
data:
diff --git a/apps/web/lib/tinybird/demo-client.ts b/apps/web/lib/tinybird/demo-client.ts
deleted file mode 100644
index f8431e98d8..0000000000
--- a/apps/web/lib/tinybird/demo-client.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import { Tinybird } from "@chronark/zod-bird";
-
-export const tbDemo = new Tinybird({
- token: process.env.TINYBIRD_DEMO_API_KEY as string,
- baseUrl: process.env.TINYBIRD_API_URL as string,
-});
diff --git a/apps/web/lib/tinybird/record-sale.ts b/apps/web/lib/tinybird/record-sale.ts
index 408698a73b..382dda67f8 100644
--- a/apps/web/lib/tinybird/record-sale.ts
+++ b/apps/web/lib/tinybird/record-sale.ts
@@ -1,3 +1,4 @@
+import z from "../zod";
import { saleEventSchemaTB } from "../zod/schemas/sales";
import { tb } from "./client";
@@ -5,3 +6,10 @@ export const recordSale = tb.buildIngestEndpoint({
datasource: "dub_sale_events",
event: saleEventSchemaTB,
});
+
+export const recordSaleWithTimestamp = tb.buildIngestEndpoint({
+ datasource: "dub_sale_events",
+ event: saleEventSchemaTB.extend({
+ timestamp: z.string(),
+ }),
+});
diff --git a/apps/web/lib/zod/schemas/sales.ts b/apps/web/lib/zod/schemas/sales.ts
index b5853b4f00..b2bb006835 100644
--- a/apps/web/lib/zod/schemas/sales.ts
+++ b/apps/web/lib/zod/schemas/sales.ts
@@ -89,7 +89,7 @@ export const trackSaleResponseSchema = z.object({
export const saleEventSchemaTB = clickEventSchemaTB
.omit({ timestamp: true })
- .and(
+ .merge(
z.object({
timestamp: z.string().optional(), //autogenerated by Tinybird
event_id: z.string(),
diff --git a/apps/web/scripts/update-sale-event.ts b/apps/web/scripts/update-sale-event.ts
new file mode 100644
index 0000000000..3dcf05d8e9
--- /dev/null
+++ b/apps/web/scripts/update-sale-event.ts
@@ -0,0 +1,77 @@
+import z from "@/lib/zod";
+import "dotenv-flow/config";
+import { tb } from "../lib/tinybird/client";
+import { recordSaleWithTimestamp } from "../lib/tinybird/record-sale";
+
+export const getSaleEvent = tb.buildPipe({
+ pipe: "get_sale_event",
+ parameters: z.object({
+ eventId: z.string(),
+ }),
+ data: z.any(),
+});
+
+// update tinybird sale event
+async function main() {
+ const eventId = "nBxldg6VxQNUCCwZ";
+ const columnName = "event_name";
+ const columnValue = "New Subscription Name";
+
+ const { data } = await getSaleEvent({ eventId });
+ const oldData = data[0];
+ if (!oldData) {
+ console.log("No data found");
+ return;
+ }
+ const updatedData = {
+ ...oldData,
+ [columnName]: columnValue,
+ };
+ console.log(updatedData);
+
+ const res = await recordSaleWithTimestamp(updatedData);
+ console.log(res);
+
+ // delete data from tinybird
+ const deleteRes = await Promise.allSettled([
+ deleteData({
+ dataSource: "dub_sale_events",
+ eventId,
+ columnName,
+ oldValue: oldData[columnName],
+ }),
+ deleteData({
+ dataSource: "dub_sale_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();