Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@code4fish
Last active April 10, 2024 03:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save code4fish/295f617a46df1913a38e8278c4314fd3 to your computer and use it in GitHub Desktop.
Save code4fish/295f617a46df1913a38e8278c4314fd3 to your computer and use it in GitHub Desktop.
Segment Webflow Form Track
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