Advertisement
ikai2

klaviyo

Apr 16th, 2024 (edited)
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.29 KB | None | 0 0
  1. <script>
  2. window.addEventListener('load', function() {
  3. var _learnq = window._learnq || [];
  4. function addedToCart() {
  5.   fetch(`${window.location.origin}/cart.js`)
  6.   .then(res => res.clone().json().then(data => {
  7.     var cart = {
  8.       total_price: data.total_price/100,
  9.       $value: data.total_price/100,
  10.       total_discount: data.total_discount,
  11.       original_total_price: data.original_total_price/100,
  12.       items: data.items
  13.     }
  14.     if (typeof item !== 'undefined') {
  15.       cart = Object.assign(cart, item)
  16.     }
  17.     if (klAjax) {
  18.         _learnq.push(['track', 'Added to Cart', cart]);
  19.         klAjax = false;
  20.       }
  21.   }))
  22. };
  23. (function (ns, fetch) {
  24.   ns.fetch = function() {
  25.     const response = fetch.apply(this, arguments);
  26.     response.then(res => {
  27.       if (`${window.location.origin}/cart/add.js`
  28.         .includes(res.url)) {
  29.             addedToCart()
  30.       }
  31.     });
  32.     return response
  33.   }
  34. }(window, window.fetch));
  35. var klAjax = true;
  36. var atcButtons = document.querySelectorAll("form[action*='/cart/add'] button[type='submit']");
  37. for (var i = 0; i < atcButtons.length; i++) {
  38.    atcButtons[i].addEventListener("click", function() {
  39.      if (klAjax) {
  40.        _learnq.push(['track', 'Added to Cart', item]);
  41.        klAjax = false;
  42.      }
  43.    })
  44. }
  45. });
  46. </script>
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement