1 line
1.9 KiB
JavaScript
1 line
1.9 KiB
JavaScript
const STORAGE_KEY="ghost-history",TIMEOUT=864e5,LIMIT=15;!async function(){try{const e=window.localStorage,r=e.getItem(STORAGE_KEY),t=(new Date).getTime();let i=[];if(r)try{i=JSON.parse(r)}catch(e){console.warn("[Member Attribution] Error while parsing history",e)}const a=i.findIndex((e=>{if(!e.time||"number"!=typeof e.time)return!1;const r=t-e.time;return!(isNaN(e.time)||r>TIMEOUT)}));let n,o,s,c,h;a>0?i.splice(0,a):-1===a&&(i=[]);try{const e=new URL(window.location.href);if(n=e.searchParams.get("ref"),o=e.searchParams.get("source"),s=e.searchParams.get("utm_source"),c=e.searchParams.get("utm_medium"),h=n||o||s||null,!h&&e.hash&&e.hash.includes("#/portal")){const e=new URL(window.location.href.replace("/#/portal",""));n=e.searchParams.get("ref"),o=e.searchParams.get("source"),s=e.searchParams.get("utm_source"),c=e.searchParams.get("utm_medium"),h=n||o||s||null}}catch(e){console.error("[Member Attribution] Parsing referrer from querystring failed",e)}const l=c||null,u=window.document.referrer||null;try{const e=new URL(window.location.href),r=e.searchParams;r.get("attribution_id")&&r.get("attribution_type")&&(i.push({time:t,id:r.get("attribution_id"),type:r.get("attribution_type"),referrerSource:h,referrerMedium:l,referrerUrl:u}),r.delete("attribution_id"),r.delete("attribution_type"),e.search="?"+r.toString(),window.history.replaceState({},"",`${e.pathname}${e.search}${e.hash}`))}catch(e){console.error("[Member Attribution] Parsing attribution from querystring failed",e)}const m=window.location.pathname;0===i.length||i[i.length-1].path!==m?i.push({path:m,time:t,referrerSource:h,referrerMedium:l,referrerUrl:u}):i.length>0&&(i[i.length-1].time=t,h&&(i[i.length-1].referrerSource=h,i[i.length-1].referrerMedium=l),u&&(i[i.length-1].referrerUrl=u)),i.length>15&&(i=i.slice(-15)),e.setItem(STORAGE_KEY,JSON.stringify(i))}catch(e){console.error("[Member Attribution] Failed with error",e)}}(); |