(function() {
const N8N_WEBHOOK = 'https://gritboss.app.n8n.cloud/webhook/vrlr-intake';
const BRIDGE_PAGE = 'https://thegritboss.com/bridge';
// Wait for the form to be ready
function waitForForm() {
const form = document.querySelector('form');
if (form) {
attachInterceptor(form);
} else {
setTimeout(waitForForm, 300);
}
}
function getVal(queryKey) {
// Try by name, id, or data-name attribute
const el = document.querySelector(
`input[name="${queryKey}"], input[id="${queryKey}"],
input[data-name="${queryKey}"], textarea[name="${queryKey}"]`
);
return el ? el.value.trim() : '';
}
function attachInterceptor(form) {
// Listen for GHL's form submit button click
const btn = form.querySelector('button[type="submit"], input[type="submit"], button');
form.addEventListener('submit', function(e) {
// Don't block — let GHL process the form normally
// We just fire off the n8n POST in the background
sendToN8n();
}, true);
// Also listen for button click in case GHL uses click event
if (btn) {
btn.addEventListener('click', function() {
setTimeout(sendToN8n, 500); // slight delay to let GHL capture values first
});
}
}
let hasFired = false;
function sendToN8n() {
if (hasFired) return;
hasFired = true;
// Read all field values using actual GHL field IDs
const firstName = getVal('first_name') || '';
const email = getVal('email') || '';
const phone = getVal('phone') || '';
const venueName = getVal('kJV51FlIAYpgOllqRAaa') || '';
const website = getVal('xHPdWKKmrlW5JGsqwdIv') || '';
const inquiries = parseFloat(getVal('bp31WMZVJuThKaorBDDs')) || 0;
const missedCalls= parseFloat(getVal('2IfxGN7MtLpSFfagrHVJ')) || 0;
const afterHours = parseFloat(getVal('WGc2RyEkcfYBdieZol0e')) || 0;
const avgValue = parseFloat(getVal('J1xB0ri6crOpmHLn3inj')) || 0;
const bookings = parseFloat(getVal('x8RyOZrKJ0EcDNRcFeUG')) || 0;
// Store all values in sessionStorage
sessionStorage.setItem('vrlr_first_name', firstName);
sessionStorage.setItem('vrlr_email', email);
sessionStorage.setItem('vrlr_venue', venueName);
sessionStorage.setItem('vrlr_website', website);
sessionStorage.setItem('vrlr_inquiries', inquiries);
sessionStorage.setItem('vrlr_missed_calls', missedCalls);
sessionStorage.setItem('vrlr_afterhours', afterHours);
sessionStorage.setItem('vrlr_avg_value', avgValue);
sessionStorage.setItem('vrlr_bookings', bookings);
sessionStorage.removeItem('vrlr_redirect_url'); // clear any old redirect
// Fire POST to n8n and store redirect URL in sessionStorage when done
fetch(N8N_WEBHOOK, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
contact_id: sessionStorage.getItem('vrlr_contact_id') || '',
first_name: firstName,
email: email,
phone: phone,
venueName: venueName,
websiteUrl: website,
inquiries30: inquiries,
missedCalls30: missedCalls,
afterHoursInquiries30: afterHours,
avgBookingValue: avgValue,
bookings30: bookings,
source: 'form_intercept',
})
})
.then(res => res.text())
.then(text => {
console.log('n8n raw response:', text);
try {
const data = JSON.parse(text);
console.log('n8n parsed response:', data);
const redirectUrl = data.redirectUrl || data.redirect_url ||
(data.redirectParams ? BRIDGE_PAGE.replace('bridge', 'revenue-leak-report-submitted') + '?' + data.redirectParams : null);
console.log('redirectUrl to store:', redirectUrl);
if (redirectUrl) {
sessionStorage.setItem('vrlr_redirect_url', redirectUrl);
console.log('stored in sessionStorage:', sessionStorage.getItem('vrlr_redirect_url'));
}
} catch(e) {
console.log('JSON parse error:', e, 'raw text:', text);
if (text.startsWith('http')) {
sessionStorage.setItem('vrlr_redirect_url', text.trim());
}
}
})
.catch(err => console.warn('n8n POST error:', err));
}
// Start watching for form
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', waitForForm);
} else {
waitForForm();
}
})();