Skip to content

fix(newsletter): include hubspotutk (hutk) in HubSpot form submissions#696

Open
mayasssh wants to merge 1 commit into
mainfrom
fix/hubspot-form-hutk
Open

fix(newsletter): include hubspotutk (hutk) in HubSpot form submissions#696
mayasssh wants to merge 1 commit into
mainfrom
fix/hubspot-form-hutk

Conversation

@mayasssh

@mayasssh mayasssh commented Jul 1, 2026

Copy link
Copy Markdown

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:

  • Submissions can't be linked to the correct contact record ("No contact record").
  • No IP address is captured, which breaks form analytics.
  • Because HubSpot can't dedupe/attribute properly, many submissions collapse onto a single existing contact instead of the real user.
image (34) image (35)

Root cause

HubSpot uses the hutk value — the hubspotutk cookie 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 in src/hooks/useNewsletter.js never read that cookie, so it was never included in the request payload.

Fix

  • Add a small getCookie util (src/utils/getCookie.js).
  • In useNewsletter, build the Forms API context object from the browser: read the hubspotutk cookie and include it as hutk, plus pageUri from window.location.href (pageName was already sent).
  • hutk is only included when the cookie actually exists. HubSpot rejects an empty hutk with an INVALID_HUTK error, so cookieless submissions omit the field entirely and still succeed.
  • ipAddress is intentionally left out — it can't be read reliably client-side. hutk is the priority for contact linkage.

Reviewer caveat

This only works if the hubspotutk cookie 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

    • Newsletter form submissions now include more page context information when available, helping improve submission tracking and handling.
    • Cookies are now used when present to support more complete newsletter request details.
  • Bug Fixes

    • Improved behavior in browser and non-browser environments so newsletter submission data is only added when available.

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.
@coderabbitai

coderabbitai Bot commented Jul 1, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

A new getCookie utility function was added to read cookies from document.cookie. The useNewsletter hook now uses this utility within a new getContext helper to build a submission context that conditionally includes hutk (from the HubSpot cookie) and pageUri, replacing the previous minimal context object.

Changes

Newsletter Context Enrichment

Layer / File(s) Summary
Cookie reading utility
src/utils/getCookie.js
New default-exported getCookie(name) function parses document.cookie, returning undefined when outside a browser or when the cookie is not found.
Context construction and request wiring
src/hooks/useNewsletter.js
Imports getCookie; adds getContext({ pageName }) that conditionally sets pageUri from window.location.href and hutk from the hubspotutk cookie; request body now uses getContext({ pageName }) instead of a static object.

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
Loading

Poem

A cookie crumb, a URI trail,
hopped into context without fail. 🐇
HubSpot now knows just where you've been,
thanks to a rabbit's tidy den.
Thump-thump, ship it, clean and small!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adding the HubSpot hutk token to newsletter form submissions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/hubspot-form-hutk

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@mayasssh
mayasssh requested a review from Kerollmops July 1, 2026 13:29
@Strift Strift added skip-changelog maintenance Issue about maintenance (CI, tests, refacto...) labels Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Issue about maintenance (CI, tests, refacto...) skip-changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants