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.

track the sale event

Kiran K258c6c4bd818
diff --git a/apps/web/app/(ee)/api/hubspot/webhook/route.ts b/apps/web/app/(ee)/api/hubspot/webhook/route.ts
index c91fc480fd..dd97e9b80f 100644
--- a/apps/web/app/(ee)/api/hubspot/webhook/route.ts
+++ b/apps/web/app/(ee)/api/hubspot/webhook/route.ts
@@ -3,6 +3,7 @@ import { HUBSPOT_CLIENT_SECRET } from "@/lib/integrations/hubspot/constants";
import { refreshAccessToken } from "@/lib/integrations/hubspot/refresh-token";
import { hubSpotWebhookSchema } from "@/lib/integrations/hubspot/schema";
import { trackHubSpotLeadEvent } from "@/lib/integrations/hubspot/track-lead";
+import { trackHubSpotSaleEvent } from "@/lib/integrations/hubspot/track-sale";
import { HubSpotAuthToken } from "@/lib/integrations/hubspot/types";
import { prisma } from "@dub/prisma";
import crypto from "crypto";
@@ -57,6 +58,7 @@ export const POST = withAxiom(async (req: AxiomRequest) => {
}
});
+// Process individual event
async function processEvent(event: any) {
const { objectTypeId, portalId, subscriptionType } =
hubSpotWebhookSchema.parse(event);
@@ -118,10 +120,13 @@ async function processEvent(event: any) {
});
}
- // Track the sale event
+ // Track the sale event when deal is closed won
if (subscriptionType === "object.propertyChange") {
- // TODO:
- // Track sale
+ await trackHubSpotSaleEvent({
+ payload: event,
+ workspace,
+ authToken,
+ });
}
}
}
diff --git a/apps/web/lib/integrations/hubspot/schema.ts b/apps/web/lib/integrations/hubspot/schema.ts
index 97371ba040..97b2f8ffef 100644
--- a/apps/web/lib/integrations/hubspot/schema.ts
+++ b/apps/web/lib/integrations/hubspot/schema.ts
@@ -37,7 +37,7 @@ export const hubSpotDealSchema = z.object({
properties: z.object({
dealname: z.string(),
amount: z.string(),
- dealstage: z.enum(["appointmentscheduled"]),
+ dealstage: z.string(),
}),
associations: z.object({
contacts: z.object({
@@ -60,6 +60,13 @@ export const hubSpotWebhookSchema = z.object({
export const hubSpotLeadEventSchema = z.object({
objectId: z.number(),
- subscriptionType: z.enum(["object.creation"]),
+ subscriptionType: z.literal("object.creation"),
objectTypeId: z.enum(HUBSPOT_OBJECT_TYPE_IDS),
});
+
+export const hubSpotSaleEventSchema = z.object({
+ objectId: z.number(),
+ subscriptionType: z.literal("object.propertyChange"),
+ propertyName: z.literal("dealstage"),
+ propertyValue: z.string(), // eg: closedwon
+});
diff --git a/apps/web/lib/integrations/hubspot/track-lead.ts b/apps/web/lib/integrations/hubspot/track-lead.ts
index c01e2d23d7..bdf0699dd8 100644
--- a/apps/web/lib/integrations/hubspot/track-lead.ts
+++ b/apps/web/lib/integrations/hubspot/track-lead.ts
@@ -17,8 +17,12 @@ export const trackHubSpotLeadEvent = async ({
const { objectId, objectTypeId, subscriptionType } =
hubSpotLeadEventSchema.parse(payload);
- // A new contact is created
- if (objectTypeId === "0-1" && subscriptionType === "object.creation") {
+ if (subscriptionType !== "object.creation") {
+ return;
+ }
+
+ // A new contact is created (deferred lead tracking)
+ if (objectTypeId === "0-1") {
const contact = await getHubSpotContact({
contactId: objectId,
accessToken: authToken.access_token,
@@ -47,7 +51,7 @@ export const trackHubSpotLeadEvent = async ({
}
// A deal is created for the contact (Eg: lead is tracked)
- if (objectTypeId === "0-3" && subscriptionType === "object.creation") {
+ if (objectTypeId === "0-3") {
const deal = await getHubSpotDeal({
dealId: objectId,
accessToken: authToken.access_token,
@@ -66,7 +70,7 @@ export const trackHubSpotLeadEvent = async ({
return;
}
- // HubSpot doesn't return the contact properties in the deal associations,
+ // HubSpot doesn't return the contact properties in the deal associations,
// so we need to get it separately
const contactInfo = await getHubSpotContact({
contactId: contact.id,
diff --git a/apps/web/lib/integrations/hubspot/track-sale.ts b/apps/web/lib/integrations/hubspot/track-sale.ts
index e69de29bb2..b829157e2a 100644
--- a/apps/web/lib/integrations/hubspot/track-sale.ts
+++ b/apps/web/lib/integrations/hubspot/track-sale.ts
@@ -0,0 +1,73 @@
+import { trackSale } from "@/lib/api/conversions/track-sale";
+import { WorkspaceProps } from "@/lib/types";
+import { getHubSpotContact } from "./get-contact";
+import { getHubSpotDeal } from "./get-deal";
+import { hubSpotSaleEventSchema } from "./schema";
+import { HubSpotAuthToken } from "./types";
+
+export const trackHubSpotSaleEvent = async ({
+ payload,
+ workspace,
+ authToken,
+}: {
+ payload: Record<string, any>;
+ workspace: Pick<WorkspaceProps, "id" | "stripeConnectId" | "webhookEnabled">;
+ authToken: HubSpotAuthToken;
+}) => {
+ const { objectId, subscriptionType, propertyName, propertyValue } =
+ hubSpotSaleEventSchema.parse(payload);
+
+ if (subscriptionType !== "object.propertyChange") {
+ console.error(`[HubSpot] Unknown subscriptionType ${subscriptionType}`);
+ return;
+ }
+
+ if (propertyName !== "dealstage") {
+ console.error(`[HubSpot] Unknown propertyName ${propertyName}`);
+ return;
+ }
+
+ if (propertyValue !== "closedwon") {
+ console.error(`[HubSpot] Unknown propertyValue ${propertyValue}`);
+ return;
+ }
+
+ const deal = await getHubSpotDeal({
+ dealId: objectId,
+ accessToken: authToken.access_token,
+ });
+
+ if (!deal) {
+ return;
+ }
+
+ const { id: dealId, properties, associations } = deal;
+
+ // Find the contact associated with the deal
+ const contact = associations.contacts.results[0];
+
+ if (!contact) {
+ return;
+ }
+
+ // HubSpot doesn't return the contact properties in the deal associations,
+ // so we need to get it separately
+ const contactInfo = await getHubSpotContact({
+ contactId: contact.id,
+ accessToken: authToken.access_token,
+ });
+
+ if (!contactInfo) {
+ return;
+ }
+
+ return await trackSale({
+ customerExternalId: contactInfo.properties.email,
+ amount: Number(properties.amount) * 100,
+ eventName: properties.dealstage,
+ paymentProcessor: "custom",
+ invoiceId: dealId,
+ workspace,
+ rawBody: deal,
+ });
+};
diff --git a/packages/hubspot-app/src/app/webhooks/webhooks-hsmeta.json b/packages/hubspot-app/src/app/webhooks/webhooks-hsmeta.json
index c80f632da3..834fc99521 100644
--- a/packages/hubspot-app/src/app/webhooks/webhooks-hsmeta.json
+++ b/packages/hubspot-app/src/app/webhooks/webhooks-hsmeta.json
@@ -20,7 +20,7 @@
},
{
"subscriptionType": "object.propertyChange",
- "propertyName": "amount",
+ "propertyName": "dealstage",
"objectType": "deal",
"active": true
}