Skip to content

Instantly share code, notes, and snippets.

@chrisjimallen
Created August 15, 2013 21:53
Show Gist options
  • Save chrisjimallen/6245322 to your computer and use it in GitHub Desktop.
Save chrisjimallen/6245322 to your computer and use it in GitHub Desktop.
How to make sure the event is tracked BEFORE the form is submitted.
jQuery(document).ready(function($){
$('form').submit(function(event){
//if analytics object exists
if(window._gat){
event.preventDefault();
optinForm = this;
_gaq.push(['_set','hitCallback', function(){
optinForm.submit();
}]);
_gaq.push(['_trackEvent', 'Forms', 'Submit', 'hbsanewtraffic']);
}
//if no analytics object, service as normal
});
});
@nasieti
Copy link

nasieti commented Nov 1, 2013

great work on this issue. and this way you can hook it up directly in the form's onSubmit too (if you can't rely on jquery or want/need to keep the code in the form itself)

onSubmit="
if(window._gat){
    event.preventDefault();
    Form = this;
    _gaq.push(['_set','hitCallback', function(){
        Form.submit(); 
    }]);
    _gaq.push(['_trackEvent', 'e_cat', 'e_action', 'e_label']);
//  alert('event sent');
}
// alert('submit');
"

@chrisjimallen
Copy link
Author

thanks nasieti, this will be useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment