Advertisement
orenchuck

gtm napo trigger mini cart

Dec 14th, 2022 (edited)
112
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.     (function () {
  3.         var TIME_SHOW_CAMPAIN = 3000;
  4.         var WAITING_TIMER_COUNTER = 60;
  5.         var HALF_OF_THE_SECOND = 500;
  6.  
  7.         function getCartCounter() {
  8.             var counter = document.querySelector('.page_header_minicart .counter .counter-number');
  9.             if (counter) {
  10.                 counter = counter.textContent;
  11.             }
  12.             return counter;
  13.         }
  14.  
  15.         function checkCounter(counter) {
  16.             var currentCounter = getCartCounter();
  17.             if (counter && currentCounter && parseInt(counter) < parseInt(currentCounter)) {
  18.                 window.adoric && window.adoric.trigger('showMiniCartOnClick');
  19.             }
  20.         }
  21.  
  22.         function campaignTriggering() {
  23.             var addToCartButton = document.querySelector('#product-addtocart-button');
  24.             var counter = getCartCounter();
  25.             if (addToCartButton && counter) {
  26.                 addToCartButton.addEventListener('click', function () {
  27.                     setTimeout(function () {
  28.                         checkCounter(counter);
  29.                     }, TIME_SHOW_CAMPAIN);
  30.                 });
  31.                 addToCartButton.addEventListener('touchend', function () {
  32.                     setTimeout(function () {
  33.                         checkCounter();
  34.                     }, TIME_SHOW_CAMPAIN);
  35.                 });
  36.             }
  37.         }
  38.  
  39.         function waitPageSelector() {
  40.             WAITING_TIMER_COUNTER--;
  41.             var nativePageSelector = document.querySelector('#product-addtocart-button');
  42.  
  43.             if (!nativePageSelector && nativePageSelector.textContent.length == 0 && WAITING_TIMER_COUNTER > 0) {
  44.                 setTimeout(waitPageSelector, HALF_OF_THE_SECOND);
  45.             } else {
  46.                 campaignTriggering();
  47.             }
  48.         }
  49.         waitPageSelector();
  50.     })();
  51. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement