Last active
April 10, 2024 03:04
-
-
Save code4fish/295f617a46df1913a38e8278c4314fd3 to your computer and use it in GitHub Desktop.
Segment Webflow Form Track
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
const formElemList = document.querySelectorAll("form"); | |
formElemList.forEach((form) => { | |
form.addEventListener("submit", (e) => { | |
// on form submission, prevent default | |
e.preventDefault(); | |
// construct a FormData object, which fires the formdata event | |
new FormData(form); | |
}); | |
form.addEventListener("formdata", (e) => { | |
// Get the form data from the event object | |
let data = e.formData; | |
let obj = {}; | |
const entries = [...data.entries()]; | |
entries.forEach((entry) => (obj[entry[0]] = entry[1])); | |
analytics.track(form.attributes[2].nodeValue, obj); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment