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 support for Singular event tracking in webhook API

Kiran Kdeaceb157518
diff --git a/apps/web/app/(ee)/api/singular/webhook/route.ts b/apps/web/app/(ee)/api/singular/webhook/route.ts
index bfe319ed02..6754bedfcc 100644
--- a/apps/web/app/(ee)/api/singular/webhook/route.ts
+++ b/apps/web/app/(ee)/api/singular/webhook/route.ts
@@ -6,6 +6,20 @@ import { prisma } from "@dub/prisma";
import { getSearchParams } from "@dub/utils";
import { NextResponse } from "next/server";
import { z } from "zod";
+import { trackSingularLeadEvent } from "./track-lead-event";
+import { trackSingularSaleEvent } from "./track-sale-event";
+
+const supportedEvents = [
+ "sng_complete_registration",
+ "sng_subscribe",
+ "sng_ecommerce_purchase",
+];
+
+const singularToDubEvent = {
+ sng_complete_registration: "lead",
+ sng_subscribe: "sale",
+ sng_ecommerce_purchase: "sale",
+};
const customEventSchema = z.object({
dub_id: z.string().min(1),
@@ -43,6 +57,13 @@ export const GET = async (req: Request) => {
install_time: installTime,
} = schema.parse(searchParams);
+ if (!supportedEvents.includes(eventName)) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: `Event ${eventName} is not supported by Singular <> Dub integration.`,
+ });
+ }
+
// Find the click event
let clickData: ClickEventTB | null = null;
const clickEvent = await getClickEvent({
@@ -97,6 +118,20 @@ export const GET = async (req: Request) => {
});
}
+ const dubEvent = singularToDubEvent[eventName];
+
+ if (dubEvent === "lead") {
+ await trackSingularLeadEvent({
+ clickData,
+ link,
+ });
+ } else if (dubEvent === "sale") {
+ await trackSingularSaleEvent({
+ clickData,
+ link,
+ });
+ }
+
return NextResponse.json({
clickId,
});
diff --git a/apps/web/app/(ee)/api/singular/webhook/track-lead-event.ts b/apps/web/app/(ee)/api/singular/webhook/track-lead-event.ts
index e69de29bb2..16b31a8602 100644
--- a/apps/web/app/(ee)/api/singular/webhook/track-lead-event.ts
+++ b/apps/web/app/(ee)/api/singular/webhook/track-lead-event.ts
@@ -0,0 +1,70 @@
+import { includeTags } from "@/lib/api/links/include-tags";
+import { createPartnerCommission } from "@/lib/partners/create-partner-commission";
+import { logConversionEvent } from "@/lib/tinybird/log-conversion-events";
+import { ClickEventTB } from "@/lib/types";
+import { prisma } from "@dub/prisma";
+import { Link } from "@dub/prisma/client";
+import { nanoid } from "@dub/utils";
+
+export const trackSingularLeadEvent = async ({
+ clickData,
+ link,
+}: {
+ clickData: ClickEventTB;
+ link: Link;
+}) => {
+ const eventQuantity = 1;
+ const eventId = nanoid(16);
+ const body = {};
+ const customer = {
+ id: "xxx",
+ };
+
+ // TODO:
+ // Record lead
+ // Send "lead.created" webhook
+
+ await Promise.allSettled([
+ prisma.link.update({
+ where: {
+ id: link.id,
+ },
+ data: {
+ leads: {
+ increment: eventQuantity,
+ },
+ },
+ include: includeTags,
+ }),
+
+ prisma.project.update({
+ where: {
+ id: link.projectId!,
+ },
+ data: {
+ usage: {
+ increment: eventQuantity,
+ },
+ },
+ }),
+
+ logConversionEvent({
+ workspace_id: link.projectId!,
+ link_id: link.id,
+ path: "/track/lead",
+ body: JSON.stringify(body),
+ }),
+ ]);
+
+ if (link.programId && link.partnerId && customer) {
+ await createPartnerCommission({
+ event: "lead",
+ programId: link.programId,
+ partnerId: link.partnerId,
+ linkId: link.id,
+ eventId,
+ customerId: customer.id,
+ quantity: eventQuantity,
+ });
+ }
+};
diff --git a/apps/web/app/(ee)/api/singular/webhook/track-sale-event.ts b/apps/web/app/(ee)/api/singular/webhook/track-sale-event.ts
index e69de29bb2..b0dd14d94b 100644
--- a/apps/web/app/(ee)/api/singular/webhook/track-sale-event.ts
+++ b/apps/web/app/(ee)/api/singular/webhook/track-sale-event.ts
@@ -0,0 +1,103 @@
+import { includeTags } from "@/lib/api/links/include-tags";
+import { notifyPartnerSale } from "@/lib/api/partners/notify-partner-sale";
+import { createPartnerCommission } from "@/lib/partners/create-partner-commission";
+import { logConversionEvent } from "@/lib/tinybird/log-conversion-events";
+import { ClickEventTB } from "@/lib/types";
+import { prisma } from "@dub/prisma";
+import { Link } from "@dub/prisma/client";
+import { nanoid } from "@dub/utils";
+import { waitUntil } from "@vercel/functions";
+
+export const trackSingularSaleEvent = async ({
+ clickData,
+ link,
+}: {
+ clickData: ClickEventTB;
+ link: Link;
+}) => {
+ const eventQuantity = 1;
+ const eventId = nanoid(16);
+ const body = {};
+ const customer = {
+ id: "xxx",
+ };
+ const amount = 100;
+
+ // TODO:
+ // Record sale
+ // Send "sale.created" webhook
+ // Notify partner sale
+
+ await Promise.allSettled([
+ prisma.link.update({
+ where: {
+ id: link.id,
+ },
+ data: {
+ sales: {
+ increment: 1,
+ },
+ saleAmount: {
+ increment: amount,
+ },
+ },
+ include: includeTags,
+ }),
+
+ prisma.project.update({
+ where: {
+ id: link.projectId!,
+ },
+ data: {
+ usage: {
+ increment: 1,
+ },
+ },
+ }),
+
+ prisma.customer.update({
+ where: {
+ id: customer.id,
+ },
+ data: {
+ sales: {
+ increment: 1,
+ },
+ saleAmount: {
+ increment: amount,
+ },
+ },
+ }),
+
+ logConversionEvent({
+ workspace_id: link.projectId!,
+ link_id: link.id,
+ path: "/track/sale",
+ body: JSON.stringify(body),
+ }),
+ ]);
+
+ if (link.programId && link.partnerId && customer) {
+ const commission = await createPartnerCommission({
+ event: "sale",
+ programId: link.programId,
+ partnerId: link.partnerId,
+ linkId: link.id,
+ eventId,
+ customerId: customer.id,
+ amount,
+ quantity: 1,
+ invoiceId: "xxx",
+ currency: "usd",
+ });
+
+ if (commission) {
+ waitUntil(
+ notifyPartnerSale({
+ link,
+ commission,
+ }),
+ );
+ }
+ }
+};