# Capture cookie info with hidden form fields

**URL:** <https://community.hubspot.com/t/capture-cookie-info-with-hidden-form-fields/13389>\
**Category:** APIs & Integrations\
**Tags:** forms, contacts\
**Created:** [March 7, 2019, 10:57am UTC](https://community.hubspot.com/t/capture-cookie-info-with-hidden-form-fields/13389 "2019-03-07T10:57:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [March 7, 2019, 10:57am UTC](https://community.hubspot.com/t/capture-cookie-info-with-hidden-form-fields/13389/1 "2019-03-07T10:57:45Z")

</div>

Hi guys,

I’m trying to capture cookie info from these cookies:

- STYXKEY-huddleReferer
- STYXKEY-huddleSource
- STYXKEY-huddleTracker

I’ve insersted each of the below as hidden form fields:

- where\_hear\_about\_huddle\_\_c
- search\_cookie\_source\_\_c
- search\_cookie\_medium\_\_c

I’ve udpated the Hubspot embed code as below but I’m unable to pull through the cookie data…

```auto
<!--[if lte IE 8]>
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2-legacy.js"></script>
<![endif]--><script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script><script>
 hbspt.forms.create({
 portalId: "MY-HUBSPOT-ID-XXXX",
 formId: "MY-FORM-ID-XXXX",
 onFormSubmit: function($form) {
 jQuery('input[name="web_activity_most_recent__c"]').val('Request_a_Demo_Generic').change();
 jQuery('input[name="search_cookie_source__c"]').val(readCookie('STYXKEY-huddleSource')).change();
 jQuery('input[name="search_cookie_medium__c"]').val(readCookie('STYXKEY-huddleReferer')).change();
 jQuery('input[name="where_hear_about_huddle__c"]').val(readCookie('STYXKEY-huddleTracker')).change();

 },
 redirectUrl: "/get-started/confirmation/"
 });
</script>

```

The cookies are being created and capturing info - snapshot below:

 ![Screenshot 2019-03-07 at 13.24.16.jpg](https://d2qpr2jzcazej7.cloudfront.net/original/2X/6/6d59c31f4a0998eb4a1a10885ba0510f0d4ec6d0.jpeg)

Any help with be much appreciated.

Thanks,

Marc

---

<div class="post-metadata">

**Author:** ![cbarley](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/cbarley/32/82559_2.png) [@cbarley](https://community.hubspot.com/u/cbarley)\
**Post date:** [March 7, 2019, 11:04pm UTC](https://community.hubspot.com/t/capture-cookie-info-with-hidden-form-fields/13389/2 "2019-03-07T23:04:36Z")

</div>

Hi @Anonymous , can you reply with the page you’re working on and your HubID? Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [March 8, 2019, 7:18am UTC](https://community.hubspot.com/t/capture-cookie-info-with-hidden-form-fields/13389/3 "2019-03-08T07:18:44Z")

</div>

Hey @cbarley , sure please see below:

Page: [Document collaboration solutions for real-time teamwork](https://www.huddle.com/get-started)

ID: \*removed\*

Before the switch we had it all setup in Marketo with workflows & smart lists - code snipet below:

```auto
<script src="//app-lon03.marketo.com/js/forms2/js/forms2.js"></script> 
<form id="mktoForm_1184" class=""></form> 
<script> 
MktoForms2.loadForm("//app-lon03.marketo.com", "MKT-ID", 1184, (function(form){ 
form.setValues({ 
"Web_Activity_Most_Recent__c":"Downloaded_Huddle:Secure_by_Design_ISO", 
"Where_hear_about_Huddle__c":readCookie('STYXKEY-huddleTracker') 
}); 
form.onSuccess(function(values, followUpUrl){ 
location.href = "/sites/default/files/white-papers-files/huddle_security_whitepaper_mar_2016.pdf";
return false; 
}); 
})); 
</script>

```

So just basically need the value of the “where\_hear\_about\_huddle\_\_c” input to be set to the value of the STYXKEY cookie upon form submission..

Any help/direction will be really appreciated.

Thanks,

Marc

---

<div class="post-metadata">

**Author:** ![cbarley](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/cbarley/32/82559_2.png) [@cbarley](https://community.hubspot.com/u/cbarley)\
**Post date:** [March 8, 2019, 4:16pm UTC](https://community.hubspot.com/t/capture-cookie-info-with-hidden-form-fields/13389/4 "2019-03-08T16:16:36Z")

</div>

Hi @Anonymous , this seems fine to me. How are you getting the cookies from the page? I’m using this function on my page to get the Cookies by name:

```auto
window.getCookie = function(name) {
 var match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
 if (match) return match[2];
}

```

Then, I change the value of the hidden field on submit:

```auto
hbspt.forms.create({ portalId: "3430672", formId: "983fd305-bdd3-40be-bc25-59be5f5650a8", css: "", onFormSubmit: function($form){ $("input[name='cookie']").val(getCookie("hubspotutk")).change();
 },
 redirectUrl: "http://www.playtimepottery.net/redirect"
});

```

This worked for me as you can see on my contact: [Screenshot by Lightshot](http://prntscr.com/mv4b7n)

If you check out this page and inspect it with Chrome Dev Tools you can see the code for yourself: [https://www.playtimepottery.net/on-form-submit](https://www.playtimepottery.net/on-form-submit)

That said, the issue is definitely with how you’re grabbing the cookies. When I plug **readCookie(‘STYXKEY-huddleTracker’)** into the console, I get **undefined**. I’d go ahead and use the code I wrote above, which works, instead of the readCookie function as it doesn’t appear to be behaving as expected.

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [March 11, 2019, 8:48am UTC](https://community.hubspot.com/t/capture-cookie-info-with-hidden-form-fields/13389/5 "2019-03-11T08:48:49Z")

</div>

Legend! Thanks so much for your help @cbarley . The script to read the cookies worked like a charm!

---

<div class="post-metadata">

**Author:** ![ianong](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ianong/32/75439_2.png) [@ianong](https://community.hubspot.com/u/ianong)\
**Post date:** [July 11, 2019, 4:54pm UTC](https://community.hubspot.com/t/capture-cookie-info-with-hidden-form-fields/13389/6 "2019-07-11T16:54:44Z")

</div>

Hi Connor,

i saw the solution you provided here. can i check how can i implement this.

i am using hubspot CMS and CRM. Would like to push hubspotutk ID to a hidden field too.

---

<div class="post-metadata">

**Author:** ![cbarley10](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/cbarley10/32/50727_2.png) [@cbarley10](https://community.hubspot.com/u/cbarley10)\
**Post date:** [July 12, 2019, 12:19am UTC](https://community.hubspot.com/t/capture-cookie-info-with-hidden-form-fields/13389/7 "2019-07-12T00:19:32Z")

</div>

Hi @ianong , I’m no longer a HubSpot employee but i’d still be happy to help out. The JS snippet I provided should certainly work for any form that you’re embedding onto a page. If you’re embedding the form, you _must_ use the callbacks like **onFormReady** or **onFormSubmit** since forms are dynamically loaded to a page with JavaScript and simply placing the JS on your page won’t guarantee the form is loaded on the page yet. You should also make sure that the “name” attribute in your JS like my example:

`$("input[name='cookie']").val(getCookie("hubspotutk")).change();`

_exactly_ matches the internal value of your hidden form field.

Finally, you should also be sure to toggle the “unstyled form” button for your form as styled forms are loaded in via an iFrame and the callbacks cannot be utilized:

> **[Screenshot](https://prnt.sc/odv2z5)**
>
> Captured with Lightshot

If this all checks out, I’d be happy to help if you send over a page with your form on it so I can take a look at your code. If I can’t troubleshoot further, I’m sure @IsaacTakushi , [@Derek\_Gervais](https://community.hubspot.com/t5/user/viewprofilepage/user-id/142) or @lscanlan can help you out here (sup guys?).
