custom/plugins/ShopsyKlaviyo6/src/Resources/views/storefront/base.html.twig line 1

Open in your IDE?
  1. {% sw_extends '@Storefront/storefront/base.html.twig' %}
  2. {% block base_body_script %}
  3.     {{ parent() }}
  4.     <script type="text/javascript">
  5.         document.addEventListener("DOMContentLoaded", function(event) {
  6.             var substring = 'shopsyKlaviyo=shopsyKlaviyo';
  7.             var checkCookie = document.cookie.indexOf(substring) !== -1;
  8.             var cookieRequired;
  9.             if (document.getElementsByName('cookieRequired')[0] !== undefined) {
  10.                 cookieRequired = document.getElementsByName('cookieRequired')[0].value;
  11.             }
  12.             if (cookieRequired !== "1" || checkCookie) {
  13.                 waitForKlaviyoElement('form button.needsclick').then((elm) => {
  14.                     const submitbutton = document.querySelectorAll('form button.needsclick');
  15.                     submitbutton[0].addEventListener('click', function () {
  16.                         const email = document.querySelectorAll('form input.needsclick[type=email]')[0].value;
  17.                         window.localStorage.setItem('klaviyoValue', btoa(email));
  18.                     });
  19.                 });
  20.                 var imported = document.createElement('script');
  21.                 imported.src = 'https://static.klaviyo.com/onsite/js/klaviyo.js?company_id={{ page.shopsyKlaviyo.apiKey }}';
  22.                 imported.setAttribute("type", "text/javascript");
  23.                 imported.async = 'async';
  24.                 document.head.appendChild(imported);
  25.             }
  26.             if (checkCookie || cookieRequired !== "1") {
  27.                 !function () {
  28.                     if (!window.klaviyo) {
  29.                         window._klOnsite = window._klOnsite || [];
  30.                         try {
  31.                             window.klaviyo = new Proxy({}, {
  32.                                 get: function (n, i) {
  33.                                     return "push" === i ? function () {
  34.                                         var n;
  35.                                         (n = window._klOnsite).push.apply(n, arguments)
  36.                                     } : function () {
  37.                                         for (var n = arguments.length, o = new Array(n), w = 0; w < n; w++)
  38.                                             o[w] = arguments[w];
  39.                                         var t = "function" == typeof o[o.length - 1] ? o.pop() : void 0,
  40.                                             e = new Promise(function (n) {
  41.                                                 window._klOnsite.push([i].concat(o, [function (i) {
  42.                                                     t && t(i), n(i)
  43.                                                 }]))
  44.                                             });
  45.                                         return e
  46.                                     }
  47.                                 }
  48.                             })
  49.                         } catch (n) {
  50.                             window.klaviyo = window.klaviyo || [], window.klaviyo.push = function () {
  51.                                 var n;
  52.                                 (n = window._klOnsite).push.apply(n, arguments)
  53.                             }
  54.                         }
  55.                     }
  56.                 }();
  57.                 (function () {
  58.                     var b = document.createElement('script');
  59.                     b.type = 'text/javascript';
  60.                     b.async = true;
  61.                     b.src = ('https:' === document.location.protocol ? 'https://' : 'http://') + 'static.klaviyo.com/onsite/js/klaviyo.js?company_id={{ page.shopsyKlaviyo.apiKey }}';
  62.                     var a = document.getElementsByTagName('script')[0];
  63.                     a.parentNode.insertBefore(b, a);
  64.                 })();
  65.             }
  66.         });
  67.         function waitForKlaviyoElement(selector) {
  68.             return new Promise(resolve => {
  69.                 if (document.querySelector(selector)) {
  70.                     return resolve(document.querySelector(selector));
  71.                 }
  72.                 const observer = new MutationObserver(mutations => {
  73.                     if (document.querySelector(selector)) {
  74.                         resolve(document.querySelector(selector));
  75.                         observer.disconnect();
  76.                     }
  77.                 });
  78.                 observer.observe(document.body, {
  79.                     childList: true,
  80.                     subtree: true
  81.                 });
  82.             });
  83.         }
  84.     </script>
  85. {% endblock %}
  86. {% block base_body %}
  87.     {{ parent() }}
  88.     {% sw_include '@ShopsyKlaviyo6/storefront/shopsy-klaviyo/shopsy-klaviyo-hidden-input-fields.html.twig' %}
  89. {% endblock %}