Files
2026-08-23 08:36:14 -05:00

51 lines
2.7 KiB
JavaScript

(function(){
function getSource(){
try{
let s=sessionStorage.getItem('ctb.src');
if(s)return s;
const p=new URLSearchParams(location.search);
s=p.get('utm_source')||p.get('src')||'';
if(!s){const v=p.get('v');if(v&&/^[a-z]{2,12}$/.test(v))s='angle:'+v;}
if(!s&&document.referrer){try{const h=new URL(document.referrer).hostname;if(h&&h!==location.hostname)s=h}catch(e){}}
s=s||'(direct)';
sessionStorage.setItem('ctb.src',s);
return s;
}catch(e){return '(direct)'}
}
window.ctbGetSource=getSource;
try{
const p2=new URLSearchParams(location.search);
const cid=p2.get('clickid')||p2.get('cid');
if(cid&&!sessionStorage.getItem('ctb.clickid'))sessionStorage.setItem('ctb.clickid',cid.slice(0,80));
}catch(e){}
window.ctbGetClickId=function(){try{return sessionStorage.getItem('ctb.clickid')||''}catch(e){return ''}};
const path=location.pathname.replace(/\/$/,'')||'/';
const page=path==='/'?'bridge':(path==='/start'?'start':(path==='/training'?'training':(path==='/join-now'?'joinnow':(/^\/join\/\d+$/.test(path)?'join':null))));
if(page)fetch('/api/public/track',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({event:page,source:getSource()})}).catch(()=>{});
// CTB Rewards featured offer: a ctb1_ clickid means this visitor earns
// coins for a REAL look at the tour — count 30 seconds of visible time on
// a join page, then report "engaged" once (server relays it to CTB).
(function(){
const cid=window.ctbGetClickId();
if(!cid||cid.indexOf('ctb1_')!==0)return;
if(page!=='join'&&page!=='joinnow')return;
try{ if(sessionStorage.getItem('ctb.engaged')===cid)return; }catch(e){}
let seen=0;
const t=setInterval(function(){
if(document.visibilityState!=='visible')return;
if(++seen<30)return;
clearInterval(t);
try{ sessionStorage.setItem('ctb.engaged',cid); }catch(e){}
fetch('/api/public/track',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({event:'engaged',source:getSource(),clickid:cid})})
.then(function(){
const d=document.createElement('div');
d.style.cssText='position:fixed;left:50%;bottom:24px;transform:translateX(-50%);z-index:9999;background:linear-gradient(135deg,#f3be43,#d89b19);color:#0a1622;font-weight:800;font-size:15px;padding:12px 20px;border-radius:12px;box-shadow:0 8px 28px rgba(0,0,0,.4);max-width:92vw;text-align:center';
d.textContent='🎉 Visit counted! Your coins land in the CTB Rewards app — check the featured offer card. (Once per day.)';
document.body.appendChild(d);
setTimeout(function(){ d.remove(); },8000);
}).catch(()=>{});
},1000);
})();
})();