Oct 28, 20229:55 AM - edited Mar 15, 20237:10 AM
Key Advisor
How to pass e-mail address as URL parameter from an HubSpot Form
SOLVE
@APak3 thanks for replying, it turns out I was experiencing the same problem. I hadn't noticed and probably wouldn't have for a while had you not commented! So I appreciate it.
I've re-written it, and it now works:
let array = [];
window.addEventListener('message', event => {
if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
const fields = ['firstname', 'lastname', 'email', 'phone', 'hs_language', 'country1'];
event.data.data.forEach(x => {
let found = fields.find(y => y === x.name);
if (found) {
let object = {
name: found,
value: x.value
};
array.push(object);
}
});
}
if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
let firstname = array.find(x => x.name === 'firstname').value;
let lastname = array.find(x => x.name === 'lastname').value;
let email = array.find(x => x.name === 'email').value;
let phone = array.find(x => x.name === 'phone').value;
let lang = array.find(x => x.name === 'hs_language').value;
let country = array.find(x => x.name === 'country1').value;
//... do things
}
});
Edit @APak3 just made an optimisation actually. When I wrote it earlier today I was rushing to get it fixed, but I was thinking about it again. This is a better version, only one for loop.
How to pass e-mail address as URL parameter from an HubSpot Form
SOLVE
Thanks for sharing this - it is a great solution. I am using this and it works. Is there any way to pass the email non-encoded? I need to grab that email into a Typeform and a%40 is added instead of the @ -
How to pass e-mail address as URL parameter from an HubSpot Form
SOLVE
Hey piersg, similarly I'm trying to pass the email field data over to the next form which includes a hidden email field so that we can tie that form fill to a contact, without the need of cookies being used. Using the script you provided on 28/10/22, I'm not able to get it to work and wondering if there might be an updated script. Thanks for your help.
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 you want to include the Redirect URL from the form settings, you can use:
Learn more about HubSpot by following me on LinkedIn or YouTube
✅ Did my answer solve your issue? Help the community by marking it as the solution.
How to pass e-mail address as URL parameter from an HubSpot Form
SOLVE
Hi @vPhilipp (thanks @dennisedson). Yes, I do this with Hubspot forms. Here's my method using Hubspot form global events (instead of customising the form embed with the onFormSubmit event). I've done it this way so the page is only redirected after the data is sent to Hubspot. I found with the onFormSubmit event sometimes the user is redirected before the data is sent.
var array = [];
window.addEventListener('message', event => {
// in the onFormSubmit global form event, grab the data and push it to the above array
if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
let vars = ['firstname', 'lastname', 'email', 'phone', 'hs_language'];
let len = vars.length;
for (i = 0; i < len; i++) {
for (j in event.data.data) {
if (event.data.data[j]["name"] == vars[i]) {
let object = {
name: '',
value: ''
};
object.name = vars[i];
object.value = event.data.data[j]["value"];
array.push(object);
}
}
}
}
// after the form has submitted and data sent to Hubspot, get the data from the array and then redirect the page, adding the data to the URL
if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
var firstname = array[0].value
var lastname = array[1].value
var email = array[2].value
var phone = array[3].value
var lang = array[4].value
{% set redirect = content_by_id(module.form.redirect_id) %}
{% if module.form_info %} // boolean to decide whether to add form info to redirect URL or not
window.location = `{{redirect.absolute_url}}?firstName=` + encodeURIComponent( firstname ) +"&lastName=" + encodeURIComponent( lastname ) +"&email=" + encodeURIComponent( email ) + "&phone=" + encodeURIComponent( phone ) + "&language=" + encodeURIComponent( lang );
{% else %}
window.location = `{{redirect.absolute_url}}`
{% endif %}
}
});
Oct 28, 20229:55 AM - edited Mar 15, 20237:10 AM
Key Advisor
How to pass e-mail address as URL parameter from an HubSpot Form
SOLVE
@APak3 thanks for replying, it turns out I was experiencing the same problem. I hadn't noticed and probably wouldn't have for a while had you not commented! So I appreciate it.
I've re-written it, and it now works:
let array = [];
window.addEventListener('message', event => {
if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
const fields = ['firstname', 'lastname', 'email', 'phone', 'hs_language', 'country1'];
event.data.data.forEach(x => {
let found = fields.find(y => y === x.name);
if (found) {
let object = {
name: found,
value: x.value
};
array.push(object);
}
});
}
if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
let firstname = array.find(x => x.name === 'firstname').value;
let lastname = array.find(x => x.name === 'lastname').value;
let email = array.find(x => x.name === 'email').value;
let phone = array.find(x => x.name === 'phone').value;
let lang = array.find(x => x.name === 'hs_language').value;
let country = array.find(x => x.name === 'country1').value;
//... do things
}
});
Edit @APak3 just made an optimisation actually. When I wrote it earlier today I was rushing to get it fixed, but I was thinking about it again. This is a better version, only one for loop.
How to pass e-mail address as URL parameter from an HubSpot Form
SOLVE
Hello
Working on this and it passing everything through as a GUID ID = "&submissionGuid=e3759fee-d41e-4867-b4b2-89941a509220 - is that the intended outcome - was really hoping for a string. Is that possible?
How to pass e-mail address as URL parameter from an HubSpot Form
SOLVE
@piersg thank you for letting me know ! However I tried your new script and I am getting "undefined" as values in my URL parameters after redirection.
Do you maybe know why ?
here is the code I used :
<!-- this is the standard form embed code -->
<!--[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({
region: "na1",
portalId: "XXX",
formId: "XXX",
sfdcCampaignId: "XXX",
version: "V2_PRERELEASE"
});
// add custom script after it
var array = [];
window.addEventListener('message', event => {
// in the onFormSubmit global form event, grab the data and push it to the above array
if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
const vars = ['firstname', 'lastname', 'email', 'company', 'phone', 'message'];
vars.forEach(x => {
let object = {
name: x,
value: event.data.data[x]
};
array.push(object);
});
}
// after the form has submitted and data sent to Hubspot, get the data from the array and then redirect the page, adding the data to the URL
if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
var firstname = array[0].value
var lastname = array[1].value
var email = array[2].value
var company = array[3].value
var phone = array[4].value
window.location = "https://redirecturl.com?firstname=" + encodeURIComponent( firstname ) +"&lastname=" + encodeURIComponent( lastname ) +"&email=" + encodeURIComponent( email ) + "&phone=" + encodeURIComponent( phone ) + "&company=" + encodeURIComponent( company );
}
});
</script>
How to pass e-mail address as URL parameter from an HubSpot Form
SOLVE
Hmm I'm not seeing that on my side. If that version isn't working for you but the previous one, I would just use the previous version. The gain in efficiency between the two isn't very much as we're dealing with short arrays.