(function(){ 'use strict'; var KEY='np_seo_cluster_attribution'; var guideMap={ 'methylene-blue':'methylene_blue', 'nitric-oxide':'nitric_oxide', 'yerba-mate':'yerba_mate', 'yerba-maté':'yerba_mate' }; var aiHosts={ 'chatgpt.com':'chatgpt','chat.openai.com':'chatgpt','perplexity.ai':'perplexity', 'www.perplexity.ai':'perplexity','claude.ai':'claude','gemini.google.com':'gemini', 'copilot.microsoft.com':'copilot','you.com':'you','poe.com':'poe','chat.mistral.ai':'mistral' }; function send(name,params){ params=params||{}; params.send_to='G-52HL3S16HQ'; if(typeof window.gtag==='function'){ window.gtag('event',name,params); } else { window.dataLayer=window.dataLayer||[]; window.dataLayer.push(Object.assign({event:name},params)); } } function clusterFor(path){ path=decodeURIComponent((path||'').toLowerCase()); for(var key in guideMap){ if(path.indexOf(key)!==-1) return guideMap[key]; } return ''; } function readAttr(){ try{return JSON.parse(sessionStorage.getItem(KEY)||'null');}catch(e){return null;} } function saveAttr(v){ try{sessionStorage.setItem(KEY,JSON.stringify(v));}catch(e){} } var path=location.pathname; var cluster=clusterFor(path); var refHost=''; try{refHost=document.referrer?new URL(document.referrer).hostname.toLowerCase():'';}catch(e){} if(aiHosts[refHost]){ send('seo_ai_referral_landing',{ai_source:aiHosts[refHost],landing_path:path,page_title:document.title}); } if(cluster && path.indexOf('/product/')===-1){ send('seo_cluster_entry',{seo_cluster:cluster,landing_path:path,page_title:document.title}); } document.addEventListener('click',function(e){ var a=e.target.closest&&e.target.closest('a[href]'); if(a){ var u; try{u=new URL(a.href,location.href);}catch(err){u=null;} if(u && u.hostname===location.hostname && u.pathname.indexOf('/product/')===0){ var c=clusterFor(path); if(c){ var attr={cluster:c,source_path:path,destination_path:u.pathname,ts:Date.now()}; saveAttr(attr); send('seo_guide_product_click',{seo_cluster:c,source_path:path,destination_path:u.pathname,link_text:(a.textContent||'').trim().slice(0,100)}); } } } var add=e.target.closest&&e.target.closest('.single_add_to_cart_button,.add_to_cart_button,[name="add-to-cart"]'); if(add){var at=readAttr();if(at)send('seo_cluster_add_to_cart',{seo_cluster:at.cluster,source_path:at.source_path,product_path:path});} },true); var at=readAttr(); if(at && (document.body.classList.contains('woocommerce-checkout')||path.indexOf('/checkout/')===0)){ var checkoutKey='np_checkout_'+at.cluster; if(!sessionStorage.getItem(checkoutKey)){send('seo_cluster_checkout_start',{seo_cluster:at.cluster,source_path:at.source_path});sessionStorage.setItem(checkoutKey,'1');} } if(at && (document.body.classList.contains('woocommerce-order-received')||path.indexOf('/order-received/')!==-1)){ var orderEl=document.querySelector('.woocommerce-order-overview__order strong,.woocommerce-order-overview__order .woocommerce-order-overview__order__value'); var totalEl=document.querySelector('.woocommerce-order-overview__total .amount,.woocommerce-order-overview__total strong'); var orderId=orderEl?(orderEl.textContent||'').trim():path; var doneKey='np_purchase_'+orderId; if(!sessionStorage.getItem(doneKey)){ var value=0;if(totalEl){value=parseFloat((totalEl.textContent||'').replace(/[^0-9,.]/g,'').replace(/,/g,''))||0;} send('seo_cluster_purchase',{seo_cluster:at.cluster,source_path:at.source_path,currency:'ZAR',value:value}); sessionStorage.setItem(doneKey,'1'); } } })();