The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add recordFakeClick
Kiran K98dc15d019fd
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.tsindex f2624af159..899335e6d0 100644--- a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts@@ -6,11 +6,11 @@ import { createPartnerCommission } from "@/lib/partners/create-partner-commissioimport {getClickEvent,getLeadEvent,- recordClick,recordLead,recordLeadWithTimestamp,recordSale,} from "@/lib/tinybird";+import { recordFakeClick } from "@/lib/tinybird/record-fake-click";import { ClickEventTB, LeadEventTB } from "@/lib/types";import { redis } from "@/lib/upstash";import { sendWorkspaceWebhook } from "@/lib/webhook/publish";@@ -18,7 +18,6 @@ import {transformLeadEventData,transformSaleEventData,} from "@/lib/webhook/transform";-import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";import { leadEventSchemaTB } from "@/lib/zod/schemas/leads";import { prisma } from "@dub/prisma";import { Customer } from "@dub/prisma/client";@@ -265,6 +264,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {url: true,domain: true,key: true,+ projectId: true,},});@@ -274,32 +274,12 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {const stripeCustomerAddress = charge.customer_details?.address;- const dummyRequest = new Request(link.url, {- headers: new Headers({- "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",- "x-forwarded-for": "127.0.0.1",- "x-vercel-ip-country": stripeCustomerAddress?.country || "US",- "x-vercel-ip-country-region": stripeCustomerAddress?.state || "CA",- "x-vercel-ip-continent": "NA",- }),- });-- const clickData = await recordClick({- req: dummyRequest,- linkId: link.id,- clickId: nanoid(16),- url: link.url,- domain: link.domain,- key: link.key,- workspaceId: workspace.id,- skipRatelimit: true,- timestamp: new Date().toISOString(),- });-- const clickEvent = clickEventSchemaTB.parse({- ...clickData,- bot: 0,- qr: 0,+ const clickEvent = await recordFakeClick({+ link,+ customer: {+ country: stripeCustomerAddress?.country,+ region: stripeCustomerAddress?.state,+ },});const customerId = createId({ prefix: "cus_" });diff --git a/apps/web/lib/actions/partners/create-commission.ts b/apps/web/lib/actions/partners/create-commission.tsindex 5517a6bad8..743a04c7a2 100644--- a/apps/web/lib/actions/partners/create-commission.ts+++ b/apps/web/lib/actions/partners/create-commission.ts@@ -4,11 +4,10 @@ import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-progrimport { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";import { createPartnerCommission } from "@/lib/partners/create-partner-commission";import { getLeadEvent } from "@/lib/tinybird";-import { recordClick } from "@/lib/tinybird/record-click";+import { recordFakeClick } from "@/lib/tinybird/record-fake-click";import { recordLeadWithTimestamp } from "@/lib/tinybird/record-lead";import { recordSaleWithTimestamp } from "@/lib/tinybird/record-sale";import { ClickEventTB, LeadEventTB } from "@/lib/types";-import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";import { createCommissionSchema } from "@/lib/zod/schemas/commissions";import { leadEventSchemaTB } from "@/lib/zod/schemas/leads";import { prisma } from "@dub/prisma";@@ -123,38 +122,16 @@ export const createCommissionAction = authActionClient} else {// else, if there's no existing lead event and there is also no custom leadEventName/Date// we need to create a dummy click + lead event- const dummyRequest = new Request(link.url, {- headers: new Headers({- "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",- "x-forwarded-for": "127.0.0.1",- "x-vercel-ip-country": "US",- "x-vercel-ip-country-region": "CA",- "x-vercel-ip-continent": "NA",- }),- });const finalLeadEventDate = leadEventDate ?? saleEventDate ?? new Date();- const clickData = await recordClick({- req: dummyRequest,- linkId,- clickId: nanoid(16),- url: link.url,- domain: link.domain,- key: link.key,- workspaceId: workspace.id,- skipRatelimit: true,- timestamp: new Date(- new Date(finalLeadEventDate).getTime() - 5 * 60 * 1000,- ).toISOString(),+ clickEvent = await recordFakeClick({+ link,+ timestamp: new Date(finalLeadEventDate).getTime() - 5 * 60 * 1000,});- clickEvent = clickEventSchemaTB.parse({- ...clickData,- bot: 0,- qr: 0,- });const leadEventId = nanoid(16);+leadEvent = leadEventSchemaTB.parse({...clickEvent,event_id: leadEventId,@@ -162,7 +139,7 @@ export const createCommissionAction = authActionClientcustomer_id: customerId,});- shouldUpdateCustomer = !customer.linkId && clickData ? true : false;+ shouldUpdateCustomer = !customer.linkId && clickEvent ? true : false;await Promise.allSettled([recordLeadWithTimestamp({diff --git a/apps/web/lib/partnerstack/import-customers.ts b/apps/web/lib/partnerstack/import-customers.tsindex 257fd6ac58..7a7dfd99c0 100644--- a/apps/web/lib/partnerstack/import-customers.ts+++ b/apps/web/lib/partnerstack/import-customers.ts@@ -2,9 +2,9 @@ import { prisma } from "@dub/prisma";import { nanoid } from "@dub/utils";import { Link, Project } from "@prisma/client";import { createId } from "../api/create-id";-import { recordClick, recordLeadWithTimestamp } from "../tinybird";+import { recordLeadWithTimestamp } from "../tinybird";+import { recordFakeClick } from "../tinybird/record-fake-click";import { redis } from "../upstash";-import { clickEventSchemaTB } from "../zod/schemas/clicks";import { PartnerStackApi } from "./api";import {MAX_BATCHES,@@ -90,6 +90,7 @@ export async function importCustomers(payload: PartnerStackImportPayload) {key: true,domain: true,url: true,+ projectId: true,},},},@@ -144,7 +145,7 @@ async function createCustomer({customer,}: {workspace: Pick<Project, "id" | "stripeConnectId">;- links: Pick<Link, "id" | "key" | "domain" | "url">[];+ links: Pick<Link, "id" | "key" | "domain" | "url" | "projectId">[];customer: PartnerStackCustomer;}) {if (links.length === 0) {@@ -174,32 +175,9 @@ async function createCustomer({const link = links[0];- const dummyRequest = new Request(link.url, {- headers: new Headers({- "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",- "x-forwarded-for": "127.0.0.1",- "x-vercel-ip-country": "US",- "x-vercel-ip-country-region": "CA",- "x-vercel-ip-continent": "NA",- }),- });-- const clickData = await recordClick({- req: dummyRequest,- linkId: link.id,- clickId: nanoid(16),- url: link.url,- domain: link.domain,- key: link.key,- workspaceId: workspace.id,- skipRatelimit: true,- timestamp: new Date(customer.created_at).toISOString(),- });-- const clickEvent = clickEventSchemaTB.parse({- ...clickData,- bot: 0,- qr: 0,+ const clickEvent = await recordFakeClick({+ link,+ timestamp: customer.created_at,});const customerId = createId({ prefix: "cus_" });diff --git a/apps/web/lib/rewardful/import-referrals.ts b/apps/web/lib/rewardful/import-referrals.tsindex 06f750bcb4..61faf3cbbe 100644--- a/apps/web/lib/rewardful/import-referrals.ts+++ b/apps/web/lib/rewardful/import-referrals.ts@@ -2,9 +2,8 @@ import { prisma } from "@dub/prisma";import { nanoid } from "@dub/utils";import { Program, Project } from "@prisma/client";import { createId } from "../api/create-id";-import { recordClick } from "../tinybird/record-click";+import { recordFakeClick } from "../tinybird/record-fake-click";import { recordLeadWithTimestamp } from "../tinybird/record-lead";-import { clickEventSchemaTB } from "../zod/schemas/clicks";import { RewardfulApi } from "./api";import { MAX_BATCHES, rewardfulImporter } from "./importer";import { RewardfulReferral } from "./types";@@ -152,32 +151,9 @@ async function createReferral({return;}- const dummyRequest = new Request(link.url, {- headers: new Headers({- "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",- "x-forwarded-for": "127.0.0.1",- "x-vercel-ip-country": "US",- "x-vercel-ip-country-region": "CA",- "x-vercel-ip-continent": "NA",- }),- });-- const clickData = await recordClick({- req: dummyRequest,- linkId: link.id,- clickId: nanoid(16),- url: link.url,- domain: link.domain,- key: link.key,- workspaceId: workspace.id,- skipRatelimit: true,- timestamp: new Date(referral.created_at).toISOString(),- });-- const clickEvent = clickEventSchemaTB.parse({- ...clickData,- bot: 0,- qr: 0,+ const clickEvent = await recordFakeClick({+ link,+ timestamp: referral.created_at,});const customerId = createId({ prefix: "cus_" });diff --git a/apps/web/lib/tinybird/record-fake-click.ts b/apps/web/lib/tinybird/record-fake-click.tsnew file mode 100644index 0000000000..67a6b2fe8c--- /dev/null+++ b/apps/web/lib/tinybird/record-fake-click.ts@@ -0,0 +1,47 @@+import { Link } from "@dub/prisma/client";+import { nanoid } from "@dub/utils";+import { clickEventSchemaTB } from "../zod/schemas/clicks";+import { recordClick } from "./record-click";++export async function recordFakeClick({+ link,+ customer,+ timestamp,+}: {+ link: Pick<Link, "id" | "url" | "domain" | "key" | "projectId">;+ customer?: {+ country?: string | null;+ region?: string | null;+ };+ timestamp?: string | number;+}) {+ const dummyRequest = new Request(link.url, {+ headers: new Headers({+ "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",+ "x-forwarded-for": "127.0.0.1",+ "x-vercel-ip-country": customer?.country || "US",+ "x-vercel-ip-country-region": customer?.region || "CA",+ "x-vercel-ip-continent": "NA",+ }),+ });++ const clickData = await recordClick({+ req: dummyRequest,+ clickId: nanoid(16),+ linkId: link.id,+ url: link.url,+ domain: link.domain,+ key: link.key,+ workspaceId: link.projectId!,+ skipRatelimit: true,+ timestamp: timestamp+ ? new Date(timestamp).toISOString()+ : new Date().toISOString(),+ });++ return clickEventSchemaTB.parse({+ ...clickData,+ bot: 0,+ qr: 0,+ });+}diff --git a/apps/web/lib/tolt/import-referrals.ts b/apps/web/lib/tolt/import-referrals.tsindex 90461779f2..05245e5f17 100644--- a/apps/web/lib/tolt/import-referrals.ts+++ b/apps/web/lib/tolt/import-referrals.ts@@ -2,8 +2,8 @@ import { prisma } from "@dub/prisma";import { nanoid } from "@dub/utils";import { Link, Project } from "@prisma/client";import { createId } from "../api/create-id";-import { recordClick, recordLeadWithTimestamp } from "../tinybird";-import { clickEventSchemaTB } from "../zod/schemas/clicks";+import { recordLeadWithTimestamp } from "../tinybird";+import { recordFakeClick } from "../tinybird/record-fake-click";import { ToltApi } from "./api";import { MAX_BATCHES, toltImporter } from "./importer";import { ToltAffiliate, ToltCustomer } from "./types";@@ -75,6 +75,7 @@ export async function importReferrals({key: true,domain: true,url: true,+ projectId: true,},},},@@ -127,7 +128,7 @@ async function createReferral({customer: Omit<ToltCustomer, "partner">;partner: ToltAffiliate;workspace: Pick<Project, "id" | "stripeConnectId">;- links: Pick<Link, "id" | "key" | "domain" | "url">[];+ links: Pick<Link, "id" | "key" | "domain" | "url" | "projectId">[];}) {if (links.length === 0) {console.log("Link not found for referral, skipping...", {@@ -155,32 +156,9 @@ async function createReferral({const link = links[0];- const dummyRequest = new Request(link.url, {- headers: new Headers({- "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",- "x-forwarded-for": "127.0.0.1",- "x-vercel-ip-country": "US",- "x-vercel-ip-country-region": "CA",- "x-vercel-ip-continent": "NA",- }),- });-- const clickData = await recordClick({- req: dummyRequest,- linkId: link.id,- clickId: nanoid(16),- url: link.url,- domain: link.domain,- key: link.key,- workspaceId: workspace.id,- skipRatelimit: true,- timestamp: new Date(customer.created_at).toISOString(),- });-- const clickEvent = clickEventSchemaTB.parse({- ...clickData,- bot: 0,- qr: 0,+ const clickEvent = await recordFakeClick({+ link,+ timestamp: customer.created_at,});const customerId = createId({ prefix: "cus_" });