Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions src/hooks/useHubspotSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,60 @@ 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(),
fields: hsData,
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
);
}
};
}