From d0265148af8d9ec36f9afa545ceea678886d9562 Mon Sep 17 00:00:00 2001 From: Ronen Mars Date: Wed, 3 Sep 2025 10:54:34 +0300 Subject: [PATCH] fix: hubspot submission --- src/hooks/useHubspotSubmission.ts | 47 +++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/src/hooks/useHubspotSubmission.ts b/src/hooks/useHubspotSubmission.ts index c8e82c0d7f..0b0129a2bf 100644 --- a/src/hooks/useHubspotSubmission.ts +++ b/src/hooks/useHubspotSubmission.ts @@ -6,17 +6,25 @@ import { LoggerService } from "@services/logger.service"; export function useHubspotSubmission({ t }: HubspotSubmissionArgs) { return async (user: { email?: string; name?: string }) => { - if (!isProduction || !hubSpotPortalId || !hubSpotFormId) return; + // Guard: require production + IDs + email + if (!isProduction || !hubSpotPortalId || !hubSpotFormId) { + LoggerService.debug(namespaces.ui.loginPage, "HubSpot submission skipped: non-production or missing IDs"); + return; + } + if (!user?.email) { + LoggerService.debug(namespaces.ui.loginPage, "HubSpot submission skipped: missing user.email"); + return; + } const hsUrl = `https://api.hsforms.com/submissions/v3/integration/submit/${hubSpotPortalId}/${hubSpotFormId}`; const hsContext = { - hutk: Cookies.get("hubspotutk"), - pageUri: window.location.href, - pageName: document.title, + hutk: Cookies.get("hubspotutk") ?? "", + pageUri: String(window.location.href || ""), + pageName: String(document.title || ""), }; const hsData = [ - { objectTypeId: "0-1", name: "email", value: user?.email }, - { objectTypeId: "0-1", name: "firstname", value: user?.name }, + { name: "email", value: user.email }, + { name: "firstname", value: user.name ?? "" }, ]; const submissionData = { submittedAt: Date.now(), @@ -24,13 +32,34 @@ export function useHubspotSubmission({ t }: HubspotSubmissionArgs) { context: hsContext, }; try { - await fetch(hsUrl, { + const controller = new AbortController(); + const timeoutId = window.setTimeout(() => controller.abort(), 8000); + + const res = await fetch(hsUrl, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(submissionData), + signal: controller.signal, }); - } catch (error) { - LoggerService.error(namespaces.ui.loginPage, t("errors.loginFailedExtended", { error }), true); + window.clearTimeout(timeoutId); + + if (!res.ok) { + const text = await res.text().catch(() => ""); + LoggerService.error( + namespaces.ui.loginPage, + `HubSpot submission failed: ${res.status} ${res.statusText} ${text ? "- " + text : ""}`, + true + ); + return; + } + + LoggerService.debug(namespaces.ui.loginPage, "HubSpot submission succeeded"); + } catch (error: any) { + LoggerService.error( + namespaces.ui.loginPage, + t("errors.loginFailedExtended", { error: String(error?.message ?? error) }), + true + ); } }; }