Why is the Google Analytics event not dispatched from onSubmit?

I would like to track how often users download files using Google Analytics events, but even though the events seem to have been sent, .gif tracking doesn't seem to work.

To upload files, users must fill out a short form. The information entered into this form is checked by the checkSubmit() function (which returns false when the information entered is invalid or incomplete, true when the information is good for sending). After sending, the user is redirected to the file.

 <script> function checkSubmit() { … if(dataIsGood) { _gaq.push(['_trackEvent', 'Download', 'the_filename.xxx']); return true; //allow the form to submit } else { _gaq.push(['_trackEvent', 'Form', 'info not okay']); return false; //keep the form from being submitted } } </script> <form action="/form/emailcaptureform" method="post" onSubmit="return checkSubmit();"> … </form> 

In the Chrome console, the Google Analytics Tracking Tracker says that the event tracking beacons are loading, but the console says that it failed to load the __utm.gif resource

This only happens with events that were clicked just before the form was submitted . Events elsewhere on my site, including the Form event that was triggered when the form information does not fit (in the else block above)

The Google Analytics snippet works by accepting _trackPageview and _trackEvent s.

The request URL for __utm.gif is well-formed : copying and pasting the URL from the gadebug output file into the location bar returns __utm.gif without a hitch.

The event is ejected without problems when pressed from another element. For example:

 <a href="#" onClick="_gaq.push(['_trackEvent', 'Download', the_filename.xxx']);">Event!</a> 

Clicking the event pauses for a few seconds, and returning true does not seem to affect adding a pause before submitting the form.

Do you have any solutions or suggestions?

+4
javascript forms form-submit google-analytics
Feb 08 2018-11-11T00:
source share
1 answer

Javascript is single threaded. When you execute _gaq.push , you push something into the queue that needs to be processed later. However, your code returns true until "later", and the GA code never runs (because the next step is to go to the next page).

I would suggest that this might be the right time to use traditional or synchronous tracking with ._trackEvent

+4
Mar 27 '11 at 16:02
source share



All Articles