I have an urgent issue and am hoping someone here has the answer. I am trying to pass a cookie value into a hidden field when my contact form is submitted.
These are the tools I am using to try and complete this:
HubSpot Form
Google Tag Manager
These are the steps I have taken (this works with other form builders like Gravity Forms, but not with HubSpot forms).
Created a custom field in HubSpot called "msclkid".
Added the field "msclkid" to my contact form as a hidden field.
Created a cookie on my website via Google Tag Manager that stores the value of the "msclkid" value.
Created a custom html tag that passes that msclkid cookie value into the hidden field "msclkid".
Everything works in the process except for step 4.
Here is the code I am using to pass the values into my hidden form field. Keep in mind, HubSpot hidden form fields do not have ids, so I am using the name element.
<script>
// msclkid
(function () {
var value = "{{url - msclkid}}" //fill with variable which information should be written into the field
var selector = "input[name='msclkid']" //fill with CSS Selector that identifies the form field
var field = document.querySelector(selector)
if(field){ field.value = value; }
})();
</script>
Does anyone see any issues with my code? The value {{url - msclkid}} is the custom GTM variable for my cookie.
Here is an image of the div for the msclkid:
Does anyone have an answer for how I can solve this using only Google Tag Manager? I am open to all ideas!