export class WpjIncomaker { constructor() { this.column_count = 4; this.product_count = 4; } loadRelatedProducts(element, id_product) { this.fetchProduts({productId: id_product}, (products) => { const filter = {products:products.map(function(product){return product.id})}; const data = { column_count: this.column_count, count: products.length, products_filter: JSON.stringify(filter) } window.wpj.blocekRuntime.insertProducts(JSON.stringify(data), function(data) { element.innerHTML = data.html; // Trigger imagesLoaded event to notify lazy load element.dispatchEvent(new CustomEvent('imagesLoaded', { bubbles: true })); }); }) } fetchProduts(params, callback) { const incoData = this.getIncomakerVariables(); const data = { "engineParams": { "engineType": "PRODUCT", "pluginUuid": "a74c8ced-44b9-4485-a6b6-aaa09206e67d", "permId": incoData.p, "productFeedType": "TAG", "currency": "CZK", "itemCount": this.product_count, "language": "cs", ...params } }; if (incoData.k) { data.cId = incoData.k; } fetch('https://dg5.incomaker.com/tracking/rest/content/engine', { method: 'POST', headers: { 'Content-Type': 'text/plain;charset=UTF-8' }, redirect: 'follow', body: JSON.stringify(data) }).then(function(response) { response.json().then(function(data) { const products = []; const productIdRegexp = /_z(\d+)/; for (const [index, product] of Object.entries(data)) { products.push({id: product.url.match(productIdRegexp)[1]}); } callback(products); }); }); } getIncomakerVariables() { let cookies = this.getClientCookies(); let data = {}; if (cookies && cookies['incomaker_k']) { data.k = cookies['incomaker_k'] } if (cookies && cookies['incomaker_c']) { data.c = cookies['incomaker_c'] } if (cookies && cookies['incomaker_p']) { data.p = cookies['incomaker_p'] } this.getSessionKey(); if (!data.p) { data.p = this.getSessionKey(); } return data; } // Incomaker support functions fixCookieSameSite(key, val) { let cdate = new Date; cdate.setFullYear(cdate.getFullYear() + 3); document.cookie = key + '=' + val + '; path=/; expires=' + cdate.toUTCString() + ';SameSite=None; Secure;' } getClientCookies() { let urlS = new URL(location.href); let incoK = null; try { incoK = urlS.searchParams.get('incoK'); if (incoK) { let cdate = new Date; cdate.setFullYear(cdate.getFullYear() + 3); document.cookie = 'incomaker_k=' + incoK + '; path=/; expires=' + cdate.toUTCString() + ';SameSite=None; Secure;' } } catch (e) { console.log(e) } try { let c = urlS.searchParams.get('incoCampId'); if (c) { let cdate = new Date; cdate.setDate(cdate.getDate() + 1); document.cookie = 'incomaker_c=' + c + '; path=/; expires=' + cdate.toUTCString() + ';SameSite=None; Secure;' } } catch (e) { console.log(e) } return document.cookie.split(';').map((function (x) { return x.trim().split('=') })).reduce((function (key, val) { let innerVal = val[1]; if (typeof innerVal === 'string') { innerVal = innerVal.replace(/"/g, '\'') } key[val[0]] = innerVal; if ((val[0].indexOf('##') !== - 1 || val[0] === 'permId') && innerVal.toLowerCase().indexOf('samesite') === - 1) { fixCookieSameSite(val[0], innerVal) } return key }), { }) } getSessionKey() { let cookies = this.getClientCookies(); let sessionKey; if (cookies['_____tempSessionKey_____'] === undefined) { sessionKey = 'e719b1c0-e884-11eb-8083-71fd1566d158'; } else { sessionKey = cookies['_____tempSessionKey_____'] } let cdate = new Date; cdate.setFullYear(cdate.getFullYear() + 3); document.cookie = '_____tempSessionKey_____=' + sessionKey + '; path=/; expires=' + cdate.toUTCString() + ';SameSite=None; Secure;'; return sessionKey } } window.wpj.Incomaker = WpjIncomaker;