Skip to content

Instantly share code, notes, and snippets.

@flexgrip
Created January 13, 2017 19:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flexgrip/0ef20e183ca7fe577b5d309620fc44bc to your computer and use it in GitHub Desktop.
Save flexgrip/0ef20e183ca7fe577b5d309620fc44bc to your computer and use it in GitHub Desktop.
Pass the GET parameters / url variables / query string, from a hubspot form to your redirect/thank-you page. Apparently, to do this now, you have to manually define the redirect url in the script. It's not exposed in hsContext anymore.
/**
* Append the form data from a HubSpot form automatically
* to the redirect URL query parameters. These values can
* then be used on the form to modify the user experience
* of the Thank You page
*
* LICENSE
* Form redirect
* Written in 2015 by Mike Axiak <maxiak@hubspot.com>
* Updated in 2016 by Seth Meranda <seth.meranda@cune.edu>
* Updated in 2017 by Ray Aguilar <flexgrip@gmail.com>
* To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
* You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
function appendFormData (form) {
/*
* Define your redirect URL here. (Yeah, it's
* stupid... But Hubspot apparently doesn't
* expose the redirect url on embedded forms
* anymore. Or at least I can't find it...)
*/
var redirect = "https://your-url-goes-here.com";
var appendFields = function (url, values) {
var data = {};
$.each(values, function (i, item) {
if (item.name !== "hs_context") {
data[item.name] = item.value;
}
});
if (url.match(/\?/)) {
return url + "&" + $.param(data);
} else {
return url + "?" + $.param(data);
}
};
var $hsContext = form.find("input[name='hs_context']");
var hsContext = JSON.parse($hsContext.val());
hsContext.redirectUrl = appendFields(redirect, form.serializeArray());
$hsContext.val(JSON.stringify(hsContext));
}
/**
* add this to the embed form onFormSubmit callback
* http://developers.hubspot.com/docs/methods/forms/advanced_form_options
**/
onFormSubmit : function($form, ctx) {
appendFormData($form);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment