const SSR_ID_PARAM = 'ssr_id'; const ENDPOINT_VENDOR_CTA = 'vendor-cta'; const ENDPOINT_VENDOR_EMAIL = 'vendor-email'; const ENDPOINT_VENDOR_CONVERSION = 'vendor-form'; const ENDPOINT_VENDOR_INITIALIZE = 'vendor-initialize'; function sendEvent(endpoint, payload, timeout = 5000) { console.log(`Sending event to ${endpoint} with payload:`, payload); return new Promise((resolve, reject) => { const timeoutId = setTimeout(() => { reject(new Error('Request timed out')); }, timeout); fetch(`https://select-software-reviews.bubbleapps.io/api/1.1/wf/${endpoint}`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(payload), mode: 'cors' }) .then(response => { clearTimeout(timeoutId); if (!response.ok) { reject(new Error(`Network response was not ok: ${response.status} ${response.statusText}`)); } else { resolve(response); } }) .catch(error => { clearTimeout(timeoutId); console.error('Network error or CORS issue:', error); reject(error); }); }); } function getSSRId() { console.log('Getting SSR ID from URL parameters'); const urlParams = new URLSearchParams(window.location.search); const ssrId = urlParams.get(SSR_ID_PARAM); console.log(`SSR ID obtained: ${ssrId}`); return ssrId; } (function initializeVendor() { console.log('Initializing vendor'); const ssrId = getSSRId(); if (ssrId) { const payload = { event: 'initialize', ssr_id: ssrId }; sendEvent(ENDPOINT_VENDOR_INITIALIZE, payload) .then(response => { console.log('Vendor initialize event sent successfully'); }) .catch(error => { console.error('Failed to send vendor initialize event', error); }); } else { console.error('SSR ID not found in the URL'); } })(); // MutationObserver to detect changes in the DOM (e.g., dialog visibility) const observer = new MutationObserver(() => { // Detect if the thank you message is visible and send conversion event const thankYouMessage = Array.from(document.querySelectorAll('div')).find(div => div.textContent.trim().includes('Thank you for requesting a demo with Willo today.')); if (thankYouMessage && thankYouMessage.offsetParent !== null) { // Check if element is visible console.log('Thank you message is visible, sending conversion event'); const payload = { event: 'conversion', ssr_id: getSSRId() }; sendEvent(ENDPOINT_VENDOR_CONVERSION, payload) .then(() => { console.log('Conversion event sent successfully for visible thank you message'); }) .catch(error => { console.error('Error sending conversion event for visible thank you message', error); }); observer.disconnect(); // Stop observing once the event is sent } }); observer.observe(document, { childList: true, subtree: true });