//#region Carro Abandonado function BraindwCartAbandoned() { try { console.log("cartAbandoned..."); var skuArray = localStorage.getItem('bdw_cartabandoned').split(','); try{skuArray = skuArray.filter(value => Object.keys(value).length !== 0);} catch{} var bdwQueryString = ''; var itemsArray = skuArray.map(item => { var itemSku = 0; var sellerId = ''; if (item.indexOf('-') > -1 ) { itemSku = item.split('-')[0]; sellerId = item.split('-')[1]; return { id: itemSku, quantity: 1, seller: sellerId } }else{ bdwQueryString += '&fq=productId:' + item; return null; } }).filter(x => x !== null); if(bdwQueryString.length>0) { bdwQueryString = `/api/catalog_system/pub/products/search?${bdwQueryString}`; fetch(bdwQueryString) .then(r => r.json()) .then(function (data) { var bdwOldArray=itemsArray; if (data.length > 0) { itemsArray = data.map(item => { return { id: item.items[0].itemId, quantity: 1, seller: item.items[0].sellers[0].sellerId } }); } itemsArray = itemsArray.concat(bdwOldArray); console.log('itemarray',itemsArray); if (itemsArray.length > 0) { vtexjs.checkout.getOrderForm().done(function (orderfrom) { vtexjs.checkout.addToCart(itemsArray, null, 1).done(function (orderfrom) { setTimeout(function () { localStorage.removeItem('bdw_cartabandoned'); }, 500); }); }); } }) .catch(error => { $("#braindw_loader").hide(); console.log('BDW CartAbandoned. Error catalog querie' + error); }) } else if (itemsArray.length > 0) { vtexjs.checkout.getOrderForm().done(function (orderfrom) { vtexjs.checkout.addToCart(itemsArray, null, 1).done(function (orderfrom) { setTimeout(function () { localStorage.removeItem('bdw_cartabandoned'); }, 500); }); }); } } catch (e) { console.log(e); } } //#endregion var _HASHBNDW = 'megusta_produccion_gimz9'; var _COMPANY = 'megusta_produccion'; var _URLBNDW = window.location.protocol + '//s.braindw.com'; //#region Persistencia var BraindwPersistence = (function () { let storageEmailKey = 'bdw_email'; function GetEmail() { var bdwEmail = ''; if (localStorage.getItem('bdw_user')) { bdwEmail = JSON.parse(localStorage.getItem('bdw_user')).email; } if (localStorage.getItem(storageEmailKey)) { bdwEmail = localStorage.getItem(storageEmailKey); } return bdwEmail; } function GetSucCookie() { let BraindwCa = decodeURIComponent(document.cookie).split(';'); for (let bnwi = 0; bnwi < BraindwCa.length; bnwi++) { if (BraindwCa[bnwi].indexOf("VTEXSC") > -1) { return BraindwCa[bnwi].split('=')[2]; } } return ''; } function GetSessionDate() { let result = null; try { let SalesChannel = GetSucCookie(); result = { SalesChannel: SalesChannel, }; } catch (e) { console.log('GetSessionDate error ', e); } return result; } function GetGuidGuest() { let BraindwCa = decodeURIComponent(document.cookie).split(';'); for (let bnwi = 0; bnwi < BraindwCa.length; bnwi++) { if (BraindwCa[bnwi].indexOf("bdw_session") > -1) { return BraindwCa[bnwi].split('=')[1]; } } return ''; } function SendPersistency(e, url) { let n = { emailguest: GetEmail(), dataset: encodeURI(JSON.stringify(e)), page: "", url: (url) ? url : window.location.href.split("?")[0], urlb: `${_URLBNDW}/Script/braindw/${_HASHBNDW}?hs=Sat Apr 21 2018 10:00:38 GMT-0300 (Hora estándar de Argentina)`, BranchOffice: GetSucCookie(), guidguest: GetGuidGuest() }; fetch('https://p.braindw.com/Api/Data/Capture', { method: "POST", body: JSON.stringify(n), credentials: 'include', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', } }) .then((resp) => resp.json()) .then(function (data) { }) .catch(function (ex) { console.log(ex); }); } function GetCartOpened() { let e = null; var items = []; if(typeof vtexjs == "undefined") return e; var data = vtexjs.checkout.orderForm; if(data != undefined){ for (var i = 0; i < data.items.length; i++) { items.push({ ProductTitle: data.items[i].name, ProductCode: data.items[i].productId, ProductUrlImage: data.items[i].imageUrl, ProductQuantity: data.items[i].quantity, ProductQuantityDetails: '', ProductPrice: (data.items[i].sellingPrice / 100).toString(), PriceTotal: (data.items[i].sellingPrice / 100) * data.items[i].quantity, Event: "Load", ProductSku: data.items[i].id, idSeller: data.items[i].seller }) } } e = { CartNumber: '', BranchOffice: GetSucCookie(), Items: items } return e; } function PushInfo(emailGuest) { let e = { sessionDate: GetSessionDate() }; let a = GetCartOpened(); a ? e.cartOpened = a : null; SendPersistency(e); } return { PushInfo, } })(); //#endregion function bindEvent(element, eventName, eventHandler) { try { if (element.addEventListener) element.addEventListener(eventName, eventHandler, false); else if (element.attachEvent) element.attachEvent('on' + eventName, eventHandler); } catch (e) { } } //#region Captures var BraindwCapture = (function () { let storageEmailKey = 'bdw_email'; function ValidateEmail(email) { var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(String(email).toLowerCase()); } function CookiePush(email) { if (ValidateEmail(email)) { console.log('CookiePush'); try { fetch(_URLBNDW + '/tracking/SetCookiesNdway?email=' + email + '&company=' + _COMPANY, { credentials: "include", mode: 'no-cors', method: 'POST', }) .then(function (resp) { return resp.text(); }) .then(function (data) { localStorage.setItem(storageEmailKey, email); console.log("storageEMailkey ", storageEmailKey); BraindwPersistence.PushInfo(email); return false; }) .catch(function (ex) { console.log(ex); }); } catch (e) { } } } function EmailsSite(textBoxSelector) { var email; if(textBoxSelector != undefined || textBoxSelector != null){ email = document.getElementById(textBoxSelector).value; console.log('Email: ',email); if (email == undefined || email == "") return; CookiePush(email); console.log('Email: ',email); } } return { EmailsSite, //CookiePush } })(); //#endregion var BraindwDomReady = function (callback) { document.readyState === "interactive" || document.readyState === "complete" ? callback() : document.addEventListener("DOMContentLoaded", callback); }; BraindwDomReady(function () { var nIntervId; if(localStorage.getItem('bdw_cartabandoned') !== null && window.location.href.indexOf('checkout/#/cart') > -1){ BraindwCartAbandoned() ; } window.addEventListener("popstate", handleUrlChange); function handleUrlChange() { if (window.location.href.includes("/checkout")) { if(document.getElementById('client-pre-email') != null){ BraindwCapture.EmailsSite('client-pre-email'); }else{ if(document.getElementById('client-email') != null){ BraindwCapture.EmailsSite('client-email'); } } } } }); try{ console.log('[abtest 0] [usertest ]'); } catch(e) {console.log('error load abtest log')}