Issues capturing the hubspotutk cookie in form submission with HubSpot API
SOLVE
Hey folks! I’m having an issue with capturing the hubspotutk cookie and sending it to HubSpot via API.
I have a sign-up form in a Laravel app hosted at example.yourdomain.com, and the HubSpot tracking script is being loaded via Google Tag Manager using a custom HTML tag:
I’ve logged the values before sending, double-checked GTM and script timing — everything seems to be working, except I’m not getting that cookie. I’ve seen some people mention browser tracking prevention might block the cookie, but the percentage is way too high for that to be the only reason.
Has anyone run into this before? Any tips to make sure the hubspotutk gets captured and properly sent with the API submission? Thanks a lot!
Issues capturing the hubspotutk cookie in form submission with HubSpot API
SOLVE
Yes, the structure you have looks correct.
The question now is whether the issue is with one of the following:
- the cookie value being captured by the JavaScript code
- the cookie value being properly passed into request body
- the cookie loading in the first place.
Theoretically, the cookie should be available upon page load and you should be able to store it in a variable even before someone tries to fill out a form - so you should not really even need to delay the form submission.
I would suggest running some tests in separate incognito/private sessions to check whether the JS function is capturing it consistently/properly with a simple console log:
Here are a few things that you can check, sometimes because of these the cookies are not captured -
Script Loading Order: The HubSpot tracking script cannot be accessible even with a 2-second lag if it hasn't been loaded or completely initialized before you try to capture the cookie. Make sure that no other scripts are interfering with the loading of the GTM tag and that the tag firing is dependable.
Asynchronous Loading: Because the script loads asynchronously, you may want to make sure the cookie is set before submitting the form by using a callback or event listener, if one is available.
I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member. Thanks!
Just to confirm, you are using a custom coded form (not an embedded HubSpot form), correct?
You mention that you are trying to pass the cookie through a hidden variable. In your API call, are you passing the cookie value into the "fields" or the "context" object in the body?
✔️ Did this post help answer your query? Help the community by marking it as a solution.
So the structure seems correct — the main issue is that sometimes the hubspotutk cookie is missing on the client side before submission, even after trying to read it with a retry loop in JavaScript.
Issues capturing the hubspotutk cookie in form submission with HubSpot API
SOLVE
Yes, the structure you have looks correct.
The question now is whether the issue is with one of the following:
- the cookie value being captured by the JavaScript code
- the cookie value being properly passed into request body
- the cookie loading in the first place.
Theoretically, the cookie should be available upon page load and you should be able to store it in a variable even before someone tries to fill out a form - so you should not really even need to delay the form submission.
I would suggest running some tests in separate incognito/private sessions to check whether the JS function is capturing it consistently/properly with a simple console log: