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 HubSpot deal tracking and update webhook configs

Kiran K67c652cc1aaf
diff --git a/apps/web/app/(ee)/api/hubspot/webhook/route.ts b/apps/web/app/(ee)/api/hubspot/webhook/route.ts
index dc2484c7b1..abce13f6d2 100644
--- a/apps/web/app/(ee)/api/hubspot/webhook/route.ts
+++ b/apps/web/app/(ee)/api/hubspot/webhook/route.ts
@@ -2,13 +2,19 @@ import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { AxiomRequest, withAxiom } from "next-axiom";
import { NextResponse } from "next/server";
+// TODO:
+// Verify the webhook signature using client secret
+// https://developers.hubspot.com/docs/apps/legacy-apps/authentication/validating-requests
+
// POST /api/hubspot/webhook – listen to webhook events from Hubspot
export const POST = withAxiom(async (req: AxiomRequest) => {
console.log("Received webhook from Hubspot")
try {
const rawBody = await req.text();
- console.log(rawBody)
+ // console.log(rawBody)
+ const body = JSON.parse(rawBody);
+ console.log(body);
return NextResponse.json("OK");
} catch (error) {
diff --git a/apps/web/lib/integrations/hubspot/track-lead.ts b/apps/web/lib/integrations/hubspot/track-lead.ts
new file mode 100644
index 0000000000..6bca22b30f
--- /dev/null
+++ b/apps/web/lib/integrations/hubspot/track-lead.ts
@@ -0,0 +1,63 @@
+import { trackLead } from "@/lib/api/conversions/track-lead";
+import { WorkspaceProps } from "@/lib/types";
+import { trackLeadRequestSchema } from "@/lib/zod/schemas/leads";
+import { z } from "zod";
+
+const singularLeadEventSchema = z.object({
+ dub_id: z.string().min(1),
+ event_name: z.string().min(1),
+ event_attributes: z
+ .string()
+ .transform((val) => {
+ try {
+ return JSON.parse(val);
+ } catch {
+ throw new Error("Invalid JSON in event_attributes");
+ }
+ })
+ .pipe(
+ z.object({
+ customer_external_id: trackLeadRequestSchema.shape.customerExternalId,
+ customer_name: trackLeadRequestSchema.shape.customerName,
+ customer_email: trackLeadRequestSchema.shape.customerEmail,
+ customer_avatar: trackLeadRequestSchema.shape.customerAvatar,
+ event_quantity: trackLeadRequestSchema.shape.eventQuantity,
+ mode: trackLeadRequestSchema.shape.mode,
+ }),
+ ),
+});
+
+export const trackSingularLeadEvent = async ({
+ queryParams,
+ workspace,
+}: {
+ queryParams: Record<string, string>;
+ workspace: Pick<WorkspaceProps, "id" | "stripeConnectId" | "webhookEnabled">;
+}) => {
+ const {
+ dub_id: clickId,
+ event_name: eventName,
+ event_attributes: {
+ customer_external_id: customerExternalId,
+ customer_name: customerName,
+ customer_email: customerEmail,
+ customer_avatar: customerAvatar,
+ event_quantity: eventQuantity,
+ mode,
+ },
+ } = singularLeadEventSchema.parse(queryParams);
+
+ return await trackLead({
+ clickId,
+ eventName,
+ customerEmail,
+ customerAvatar,
+ customerExternalId,
+ customerName,
+ eventQuantity,
+ mode,
+ metadata: null,
+ workspace,
+ rawBody: queryParams,
+ });
+};
diff --git a/apps/web/lib/integrations/hubspot/track-sale.ts b/apps/web/lib/integrations/hubspot/track-sale.ts
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/apps/web/scripts/hubspot.ts b/apps/web/scripts/hubspot.ts
index 44323fff4d..1d912b9360 100644
--- a/apps/web/scripts/hubspot.ts
+++ b/apps/web/scripts/hubspot.ts
@@ -8,6 +8,7 @@ async function main() {
"crm.objects.contacts.read",
"crm.objects.contacts.write",
"crm.objects.leads.read",
+ "crm.objects.deals.read",
];
const REDIRECT_URI =
diff --git a/packages/hubspot-app/src/app/app-hsmeta.json b/packages/hubspot-app/src/app/app-hsmeta.json
index acbc98ead8..bc2d6c98da 100644
--- a/packages/hubspot-app/src/app/app-hsmeta.json
+++ b/packages/hubspot-app/src/app/app-hsmeta.json
@@ -15,7 +15,8 @@
"oauth",
"crm.objects.contacts.read",
"crm.objects.contacts.write",
- "crm.objects.leads.read"
+ "crm.objects.leads.read",
+ "crm.objects.deals.read"
],
"optionalScopes": [],
"conditionallyRequiredScopes": []
diff --git a/packages/hubspot-app/src/app/webhooks/webhooks-hsmeta.json b/packages/hubspot-app/src/app/webhooks/webhooks-hsmeta.json
index edb43fb7a7..c80f632da3 100644
--- a/packages/hubspot-app/src/app/webhooks/webhooks-hsmeta.json
+++ b/packages/hubspot-app/src/app/webhooks/webhooks-hsmeta.json
@@ -15,7 +15,13 @@
},
{
"subscriptionType": "object.creation",
- "objectType": "lead",
+ "objectType": "deal",
+ "active": true
+ },
+ {
+ "subscriptionType": "object.propertyChange",
+ "propertyName": "amount",
+ "objectType": "deal",
"active": true
}
]