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.

revert some changes (will do this in separate PR)

Kiran K1ce2c45f529a
diff --git a/apps/web/lib/actions/partners/create-commission.ts b/apps/web/lib/actions/partners/create-commission.ts
index f1c918f421..a220254df3 100644
--- a/apps/web/lib/actions/partners/create-commission.ts
+++ b/apps/web/lib/actions/partners/create-commission.ts
@@ -5,10 +5,11 @@ import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-progr
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { createPartnerCommission } from "@/lib/partners/create-partner-commission";
import { getLeadEvent } from "@/lib/tinybird";
-import { recordFakeClick } from "@/lib/tinybird/record-fake-click";
+import { recordClick } from "@/lib/tinybird/record-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";
@@ -126,20 +127,46 @@ 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 (using the customer's country if available)
+ 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",
+ ...(customer.country
+ ? {
+ "x-vercel-ip-country": customer.country.toUpperCase(),
+ "x-vercel-ip-continent":
+ COUNTRIES_TO_CONTINENTS[customer.country.toUpperCase()],
+ }
+ : {
+ "x-vercel-ip-country": "US",
+ "x-vercel-ip-country-region": "CA",
+ "x-vercel-ip-continent": "NA",
+ }),
+ }),
+ });
const finalLeadEventDate = leadEventDate ?? saleEventDate ?? new Date();
- clickEvent = await recordFakeClick({
- link,
- timestamp: new Date(finalLeadEventDate).getTime() - 5 * 60 * 1000,
- ...(customer.country && {
- country: customer.country,
- continent: COUNTRIES_TO_CONTINENTS[customer.country.toUpperCase()],
- }),
+ 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 = clickEventSchemaTB.parse({
+ ...clickData,
+ bot: 0,
+ qr: 0,
+ });
const leadEventId = nanoid(16);
-
leadEvent = leadEventSchemaTB.parse({
...clickEvent,
event_id: leadEventId,
@@ -147,7 +174,7 @@ export const createCommissionAction = authActionClient
customer_id: customerId,
});
- shouldUpdateCustomer = !customer.linkId && clickEvent ? true : false;
+ shouldUpdateCustomer = !customer.linkId && clickData ? true : false;
await Promise.allSettled([
recordLeadWithTimestamp({
diff --git a/apps/web/lib/partnerstack/import-customers.ts b/apps/web/lib/partnerstack/import-customers.ts
index 8ca03ab917..165c7d8372 100644
--- a/apps/web/lib/partnerstack/import-customers.ts
+++ b/apps/web/lib/partnerstack/import-customers.ts
@@ -2,11 +2,10 @@ import { prisma } from "@dub/prisma";
import { nanoid } from "@dub/utils";
import { Link, Project } from "@prisma/client";
import { createId } from "../api/create-id";
-
-import { recordLeadWithTimestamp } from "../tinybird";
+import { recordClick, recordLeadWithTimestamp } from "../tinybird";
import { logImportError } from "../tinybird/log-import-error";
-import { recordFakeClick } from "../tinybird/record-fake-click";
import { redis } from "../upstash";
+import { clickEventSchemaTB } from "../zod/schemas/clicks";
import { PartnerStackApi } from "./api";
import {
MAX_BATCHES,
@@ -92,7 +91,6 @@ export async function importCustomers(payload: PartnerStackImportPayload) {
key: true,
domain: true,
url: true,
- projectId: true,
},
},
},
@@ -147,7 +145,7 @@ async function createCustomer({
importId,
}: {
workspace: Pick<Project, "id" | "stripeConnectId">;
- links: Pick<Link, "id" | "key" | "domain" | "url" | "projectId">[];
+ links: Pick<Link, "id" | "key" | "domain" | "url">[];
customer: PartnerStackCustomer;
importId: string;
}) {
@@ -194,9 +192,32 @@ async function createCustomer({
const link = links[0];
- const clickEvent = await recordFakeClick({
- link,
- timestamp: customer.created_at,
+ 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 customerId = createId({ prefix: "cus_" });
diff --git a/apps/web/lib/rewardful/import-customers.ts b/apps/web/lib/rewardful/import-customers.ts
index 52d29c1760..02e2bc7bdc 100644
--- a/apps/web/lib/rewardful/import-customers.ts
+++ b/apps/web/lib/rewardful/import-customers.ts
@@ -3,8 +3,9 @@ import { nanoid } from "@dub/utils";
import { Program, Project } from "@prisma/client";
import { createId } from "../api/create-id";
import { logImportError } from "../tinybird/log-import-error";
-import { recordFakeClick } from "../tinybird/record-fake-click";
+import { recordClick } from "../tinybird/record-click";
import { recordLeadWithTimestamp } from "../tinybird/record-lead";
+import { clickEventSchemaTB } from "../zod/schemas/clicks";
import { RewardfulApi } from "./api";
import { MAX_BATCHES, rewardfulImporter } from "./importer";
import { RewardfulImportPayload, RewardfulReferral } from "./types";
@@ -157,9 +158,32 @@ async function createCustomer({
return;
}
- const clickEvent = await recordFakeClick({
- link,
- timestamp: referral.created_at,
+ 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 customerId = createId({ prefix: "cus_" });
diff --git a/apps/web/lib/tolt/import-customers.ts b/apps/web/lib/tolt/import-customers.ts
index e9ffcc05f6..40cc54387e 100644
--- a/apps/web/lib/tolt/import-customers.ts
+++ b/apps/web/lib/tolt/import-customers.ts
@@ -2,13 +2,12 @@ import { prisma } from "@dub/prisma";
import { nanoid } from "@dub/utils";
import { Link, Project } from "@prisma/client";
import { createId } from "../api/create-id";
-
-import { recordLeadWithTimestamp } from "../tinybird";
+import { recordClick, recordLeadWithTimestamp } from "../tinybird";
import { logImportError } from "../tinybird/log-import-error";
-import { recordFakeClick } from "../tinybird/record-fake-click";
+import { clickEventSchemaTB } from "../zod/schemas/clicks";
import { ToltApi } from "./api";
import { MAX_BATCHES, toltImporter } from "./importer";
-import { ToltCustomer, ToltImportPayload } from "./types";
+import { ToltAffiliate, ToltCustomer, ToltImportPayload } from "./types";
export async function importCustomers(payload: ToltImportPayload) {
let { importId, programId, toltProgramId, startingAfter } = payload;
@@ -71,7 +70,6 @@ export async function importCustomers(payload: ToltImportPayload) {
key: true,
domain: true,
url: true,
- projectId: true,
},
},
},
@@ -95,7 +93,7 @@ export async function importCustomers(payload: ToltImportPayload) {
createReferral({
workspace,
customer,
-
+ partner,
links: partnerEmailToLinks.get(partner.email) ?? [],
importId,
}),
@@ -120,12 +118,13 @@ async function createReferral({
customer,
workspace,
links,
+ partner,
importId,
}: {
customer: Omit<ToltCustomer, "partner">;
-
+ partner: ToltAffiliate;
workspace: Pick<Project, "id" | "stripeConnectId">;
- links: Pick<Link, "id" | "key" | "domain" | "url" | "projectId">[];
+ links: Pick<Link, "id" | "key" | "domain" | "url">[];
importId: string;
}) {
const commonImportLogInputs = {
@@ -164,9 +163,32 @@ async function createReferral({
const link = links[0];
- const clickEvent = await recordFakeClick({
- link,
- timestamp: customer.created_at,
+ 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 customerId = createId({ prefix: "cus_" });