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 settings with lead trigger event options and improved schema validation

Kiran K894a3425cf21
diff --git a/apps/web/app/(ee)/api/hubspot/webhook/route.ts b/apps/web/app/(ee)/api/hubspot/webhook/route.ts
index 32cce2f85c..b9343a0172 100644
--- a/apps/web/app/(ee)/api/hubspot/webhook/route.ts
+++ b/apps/web/app/(ee)/api/hubspot/webhook/route.ts
@@ -102,6 +102,12 @@ async function processWebhookEvent(event: any) {
return;
}
+ const integrationSettings = hubSpotSettingsSchema.parse(
+ installation.settings ?? {},
+ );
+
+ console.log("[HubSpot] Integration settings", integrationSettings);
+
// Track a deferred lead event
if (objectTypeId === "0-1") {
await trackHubSpotLeadEvent({
@@ -123,13 +129,11 @@ async function processWebhookEvent(event: any) {
// Track the sale event when deal is closed won
if (subscriptionType === "object.propertyChange") {
- const settings = hubSpotSettingsSchema.parse(installation.settings ?? {});
-
await trackHubSpotSaleEvent({
payload: event,
workspace,
authToken,
- closedWonDealStageId: settings?.closedWonDealStageId,
+ closedWonDealStageId: integrationSettings?.closedWonDealStageId,
});
}
}
diff --git a/apps/web/lib/integrations/hubspot/constants.ts b/apps/web/lib/integrations/hubspot/constants.ts
index 6d80c16be2..90bf30ae78 100644
--- a/apps/web/lib/integrations/hubspot/constants.ts
+++ b/apps/web/lib/integrations/hubspot/constants.ts
@@ -3,8 +3,19 @@ export const HUBSPOT_OBJECT_TYPE_IDS = [
"0-3", // deal
] as const;
+// Reomve this
export const HUBSPOT_DEFAULT_CLOSED_WON_DEAL_STAGE_ID = "closedwon";
+export const HUBSPOT_DEFAULT_SETTINGS = {
+ leadTriggerEvent: "dealCreated",
+ closedWonDealStageId: HUBSPOT_DEFAULT_CLOSED_WON_DEAL_STAGE_ID,
+};
+
+export const LEAD_TRIGGER_EVENT_OPTIONS = [
+ "lifecycleStageReached",
+ "dealCreated",
+] as const;
+
export const HUBSPOT_DUB_CONTACT_PROPERTIES = [
{
label: "Dub Click ID",
diff --git a/apps/web/lib/integrations/hubspot/schema.ts b/apps/web/lib/integrations/hubspot/schema.ts
index a49f89030b..d95fefe1c6 100644
--- a/apps/web/lib/integrations/hubspot/schema.ts
+++ b/apps/web/lib/integrations/hubspot/schema.ts
@@ -2,6 +2,7 @@ import z from "@/lib/zod";
import {
HUBSPOT_DEFAULT_CLOSED_WON_DEAL_STAGE_ID,
HUBSPOT_OBJECT_TYPE_IDS,
+ LEAD_TRIGGER_EVENT_OPTIONS,
} from "./constants";
// Authentication
@@ -17,7 +18,7 @@ export const hubSpotAuthTokenSchema = z.object({
// Integration settings
export const hubSpotSettingsSchema = z.object({
leadTriggerEvent: z
- .enum(["lifecycleStageReached", "dealCreated"])
+ .enum(LEAD_TRIGGER_EVENT_OPTIONS)
.nullish()
.default("dealCreated")
.describe(
diff --git a/apps/web/lib/integrations/hubspot/ui/settings.tsx b/apps/web/lib/integrations/hubspot/ui/settings.tsx
index 0f2932f5e9..d3ff1a1ca6 100644
--- a/apps/web/lib/integrations/hubspot/ui/settings.tsx
+++ b/apps/web/lib/integrations/hubspot/ui/settings.tsx
@@ -2,11 +2,17 @@
import useWorkspace from "@/lib/swr/use-workspace";
import { InstalledIntegrationInfoProps } from "@/lib/types";
-import { Button } from "@dub/ui";
+import { Button, CardSelector } from "@dub/ui";
import { useAction } from "next-safe-action/hooks";
import { useState } from "react";
import { toast } from "sonner";
-import { HUBSPOT_DEFAULT_CLOSED_WON_DEAL_STAGE_ID } from "../constants";
+import { z } from "zod";
+import {
+ HUBSPOT_DEFAULT_CLOSED_WON_DEAL_STAGE_ID,
+ HUBSPOT_DEFAULT_SETTINGS,
+ LEAD_TRIGGER_EVENT_OPTIONS,
+} from "../constants";
+import { hubSpotSettingsSchema } from "../schema";
import { updateHubSpotSettingsAction } from "../update-hubspot-settings";
export const HubSpotSettings = ({
@@ -14,9 +20,22 @@ export const HubSpotSettings = ({
settings,
}: InstalledIntegrationInfoProps) => {
const { id: workspaceId } = useWorkspace();
+
+ const hubSpotSettings = hubSpotSettingsSchema.parse({
+ ...HUBSPOT_DEFAULT_SETTINGS,
+ ...(settings as z.infer<typeof hubSpotSettingsSchema>),
+ });
+
+ const [leadTriggerEvent, setLeadTriggerEvent] = useState(
+ hubSpotSettings.leadTriggerEvent,
+ );
+
+ const [leadLifecycleStageId, setLeadLifecycleStageId] = useState(
+ hubSpotSettings.leadLifecycleStageId,
+ );
+
const [closedWonDealStageId, setClosedWonDealStageId] = useState(
- (settings as any)?.closedWonDealStageId ||
- HUBSPOT_DEFAULT_CLOSED_WON_DEAL_STAGE_ID,
+ hubSpotSettings.closedWonDealStageId,
);
const { executeAsync, isPending } = useAction(updateHubSpotSettingsAction, {
@@ -37,7 +56,9 @@ export const HubSpotSettings = ({
await executeAsync({
workspaceId,
- closedWonDealStageId: closedWonDealStageId || null,
+ leadTriggerEvent,
+ leadLifecycleStageId,
+ closedWonDealStageId,
});
};
@@ -46,32 +67,95 @@ export const HubSpotSettings = ({
}
return (
- <form className="mt-4 flex items-end gap-2" onSubmit={onSubmit}>
- <div className="w-full rounded-lg border border-neutral-200 bg-white">
+ <form className="mt-4 space-y-4" onSubmit={onSubmit}>
+ <div className="rounded-lg border border-neutral-200 bg-white">
<div className="flex items-center gap-x-2 border-b border-neutral-200 px-4 py-4">
<p className="text-sm font-medium text-neutral-700">
- Closed Won Deal Stage ID
+ HubSpot Integration Settings
</p>
</div>
- <div className="p-4">
- <p className="text-sm leading-normal text-neutral-600">
- Enter the HubSpot deal stage ID that represents a closed won deal.
- This will be used to track when deals are marked as closed won in
- HubSpot.
- </p>
+ <div className="space-y-6 p-4">
+ <div>
+ <p className="mb-4 text-sm leading-normal text-neutral-600">
+ Choose when leads should be tracked in Dub. This determines the
+ trigger event for lead attribution.
+ </p>
+
+ <CardSelector
+ options={[
+ {
+ key: "lifecycleStageReached",
+ label: "Lifecycle Stage Reached",
+ description:
+ "Track leads when a contact reaches a specific lifecycle stage",
+ },
+ {
+ key: "dealCreated",
+ label: "New Deal Created",
+ description: "Track leads when a deal is created in HubSpot",
+ },
+ ]}
+ value={leadTriggerEvent ?? undefined}
+ onChange={(value) => {
+ const newValue =
+ value as (typeof LEAD_TRIGGER_EVENT_OPTIONS)[number];
+
+ setLeadTriggerEvent(newValue);
- <div className="relative mt-4 rounded-md shadow-sm">
- <input
- className="w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
- placeholder={`Enter deal stage ID (e.g., ${HUBSPOT_DEFAULT_CLOSED_WON_DEAL_STAGE_ID})`}
- type="text"
- autoComplete="off"
- name="closedWonDealStageId"
- value={closedWonDealStageId}
- onChange={(e) => setClosedWonDealStageId(e.target.value)}
+ if (newValue === "dealCreated") {
+ setLeadLifecycleStageId(null);
+ }
+ }}
+ gridCols="2"
/>
</div>
+
+ {leadTriggerEvent === "lifecycleStageReached" && (
+ <div>
+ <label className="mb-2 block text-sm font-semibold text-neutral-700">
+ Lead Lifecycle Stage ID
+ </label>
+ <p className="mb-3 text-sm leading-normal text-neutral-600">
+ Enter the HubSpot contact lifecycle stage ID that represents a
+ qualified lead. This will be used to track lead when contacts
+ reach this lifecycle stage.
+ </p>
+ <div className="relative rounded-md shadow-sm">
+ <input
+ className="w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ placeholder="customer"
+ type="text"
+ autoComplete="off"
+ name="leadLifecycleStageId"
+ value={leadLifecycleStageId ?? ""}
+ onChange={(e) => setLeadLifecycleStageId(e.target.value)}
+ />
+ </div>
+ </div>
+ )}
+
+ <div>
+ <label className="mb-2 block text-sm font-semibold text-neutral-700">
+ Closed Won Deal Stage ID
+ </label>
+ <p className="mb-3 text-sm leading-normal text-neutral-600">
+ Enter the HubSpot deal stage ID that represents a closed won deal.
+ This will be used to track sale when deals are marked as closed
+ won in HubSpot.
+ </p>
+ <div className="relative rounded-md shadow-sm">
+ <input
+ className="w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ placeholder={HUBSPOT_DEFAULT_CLOSED_WON_DEAL_STAGE_ID}
+ type="text"
+ autoComplete="off"
+ name="closedWonDealStageId"
+ value={closedWonDealStageId ?? ""}
+ onChange={(e) => setClosedWonDealStageId(e.target.value)}
+ />
+ </div>
+ </div>
</div>
<div className="flex items-center justify-end rounded-b-lg border-t border-neutral-200 bg-neutral-50 px-4 py-3">
diff --git a/apps/web/lib/integrations/hubspot/update-hubspot-settings.ts b/apps/web/lib/integrations/hubspot/update-hubspot-settings.ts
index a23b8f707a..a9969520d9 100644
--- a/apps/web/lib/integrations/hubspot/update-hubspot-settings.ts
+++ b/apps/web/lib/integrations/hubspot/update-hubspot-settings.ts
@@ -7,17 +7,16 @@ import { revalidatePath } from "next/cache";
import { z } from "zod";
import { hubSpotSettingsSchema } from "./schema";
-const schema = hubSpotSettingsSchema
- .pick({ closedWonDealStageId: true })
- .extend({
- workspaceId: z.string(),
- });
+const schema = hubSpotSettingsSchema.extend({
+ workspaceId: z.string(),
+});
export const updateHubSpotSettingsAction = authActionClient
.schema(schema)
.action(async ({ parsedInput, ctx }) => {
const { workspace } = ctx;
- const { closedWonDealStageId } = parsedInput;
+ const { leadTriggerEvent, leadLifecycleStageId, closedWonDealStageId } =
+ parsedInput;
const installedIntegration = await prisma.installedIntegration.findFirst({
where: {
@@ -41,6 +40,8 @@ export const updateHubSpotSettingsAction = authActionClient
data: {
settings: {
...current,
+ leadTriggerEvent,
+ leadLifecycleStageId,
closedWonDealStageId,
},
},