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.

Merge branch 'main' into link-based-coupon-codes

Kiran K08456cd14c89
diff --cc apps/web/lib/actions/partners/create-commission.ts
index 743a04c7a2,77ed431a9b..e0df5508aa
--- a/apps/web/lib/actions/partners/create-commission.ts
+++ b/apps/web/lib/actions/partners/create-commission.ts
@@@ -121,17 -124,47 +123,21 @@@ export const createCommissionAction = a
leadEvent = leadEventSchemaTB.parse(existingLeadEvent.data[0]);
} 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
+ // 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();
- 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,
++ ...(customer.country && {
++ country: customer.country,
++ continent: COUNTRIES_TO_CONTINENTS[customer.country.toUpperCase()],
++ }),
});
- clickEvent = clickEventSchemaTB.parse({
- ...clickData,
- bot: 0,
- qr: 0,
- });
const leadEventId = nanoid(16);
+
leadEvent = leadEventSchemaTB.parse({
...clickEvent,
event_id: leadEventId,
diff --cc apps/web/lib/partnerstack/import-customers.ts
index 7a7dfd99c0,165c7d8372..8ca03ab917
--- a/apps/web/lib/partnerstack/import-customers.ts
+++ b/apps/web/lib/partnerstack/import-customers.ts
@@@ -2,9 -2,10 +2,11 @@@ 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 { 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,
@@@ -143,15 -142,28 +144,28 @@@ async function createCustomer(
workspace,
links,
customer,
+ importId,
}: {
workspace: Pick<Project, "id" | "stripeConnectId">;
- links: Pick<Link, "id" | "key" | "domain" | "url">[];
+ links: Pick<Link, "id" | "key" | "domain" | "url" | "projectId">[];
customer: PartnerStackCustomer;
+ importId: string;
}) {
+ const commonImportLogInputs = {
+ workspace_id: workspace.id,
+ import_id: importId,
+ source: "partnerstack",
+ entity: "customer",
+ entity_id: customer.customer_key || customer.email,
+ } as const;
+
if (links.length === 0) {
- console.log(
- `Link not found for customer. See the details at https://app.partnerstack.com/partners/${customer.partnership_key}/details`,
- );
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "LINK_NOT_FOUND",
+ message: `Link not found for customer ${customer.customer_key}.`,
+ });
+
return;
}
diff --cc apps/web/lib/rewardful/import-customers.ts
index 61faf3cbbe,02e2bc7bdc..52d29c1760
--- a/apps/web/lib/rewardful/import-customers.ts
+++ b/apps/web/lib/rewardful/import-customers.ts
@@@ -2,19 -2,17 +2,16 @@@ import { prisma } from "@dub/prisma"
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 { 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";
+ import { RewardfulImportPayload, RewardfulReferral } from "./types";
+
+ export async function importCustomers(payload: RewardfulImportPayload) {
+ const { importId, programId, campaignId, page = 1 } = payload;
- export async function importReferrals({
- programId,
- page,
- }: {
- programId: string;
- page: number;
- }) {
const { workspace, ...program } = await prisma.program.findUniqueOrThrow({
where: {
id: programId,
diff --cc apps/web/lib/tolt/import-customers.ts
index 05245e5f17,40cc54387e..e9ffcc05f6
--- a/apps/web/lib/tolt/import-customers.ts
+++ b/apps/web/lib/tolt/import-customers.ts
@@@ -2,19 -2,16 +2,17 @@@ 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 { logImportError } from "../tinybird/log-import-error";
-import { clickEventSchemaTB } from "../zod/schemas/clicks";
+import { recordFakeClick } from "../tinybird/record-fake-click";
import { ToltApi } from "./api";
import { MAX_BATCHES, toltImporter } from "./importer";
- import { ToltAffiliate, ToltCustomer } from "./types";
-import { ToltAffiliate, ToltCustomer, ToltImportPayload } from "./types";
++import { ToltCustomer, ToltImportPayload } from "./types";
+
+ export async function importCustomers(payload: ToltImportPayload) {
+ let { importId, programId, toltProgramId, startingAfter } = payload;
- export async function importReferrals({
- programId,
- startingAfter,
- }: {
- programId: string;
- startingAfter?: string;
- }) {
const { workspace } = await prisma.program.findUniqueOrThrow({
where: {
id: programId,
@@@ -99,8 -93,9 +95,9 @@@
createReferral({
workspace,
customer,
-- partner,
++
links: partnerEmailToLinks.get(partner.email) ?? [],
+ importId,
}),
),
);
@@@ -123,18 -118,30 +120,29 @@@ 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">[];
+ links: Pick<Link, "id" | "key" | "domain" | "url" | "projectId">[];
+ importId: string;
}) {
+ const commonImportLogInputs = {
+ workspace_id: workspace.id,
+ import_id: importId,
+ source: "tolt",
+ entity: "customer",
+ entity_id: customer.customer_id,
+ } as const;
+
if (links.length === 0) {
- console.log("Link not found for referral, skipping...", {
- customerEmail: customer.email,
- partnerEmail: partner.email,
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "LINK_NOT_FOUND",
+ message: `Link not found for customer ${customer.customer_id}`,
});
+
return;
}
diff --cc apps/web/lib/zod/schemas/programs.ts
index 930e2c266b,7f283fc242..486a177db5
--- a/apps/web/lib/zod/schemas/programs.ts
+++ b/apps/web/lib/zod/schemas/programs.ts
@@@ -24,11 -29,12 +29,13 @@@ export const ProgramSchema = z.object(
cookieLength: z.number(),
holdingPeriodDays: z.number(),
minPayoutAmount: z.number(),
- linkStructure: z.nativeEnum(LinkStructure),
+ linkStructure: z.nativeEnum(PartnerLinkStructure),
linkParameter: z.string().nullish(),
+ urlValidationMode: z.nativeEnum(PartnerUrlValidationMode),
+ maxPartnerLinks: z.number(),
landerPublishedAt: z.date().nullish(),
autoApprovePartnersEnabledAt: z.date().nullish(),
+ couponCodeTrackingEnabledAt: z.date().nullish(),
rewards: z.array(RewardSchema).nullish(),
discounts: z.array(DiscountSchema).nullish(),
defaultFolderId: z.string().nullable(),
diff --cc apps/web/ui/partners/add-edit-discount-sheet.tsx
index 4da118f616,aa922991d1..21fbb7eacc
--- a/apps/web/ui/partners/add-edit-discount-sheet.tsx
+++ b/apps/web/ui/partners/add-edit-discount-sheet.tsx
@@@ -95,9 -75,15 +95,16 @@@ function DiscountSheetContent(
formState: { errors },
} = useForm<FormData>({
defaultValues: {
+ amount:
+ discount?.type === "flat" ? discount.amount / 100 : discount?.amount,
+ type: discount?.type || "percentage",
+ maxDuration:
+ discount?.maxDuration === null ? Infinity : discount?.maxDuration || 0,
+ couponId: discount?.couponId || "",
+ couponTestId: discount?.couponTestId || "",
includedPartnerIds: null,
excludedPartnerIds: null,
+ maxDuration: 0,
},
});
diff --cc packages/prisma/schema/program.prisma
index d5459c1653,9e1b5834f2..60dd2d14a5
--- a/packages/prisma/schema/program.prisma
+++ b/packages/prisma/schema/program.prisma
@@@ -23,34 -23,41 +23,41 @@@ enum PartnerLinkStructure
path
}
+ enum PartnerUrlValidationMode {
+ domain // domain match (e.g. if URL is example.com/path, example.com and example.com/another-path are allowed)
+ exact // exact match (e.g. if URL is example.com/path, only example.com/path is allowed)
+ }
+
model Program {
- id String @id @default(cuid())
- id String @id @default(cuid())
- workspaceId String
- defaultFolderId String?
- name String
- slug String @unique
- logo String?
- wordmark String?
- brandColor String?
- domain String?
- url String?
- urlValidationMode PartnerUrlValidationMode @default(domain)
- linkStructure PartnerLinkStructure @default(short)
- linkParameter String? // null for SHORT, "via" for QUERY, "refer" for PATH
- maxPartnerLinks Int @default(10) // maximum number of links partners can create
-
- cookieLength Int @default(90)
- holdingPeriodDays Int @default(0) // number of days to wait before earnings are added to a payout
- minPayoutAmount Int @default(0) // Default minimum payout amount of $0
- embedData Json? @db.Json
- landerData Json? @db.Json
++ id String @id @default(cuid())
+ workspaceId String
+ defaultFolderId String?
+ name String
- slug String @unique
++ slug String @unique
+ logo String?
+ wordmark String?
+ brandColor String?
+ domain String?
+ url String?
- cookieLength Int @default(90)
- holdingPeriodDays Int @default(0) // number of days to wait before earnings are added to a payout
- minPayoutAmount Int @default(0) // Default minimum payout amount of $0
- embedData Json? @db.Json
- landerData Json? @db.Json
++ urlValidationMode PartnerUrlValidationMode @default(domain)
++ linkStructure PartnerLinkStructure @default(short)
++ linkParameter String? // null for SHORT, "via" for QUERY, "refer" for PATH
++ maxPartnerLinks Int @default(10) // maximum number of links partners can create
++ cookieLength Int @default(90)
++ holdingPeriodDays Int @default(0) // number of days to wait before earnings are added to a payout
++ minPayoutAmount Int @default(0) // Default minimum payout amount of $0
++ embedData Json? @db.Json
++ landerData Json? @db.Json
landerPublishedAt DateTime?
- resources Json? @db.Json
- termsUrl String? @db.Text
- helpUrl String? @db.Text
- resources Json? @db.Json
- termsUrl String? @db.Text
- helpUrl String? @db.Text
++ resources Json? @db.Json
++ termsUrl String? @db.Text
++ helpUrl String? @db.Text
supportEmail String?
ageVerification Int?
- linkStructure LinkStructure @default(short)
- linkParameter String? // null for SHORT, "via" for QUERY, "refer" for PATH
autoApprovePartnersEnabledAt DateTime?
- createdAt DateTime @default(now())
- updatedAt DateTime @updatedAt
+ couponCodeTrackingEnabledAt DateTime?
- createdAt DateTime @default(now())
- updatedAt DateTime @updatedAt
++ createdAt DateTime @default(now())
++ updatedAt DateTime @updatedAt
workspace Project @relation(fields: [workspaceId], references: [id])
primaryDomain Domain? @relation(fields: [domain], references: [slug], onUpdate: Cascade)