APIs & Integrations

MTarnopolska
Contributor

Receiving cookie ID from the form integrated on the back-end

SOLVE

Hello,

 

I would like to know the logic here:

 

On our website, we have multiple forms that are connected on the back-end with the HubSpot forms. So, it's never a HubSpot form integrated directly in the website.

When a person fills in a form, the info will first go to our central distribution system and only then will be pushed through a HubSpot form and land in HubSpot. When the info arrives in HubSpot, we know that HubSpot will receive the cookie ID too. It's important to underline, that our central system does not receive any cookie ID.

 

My question is: How HubSpot receieves the cookie ID in this case if the central system that pushes the info through HubSpot form integrated in the back-end, does not receive it?

 

Thank you in advance for your help!

 

Kind regards,

Mila

1 Accepted solution
sylvain_tirreau
Solution
Top Contributor

Receiving cookie ID from the form integrated on the back-end

SOLVE

Hi,

 

Hubspot connects this to the internal mechanics of its JavaScript scripts (found on the site via the tracking code).

If you want to retrieve the tracking ID, you have to send it to the backend yourself.

You should do something like this:

 

function getHubSpotCookie() {
  const name = "hubspotutk=";
  const decodedCookie = decodeURIComponent(document.cookie);
  const cookies = decodedCookie.split(';');
  for (let i = 0; i < cookies.length; i++) {
    let c = cookies[i].trim();
    if (c.indexOf(name) === 0) {
      return c.substring(name.length, c.length);
    }
  }
  return null;
}

const hubspotutk = getHubSpotCookie();
console.log("HubSpot Cookie ID to send:", hubspotutk);

 

View solution in original post

6 Replies 6
GiantFocal
Top Contributor | Gold Partner
Top Contributor | Gold Partner

Receiving cookie ID from the form integrated on the back-end

SOLVE

Hi @MTarnopolska,

 

What's happening here is that HubSpot relies on the hubspotutk cookie to track visitors and associate them with form submissions. In a standard implementation where HubSpot forms are directly on your site, this happens automatically.

 

In your case, since you're using custom forms that submit to your central system first, HubSpot doesn't have direct access to the cookie during submission. The solution is to manually capture and pass the hubspotutk cookie value along with your form data. You'll need to add some JavaScript to your forms that grabs this cookie value from the user's browser and includes it with the submission to your central system.

 

Your central system would then need to include this cookie value when it pushes the data to HubSpot's API. This is typically done by adding the "hutk" parameter in your API requests to HubSpot. So even though your central system doesn't use the cookie directly, it's just passing it through like a messenger.

 

If you're using the Forms API specifically, there's a field dedicated to this purpose where you can pass the cookie value.

Best regards,
Ernesto @ GiantFocal
Found this answer helpful?
Marking it as the solution helps both the community and me - thanks in advance!
MTarnopolska
Contributor

Receiving cookie ID from the form integrated on the back-end

SOLVE

Hello @GiantFocal ,

 

Thank you for your answer. The reason why I am asking this question is that I know that we don't have any additional javascript code that would help to transfer the cookie information through our central system to HubSpot. Nevertheless, I see that for new contacts that came into our CRM through the form like described previously, I do have a pageview that usually looks like this: 

MTarnopolska_0-1747212647131.png

Is it something that is default by HubSpot ? 

0 Upvotes
sylvain_tirreau
Top Contributor

Receiving cookie ID from the form integrated on the back-end

SOLVE

To better understand your environment and your code: you've put your own form on a page that contains the tracking code, then you send the form result to your internal system, then your internal system sends the form data back to Hubspot via the Forms API?!

 

If that's what you're doing (?), here's how it works: if you put the tracking code on an internal page (external website) *and* you put a form on that page, the form submission will be automatically picked up by Hubspot, even if your internal system doesn't send it.

sylvain_tirreau
Solution
Top Contributor

Receiving cookie ID from the form integrated on the back-end

SOLVE

Hi,

 

Hubspot connects this to the internal mechanics of its JavaScript scripts (found on the site via the tracking code).

If you want to retrieve the tracking ID, you have to send it to the backend yourself.

You should do something like this:

 

function getHubSpotCookie() {
  const name = "hubspotutk=";
  const decodedCookie = decodeURIComponent(document.cookie);
  const cookies = decodedCookie.split(';');
  for (let i = 0; i < cookies.length; i++) {
    let c = cookies[i].trim();
    if (c.indexOf(name) === 0) {
      return c.substring(name.length, c.length);
    }
  }
  return null;
}

const hubspotutk = getHubSpotCookie();
console.log("HubSpot Cookie ID to send:", hubspotutk);

 

MTarnopolska
Contributor

Receiving cookie ID from the form integrated on the back-end

SOLVE

@sylvain_tirreau , thank you for your answer ! It will help us in one more project 🙂

BérangèreL
Community Manager
Community Manager

Receiving cookie ID from the form integrated on the back-end

SOLVE

Hi @MTarnopolska, thanks for your post in the HubSpot Community!

For reference, here is the Forms endpoint documentation.

Also, I'd love to put you in touch with our Top Experts: Hi @Teun, @Anton and @sylvain_tirreau do you have any insights to share with @MTarnopolska, please?

Have a beautiful day and thanks so much in advance for your valuable contributions!
Bérangère


HubSpot’s AI-powered customer agent resolves up to 50% of customer queries instantly, with some customers reaching up to 90% resolution rates.
Learn More.


Saviez vous que la Communauté est disponible en français?
Rejoignez les discussions francophones en changeant votre langue dans les paramètres! !