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.

Enhance HubSpot integration by adding customer lookup in trackLead and trackSale functions, improving error handling for missing customers.

Kiran K7fc6e0807574
diff --git a/apps/web/lib/integrations/hubspot/track-lead.ts b/apps/web/lib/integrations/hubspot/track-lead.ts
index a0996ce67f..4fd994c747 100644
--- a/apps/web/lib/integrations/hubspot/track-lead.ts
+++ b/apps/web/lib/integrations/hubspot/track-lead.ts
@@ -98,10 +98,27 @@ export const trackHubSpotLeadEvent = async ({
return;
}
+ const customer = await prisma.customer.findFirst({
+ where: {
+ projectId: workspace.id,
+ OR: [
+ { externalId: contactInfo.id },
+ { externalId: contactInfo.properties.email },
+ ],
+ },
+ });
+
+ if (!customer) {
+ console.error(
+ `[HubSpot] No customer found for contact ID ${contactInfo.id} or email ${contactInfo.properties.email}.`,
+ );
+ return;
+ }
+
const trackLeadResult = await trackLead({
clickId: "",
eventName: `Deal ${properties.dealstage}`,
- customerExternalId: contactInfo.properties.email,
+ customerExternalId: customer.externalId!,
customerName: `${contactInfo.properties.firstname} ${contactInfo.properties.lastname}`,
customerEmail: contactInfo.properties.email,
mode: "async",
@@ -156,10 +173,27 @@ export const trackHubSpotLeadEvent = async ({
return;
}
+ const customer = await prisma.customer.findFirst({
+ where: {
+ projectId: workspace.id,
+ OR: [
+ { externalId: contactInfo.id },
+ { externalId: contactInfo.properties.email },
+ ],
+ },
+ });
+
+ if (!customer) {
+ console.error(
+ `[HubSpot] No customer found for contact ID ${contactInfo.id} or email ${contactInfo.properties.email}.`,
+ );
+ return;
+ }
+
const trackLeadResult = await trackLead({
clickId: "",
eventName: `Contact ${properties.lifecyclestage}`,
- customerExternalId: contactInfo.properties.email,
+ customerExternalId: customer.externalId!,
customerName: `${contactInfo.properties.firstname} ${contactInfo.properties.lastname}`,
customerEmail: contactInfo.properties.email,
mode: "async",
diff --git a/apps/web/lib/integrations/hubspot/track-sale.ts b/apps/web/lib/integrations/hubspot/track-sale.ts
index 4fb71e8a71..3e158622f1 100644
--- a/apps/web/lib/integrations/hubspot/track-sale.ts
+++ b/apps/web/lib/integrations/hubspot/track-sale.ts
@@ -1,5 +1,6 @@
import { trackSale } from "@/lib/api/conversions/track-sale";
import { WorkspaceProps } from "@/lib/types";
+import { prisma } from "@dub/prisma";
import { z } from "zod";
import { HubSpotAuthToken } from "../types";
import { HubSpotApi } from "./api";
@@ -71,8 +72,25 @@ export const trackHubSpotSaleEvent = async ({
return;
}
+ const customer = await prisma.customer.findFirst({
+ where: {
+ projectId: workspace.id,
+ OR: [
+ { externalId: contactInfo.id },
+ { externalId: contactInfo.properties.email },
+ ],
+ },
+ });
+
+ if (!customer) {
+ console.error(
+ `[HubSpot] No customer found for contact ID ${contactInfo.id} or email ${contactInfo.properties.email}.`,
+ );
+ return;
+ }
+
return await trackSale({
- customerExternalId: contactInfo.properties.email,
+ customerExternalId: customer.externalId!,
amount: Number(properties.amount) * 100,
eventName: `${properties.dealname} ${properties.dealstage}`,
paymentProcessor: "custom",