function uuidv4() { return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)); } function splitString(stringToSplit, separator) { var arrayOfStrings = stringToSplit.split(separator); } function serialize(obj, prefix) { var str = [], p; for (p in obj) { if (obj.hasOwnProperty(p) && obj[p]) { var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p]; str.push((v !== null && typeof v === "object") ? serialize(v, k) : encodeURIComponent(k) + "=" + encodeURIComponent(v)); } } return str.join("&"); } function setCookie(cname, cvalue, exdays,domain=null) { var d = new Date(); d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); var expires = "expires=" + d.toUTCString(); document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/;"+(domain?("domain="+window.location.hostname.replace(window.location.hostname.split(".")[0]+".","")):""); } function getCookie(cname) { var name = cname + "="; var decodedCookie = decodeURIComponent(document.cookie); var ca = decodedCookie.split(";"); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == " ") { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; } function isEmpty(obj) { return obj && Object.keys(obj).length === 0 && Object.getPrototypeOf(obj) === Object.prototype; } if(getCookie("hoopCliente")&&!getCookie("client_deal")){ setCookie("client_deal",getCookie("external_ref"),90,1); } if(!getCookie("hoopCliente")&&getCookie("client_deal")){ setCookie("hoopCliente",1,90); } function sendEvent({status = null, name = null, email = null, phone = null, cep = null, cpf = null, prods = null, prod_name = null, prod_sku = null, prod_img = null, prod_category = null, variation_id = null , prod_id = null, id = null, final_value = null, reference = null,client_id = null, ecommerce = null,url_key=null}) { if (getCookie("external_ref") == "") setCookie("external_ref", uuidv4(), 90); var external_ref = getCookie("external_ref"); var utms={}; var queryString = window.location.search; var urlParams = new URLSearchParams(queryString); urlParams.forEach(function(value, key) { if(key=="campaign_id") key="ad_"+key; utms[key] = value; if(key=="hclid"){ setCookie("hoopCliente", 1, 90); } }); if(!isEmpty(utms)){ utms.url = window.location.href; var utc = new Date().toJSON().slice(0,10).replace(/-/g,"/"); utms.date = utc.split("/")[2] + "/" + utc.split("/")[1] + "/" + utc.split("/")[0]; } function readTextFile(file, callback) { var rawFile = new XMLHttpRequest(); rawFile.overrideMimeType("application/json"); rawFile.open("GET", file, true); rawFile.onreadystatechange = function() { if (rawFile.readyState === 4 && rawFile.status == "200") { callback(rawFile.responseText); } } rawFile.send(null); } if(getCookie("isBot")===""){ readTextFile("https://s3-sa-east-1.amazonaws.com/hoopdecor/static/production/files/user-agents-blocked.json", function(text){ var isBot=text.includes(navigator.userAgent); setCookie("isBot",isBot?1:0,90); }); } const data = { api_token: "596640c0ac0e4b3a8be8f3a2e977b748", company_id: 115311, campaign_id: 789, topic_id: 1125, ecommerce: ecommerce, crosClient:getCookie("client_deal"), fbp:getCookie("_fbp"), status: status, external_ref: external_ref, name: name, email: email, phone: phone, cep: cep, cpf: cpf, prods: prods, final_value:final_value, utms: utms, prod_sku: prod_sku, prod_id: prod_id, prod_name: prod_name, prod_img: prod_img, prod_category: prod_category, variation_id: variation_id, reference: reference, url: window.location.href, id: id, client_id:client_id, url_key:url_key, } if(getCookie("isBot")!=="1") fetch("https://hoopdecor.com/api/external/ecommerces?"+serialize(data)) .then(response => response.json()) .then(response => { var oldActions=JSON.parse(getCookie("hoop_actions")?getCookie("hoop_actions"):"[]"); var newActions=oldActions.concat(response); var newValue=[]; try{ newValue=JSON.stringify(newActions); if(newValue.length>1600){ newValue.shift(); } }catch (e) { newValue=[]; } setCookie("hoop_actions",newValue, 10); if(response.length){ setCookie("tracking_id",response[0].deal_id, 10); window.verifyCupons(0); } }); } (function () { var myAppJavaScript = function myAppJavaScript() { function HoopTrackModel() { var _this2 = this; this.plataform = "tray"; this.selectedBuy = null; this.data = { name: null, email: null, phone: null, cep: null, cpf: null, cart: [], prod_name: null, prod_sku: null, prod_id: null, prod_ref: null, prod_img: [], id: null, final_value: null, prod_category: [], client_id: null }; this.functions = { getCartSession: function getCartSession() { return document.querySelector("html").dataset.session || null; }, getAjax: function getAjax(e, t, a, r) { var request = new XMLHttpRequest(); var params = a; params = "?" + Object.keys(params).map(function (key) { return key + "=" + encodeURIComponent(params[key]); }).join("&"); var url = t + params; request.onreadystatechange = function () { if (request.readyState == 4 && typeof JSON.parse(request.response).causes == "undefined") { console.log("get Cart"); console.log(request.response); r(JSON.parse(request.response)); } }; request.open(e, url); request.send(); }, getCart: function getCart() { var cartSession = _this2.functions.getCartSession(); if (cartSession) { _this2.data.cart = []; var e = "/web_api/cart/" + cartSession; _this2.functions.getAjax("GET", e, {}, function (e) { console.log(e); e.forEach(function (item) { var div = document.createElement("div"); div.innerHTML = item.Cart.product_name; var name = div.textContent || div.innerText || ""; _this2.data.cart.push({ name: name, variation_id: item.Cart.variant_id == "0" ? item.Cart.product_id : item.Cart.variant_id, quantity: item.Cart.quantity, value: item.Cart.price, img: [item.Cart.product_image.https], sku: item.Cart.variant_id !== "0" ? item.Cart.product_id + "-" + item.Cart.variant_id : null }); _this2.data.client_id = item.Cart.customer_id; }); _this2.events.shopping_cart(); }); } }, getCategory: function getCategory() { var array = []; dataLayer[0].breadcrumbDetails.forEach(function (value) { array.push(value.name); }); return array; }, getPage: function getPage() { if (dataLayer && dataLayer[0] && dataLayer[0].nameProduct) { return "product"; } else if (location.pathname == "/checkout/cart") { return "cart"; } else if (location.pathname == "/checkout" || location.pathname == "/checkout/") { return "checkout"; } } }; this.triggers = { buy: function buy() { document.querySelector("#form_comprar").addEventListener("submit", function () { setTimeout(function () { _this2.functions.getCart(); }, 1000); }); }, productOpen: function productOpen() { _this2.data.prod_name = dataLayer[0].nameProduct; _this2.data.prod_id = dataLayer[0].listSku.length ? dataLayer[0].listSku[0].idSku.split("-")[1] : dataLayer[0].idProduct; _this2.data.prod_img = [dataLayer[0].urlImage]; _this2.data.prod_sku = dataLayer[0].listSku.length ? dataLayer[0].listSku[0].idSku : null; _this2.data.prod_ref = dataLayer[0].reference; if (_this2.data.prod_ref && !_this2.data.prod_sku) { _this2.data.prod_sku = _this2.data.prod_ref; } _this2.events.product_open(); }, checkout: function checkout() { var info = globalCart.data.cart.customer; _this2.data.email = info.email; _this2.data.name = info.name; _this2.data.cep = info.address.zipCode; _this2.data.phone = info.cellphone; _this2.data.client_id = info.id; if (!_this2.data.email.includes("*")) _this2.events.checkout(); }, order: function order() { _this2.data.id = globalCart.data.cart.orderId; _this2.data.final_value = globalCart.data.cart.total; _this2.events.purchase(); }, cart: function cart() { _this2.data.cart = []; _this.model.cart.products().forEach(function (item) { _this2.data.cart.push({ name: item.name(), sku: item.variant_id() == "0" ? null : item.sku_ga(), variation_id: item.variant_id() == "0" ? item.id() : item.variant_id(), quantity: item.quantity(), value: parseFloat(item.price()), img: [item.images.large()], reference: item.variant_id() == "0" ? item.reference() : null }); }); _this2.events.shopping_cart(); } }; this.observers = { watchCheckout: function watchCheckout() { var aux = 1; new MutationObserver(function () { if (globalCart.data.cart.customer && globalCart.data.cart.customer.id && (globalCart.data.cart.customer.phone || globalCart.data.cart.customer.cellphone) && globalCart.data.cart.customer.address && aux) { aux = 0; _this2.triggers.checkout(globalCart.data.cart.customer); } }).observe(document, { subtree: true, childList: true }); }, watchCart: function watchCart() { var lastTotal = _this.model.total(); new MutationObserver(function () { var total = _this.model.total(); if (lastTotal != total) { lastTotal = total; _this2.triggers.cart(); } console.log(lastTotal); }).observe(document.querySelector("[data-cart-template='products']"), { subtree: true, childList: true }); }, watchOrder: function watchOrder() { var aux = 1; new MutationObserver(function () { if (globalCart.data.cart.orderId && aux) { aux = 0; _this2.triggers.order(); } }).observe(document, { subtree: true, childList: true }); }, watchCopy: function watchCopy() { var copy = document.querySelector('h1[itemprop="name"],h1[class="product-name"]'); if (!copy) { copy = document.querySelector('h2[itemprop="name"],h3[itemprop="name"]'); } if (copy) copy.oncopy = function () { window.hoopTrack.events.copy(); }; }, replaceWhats: function replaceWhats() { var tracking_id = window.getCookie("tracking_id"); if (tracking_id) { tracking_id = (tracking_id >>> 0).toString(2).replaceAll("0", "%E2%80%8B").replaceAll("1", "%E2%80%8C"); Array.from(document.querySelectorAll('a[href*="https://api.whatsapp.com/send"],a[href*="https://wa.me/"]')).forEach(function (el) { var href = el.getAttribute("href"); if (href.includes("https://wa.me/") && !href.includes("text") && !href.includes("message")) { href += "?text=Ola, me chamo "; } var textParamIndex = href.indexOf("text="); if (textParamIndex !== -1) { var beforeText = href.substring(0, textParamIndex + 5); // Inclui "&text=" var textValue = href.substring(textParamIndex + 5); // Após "&text=" if (textValue.length > 0) { textValue = textValue[0] + tracking_id + textValue.substring(1); } var newHref = beforeText + textValue; el.setAttribute("href", newHref); } el.addEventListener("click", function () { return window.hoopTrack.events.thumb_click(href); }); }); } } }; this.events = { shopping_cart: function shopping_cart() { window.sendEvent({ status: "shopping_cart", prods: _this2.data.cart.length ? _this2.data.cart : "empty", client_id: _this2.data.client_id, ecommerce: "tray" }); }, product_open: function product_open() { window.sendEvent({ status: "product_open", prod_name: _this2.data.prod_name, prod_sku: _this2.data.prod_sku, prod_img: _this2.data.prod_img, prod_category: _this2.data.prod_category, variation_id: _this2.data.prod_id, reference: _this2.data.prod_ref, url_key: window.location.pathname, ecommerce: "tray" }); }, checkout: function checkout() { window.sendEvent({ status: "checkout", name: _this2.data.name, email: _this2.data.email, phone: _this2.data.phone, cep: _this2.data.cep, prods: _this2.data.products, client_id: _this2.data.client_id, ecommerce: "tray" }); }, purchase: function purchase() { window.sendEvent({ status: "purchase", id: _this2.data.id, final_value: _this2.data.final_value, ecommerce: "tray" }); }, clear: function clear() { window.sendEvent({}); }, copy: function copy() { window.sendEvent({ status: "copy", prod_name: _this2.data.prod_name, prod_sku: _this2.data.prod_sku, prod_img: _this2.data.prod_img, prod_category: _this2.data.prod_category, variation_id: _this2.data.prod_id, reference: _this2.data.prod_ref, ecommerce: "tray" }); }, thumb_click: function thumb_click(text) { // Extrair o link do produto da string var linkPrefix = "Link%3A+"; var linkStartIndex = text.indexOf(linkPrefix) + linkPrefix.length; var productLink = decodeURIComponent(text.substring(linkStartIndex).trim()); // Criar um objeto URL e obter o pathname var url = new URL(productLink); var pathName = url.pathname; window.sendEvent({ status: "thumb_whats", url_key: pathName }); } }; this.init = function () { var page = this.functions.getPage(); setTimeout(this.observers.replaceWhats, 1500); switch (page) { case "product": this.triggers.productOpen(); this.observers.watchCopy(); this.triggers.buy(); setTimeout(this.functions.getCart.bind(this.functions), 1500); break; case "checkout": this.events.checkout(); this.observers.watchCheckout(); this.observers.watchOrder(); break; case "cart": this.observers.watchCart(); break; case "purchase": this.triggers.order(); break; default: this.events.clear(); setTimeout(this.functions.getCart(), 1500); break; } }; } window.hoopTrack = new HoopTrackModel(); window.hoopTrack.init(); }; myAppJavaScript(); })();