let htDeviceId, htSessionId, htNewSession = false; function generateDeviceID() { return (Date.now().toString().substring(1)+(Math.random().toString().substr(2)) + (Math.random().toString().substr(2))).substr(0,27); } function getCookie (name) { let value = `; ${document.cookie}`; let parts = value.split(`; ${name}=`); if (parts.length === 2) return parts.pop().split(';').shift(); } function setCookie (name, value, expiration) { document.cookie = `${name}=${value};path=/;expires=${expiration}`; } function getCookieExpiration(offset = null) { offset = offset || (10 * 365 * 24 * 60 * 60 * 1000); let d = new Date(); d.setTime(d.getTime() + offset); return d.toUTCString(); } function getDeviceId() { htDeviceId = htDeviceId || getCookie('ht_device_id'); if(!htDeviceId) { htDeviceId = generateDeviceID(); setCookie('ht_device_id', htDeviceId, getCookieExpiration()); } return htDeviceId; } function getSessionId() { htSessionId = htSessionId || getCookie('ht_session_id'); if(!htSessionId) { htNewSession = true; htSessionId = generateDeviceID(); setCookie('ht_session_id', htSessionId, getCookieExpiration(30 * 60 * 1000)); } return htSessionId; } function getScreenPlatform() { let windowWidth = window.innerWidth; let screenPlatform = ""; let sizeToPlatformMap = { "desktop": 1025, "tablet.landscape": 961, "tablet.portrait": 768, "phone": 0, }; for (let platform of Object.keys(sizeToPlatformMap)){ let minSize = sizeToPlatformMap[platform]; if (windowWidth >= minSize){ screenPlatform = platform; break; } } return screenPlatform; } function trackEvent(eventName, additionalData = {}) { let screenPlatform = getScreenPlatform(); let eventData = { "device_id": getDeviceId(), "tracking_id": getDeviceId(), "session_platform": screenPlatform, "session_screen_platform": screenPlatform, "session_id": getSessionId(), "session_new_session": htNewSession, "session_user_agent": window.navigator.userAgent, "page_url": window.location.origin + window.location.pathname, "page_url_qs": window.location.search.substring(1), "page_type": 'rsoc_landing', "site_js_site": 'rsoc', "event_topic":"user_events", "theme_name": "hometalk", "event_name":eventName } eventData = { ...eventData, ...additionalData }; var xhr = new XMLHttpRequest(); xhr.open("POST", 'https://www.hometalk.com/eventStream', true); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.send(JSON.stringify(eventData)); } trackEvent('New Page'); window.htTracker = trackEvent; if (typeof gtag === 'function') { gtag('config', 'G-0HSPVPZN5P'); }