问题
I have a few different mixpanel events being sent from my page. A couple of them are sent upon form submissions (two different forms, each in its own modal). Of all the events, one of the form submission events fails intermittently and I can't figure out why. Both form submissions have jQuery like:
$(document).on 'submit', '#myForm', (e) ->
mixpanel.track('my form submitted')
One form is reliable, the other is not (it fails about 3/4 of the time). I changed the unreliable one to:
$(document).on 'submit', '#myForm', (e) ->
window.mixpanel.track('my form submitted')
and it seems to work most of the time, but still not always. Am I imagining the difference, or could changing mixpanel.track
to window.mixpanel.track
have changed something. And what oh what am I missing here?
回答1:
I think this is caused by Mixpanels asynchronous behaviour. When you call submit, the track() instruction starts executing in a non blocking way, allowing to submit the form even when the track() call to the Mixpanel API is still ongoing. If this happens, your form will be submitted before the track() call is completed.
The same thing is happening when clicking on links and is explained in this post. It's the reason Mixpanel offers the track_links() and track_forms() methods. These solve the problem as follows and may be helpful in your case.
This function will wait up to 300 ms for the mixpanel servers to respond, if they have not responded by that time it will head to the link without ensuring that your event has been tracked. To configure this timeout please see the mixpanel.set_config docs below.
来源:https://stackoverflow.com/questions/24292206/mixpanel-track-event-failing-intermittently