Skip to content

Instantly share code, notes, and snippets.

@4briang
Forked from diviengine/track-form-submissions.js
Created August 29, 2019 03:49
Show Gist options
  • Select an option

  • Save 4briang/069dd70e05df7fb1b8e03b2b5e6610ad to your computer and use it in GitHub Desktop.

Select an option

Save 4briang/069dd70e05df7fb1b8e03b2b5e6610ad to your computer and use it in GitHub Desktop.
<script type="text/javascript">
jQuery(document).ready(function($){
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-xxxxxxxxx-x', 'auto'); // change UA-xxxxxxxxx-x to be your tracking UA.
$("#contact form").each(function() {
var jqForm = $(this);
var jsForm = this;
var action = jqForm.attr("action");
jqForm.submit(function(event) {
event.preventDefault(); // prevent the form from submitting so we can sent the tracking code, then continue with the submission later.
ga('send', {
hitType: 'event',
eventCategory: 'Form Submits', // add any name for the category
eventAction: 'Form Submitted', // add any name for the action
hitCallback : function () {
console.log("event sent ok");
}
});
setTimeout(function() { // now wait 300 milliseconds...
jsForm.submit(); // ... and continue with the form submission
},300);
});
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment