I've got 6 buttons which each have event listeners. For any given button, I have the click event currently firing a tracking event, and then submitting a hidden form. This works most the time, but occasionally the tracking event doesn't fire. The form always submits. My code:
var executeTracking = function(){
tracking.track();
}
var buttons = document.getElementsByClassName('form-submit-button');
buttons[0].addEventListener('click', function(){
executeTracking();
submitForm();
}
On desktop for Chrome this always works correctly. On Firefox it doesn't fire the first function maybe 10% of the time. Same on mobile safari. It seems like sometimes it doesn't work well after going back to the page, once submitting the form.
I've tried always doing a hard reload of the page. I've tried placing the javascript directly in the HTML file. I've tried using setTimeouts. I've also tried doing a callback like this:
var submitForm = function(self) {
// alert('form')
self.parentNode.getElementsByTagName('form')[0].submit();
}
var fireTracking = function(cta, self, callback) {
// alert('mixpanel')
tracking.track();
callback(self);
}
var buttons = document.getElementsByClassName('form-submit-button');
buttons[0].addEventListener('click', function(){
fireTracking(cta, this, submitForm);
}, true);
Still, while the form always submits, the tracking event doesn't always fire on all devices (firefox desktop and safari mobile don't always run the tracking event before submitting).
Finally, when I've run the Alert functions which are currently commented out, everything works fine. It's as if the pause in the code allows the tracking function to execute. I've tried to mimic this by putting a setTimeout function in to pause the form submission. This doesn't seem to work and the form submits as fast as it can anyway.