Edrone

esklep

Aug 4th, 2022 (edited)
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.       <!--
  2.  * edrone JavaScript Library.
  3.  * https://edrone.me/
  4.  *
  5.  * GPL licenses.
  6.  * https://edrone.me/integration-license/
  7. -->
  8. <script>
  9. window._edrone = window._edrone || {};
  10. _edrone.isInitialized = false;
  11. function edrone(){
  12.     if (!_edrone.isInitialized) {
  13.         _edrone.isInitialized = true;
  14.  
  15.     window.customerPrivacy.onMarketingConsentGranted(function () {
  16.         (function (srcjs) {
  17.             _edrone.app_id = '6284b42821a07';
  18.             _edrone.version = '1.0.1';
  19.             _edrone.platform = 'esklep';
  20.  
  21.             frontAPI.getBasketInfo(basketInfo => {
  22.                 var basketCount = localStorage.getItem('edrone_basket_count') ? parseInt(localStorage.getItem('edrone_basket_count')) : 0;
  23.                 var basketFlag = basketInfo.basket.count > basketCount;
  24.                 var basketProduct = null;
  25.                 if (basketFlag) {
  26.                     basketProduct = basketInfo.products.pop();
  27.                 }
  28.                 localStorage.setItem('edrone_basket_count', basketInfo.basket.count);
  29.  
  30.                 frontAPI.getUser(userdata => {
  31.  
  32.  
  33.                     if (!userdata.error_description) {
  34.                         _edrone.email = userdata.email;
  35.                         _edrone.first_name = userdata.firstname;
  36.                         _edrone.last_name = userdata.lastname;
  37.                     }
  38.  
  39.                     if (basketFlag && basketProduct) {
  40.                         _edrone.product_ids = basketProduct.product_id;
  41.                         _edrone.product_titles = basketProduct.name;
  42.                         _edrone.product_images = window.location.origin + '/environment/cache/images/0_0_productGfx_' + basketProduct.image_filename;
  43.                     }
  44.  
  45.                     _edrone.action_type = basketFlag ? 'add_to_cart' : 'other';
  46.  
  47.                     frontAPI.getDefaults(function (defaults) {
  48.                         _edrone.platform_version = defaults.shop_version;
  49.                     })
  50.  
  51.                     var category_id = document.querySelector('body').id.split('shop_category')[1];
  52.                     if (category_id) {
  53.                         _edrone.product_category_ids = category_id;
  54.                         frontAPI.getCategory((category) => {
  55.                             _edrone.product_category_ids = category ? category.category_id : null
  56.                             _edrone.product_category_names = category ? category.translation.name : null
  57.                         }, {id: category_id});
  58.                     }
  59.                     if (userdata.billing_address && !Boolean(userdata.error_description)) {
  60.                         _edrone.country = userdata.billing_address.country_code;
  61.                         _edrone.city = userdata.billing_address.city;
  62.                     }
  63.                 });
  64.  
  65.             })
  66.  
  67.             var newsletterForm = document.querySelectorAll('form[action*="newsletter/sign"]');
  68.             if (newsletterForm.length !== 0) {
  69.                 newsletterForm.forEach(function (form, index, array) {
  70.                     var emailInput = form.querySelector('input[name="email"]');
  71.                     if (emailInput) {
  72.                         form.addEventListener('submit', function () {
  73.                             _edrone.email = emailInput.value;
  74.                             _edrone.customer_tags = 'From PopUp';
  75.                             _edrone.action_type = 'subscribe';
  76.                             _edrone.init();
  77.                         })
  78.                     }
  79.                 })
  80.             }
  81.  
  82.             //ADD_TO_CART
  83.             function addToCartAction() {
  84.                 setTimeout(function(){
  85.                     var frontstoreBasket = JSON.parse(localStorage.getItem('frontstoreBasket'));
  86.                     var products = JSON.parse(localStorage.getItem("frontstoreBasket")).products;
  87.                     var product = products[products.length - 1];
  88.                     var product_id = product.product_id;
  89.                     var main_image = frontAPI.getProducts({
  90.                         ids: [product_id]
  91.                     });
  92.                     localStorage.removeItem("productAddedToCart", '');
  93.                     localStorage.setItem('edrone_basket_count', frontstoreBasket.basket.count);
  94.  
  95.                     _edrone.product_ids = product_id;
  96.                     _edrone.product_titles = encodeURIComponent(product.name);
  97.                     _edrone.product_images = window.location.origin + '/environment/cache/images/0_0_productGfx_' + main_image.list[0].main_image_filename;
  98.                     _edrone.product_urls = product.url;
  99.                     _edrone.product_category_ids = product.category.category_id;
  100.                     _edrone.product_category_names = product.category.translation.name;
  101.                     _edrone.product_brand_ids = product.producer !== undefined ? product.producer.producer_id : '';
  102.                     _edrone.product_brand_names = product.producer !== undefined ? product.producer.name : '';
  103.                     _edrone.action_type = 'add_to_cart';
  104.                     _edrone.init();
  105.                 }, 2000);
  106.             }
  107.  
  108.             function addToCartAjax() {
  109.                 shoper.addEvent('cart:add:ajax', function () {
  110.                     localStorage.setItem("productAddedToCart", "ajax");
  111.                     if (localStorage.getItem("productAddedToCart") === "ajax") {
  112.                         addToCartAction();
  113.                     }
  114.                 });
  115.             }
  116.  
  117.             if (window.shoper) {
  118.                 addToCartAjax();
  119.             } else {
  120.                 window.addEventListener('skin:initialized', function() {
  121.                     addToCartAjax();
  122.                 });
  123.             }
  124.  
  125.             var addToBasketBtn = document.querySelectorAll('.addtobasket');
  126.             if(addToBasketBtn) {
  127.                 for (var i = 0; i < addToBasketBtn.length; i++) {
  128.                     addToBasketBtn[i].addEventListener('click', function() {
  129.                         localStorage.setItem("productAddedToCart", "add");
  130.                     })
  131.                 }
  132.             }
  133.  
  134.             if (localStorage.getItem("productAddedToCart") === "add") {
  135.                 addToCartAction();
  136.             }
  137.             //END ADD_TO_CART
  138.  
  139.             var doc = document.createElement('script');
  140.             doc.type = 'text/javascript';
  141.             doc.async = true;
  142.             doc.src = ('https:' == document.location.protocol ? 'https:' : 'http:') + srcjs;
  143.             var s = document.getElementsByTagName('script')[0];
  144.             s.parentNode.insertBefore(doc, s);
  145.             _edrone.isInitialized = true;
  146.         })("//d3bo67muzbfgtl.cloudfront.net/edrone_2_0.js");
  147.  
  148.         (function () {
  149.  
  150.             function findProductIdOnProductPage() {
  151.                 var regex = /(\/c\/)/g;
  152.                 if (regex.exec(window.location.pathname) !== null) {
  153.                     return false;
  154.                 } //check for category page
  155.                 var body_id = document.querySelector('body').id;
  156.                 var id_from_input = document.querySelector('[name^="product_id"]') ? document.querySelector('[name^="product_id"]').value : false;
  157.                 if (~body_id.indexOf('shop_product')) {
  158.                     var id_from_body = body_id.split('shop_product')[1];
  159.                 }
  160.                 var product_id = (id_from_input || (!isNaN(id_from_body) ? id_from_body : false));
  161.                 return product_id;
  162.             }
  163.  
  164.             function edroneProductView() {
  165.                 if (document.querySelectorAll("body")[0].id !== undefined) {
  166.                     var product_id = findProductIdOnProductPage();
  167.                     if (product_id) {
  168.                         window._edrone = window._edrone || {};
  169.                         window._edrone_send_handler = function () {
  170.                             _edrone.first_run = false;
  171.                             frontAPI.getProduct(function (product) {
  172.                                 _edrone.product_ids = product_id;
  173.                                 _edrone.product_titles = encodeURIComponent(product.name);
  174.                                 _edrone.product_category_ids = product.category.id;
  175.                                 _edrone.product_category_names = product.category.name;
  176.                                 _edrone.product_images = window.location.origin + '/environment/cache/images/0_0_productGfx_' + product.main_image_filename;
  177.                                 _edrone.product_urls = product.url;
  178.                                 _edrone.product_brand_ids = product.producer !== undefined ? product.producer.id : '';
  179.                                 _edrone.product_brand_names = product.producer !== undefined ? product.producer.name : '';
  180.                                 _edrone.action_type = 'product_view';
  181.                                 _edrone.init();
  182.                             }, {
  183.                                 id: product_id
  184.                             });
  185.                         };
  186.                     }
  187.                 }
  188.             }
  189.  
  190.             findProductIdOnProductPage() ? edroneProductView() : window.addEventListener('load', edroneProductView);
  191.         }());
  192.     })
  193. }
  194. }
  195.  
  196. if (window.frontAPI) {
  197.     edrone();
  198. } else {
  199.     document.addEventListener('frontAPIReady', () => {
  200.         edrone();
  201.     });
  202. }
  203. </script>
Add Comment
Please, Sign In to add comment