mootoolsanalyticsonbeforeunloadmixpanel

Tracking Web App Page Close, AJAX request cancelled


I have some web app. It is desktop based. I have an issue where the action is getting canceled before I save my data. I am using an unload event in javascript to send some data to mixpanel (just some ajax call). The problem is that I believe that this request gets "cancelled" on occasion.

Is there a way to prevent the page from changing until I get to a callback? I feel like the answer is a no unless I am able to make the call synchronous. I don't think I have that ability with mixpanel.

I am currently using the unload event in mootools. I should probably use beforeunload? Is there a way to tell unload that I am done?


Solution

  • The short answer is no. Synchronous ajax would appear to be the only reliable option here.

    If you're controlling the client-side code, it's easy to make it synchronous. Simply add async: false ton your Request options. The server won't care.

    One potential gotcha for those of us accustomed to writing asynchronous ajax: Nothing after the Request.send() call will get executed until after the onComplete handler is done. This means that details such as displaying "Please wait..." have to appear first in the code. With asynchronous requests, the order obviously doesn't matter, and it's common to send the request first.

    As for beforeunload, it's probably not useful here. The value of beforeunload is to allow the user to cancel the impending page load. If you don't need this functionality, go with unload.