Advertisement
NikolayBezay

Adoric recommendation product click embed example;

Jun 1st, 2021
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.04 KB | None | 0 0
  1. <script>
  2. (function() {
  3.     function adoricAddToCart(productId) {
  4.         var data = {
  5.             "id": productId,
  6.             "quantity":1
  7.         };
  8.         var xhr = new XMLHttpRequest();
  9.         xhr.withCredentials = true;
  10.         xhr.addEventListener('readystatechange', function () {
  11.             if (this.readyState === 4) {
  12.                 console.log(this.responseText);
  13.                 window.location.reload();
  14.             }
  15.         });
  16.         xhr.open('POST', window.location.origin + '/cart/add.js');
  17.         xhr.setRequestHeader('content-type', 'application/json');
  18.         xhr.setRequestHeader('cache-control', 'no-cache');
  19.         xhr.send(JSON.stringify(data));
  20.     }
  21.  
  22.     if (window.Element && !Element.prototype.closest) {
  23.        Element.prototype.closest = function(s) {
  24.            var matches = (this.document || this.ownerDocument).querySelectorAll(s),
  25.                i,
  26.                el = this;
  27.             do {
  28.             i = matches.length;
  29.             while (--i >= 0 && matches.item(i) !== el) {};
  30.             } while ((i < 0) && (el = el.parentElement));
  31.            return el;
  32.        };
  33.    }    
  34.    var currentCampaignRepeater = document.querySelector('.' + self.identifier + ' .repeater-content');
  35.    currentCampaignRepeater.addEventListener('click', function(event) {
  36.        var productItem = event.target.closest('.repeater-item');
  37.        if (event.target.getAttribute('role') === 'button' && productItem) {
  38.            var productInformation = JSON.parse(productItem.dataset.productInfo);
  39.             console.log('productInformation - ', productInformation);
  40.            var  productId = 'someID'; /* take it from productInformation obj*/
  41.            window.ga && window.ga('t0.send', 'event', {
  42.                eventCategory: "Adoric - " + self.campaignTitle + '(' + self.versionTitle + ')',
  43.                eventAction: 'click',
  44.                eventLabel: 'Added to the cart - ' + productInformation.title,
  45.            });
  46.  
  47.            adoricAddToCart(productId);
  48.        }
  49.    });
  50. })();
  51. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement