(function(){
var WEBHOOK = "https:\/\/bellandlyons.com\/wp-json\/bell-lyons\/v1\/lead-webhook";
var EMAIL_ENDPOINT = "https:\/\/bellandlyons.com\/wp-json\/bell-lyons\/v1\/lead-notify-full";
var ZAPIER_ACORD = "https://hooks.zapier.com/hooks/catch/27012159/uvqfadw/";
var IS_COMMERCIAL = window.location.pathname.indexOf('commercial') >= 0;
/* Fix 1: Set the missing data-webhook attribute */
document.querySelectorAll('[data-assistant-root]').forEach(function(r){
if(!r.dataset.webhook) r.dataset.webhook = WEBHOOK;
});
/* Fix 2: Patch global postLead */
var emailSent = false;
var _orig = window.postLead;
if(typeof _orig === 'function'){
window.postLead = function(payload, target){
var result = _orig(payload, target || WEBHOOK);
window.__blLastLeadPayload = payload;
if (!IS_COMMERCIAL && !emailSent) {
emailSent = true;
try {
fetch(EMAIL_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
}).catch(function(){});
} catch(e){}
}
return result;
};
}
/* Fix 3: Track payloads from ALL lead-related fetch calls */
var lastPayload = null;
var origFetch = window.fetch;
window.fetch = function(url, opts){
if(typeof url === 'string' && opts && opts.method === 'POST'){
// Catch WordPress lead-webhook calls
if(url.indexOf('lead-webhook') !== -1){
try {
lastPayload = JSON.parse(opts.body);
window.__blLastLeadPayload = lastPayload;
} catch(e){}
}
// Catch Convex chatbot calls (necessary-curlew)
if(url.indexOf('webhooks/lead') !== -1){
try {
var convexData = JSON.parse(opts.body);
var contact = convexData.contact || {};
var answers = convexData.answers || {};
// Map Convex chatbot format to flat format
var mapped = {
business_name: answers.business_name || answers.company_name || contact.name || '',
contact_name: contact.name || '',
_contact_name: contact.name || '',
contact_email: contact.email || '',
_contact_email: contact.email || '',
contact_phone: contact.phone || '',
_contact_phone: contact.phone || '',
address: answers.address || answers.business_address || '',
coverage_needed: answers.coverage_needed || answers.insurance_type || convexData.insurance_type || '',
entity_type: answers.entity_type || answers.business_type || '',
nature_of_business: answers.nature_of_business || answers.industry || '',
annual_revenue: answers.annual_revenue || answers.revenue || '',
employees: answers.employees || answers.num_employees || '',
years_in_business: answers.years_in_business || '',
_raw_convex: convexData
};
lastPayload = mapped;
window.__blLastLeadPayload = mapped;
console.log('[BL-LEAD] Captured Convex chatbot data:', mapped.business_name);
} catch(e){ console.warn('[BL-LEAD] Parse error:', e); }
}
}
return origFetch.apply(this, arguments);
};
/* Fix 4: Completion observer — also sends to Zapier for commercial */
document.querySelectorAll('[data-assistant-root]').forEach(function(root){
var log = root.querySelector('.assistant-log');
if(!log) return;
var finalSent = false;
new MutationObserver(function(){
if(finalSent) return;
if(!root.querySelector('.bl-confirmation-wrap')) return;
finalSent = true;
if(lastPayload){
var final = Object.assign({}, lastPayload, {
destination_email: lastPayload.destination_email || 'quotes@bellandlyons.com',
quote_request_stage: 'completed_flow'
});
origFetch(WEBHOOK, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(final)
}).catch(function(){});
if (!IS_COMMERCIAL && !emailSent) {
emailSent = true;
origFetch(EMAIL_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(final)
}).catch(function(){});
}
// For commercial page: send lead data + ACORD PDF links to Zapier
if (IS_COMMERCIAL) {
console.log('[BL-ACORD] Commercial lead completed — sending to Zapier');
var lob = final.coverage_needed || '';
var acordForms = ['125'];
var lobLower = lob.toLowerCase();
if (lobLower.indexOf('general') >= 0 || lobLower.indexOf('liability') >= 0) acordForms.push('126');
if (lobLower.indexOf('worker') >= 0 || lobLower.indexOf('comp') >= 0) acordForms.push('130');
if (lobLower.indexOf('auto') >= 0) acordForms.push('127');
if (lobLower.indexOf('property') >= 0 || lobLower.indexOf('bop') >= 0) acordForms.push('140');
var pdfBase = window.location.origin + '/wp-json/bell-lyons/v1/acord-pdf-proxy?form=';
var zapierPayload = {
business_name: final.business_name || '',
contact_name: final.contact_name || final._contact_name || '',
email: final.contact_email || final._contact_email || '',
phone: final.contact_phone || final._contact_phone || '',
address: final.address || '',
lines_of_business: lob,
entity_type: final.entity_type || '',
annual_revenue: final.annual_revenue || '',
employees: final.employees || '',
subject: 'New ACORD Application — ' + (final.business_name || 'Commercial Lead'),
pdf_url_1: pdfBase + acordForms[0],
pdf_url_2: acordForms[1] ? pdfBase + acordForms[1] : '',
pdf_url_3: acordForms[2] ? pdfBase + acordForms[2] : '',
pdf_forms: acordForms.join(', '),
num_forms: acordForms.length,
timestamp: new Date().toISOString()
};
origFetch(ZAPIER_ACORD, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(zapierPayload)
}).then(function(r) {
console.log('[BL-ACORD] Zapier response:', r.status);
}).catch(function(e) {
console.warn('[BL-ACORD] Zapier error:', e);
});
// Also trigger the ACORD form filler (snippet #21) via event
window.dispatchEvent(new CustomEvent('bl-acord-trigger', { detail: final }));
}
}
}).observe(log, { childList: true, subtree: true });
});
})();