Skip to content

Instantly share code, notes, and snippets.

@psapir
Created January 14, 2018 02:59
Show Gist options
  • Save psapir/0ac18613823810f28f67b818fe1f0c68 to your computer and use it in GitHub Desktop.
Save psapir/0ac18613823810f28f67b818fe1f0c68 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
function yourSubmitFunction(e, $) {
e.preventDefault();
try {
var formdata = $('form').serialize();
$.ajax({
type: "POST",
url: "http://cloud.mktgmtdcanada.ca/emailNewsletterSingUp",
data: formdata
});
}
catch(err) {
//code to handle errors. console.log is just an example
console.log(err);
}
finally {
// This submits the form. If your code is asynchronous, add to callback instead
gaForm(e);
lp.jQuery('.lp-pom-form form').submit();
}
}
lp.jQuery(function($) {
$('.lp-pom-form .lp-pom-button').unbind('click tap touchstart').bind('click.formSubmit', function(e) {
if ( $('.lp-pom-form form').valid() ) yourSubmitFunction(e, $);
});
$('form').unbind('keypress').bind('keypress.formSubmit', function(e) {
if(e.which === 13 && e.target.nodeName.toLowerCase() !== 'textarea' && $('.lp-pom-form form').valid() )
yourSubmitFunction(e, $);
});
});
//ga form submission event
function gaForm(event) {
var $form, $formContainer, params;
event.preventDefault();
event.stopPropagation();
$formContainer = lp.jQuery(event.currentTarget).closest('.lp-pom-form');
$form = $formContainer.children('form');
if ($form.valid()) {
if(typeof eventTracker !== 'undefined'){
if (!eventTracker._isGaLoaded()) {
return $form.submit();
}
params = lp.jQuery.extend({
category: 'Form',
action: 'Submit',
label: "#" + ($formContainer.attr('id'))
}, $formContainer.data('ubGAParams'));
eventTracker._logEvent(params);
return ga('send', 'event', params.category, params.action, params.label, {
hitCallback: function() {
return $form.submit();
}
});
}else{
return $form.submit();
}
}
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment