(function () { const HISTORY_LENGTH = 1000; const url = window.location.href; const utm_params = new URLSearchParams(window.location.search); const utm_source = utm_params.get('utm_source'); const utm_medium = utm_params.get('utm_medium'); const utm_campaign = utm_params.get('utm_campaign'); const utm_content = utm_params.get('utm_content'); const utm_term = utm_params.get('utm_term'); const referrer = document.referrer; const user_agent = navigator.userAgent; const user_language = navigator.language; const user_vendor = navigator.vendor; const user_screen_width = window.screen.width; const user_screen_height = window.screen.height; const user_timezone = new Date().getTimezoneOffset(); const user_timezone_name = Intl.DateTimeFormat().resolvedOptions().timeZone; let deviceId = localStorage.getItem('deviceId'); if (!deviceId) { deviceId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); localStorage.setItem('deviceId', deviceId); } let sessionId = localStorage.getItem('sessionId'); if (!sessionId) { sessionId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); localStorage.setItem('sessionId', sessionId); } let sessionTime = localStorage.getItem('sessionTime'); const now = new Date().getTime(); const diff = now - (sessionTime || 0); const history = JSON.parse(localStorage.getItem('_whoami') || null) || []; let isNewSession = false; if (diff > 1800000) { sessionId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); localStorage.setItem('sessionId', sessionId); isNewSession = true; } if (history.length > HISTORY_LENGTH) { history = history.slice(0, HISTORY_LENGTH - 1); } if (isNewSession || utm_campaign || referrer || utm_source || utm_medium || utm_content || utm_term) { history.push({ url, at: Math.round(new Date().getTime() / 1000), sessionId, utm_source, utm_medium, utm_campaign, utm_content, utm_term, referrer, }) } sessionTime = new Date().getTime(); localStorage.setItem('sessionTime', sessionTime); localStorage.setItem('_whoami', JSON.stringify(history)); function getFirst(prop) { const item = history.find(item => item[prop]); return item ? item[prop] : null; } fetch('/api/whoami', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ session_id: sessionId, device_id: deviceId, user_agent, user_language, user_screen_width, user_screen_height, user_timezone, user_timezone_name, first_lp: history[0].url, last_lp: history[history.length - 1].url, first_source: history[0].utm_source, last_source: history[history.length - 1].utm_source, first_medium: history[0].utm_medium, last_medium: history[history.length - 1].utm_medium, first_campaign: history[0].utm_campaign, last_campaign: history[history.length - 1].utm_campaign, first_content: history[0].utm_content, last_content: history[history.length - 1].utm_content, first_term: history[0].utm_term, last_term: history[history.length - 1].utm_term, first_referrer: history[0].referrer, last_referrer: history[history.length - 1].referrer, first_at: history[0].at, last_at: history[history.length - 1].at, utm_source: getFirst('utm_source'), utm_medium: getFirst('utm_medium'), utm_campaign: getFirst('utm_campaign'), utm_content: getFirst('utm_content'), utm_term: getFirst('utm_term'), referrer: getFirst('referrer'), }) }); })();