// Grab UTMs from the current page URL and append to all checkout links
(function() {
var params = new URLSearchParams(window.location.search);
var utmKeys = ['utm_source','utm_medium','utm_campaign','utm_content','utm_term'];
var utmString = utmKeys
.filter(k => params.has(k))
.map(k => k + '=' + params.get(k))
.join('&');
if (!utmString) return;
// Re-run on DOM mutations in case Buy Button loads async
var observer = new MutationObserver(function() {
document.querySelectorAll('a[href*="myshopify.com"], a[href*="checkout"]').forEach(function(link) {
var url = new URL(link.href);
utmKeys.forEach(k => {
if (params.has(k)) url.searchParams.set(k, params.get(k));
});
link.href = url.toString();
});
});
observer.observe(document.body, { childList: true, subtree: true });
})();