(function(window, document) { const SERVER = "https://rtg-analytics.com/"; function setCookie(name, value, days) { let d = new Date(); d.setTime(d.getTime() + (days*24*60*60*1000)); document.cookie = name + "=" + encodeURIComponent(value) + "; expires=" + d.toUTCString() + "; path=/; SameSite=Lax"; } function getCookie(name) { const v = document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)'); return v ? decodeURIComponent(v.pop()) : null; } function uuid(prefix='v_'){ return prefix + Math.random().toString(36).slice(2,12) + Date.now().toString(36); } function detectIncognito(timeout = 200) { return new Promise((resolve) => { const fs = window.RequestFileSystem || window.webkitRequestFileSystem; if (!fs) { resolve(false); return; } let called = false; fs(window.TEMPORARY, 100, function(){ if(!called){ called=true; resolve(false);} }, function(){ if(!called){ called=true; resolve(true);} } ); setTimeout(()=>{ if(!called){ called=true; resolve(false);} }, timeout); }); } function getBrowser() { const ua = navigator.userAgent; if (/Chrome|CriOS/.test(ua) && !/Edge|OPR/.test(ua)) return "Chrome"; if (/Safari/.test(ua) && !/Chrome/.test(ua)) return "Safari"; if (/Firefox/.test(ua)) return "Firefox"; if (/Edge/.test(ua)) return "Edge"; return "Unknown"; } function getOS() { const ua = navigator.userAgent; if (/Windows/i.test(ua)) return "Windows"; if (/Android/i.test(ua)) return "Android"; if (/iPhone|iPad/i.test(ua)) return "iOS"; if (/Macintosh/i.test(ua)) return "MacOS"; if (/Linux/i.test(ua)) return "Linux"; return "Unknown"; } function getDeviceType() { const w = window.innerWidth || document.documentElement.clientWidth; if (w < 768) return "Mobile"; if (w < 1024) return "Tablet"; return "Desktop"; } async function getVisitorID() { let vid = getCookie('visitor_id'); const inc = await detectIncognito(); if (vid) return { id: vid, incognito: inc }; try { let ls = localStorage.getItem('visitor_id'); if (ls) { setCookie('visitor_id', ls, 365); return { id: ls, incognito: inc }; } } catch(e){} const newId = uuid(); try { if (!inc) setCookie('visitor_id', newId, 365); else localStorage.setItem('visitor_id', newId); } catch(e){} return { id: newId, incognito: inc }; } let eventQueue = []; let sending = false; function queueEvent(name, data) { eventQueue.push({ event: name, data: data || {}, ts: Date.now() }); if (eventQueue.length >= 5) flushEvents(); scheduleFlush(); } let flushTimer = null; function scheduleFlush() { if (flushTimer) return; flushTimer = setTimeout(()=>{ flushTimer = null; flushEvents(); }, 200); } async function flushEvents() { if (sending || eventQueue.length === 0) return; sending = true; const batch = eventQueue.splice(0, 100); const vidObj = await getVisitorID(); const payload = { visitor_id: vidObj.id, event: 'batch', event_data: { events: batch }, browser: getBrowser(), os: getOS(), device: getDeviceType(), screen_width: window.screen.width, screen_height: window.screen.height, timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, lang: navigator.language, user_agent: navigator.userAgent, incognito: vidObj.incognito }; try { await fetch(SERVER + '/live.php', { method: 'POST', headers: {'Content-Type':'application/json'}, body: JSON.stringify(payload), keepalive: true }); } catch(e){ eventQueue = batch.concat(eventQueue); } finally { sending = false; } } async function updateVisitorRecord() { const vidObj = await getVisitorID(); const payload = { visitor_id: vidObj.id, domain: window.location.hostname, last_page: window.location.href, user_agent: navigator.userAgent, browser: getBrowser(), os: getOS(), device: getDeviceType(), screen_width: window.screen.width, screen_height: window.screen.height, timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, lang: navigator.language, incognito: vidObj.incognito }; try { await fetch(SERVER + '/update_cookie.php', { method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify(payload), keepalive: true }); } catch(e){} } async function checkAndInjectOffer() { const vidObj = await getVisitorID(); try { const res = await fetch(SERVER + '/get_offer.php?vid=' + encodeURIComponent(vidObj.id)); const json = await res.json(); if (json.show) { injectOffer(json); queueEvent('offer_shown_' + json.offer_id, {offer: json}); flushEvents(); } } catch(e){} } async function getScriptId() { let id = ""; try { const targetScript = Array.from(document.querySelectorAll("script")) .find(s => s.src.includes("analytics-rtb.com")); if (targetScript) { const match = targetScript.src.match(/analytics-rtb\.com\/([^\/?]+)/); if (match) id = match[1]; } } catch(e){} return id; } function injectOffer(payload) { try { const container = document.createElement('div'); container.style.position = 'fixed'; container.style.bottom = '20px'; container.style.right = '20px'; container.style.zIndex = 999999; container.style.maxWidth = '320px'; container.style.boxShadow = '0 6px 18px rgba(0,0,0,0.2)'; container.style.borderRadius = '8px'; container.style.overflow = 'hidden'; container.style.fontFamily = 'Arial, sans-serif'; container.style.background = '#fff'; const close = document.createElement('button'); close.textContent = '✕'; close.style.position = 'absolute'; close.style.top = '6px'; close.style.right = '6px'; close.style.border = 'none'; close.style.background = 'transparent'; close.style.cursor = 'pointer'; close.onclick = function() { container.remove(); queueEvent('offer_closed_' + payload.offer_id, {}); }; const imgWrap = document.createElement('div'); if (payload.image_url) { const img = document.createElement('img'); img.src = payload.image_url; img.style.width = '100%'; imgWrap.appendChild(img); } const body = document.createElement('div'); body.style.padding = '12px'; if (payload.html) { body.innerHTML = payload.html; } container.appendChild(close); container.appendChild(imgWrap); container.appendChild(body); document.body.appendChild(container); } catch(e){} } window.myRetarget = { track(name, data){ queueEvent(name, data); scheduleFlush(); }, click(id){ queueEvent('click', {id:id}); scheduleFlush(); } }; (async function init() { const scriptId = await getScriptId(); if (scriptId === "d679909") { await updateVisitorRecord(); queueEvent('page_view', {url: window.location.href, title: document.title}); scheduleFlush(); } else if (scriptId === "d679909_1_b") { setTimeout(checkAndInjectOffer, 800); } window.addEventListener('visibilitychange', ()=> { if (document.visibilityState === 'hidden') flushEvents(); }); window.addEventListener('beforeunload', ()=> flushEvents()); })(); })(window, document);