Advertisement
Dzhubal

Untitled

Sep 19th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getClosest(el, s) {
  2.     var r = undefined;
  3.     while (el) {
  4.         if (el.matches(s)) {
  5.             r = el;
  6.             break;
  7.         } else if (el.tagName.toLowerCase()=='body') {
  8.             break;
  9.         };
  10.         el = el.parentElement;
  11.     };
  12.     return r;
  13. };
  14.  
  15. if (document.querySelectorAll('body.template-product').length==1) {
  16.     var name = document.querySelector('h1[itemprop="name"]').textContent.trim(),
  17.         price = document.querySelector('span[itemprop="price"]').textContent.trim(),
  18.         img = location.protocol + document.querySelector('img.photo-zoom-link__initial').getAttribute('data-srcset').split(' ')[0],
  19.         url = location.origin + location.pathname;
  20.     dashly.track('$product_viewed', {
  21.         '$name': name,
  22.         '$img': img,
  23.         '$url': url,
  24.         '$amount': price
  25.     });
  26.     dashly.identify([{op: 'union', key: '$viewed_products', value: name}]);
  27.  
  28.     document.addEventListener('click', function(e) {
  29.         if (e.target.matches('form[action="/cart/add"] button.add-to-cart, form[action="/cart/add"] button.add-to-cart *')) {
  30.             var name = document.querySelector('h1[itemprop="name"]').textContent.trim(),
  31.                 price = document.querySelector('span[itemprop="price"]').textContent.trim(),
  32.                 img = location.protocol + document.querySelector('img.photo-zoom-link__initial').getAttribute('data-srcset').split(' ')[0],
  33.                 url = location.origin + location.pathname,
  34.                 size = document.querySelector('.variant-input-wrap select option:checked').value;
  35.             dashly.track('$cart_added', {
  36.                 '$name': name,
  37.                 '$img': img,
  38.                 '$url': url,
  39.                 '$amount': price,
  40.                 'Size': size
  41.             });
  42.             dashly.identify([{op: 'union', key: '$cart_items', value: name}]);
  43.         };
  44.     });
  45. };
  46.  
  47. if (location.pathname == '/cart') {
  48.     function get_items() {
  49.         var items = document.querySelectorAll('a.cart__product-name').length,
  50.             cartAmount = items==0?0:document.querySelector('.hulkapps-cart-original-total span.money').textContent.trim();
  51.         dashly.identify([
  52.             {
  53.                 op: items == 0 ? 'delete' : 'update_or_create',
  54.                 key: '$cart_items',
  55.                 value: items == 0 ? 0 : Array.from(document.querySelectorAll('a.cart__product-name')).map(function(elem) {
  56.                     return elem.textContent.trim();
  57.                 })
  58.             },
  59.             { op: items == 0 ? 'delete' : 'update_or_create', key: '$cart_amount', value: cartAmount }
  60.         ])
  61.         localStorage['lastAmount'] = cartAmount;
  62.     };
  63.     setTimeout(get_items, 1000);
  64.     dashly.track('$cart_viewed');
  65.     document.addEventListener('click', function(e) {
  66.         if (e.target.matches('a[href*="&quantity=0"]')) {
  67.             dashly.track('Removed item from cart', {
  68.                 '$name': getClosest(e.target, '.grid__item').querySelector('a.cart__product-name').textContent.trim()
  69.             });
  70.             setTimeout(get_items, 1000);
  71.         } else if (e.target.matches('button[name="checkout"]')) {
  72.             dashly.track('$order_started');
  73.             localStorage['orderStarted'] = true;
  74.         };
  75.     });
  76. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement