// Oplatisa UTN "use strict" class UTN { SESSION_TIMEOUT = 20 data = { pageType: "page", ip: "", url: "", sessionID: "", userHash: "", referrer: "", datetime: -1, productMicrodata: { title: "", id: "", brand: "", MPN: "", EAN: "", SKU: "", }, order: {}, cart: [], vendorId: "", } constructor(vendor_id, ip){ this.data.vendorId = vendor_id this.data.ip = ip this.init() } async init(){ try { this.getPageType() } catch (error) { console.error(error) } try { this.getUrlAddress() } catch (error) { console.error(error) } try { this.getDateTime() } catch (error) { console.error(error) } try { this.getSessionID() } catch (error) { console.error(error) } try { this.getUserHash() } catch (error) { console.error(error) } try { this.getReferer() } catch (error) { console.error(error) } try { this.getProductMicrodata() } catch (error) { console.error(error) } try { this.getOrderData() } catch (error) { console.error(error) } try { await this.getCartData() } catch (error) { console.error(error) } try { this.sendToOplatisa() } catch (error) { console.error(error) } } getPageType() { if(!dataLayer || !dataLayer[0].shoptet) return this.data.pageType = dataLayer[0].shoptet.pageType } getUrlAddress() { this.data.url = document.URL.split("/?", 1)[0] if(!this.data.url.endsWith("/")){ this.data.url += "/" } } getSessionID() { const currentTime = new Date() if(!localStorage.getItem("utnSession")){ const hash = this.md5(this.data.ip + this.data.datetime) localStorage.setItem("utnSession", JSON.stringify({ id: hash, datetime: currentTime.getTime() })) this.data.sessionID = hash return } let utnSession = JSON.parse(localStorage.getItem("utnSession")) if((currentTime - utnSession.datetime) / (1000*60) < this.SESSION_TIMEOUT){ utnSession.datetime = currentTime.getTime() localStorage.setItem("utnSession", JSON.stringify(utnSession)) this.data.sessionID = utnSession.id return } localStorage.removeItem("utnSession") this.getSessionID() } getUserHash() { if(!localStorage.getItem("utnUser")){ const hash = this.md5(this.data.ip + this.data.datetime) localStorage.setItem("utnUser", hash) } this.data.userHash = localStorage.getItem("utnUser") } getReferer() { this.data.referrer = document.referrer } getProductMicrodata() { let microdataAll = this.parseMicrodata().items let microdataProduct = microdataAll.filter(el=>el.type[0]=="https://schema.org/Product" || el.type[0]=="http://schema.org/Product")[0] let microdataBrand = microdataAll.filter(el=>el.type[0]=="https://schema.org/Brand" || el.type[0]=="http://schema.org/Brand")[0] if(!microdataProduct){ return } if("properties" in microdataProduct){ microdataProduct = microdataProduct.properties } if("name" in microdataProduct){ this.data.productMicrodata.title = microdataProduct.name[0] } if("identifier" in microdataProduct){ this.data.productMicrodata.id = microdataProduct.identifier[0] } if("sku" in microdataProduct){ this.data.productMicrodata.SKU = microdataProduct.sku[0] } if(!microdataBrand){ return } if("properties" in microdataBrand){ microdataBrand = microdataBrand.properties } if("name" in microdataBrand){ this.data.productMicrodata.brand = microdataBrand.name[0] } } getDateTime() { const currentTime = new Date() this.data.datetime = currentTime.toISOString().slice(0, -1) + "0+00:00" } getOrderData() { if(document.URL.includes("/objednavka/krok-2")){ localStorage.setItem("utnOrderDetails", JSON.stringify({ products: [...document.querySelectorAll(".cart-item-name a")] .map(e=>e.closest(".order-summary-item")) .map(e=>({ url: e.querySelector("a").href, sku: e.getAttribute("data-micro-sku")}) ), datetime: new Date().getTime() })) } if(document.URL.includes("/objednavka/dakujeme")){ if(dataLayer[0].shoptet.order){ this.data.order.products = dataLayer[0].shoptet.order.content.map((x) => {return {"name": x.name, "id": x.id, "quantity": x.quantity, "SKU": x.sku}}) this.data.pageType = "order" } if(localStorage.getItem("utnOrderDetails")){ const utnOrderDetails = JSON.parse(localStorage.getItem("utnOrderDetails")) if((new Date() - utnOrderDetails.datetime) / (1000*60) < 20){ this.data.order.products.map(e=>{ e.url = utnOrderDetails.products.find( product=>product.sku==e.SKU ).url return e }) } } } } async getCartData(){ const response = await fetch("/action/Cart/GetCartContent/?simple_ajax_cart=1") if(!response.ok) return const data = await response.json() let cartElement = document.createElement('div') cartElement.innerHTML = data.payload.content.trim() this.data.cart = [...cartElement.querySelectorAll(".cart-widget-product")].map( e=>{ return{ name: e.querySelector(".cart-widget-product-name a").childNodes[0].data.trim(), url: e.querySelector(".cart-widget-product-name a").href, quantity: parseInt(e.querySelector(".cart-widget-product-amount .quantity input[name='amount']").value) } } ) if(!this.data.cart.length){ this.data.cart = [...cartElement.querySelectorAll(".cart-reca-single-href")].map( e=>{ return { name: e.querySelector(".cr-name").childNodes[2].textContent.trim(), url: e.href, quantity: parseInt( e.querySelector( ".cr-name small[data-testid='cartWidgetProductAmount']" ).innerHTML ) } } ) } } parseCookie(str){ if(!str) { return str } return str .split(';') .map(v => v.split('=')) .reduce((acc, v) => { acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim()); return acc; }, {}); } parseMicrodata() { let result = {} let items = [] document.querySelectorAll("[itemscope]") .forEach((el, i) => { let item = { "type": [el.getAttribute("itemtype")], "properties": {} } const props = el.querySelectorAll("[itemprop]") props.forEach(prop => { item.properties[prop.getAttribute("itemprop")] = [ prop.content || prop.textContent || prop.src ] if (prop.matches("[itemscope]") && prop.matches("[itemprop]")) { let _item = { "type": [prop.getAttribute("itemtype")], "properties": {} } prop.querySelectorAll("[itemprop]") .forEach(function(_prop) { _item.properties[_prop.getAttribute("itemprop")] = [ _prop.content || _prop.textContent || _prop.src ] }) item.properties[prop.getAttribute("itemprop")] = [_item] } }) items.push(item) }) result.items = items return result; } md5(n){var r="0123456789abcdef";function t(n){var t,u="";for(t=0;t<=3;t++)u+=r.charAt(n>>8*t+4&15)+r.charAt(n>>8*t&15);return u}function u(n,r){var t=(65535&n)+(65535&r);return(n>>16)+(r>>16)+(t>>16)<<16|65535&t}function e(n,r,t,e,f,o){return u(function(n,r){return n<>>32-r}(u(u(r,n),u(e,o)),f),t)}function f(n,r,t,u,f,o,c){return e(r&t|~r&u,n,r,f,o,c)}function o(n,r,t,u,f,o,c){return e(r&u|t&~u,n,r,f,o,c)}function c(n,r,t,u,f,o,c){return e(r^t^u,n,r,f,o,c)}function a(n,r,t,u,f,o,c){return e(t^(r|~u),n,r,f,o,c)}var i,h,v,g,l,A=function(n){var r,t=1+(n.length+8>>6),u=new Array(16*t);for(r=0;r<16*t;r++)u[r]=0;for(r=0;r>2]|=n.charCodeAt(r)<>2]|=128<