The client has a complicated form setup.
Parse the text. Then, create a faux form and submit it by breaking it down.
The code below is customized for this client, but the same solution can be applied to other clients who might have this kind of problem. We just need to modify the condition of parsing depending on the text format.
var textInsideBooklyBox = jQuery('div.bookly-box').text();
var index = textInsideBooklyBox.indexOf("Please provide your details in the form below to proceed with booking.");
var trimmedText = textInsideBooklyBox.substring(0, index).trim();
var startIndex = trimmedText.indexOf("You selected a booking for ") + "You selected a booking for ".length;
var endIndex = trimmedText.indexOf(" by Natalie Hamilton at");
var selectedBooking = trimmedText.substring(startIndex, endIndex).trim();
var words = selectedBooking.split(" ");
var lastWord = words[words.length - 1];
var sessionType;
if (lastWord === "Basic" || lastWord === "Chic" || lastWord === "Luxe") {
sessionType = "Others";
} else {
sessionType = lastWord;
}
sessionType = sessionType.replace(/-/g, "");
var startIndex = trimmedText.indexOf("by Natalie Hamilton at ") + "by Natalie Hamilton at ".length;
var endIndex = trimmedText.indexOf(" on ");
var selectedTime = trimmedText.substring(startIndex, endIndex).trim();
var pmOnIndex = trimmedText.indexOf(" pm on ");
var amOnIndex = trimmedText.indexOf(" am on ");
var startIndex, endIndex;
if (pmOnIndex !== -1) {
startIndex = pmOnIndex + " pm on ".length;
endIndex = trimmedText.indexOf(". The price for the service is ", startIndex);
} else if (amOnIndex !== -1) {
startIndex = amOnIndex + " am on ".length;
endIndex = trimmedText.indexOf(". The price for the service is ", startIndex);
}
var selectedDate = trimmedText.substring(startIndex, endIndex).trim();
var searchString = ". The price for the service is ";
var startIndex = trimmedText.indexOf(searchString) + searchString.length;
var bookingPrice = trimmedText.substring(startIndex).trim();
bookingPrice = bookingPrice.slice(0, -1);
var form_id = 'mcfx_custom_id_book_now'; // Fake form name
var fields = {
"your_name": document.querySelector('[id^="bookly-full-name-"]').value,
"your_phone": document.querySelector('[id^="bookly-phone-"]').value,
"your_email": document.querySelector('[id^="bookly-email-"]').value,
"your_notes": document.querySelector('[id^="bookly-notes-"]').value,
"session_type": sessionType,
"selected_booking": selectedBooking,
"selected_time": selectedTime,
"selected_date": selectedDate,
"booking_price": bookingPrice,
}
var wrapper = document.createElement('div');
var form = '<form id="' + form_id + '">'; // Create a fake form
Object.keys(fields).map(function(field) {
var label = field;
var val = fields[field];
form += '<input type="hidden" value="' + val + '" name="' + label + '">';
});
form += '</form>';
wrapper.innerHTML = form;
mcfx(function(tracker) {
tracker.capture(wrapper.firstChild);
console.log('MCFX capture success');
});
The lead was captured.