Files
season/snippets/whatsapp-coupon-widget.liquid
T
2026-10-02 19:32:54 -06:00

62 lines
2.8 KiB
Plaintext

{%- comment -%}
WhatsApp Coupon Widget (Aitana) — rendered right before </body> in layout/theme.liquid.
Performance best practices baked in:
• preconnect + dns-prefetch → in <head> (layout/theme.liquid), warms TLS to the widget host
• First-interaction only → loads on first scroll/touch/click/key/mouse move (no timer)
• fetchpriority="low" → lets fonts/images/CSS win the network
• crossorigin="anonymous" → enables cacheable, CORS-safe fetch
IMPORTANT: this theme's rocket-delay snippet holds every <script> without a
`nolazy` attribute (including scripts injected later). Both the loader and the
injected widget script carry `nolazy` so they are not intercepted; the loader
already defers itself.
The modal open delay is controlled from the Aitana admin (Settings → Lead
capture → "Modal auto-open delay"), no theme redeploy needed.
Not loaded inside the theme editor so the modal doesn't interrupt editing.
{%- endcomment -%}
{%- unless request.design_mode -%}
<script nolazy>
(function () {
var SRC = "https://bpuemzaabpwmxvfcorjc.supabase.co/functions/v1/coupon-widget";
var EVENTS = ['scroll', 'touchstart', 'pointerdown', 'mousemove', 'keydown', 'wheel'];
var triggered = false;
function inject() {
var s = document.createElement('script');
s.src = SRC; s.defer = true; s.crossOrigin = 'anonymous';
s.setAttribute('fetchpriority', 'low');
s.setAttribute('nolazy', '');
document.body.appendChild(s);
}
// Loads ONLY on the visitor's first interaction (no timer): keeps the widget
// and Aitana's Lottie player out of the initial load / Lighthouse window.
// Interactions during page load count too; injection then waits for `load`.
function onFirstInteraction() {
if (triggered) return; triggered = true;
EVENTS.forEach(function (ev) { window.removeEventListener(ev, onFirstInteraction, { passive: true }); });
if (document.readyState === 'complete') inject();
else window.addEventListener('load', inject, { once: true });
}
EVENTS.forEach(function (ev) { window.addEventListener(ev, onFirstInteraction, { passive: true }); });
function clickAitana() {
var fab = document.getElementById('aitana-fab');
if (!fab) return false;
fab.click();
return true;
}
document.addEventListener('click', function (event) {
var trigger = event.target.closest('[data-open-aitana]');
if (!trigger) return;
if (clickAitana()) return;
if (!document.querySelector('script[src="' + SRC + '"]')) inject();
triggered = true;
var tries = 0;
var timer = window.setInterval(function () {
tries += 1;
if (clickAitana() || tries > 40) window.clearInterval(timer);
}, 150);
});
})();
</script>
{%- endunless -%}