1

I'm using the following code to track when a user exits a page on my site:

//track on page exit
function storeData(){
    _gaq.push(['_trackEvent', 'Application Form', 'exit-form_application_', '', 4, false]);
}
$(window).on('unload',storeData);

However, I don't want to trigger the event when the form is submitted, therefore how could I prevent this?

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
user1961082
  • 1,015
  • 17
  • 41

1 Answers1

0

This worked for me:

$(window).bind('beforeunload', function() {
           _gaq.push(['_trackEvent', 'Application Form', 'exit-form_application_', '', 4, false]);
        });

        $("#formID").submit(function(){
            $(window).unbind('beforeunload');
        });
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
user1961082
  • 1,015
  • 17
  • 41
  • does the call to Google always finish in time, though? before the new page is loaded? Does it correctly register this event? – Mansiemans Jun 05 '13 at 11:10
  • no - if the page is unloaded for an outbound link, GA may not have sent the event. You need to use hitcallback as described here: [Use Google Analytics hitCallback](http://stackoverflow.com/questions/4086587/track-event-in-google-analytics-upon-clicking-form-submit/12461558#12461558). @Mansiemans – Mark Hansen Mar 18 '14 at 18:39