Apr 3, 2017 7:29 PM
I need to embed a HS form on an external (not CoS) website. And when the user submits the form, the redirect URL needs to include form fields as parameters.
Is this possible?
We figured out a way of doing this on a CoS landing page using a native HS form, but it was pretty tricky. In this case, I need to embed the form on an external page rather than use a CoS page.
What I’m trying to accomplish is capture the lead’s info (name, company, email) and then pass that info off to a scheduling app which can use parameters to pre-fill a scheduling form so the user doesn’t have to re-enter their info. And I want to do it through a HS form first so that we capture the lead and source information in HS. If we did it straight in the scheduling app then we could create a lead using Zapier but without Source info.
Every other form app I’ve used has been able to do this. Still surprised HS can’t do it off the shelf. But I hope someone has a creative workaround. Thanks.
Solved! Go to Solution.
Apr 4, 2017 9:11 AM
You could achieve this by using the onFormSubmit callback and switching the form to use an inline message instead of a redirect (explained below).
hbspt.forms.create({
portalId: 'xxxxxxxx',
formId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
inlineMessage: 'Your submit message here',
onFormSubmit: function($form){
setTimeout( function() {
var formData = $form.serialize();
window.location = "http://www.yoururl.com?" + formData;
}, 250 ); // Redirects to url with query string data from form fields after 250 milliseconds.
}
});
Because you have access to the jQuery $form object inside the onFormSubmit listener you can serialize the forms data and append it to a url to redirect to after the form is submitted. This only works if you have the inlineMessage set and disable the redirectUrl for the form.
See if that helps you out.
Dec 18, 2019 7:57 AM
Try setting your setTimeout to something higher than 100 milliseconds and see if that helps. I haven't had or seen this issue before - and in theory it shouldn't because the onFormSubmit is processed after the form is validated and request is sent.
My theory is that if you redirect too quickly it may terminate the XHR request over to HubSpot's servers. I haven't actually tested this issue myself yet though - let me know what you find.
Dec 15, 2021 5:40 AM - edited Dec 15, 2021 5:41 AM
For anyone searching for a solution where you can use the redirect settings from the form:
window.addEventListener('message', event => {
if (event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
const input = document.querySelector('select[name="input_name"]');
const context = document.querySelector('input[name="hs_context"]');
const contextObject = JSON.parse(context.value);
const redirectUrl = contextObject.redirectUrl; // Retrieve the redirectUrl from the form settings
if (input) {
if (input.value && redirectUrl) {
window.location.href = redirectUrl + `?input_name=${input.value}`;
}
}
}
});
Dec 15, 2021 5:40 AM - edited Dec 15, 2021 5:41 AM
For anyone searching for a solution where you can use the redirect settings from the form:
window.addEventListener('message', event => {
if (event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
const input = document.querySelector('select[name="input_name"]');
const context = document.querySelector('input[name="hs_context"]');
const contextObject = JSON.parse(context.value);
const redirectUrl = contextObject.redirectUrl; // Retrieve the redirectUrl from the form settings
if (input) {
if (input.value && redirectUrl) {
window.location.href = redirectUrl + `?input_name=${input.value}`;
}
}
}
});
Feb 14, 2023 11:08 AM - edited Feb 14, 2023 11:08 AM
Hi Teun - where do I put this code?
Mar 18, 2022 2:13 PM
@Teun , sorry, where do I put your code? Does that go in the <head> section? Do I still need to embed the form using HubSpot javascript embed code?
Feb 14, 2023 11:08 AM
were you able to figure this out?
Nov 24, 2021 11:32 AM
Alternatively, one can use onFormSubmitted:
...
onFormSubmitted: function($form){
window.location = "https://www.yoururl.com?" + $form.serialize();
}
...
Dec 2, 2021 12:54 PM - edited Dec 2, 2021 12:56 PM
Actually - you'll have problems with that - because HubSpot doesn't pass the $form argument to that callback like it does for onFormSubmit (this is very dumb and I had a lengthy chat w/ HubSpot support about it).
I would reccomend using onFormSubmit over onFormSubmitted - or if you plan on using onFormSubmitted - you'll need to "cache" the $form in a global variable which would mean setting it via onFormSubmit and then referencing it in the onFormSubmitted callback.
Jun 30, 2021 4:30 AM
@derekcavaliero I tried this solution
hbspt.forms.create({
portalId: 'xxxxxxxx',
formId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
inlineMessage: 'Your submit message here',
onFormSubmit: function($form){
setTimeout( function() {
var formData = $form.serialize();
window.location = "http://www.yoururl.com?" + formData;
}, 250 ); // Redirects to url with query string data from form fields after 250 milliseconds.
}
});
It displays thank you message, but doesn't redirect...
Feb 14, 2023 5:04 PM
@andriigo my appologies, I made a mistake the code I provided, it should be:
window.location.replace("http://www.yoururl.com?" + formData);
instead of:
window.location = "http://www.yoururl.com?" + formData;
Feb 14, 2023 11:01 AM
Hi @derekcavaliero - when i embed this code onto my site (after adjusting the code with my portal ID, etc, the form doesn't even appear on my page. Any advice?
Feb 14, 2023 5:05 PM
Are you seeing any errors in the dev tools console?
Feb 15, 2023 3:39 PM
Hey, @derekcavaliero thanks for continuing to try to support this thread. You rock 🙌 — Jaycee
Jun 30, 2021 4:14 AM
@derekcavaliero This only works if you have the inlineMessage set and disable the redirectUrl for the form.
How to disable redirectUrl for the form?
Feb 14, 2023 5:05 PM
Inside the form settings in hubspot, or you can set it explicitly in the embed code with the inlineMessage option.
Apr 24, 2021 1:42 PM
I built off of Derek's answer because I needed to use the built in landing pages, I was not using the form embed. The following worked for me.
<script>
//code for redirecting to brew ninja signup flow after they enter their email
$(document).ready(function() {
setTimeout(function() {
var form = $('[data-form-id=xxx-xxx-xx-your-form-id]');
form.submit(function() {
var email = $('[data-form-id=xxx-xxx-xx-your-form-id]').find('[name=email]').val();
setTimeout(function() {
window.location.href = 'https://yoursite.com?email=' + email;
}, 500);
});
}, 500);
});
</script>
Feb 14, 2023 11:00 AM
Hi @Sheamus - can you walk me through how you implemented this ?
Jul 23, 2018 8:59 AM
Happy to help!
One thing I did not mention in my solution was to make 100% sure that you exclude any PII in the form data from being collected by Google Analytics.
Since we're talking about appending the data to the URL via a query string - GA will automatically capture that data inside its pageview reports. This means you'll likely need to add some custom logic around your tracking to make sure you adhere to the GA terms of use regarding collection of PII.
If you're using GTM (Google Tag Manager) to implement your web tracking (if you aren't I highly suggest you investigate) - this article is a great resource for understanding how to exclude data from being collected:
Use customTask to automatically strip all personally identifiable information (PII) from requests sent to Google Analytics from your website.
May 28, 2018 11:24 AM
Hi @Lloyd_Silver,
I'm having the same issue, but on a COS landing page.
As you've apparently solved it before, any chance you can share the solution here?
Thanks a lot!
Jul 19, 2018 11:51 AM
Hey @Jeromecollomb, I know it's been a while since you asked your question, but I just posted an answer to another question that might help you out.
@derekcavaliero you are the man. This answer saved me a lot of time!
I like kudos almost as much as cake – a close second.
May 7, 2020 12:19 PM - edited May 7, 2020 12:24 PM
Hi @John I came across this thread and we are having a very similar issue. I am passing just the email parameter to test from a hubspot form to a wordpress page (that includes the HubSpot tracking cod) . First line is the parameter they way i have it in the redirect and then the second is how it actually comes out. Any suggestions?
?email={{contact.email}} email=%7B%7Bcontact.email%7D%7D
Basically I am trying to follor the directions in this article but it does not seem to pass the parameter.
Apr 11, 2022 5:03 PM
well i'm years late (never saw this reply). The issue was that you were going from wordpress to hubspot and my solution only worked on a hubspot page
I like kudos almost as much as cake – a close second.