Created
July 24, 2020 15:09
-
-
Save derekcavaliero/c27deb0748e07d244a99bd015127289d to your computer and use it in GitHub Desktop.
HubSpot - Global Form Submit Event Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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