Does anyone know if there’s a way to put a hidden text field in a Hubspot form and have it collect the current page url when the user submits the form? (through javascript or otherwise?)
I feel like this is pretty basic but I’m not sure where to start. Thanks.
That error means that the jQuery function call (i.e. $()) is undefined. I'd expect to see that error if you didn't have jQuery on your secondary domain.
I am having a problem populating hidden fields when they are hidden, but not when they are normal. I am using javascript to point at the form field like this:
This javascript works to populate the field when the form field is added in HubSpot's form editor normally, but it does not work when I check the box to "Make this field hidden" in the HubSpot form editor.
My clunky workaround is to add the field in HubSpot's form editor as a normal (not hidden) field, but then add display:none that field's class to my CSS file:
.hs_last_known_conversion_page { display: none; }
Doing this does work, and the field populates and passes through. But it's a hack that I will certainly forget by next year and then won't be able to figure why fields that aren't marked as hidden in HubSpot are being hidden.
Is there a better way to be doing this in 2019? I'm not sure if some of the information on this page hasn't changed in the past couple of years.
Is there a reason why the form_page_url token would work on one domain and not a secondary domain?
Using the code in this post helps collect the current url a form is submitted on in my primary domain. However, using the same exact setup, on a secondary domain it doesn't.
I even created a second property to track separately from the the token on the primary domain and its not functioning either.
Browser console does through this error:
TypeError: $ is not a function
at onFormReady (www.not_a_real_site.com./:313)
at Object.runCustomerCallbackFunction (v2.js:9)
at t.componentDidMount (v2.js:11)
at r.notifyAll (v2.js:1)
at r.close (v2.js:3)
at r.closeAll (v2.js:4)
at r.perform (v2.js:4)
at f (v2.js:3)
at r.perform (v2.js:4)
at Object.batchedUpdates (v2.js:2)
Thank you all for the attempts to help.
HubSpot had a cache problem. My custom html box was not even loaded, so referring to the variable never would be possible. Deleting the whole module and setting up a new html box helped.
Perhaps others need this information too Later, the code from das_kreitz works perfectly.
Thanks for the quick reply and fixes. I changed the W to w, and I am still getting no luck. I checked that the sample_pn_url is the id. Any other suggestions?
Venkie, did you get this to work? Often it’s the input name that spoils the party. To clarify, it’s not the “id”, it’s the “name”. Good idea to view the form source code and see if the name matches with your script.
Of course I created a new conversion property “last conversion location”, which generated the field “name” last_conversion_location then added the hidden field on the form.
Gonna go out on a limb here and suggest renaming your custom URL tracking field something besides “website”. HS already has a field for Website URL with an internal name of “website”. Try using the code I pasted above and rename your field to “last_conversion_location”.
Thanks for the help guys. I cant seem to get my code to work with these steps above. Here is what I got:
hbspt.forms.create({
portalId: ‘XXXXXX’,
formId: 'XXXX’
onFormReady: function($form) {
$(‘input[name=“sample_pn_url”]’).val(Window.location.href).change();
}
});
sample_pn_url is the hidden field I am trying to write the URL to.
You could pass an onFormReady callback function in the form embed code, and execute that within that callback. That should populate the hidden form field once the form has been rendered in the DOM. It’d look something like this:
The current page (or location) of the browser is stored in Window.location (see below). You could populate a hidden form field with this value by targeting the input using jQuery and setting the value to window.location. The form embed document below has some examples of this.
The question is where do I put this code? I tried putting it in the embed code but it didn’t work. I feel like I’m missing something (or a few things). Any other advice on this greatly appreciated. Thanks.