I am working on a tracking script for a fairly sophisticated CRM to track form actions in Google Analytics. I try to balance the desire to track form actions with to never prevent the form from working.
Now I know that doing something like this does not work.
$('form').submit(function(){ _gaq.push(['_trackEvent', 'Form', 'Submit', $(this).attr('action')]); });
The DOM is unloaded before it has the ability to process.
So, many code examples recommend something like this:
$('form').submit(function(e){ e.preventDefault(); var form = this; _gaq.push(['_trackEvent', 'Form', 'Submit', $(this).attr('action')]);
This is true in most cases , but it makes me nervous. What if something happens between e.preventDefault(); and when do I communicate with the proposal to send the DOM? I completely disrupted the form.
I reflected on some other analytics implementations, and I noticed something like this :
$('form').mousedown(function(){ _gaq.push(['_trackEvent', 'Form', 'Submit', $(this).attr('action')]); }); $('form').keydown(function(e){ if(e.which===13)
Basically, instead of interrupting form submission, unload it, assuming that if someone is silent or presses the Enter key, then this form is submitted. Obviously, this will lead to a certain number of false positives, but completely eliminates the use of e.preventDefault(); , which in my opinion eliminates the risk that I can ever prevent the successful submission of the form.
So my question is:
- Is it possible to accept the standard form tracking fragment and prevent it from ever completely preventing the form from being submitted?
- Is mousedown / keydown an alternative viable?
- Are there any filing cases that he may miss? In particular, are there other ways to serve in addition to the mouse and keyboard? And will the browser always have time to handle javascript before starting to unload the page?