Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save derekcavaliero/c27deb0748e07d244a99bd015127289d to your computer and use it in GitHub Desktop.
Save derekcavaliero/c27deb0748e07d244a99bd015127289d to your computer and use it in GitHub Desktop.
HubSpot - Global Form Submit Event Example
<script>
(function() {
var dataArrayToObject = function(data) {
var fields = {};
for ( var i = 0; i < data.length; i++ ) {
fields[data[i].name] = data[i].value;
}
return fields;
};
window.addEventListener('message', function(event) {
if (event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
// Take submitted form data array of objects and transform into a easier to use object of key:value pairs
var fields = dataArrayToObject(event.data.data);
// You can now access the submitted data via the fields object.
// e.g: fields.firstname
// Push data to GTM and/or other analytics libraries.
// window.dataLayer.push();
}
});
})(window, document);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment