Shoora
4/29/2019 - 2:33 PM

core.js

<script>
  window.dp6 = {
    protocol: location.protocol,
    hostname: location.hostname,
    path: location.pathname.toLowerCase(),
    dataLayer: google_tag_manager[{{Container ID}}].dataLayer,
    bannerPositions: {},
    event: function(c, a, l, v, ni){
      dataLayerGA.push({
        event: "eventGA",
        eventGA: {
          category: c,
          action: a, 
          label: l,
          value: v, 
          nointeraction: ni
        }
      })
    },
    sanitize: function (str, opt_relaxed) {
      if (!str)
        return '';

      str = str.toLowerCase()
      .replace(/^\s+|\s+$/g, '')
      .replace(/\ +/g, '_')
      .replace(/[áàâãåäæª]/g, 'a')
      .replace(/[éèêëЄ€]/g, 'e')
      .replace(/[íìîï]/g, 'i')
      .replace(/[óòôõöøº]/g, 'o')
      .replace(/[úùûü]/g, 'u')
      .replace(/[碩]/g, 'c');

      if (!opt_relaxed) {
        str = str.replace(/[^a-z0-9_-]/g, '_');
      }

      return str.replace(/_+/g, '_');
    },
    capitalize: function (text, optWholeText) {
      if (!text)
        return '';

      if (optWholeText === true)
        return jQuery.map(text.split(' '), dp6.capitalize).join(' ');

      return text.slice(0, 1).toUpperCase() + text.slice(1).toLowerCase();
    },
    listenerSearch: function() {
      var search = document.getElementsByClassName('plugin-search-suggestion')[0]
      if (search == null) return;
      search.addEventListener('submit', function(evt) {
        var searchValue = evt.target.getElementsByClassName('input-search')[0].value;
        var now = new Date();
        var time = now.getTime();
        var minutes = 30;
        time += 60000 * minutes;
        now.setTime(time);
        window.document.cookie = 'wasrc='+searchValue+';path=/; expires=' + now.toUTCString();
      });
    },
    filterByAttribute: function (arr, attr, value) {
      var newArr = [];
      for (var i = 0; i < arr.length; i++)
        if (arr[i][attr] === value)
          newArr.push(arr[i]);
      return newArr;
    },    
    cookie: function (key, value, opts) {
      var cookie;
      if (value === undefined) {
        key = ('; ' + key + '=');
        cookie = ('; ' + document.cookie);
        var index = cookie.indexOf(key);
        var end;
        if (index !== -1) {
          cookie = cookie.substring(index + key.length);
          end = cookie.indexOf(';');
          return window.unescape(end !== -1 ? cookie.substring(0, end) : cookie);
        }
        return null;
      } else {
        var exdate;
        if (typeof opts !== 'object') {
          opts = {
            exdays: opts
          };
        }
        cookie = key + "=" + escape(value);
        if (opts.exdays) {
          exdate = new Date();
          exdate.setDate(exdate.getDate() + opts.exdays);
          cookie += "; expires=" + exdate.toUTCString();
        }

        if (opts.domain) {
          cookie += "; domain=" + opts.domain;
        }

        cookie += "; path=" + (opts.path || '/');

        return (document.cookie = cookie);
      }
    },
    extractParamFromUri: function (uri, paramName) {
      if (!uri) {
        return;
      }
      uri = uri.split('#')[0];
      var parts = uri.split('?');
      if (parts.length === 1) {
        return;
      }
      var query = decodeURI(parts[1]);
      paramName += '=';
      var params = query.split('&');
      for (var i = 0, param; param = params[i]; ++i) {
        if (param.indexOf(paramName) === 0) {
          return unescape(param.split('=')[1]);
        }
      }
    },

    custom: function (key, value) {
      var obj = {};
      obj[key] = value;
      dataLayerGA.push(obj);
    },
    trackDuplicatedTransaction: function () {
      var purchase = dp6.ecommerce.purchase || {};
      var actionField = purchase.actionField || {};
      var id = actionField.id;
      var cookie = dp6.cookie('dp6transactionid');
      if (id) {
        if (cookie && cookie === id) {
          dp6.ecommerce.purchase = null;
          dp6.transactionEvent = ['TransacaoDuplicada', id, id, actionField.revenue, true];
        } else {
          dp6.transactionEvent = ['Transacao', id, dp6.user.customerId, actionField.revenue, true];
        }
        dp6.cookie('dp6transactionid', id, {
          exdays: 180,
          domain: {{GA Domain}}
        });
      } else {
       //recupera o valor do ID
        id = $('.checkout-wrapper .checkout-body-holder .order-number strong').html() != '' ? $('.checkout-wrapper .checkout-body-holder .order-number strong').html() : '';
        if (id) {
          if (cookie && cookie === id) {
            dp6.ecommerce.purchase = null;
            dp6.transactionEvent = ['TransacaoDuplicada', id, id, actionField.revenue, true];
          } else {
            dp6.transactionEvent = ['Transacao', id, dp6.user.customerId, actionField.revenue, true];
          }
          dp6.cookie('dp6transactionid', id, {
            exdays: 180,
            domain: {{GA Domain}}
          });
        }
      }
    },
    trackSearch: function () {
      var uri = location.pathname + location.search;
      var Ntt = dp6.extractParamFromUri(uri, 'Ntt');
      var mi = dp6.extractParamFromUri(uri, 'mi');
      var redirectParam = dp6.extractParamFromUri(uri, 'redirectParam');
      var prevPage = window.document.referrer;
      //var wasrc = String(document.cookie).split('wasrc=')[1] ? String(document.cookie).split('wasrc=')[1].split(';')[0] : '';

      if (!Ntt && !redirectParam) return;

      //dp6.page.vpv = uri + '&nsCat=' + (!mi && prevPage.indexOf('netshoes.com.br') > -1 && wasrc.replace(' ', '+') == Ntt ? 'natural' : 'artificial');
      dp6.page.vpv = uri + '&nsCat=' + (!mi && prevPage.indexOf('netshoes.com.br') > -1 ? 'natural' : 'artificial');
    }
  };
  dp6.start = function () {
    try {
      dp6.ecommerce = dp6.dataLayer.get('ecommerce') || {};
      dp6.page = dp6.dataLayer.get('page') || {};
      dp6.user = dp6.dataLayer.get('user') || {};
      dp6.trackSearch();
      dp6.listenerSearch();
      dp6.trackDuplicatedTransaction();
      dataLayerGA.push({ecommerce: dp6.ecommerce, page: dp6.page, event: 'core.ready'});
    } catch ($$e) {
      if(window.ga) {
        ga("send", "exception", {
          "exDescription": $$e
        });
      }
      checkIsReady();
    }
  }
  
  function checkIsReady () {
    var checkReady = setInterval(function() {   
      if(window.$ && window.ga) {
        clearInterval(checkReady);
        dp6.start();
      }
    }, 100)
  }
  
  dp6.start();
  
  
</script>