I want to track mouse click events on a set of user interface components across multiple pages. To do this, I use the following jquery / ajax call (trimmed u):
1.Ajax call that will add a click record.
myClickLogger = { endpoint: '/path/to/my/logging/endpoint.html', logClickEvent: function(clickCode) { $.ajax({ 'type': 'POST', 'url': this.endpoint, 'async': true, 'cache': false, 'global': false, 'data': { 'clickCode':clickCode }, 'error': function(xhr,status,err){ alert("DEBUG: status"+status+" \nError:"+err); }, 'success': function(data){ if(data.status!=200){ alert("Error occured!"); } } }); } };
2.JQuery click event, which will call ajax logger (clickCode is the identifier for which the button / image is clicked):
$(document).ready(function() { $(".myClickEvent[clickName]").click(function() { var clickCode = $(this).attr("clickName"); myClickLogger.logClickEvent(clickCode); }); });
The above ajax (1.) call is “canceled” by the browser whenever a click on a button is tracked, it goes to a new page.
If I changed 'aysnc' to 'false' then ajax call will succeed.
In addition, click events that do not fall on a new page will fail. Only events that click on a new page are canceled.
I do not want the call to be synchronous.
Any ideas what could be the problem? How can I guarantee that the asynchronous call will be completed before the click event moves to a new page?
source share