Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getClosest(el, s) {
- var r = undefined;
- while (el) {
- if (el.matches(s)) {
- r = el;
- break;
- } else if (el.tagName.toLowerCase()=='body') {
- break;
- };
- el = el.parentElement;
- };
- return r;
- };
- if (document.querySelectorAll('body.template-product').length==1) {
- var name = document.querySelector('h1[itemprop="name"]').textContent.trim(),
- price = document.querySelector('span[itemprop="price"]').textContent.trim(),
- img = location.protocol + document.querySelector('img.photo-zoom-link__initial').getAttribute('data-srcset').split(' ')[0],
- url = location.origin + location.pathname;
- dashly.track('$product_viewed', {
- '$name': name,
- '$img': img,
- '$url': url,
- '$amount': price
- });
- dashly.identify([{op: 'union', key: '$viewed_products', value: name}]);
- document.addEventListener('click', function(e) {
- if (e.target.matches('form[action="/cart/add"] button.add-to-cart, form[action="/cart/add"] button.add-to-cart *')) {
- var name = document.querySelector('h1[itemprop="name"]').textContent.trim(),
- price = document.querySelector('span[itemprop="price"]').textContent.trim(),
- img = location.protocol + document.querySelector('img.photo-zoom-link__initial').getAttribute('data-srcset').split(' ')[0],
- url = location.origin + location.pathname,
- size = document.querySelector('.variant-input-wrap select option:checked').value;
- dashly.track('$cart_added', {
- '$name': name,
- '$img': img,
- '$url': url,
- '$amount': price,
- 'Size': size
- });
- dashly.identify([{op: 'union', key: '$cart_items', value: name}]);
- };
- });
- };
- if (location.pathname == '/cart') {
- function get_items() {
- var items = document.querySelectorAll('a.cart__product-name').length,
- cartAmount = items==0?0:document.querySelector('.hulkapps-cart-original-total span.money').textContent.trim();
- dashly.identify([
- {
- op: items == 0 ? 'delete' : 'update_or_create',
- key: '$cart_items',
- value: items == 0 ? 0 : Array.from(document.querySelectorAll('a.cart__product-name')).map(function(elem) {
- return elem.textContent.trim();
- })
- },
- { op: items == 0 ? 'delete' : 'update_or_create', key: '$cart_amount', value: cartAmount }
- ])
- localStorage['lastAmount'] = cartAmount;
- };
- setTimeout(get_items, 1000);
- dashly.track('$cart_viewed');
- document.addEventListener('click', function(e) {
- if (e.target.matches('a[href*="&quantity=0"]')) {
- dashly.track('Removed item from cart', {
- '$name': getClosest(e.target, '.grid__item').querySelector('a.cart__product-name').textContent.trim()
- });
- setTimeout(get_items, 1000);
- } else if (e.target.matches('button[name="checkout"]')) {
- dashly.track('$order_started');
- localStorage['orderStarted'] = true;
- };
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement