Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Created August 5, 2015 18:06
Show Gist options
  • Save james2doyle/ce85dee7668cb33f413c to your computer and use it in GitHub Desktop.
Save james2doyle/ce85dee7668cb33f413c to your computer and use it in GitHub Desktop.
jQuery contact form submit with Google Analytics event
$('#contact-form').submit(function(e) {
// delay the submit
e.preventDefault();
// we need this to call the form in a scoped function
var self = this;
ga('send', {
eventAction: "submit",
eventCategory: "form",
eventLabel: "contact-form",
hitType: "event",
useBeacon: true, // useBeacon API, allows sending after page change
hitCallback: function() {
// now that the form has finished, submit normally
self.submit();
}
});
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment