fix(newsletter): include hubspotutk (hutk) in HubSpot form submissions#696
fix(newsletter): include hubspotutk (hutk) in HubSpot form submissions#696mayasssh wants to merge 1 commit into
Conversation
Read the hubspotutk cookie and pass it (plus pageUri) in the Forms API context object so submissions link to the correct contact record. Omit hutk entirely when the cookie is absent to avoid INVALID_HUTK errors.
📝 WalkthroughWalkthroughA new ChangesNewsletter Context Enrichment
Estimated code review effort: 1 (Trivial) | ~5 minutes Sequence Diagram(s)sequenceDiagram
participant useNewsletter
participant getContext
participant getCookie
participant HubSpotAPI
useNewsletter->>getContext: getContext({ pageName })
getContext->>getCookie: getCookie("hubspotutk")
getCookie-->>getContext: cookie value or undefined
getContext-->>useNewsletter: context { pageName, pageUri?, hutk? }
useNewsletter->>HubSpotAPI: submit request body with context
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Problem
Newsletter submissions from the mini-dashboard are sent to the HubSpot Forms API without the visitor's HubSpot tracking token (
hutk). In HubSpot this causes:Root cause
HubSpot uses the
hutkvalue — thehubspotutkcookie set in the visitor's browser by the HubSpot tracking code — to tie a Forms API submission to that browser's tracked activity and the right contact. The current integration insrc/hooks/useNewsletter.jsnever read that cookie, so it was never included in the request payload.Fix
getCookieutil (src/utils/getCookie.js).useNewsletter, build the Forms APIcontextobject from the browser: read thehubspotutkcookie and include it ashutk, pluspageUrifromwindow.location.href(pageNamewas already sent).hutkis only included when the cookie actually exists. HubSpot rejects an emptyhutkwith anINVALID_HUTKerror, so cookieless submissions omit the field entirely and still succeed.ipAddressis intentionally left out — it can't be read reliably client-side.hutkis the priority for contact linkage.Reviewer caveat
This only works if the
hubspotutkcookie actually exists in the browser at submit time. That requires the HubSpot tracking code to be loaded on the page where the mini-dashboard runs, on the same domain the cookie is scoped to. If the tracking script isn't present on that page, this PR alone won't fully fix the attribution issue — that needs to be confirmed separately.Summary by CodeRabbit
New Features
Bug Fixes