console.log("Update User Context"); //datos del evento var fechaEvento = new Date(); var parametrosUrl = Object.fromEntries(new URLSearchParams(window.location.search)); var solicitudMotor = ""; if (typeof solicitud !== "undefined") { solicitudMotor = solicitud; } var datosEnvioEvento = { "event_type": "page_view", "ref": document.referrer || '', "url": window.location.href || '', "res": `${window.innerWidth || screen.width}`, "utm_source": parametrosUrl["utm_source"] || "", "utm_medium": parametrosUrl["utm_medium"] || "", "utm_campaign": parametrosUrl["utm_campaign"] || "", "attr_source": parametrosUrl["attr_source"] || "", "attr_medium": parametrosUrl["attr_medium"] || "", "attr_content": parametrosUrl["attr_content"] || "", "request_engine_id": solicitudMotor || "" }; var datosFetch = []; //check user_id if (typeof parametrosUrl.uid !== "undefined") { //procedente de parametro (entrada al motor desde web) console.log("procedente de parametro"); datosEnvioEvento["user_id"] = parametrosUrl.uid; datosFetch["is_first_visit"] = false; fechaEvento.setDate(fechaEvento.getDate() + 365); document.cookie = `orbook_user_id=${datosEnvioEvento["user_id"]}; expires=${fechaEvento.toUTCString()};path=/; Secure; SameSite=None`; //domain=.orbook.es; } else { //procedente de cookie console.log("COOKIE: " + getCookieSGOrbook('orbook_user_id')); function getCookieSGOrbook(name) { var match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)')); return match ? decodeURIComponent(match[2]) : null; } if (getCookieSGOrbook('orbook_user_id') !== null) { //USER_ID EXISTE datosEnvioEvento["user_id"] = getCookieSGOrbook('orbook_user_id'); datosFetch["is_first_visit"] = false; } else { //USER_ID A PRIORI NO EXISTE -> enviar vacĂ­o para que se genere uno nuevo durante el fetch o lo lea desde orbook.es (porque ya haya entrado al motor) datosFetch["is_first_visit"] = true; } } //fetch evento fetch(`https://comunes.orbook.es/controllers/eventos/init.php`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: JSON.stringify(datosEnvioEvento), keepalive: true, credentials: 'include' }) .then(response => response.text()) .then(data => { data = JSON.parse(data); if (datosFetch["is_first_visit"]) { //PRIMERA VISITA (USER_ID NO EXISTE) console.log("primera visita"); fechaEvento.setDate(fechaEvento.getDate() + 365); document.cookie = `orbook_user_id=${data.user_id}; expires=${fechaEvento.toUTCString()}; path=/; Secure; SameSite=Lax`; } else { //RETURNING USER (USER_ID EXISTE) console.log("returning"); } }).catch(e => console.error('Tracking error:', e));