(function () { function getAllQueryParams() { const qs = new URLSearchParams(window.location.search); const obj = {}; for (const [key, value] of qs.entries()) { obj[key] = value; } const scripts = Array.from(document.getElementsByTagName('script')); const script = scripts.find(s => s.src.includes('js/pixel.js')); const url = new URL(script.src); for (const [key, value] of url.searchParams.entries()) { obj[key] = value; } return obj; } function getMarketingParams(params) { const keys = [ 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'gclid', 'fbclid', 'ttclid', 'li_fat_id', 'twclid', 'rdt_cid' ]; return Object.fromEntries(keys.filter(k => params[k]).map(k => [k, params[k]])); } function generateUuid() { return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, c => (+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16) ); } function getSessionId() { const key = 'tracking_session_id'; let id = sessionStorage.getItem(key); if (!id) { let uuid = generateUuid(); id = 'sess_' + uuid; sessionStorage.setItem(key, id); } return id; } function getSessionTime() { const key = 'tracking_session_time'; let time = sessionStorage.getItem(key); if (!time) { time = new Date().toISOString(); sessionStorage.setItem(key, time); } return time; } function getFingerprint() { const data = [ navigator.userAgent, navigator.language, screen.width + 'x' + screen.height, screen.colorDepth, window.devicePixelRatio, Intl.DateTimeFormat().resolvedOptions().timeZone, (() => { if (/windows/i.test(navigator.userAgent)) { return "Windows" } else if (/iphone/i.test(navigator.userAgent)) { return "iOS" } else if (/ipad/i.test(navigator.userAgent)) { return "iOS" } else if (/macintosh/i.test(navigator.userAgent)) { return "Mac OS" } else { return ""; } })(), navigator.hardwareConcurrency, navigator.maxTouchPoints, !!window.TouchEvent, !!window.localStorage, !!window.sessionStorage, Object.keys(navigator).length ].join('||'); return hashString(data); } function hashString(str) { let hash = 5381; for (let i = 0; i < str.length; i++) { hash = (hash * 33) ^ str.charCodeAt(i); } return (hash >>> 0).toString(16); } function findInputValueLike(keywords = []) { const inputs = document.querySelectorAll('input, textarea, select'); for (const input of inputs) { const name = input.name?.toLowerCase() || ''; const id = input.id?.toLowerCase() || ''; const placeholder = input.placeholder?.toLowerCase() || ''; const label = document.querySelector(`label[for="${input.id}"]`)?.innerText?.toLowerCase() || ''; for (const keyword of keywords) { if ([name, id, placeholder, label].some(s => s.includes(keyword))) { return input.value; } } } return ''; } async function sendLeadPayload() { const params = getAllQueryParams(); const firstName = findInputValueLike(['first', 'firstname', 'name']); const lastName = findInputValueLike(['last', 'lastname', 'last_name']); const name = `${firstName} ${lastName}`.trim(); const payload = { session_id: getSessionId(), session_time: getSessionTime(), fingerprint: getFingerprint(), current_url: encodeURI(document.URL), referrer: document.referrer, contact: { name, email: findInputValueLike(['email']), phone: findInputValueLike(['phone', 'tel']), city: findInputValueLike(['city', 'town']), country: findInputValueLike(['country']), address: findInputValueLike(['address', 'street']), }, marketing: getMarketingParams(params), tag: params['tag'] || '' }; await fetch('https://yuna-dev.positano.kg/api/track-lead', { method: 'POST', headers: {'Content-Type': 'application/json'}, credentials: 'include', body: JSON.stringify(payload) }); } async function sendFormSubmittedEvent() { const params = getAllQueryParams(); const sessionId = params.tracking_session_id || sessionStorage.getItem('tracking_session_id') const name = params.name || ''; const surname = params.surname || ''; const email = params.email || ''; const payload = { session_id: sessionId, session_time: getSessionTime(), current_url: encodeURI(document.URL), referrer: document.referrer, event: 'form_submitted', via: params.tracking_session_id ? 'url' : sessionStorage.getItem('tracking_session_id') ? 'session_storage' : 'inferred_referrer', contact: { name: `${name} ${surname}`.trim(), email: email }, marketing: getMarketingParams(params), timestamp: new Date().toISOString() }; await fetch('https://yuna-dev.positano.kg/api/track-event', { method: 'POST', headers: {'Content-Type': 'application/json'}, credentials: 'include', body: JSON.stringify(payload) }); } function autoSendWhenFilled() { setInterval(() => { const hasName = findInputValueLike(['first', 'firstname', 'name']); const hasEmail = findInputValueLike(['email']); const hasPhone = findInputValueLike(['phone', 'tel']); if (hasName || hasEmail || hasPhone) { sendLeadPayload().then(); } }, 3000); const forms = document.querySelectorAll('form'); for (const form of forms) { form.addEventListener('submit', async function (e) { e.preventDefault(); await sendLeadPayload().then(); form.submit(); }); } const buttons = document.querySelectorAll('button'); for (const button of buttons) { if (button.type === 'submit') { button.addEventListener('click', async function (e) { e.preventDefault(); await sendLeadPayload().then(); const form = button.closest('form'); if (form) { form.submit(); } }); } } } function isTrackedFromFormServices() { const referrer = URL.parse(document.referrer); if (!referrer) return null; return [ 'www.jotform.com', 'jotform.com' ].includes(referrer.host); } getSessionTime(); sendLeadPayload().then(); if ( window.location.search.includes('tracking_session_id') || isTrackedFromFormServices() ) { sendFormSubmittedEvent().then(); } else { autoSendWhenFilled(); } })();