console.log("INIT ORBOOK EVENTS"); function orbookEvent(eventType) { console.log("[script] orbook event " + eventType); //datos del evento var fechaEvento = new Date(); var parametrosUrl = Object.fromEntries(new URLSearchParams(window.location.search)); var datosFetch = []; //atribucion if (eventType == "page_view") { var atribuciones = parametrosUrl; } else { var atribuciones = []; } //id solicitud motor var solicitudMotor = ""; if (typeof solicitud !== "undefined") { solicitudMotor = solicitud; } //objeto datos var datosEnvioEvento = { "propiedad": window.orbookEvents.propiedad || "", "event_type": eventType, "ref": document.referrer || '', "url": window.location.href || '', "res": `${window.innerWidth || screen.width}`, "utm_source": atribuciones["utm_source"] || "", "utm_medium": atribuciones["utm_medium"] || "", "utm_campaign": atribuciones["utm_campaign"] || "", "attr_source": atribuciones["attr_source"] || "", "attr_medium": atribuciones["attr_medium"] || "", "attr_content": atribuciones["attr_content"] || "", "request_engine_id": solicitudMotor || "" }; //check user_id function getCookieSGOrbook(name) { var match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)')); return match ? decodeURIComponent(match[2]) : null; } //procedente de parametro en URL (entrada al motor desde web) if (typeof parametrosUrl.uid !== "undefined") { //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; //procedente de cookie } else if (getCookieSGOrbook('orbook_user_id') !== null) { //USER_ID EXISTE datosEnvioEvento["user_id"] = getCookieSGOrbook('orbook_user_id'); datosFetch["is_first_visit"] = false; //no se encuentra user_id } else { datosFetch["is_first_visit"] = true; } //fetch evento fetch(`https://comunes.orbook.es/controllers/events/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)); }