UNPKG

palzintrack

Version:

Palzin Track Client

2 lines (1 loc) 4.03 kB
!function(){"use strict";(t=>{const{screen:{width:e,height:n},navigator:{language:r},location:a,innerWidth:o,innerHeight:c,localStorage:i,document:s,history:l}=t,{hostname:d,pathname:h,search:u}=a,{currentScript:p,referrer:f}=s;if(!p)return;const g="data-",y=p.getAttribute.bind(p),m=y(g+"api-key"),v=y(g+"user-id"),w=t.navigator.userAgent,S=y(g+"project-id"),O="https://api.palzin.live/v1/",A=y(g+"tag"),b="false"!==y(g+"auto-track"),j="true"===y(g+"exclude-search"),E=y(g+"domains")||"",x=E.split(",").map((t=>t.trim())),T=`${e}x${n}`;Math.max(s.documentElement.clientWidth||0,t.innerWidth||0),Math.max(s.documentElement.clientHeight||0,t.innerHeight||0);const k=300,C=a.search,I=new URLSearchParams(t.location.search).get("utm_source")||void 0,L="events",P=0===s.referrer.indexOf(a.protocol+"//"+a.host)?null:s.referrer,z=function(){let t=a;const e=s.querySelector("link[rel='canonical'][href]");if(e){const n=s.createElement("a");n.href=e.href,n.hostname.replace(/^www\./,"")===a.hostname.replace(/^www\./,"")&&(t=n)}return t.pathname+t.search||"/"}(),N=t=>{if(t){try{const e=decodeURI(t);if(e!==t)return e}catch{return t}return encodeURI(t)}},$=()=>({projectId:S,hostname:d,screen:T,language:r,userId:v,title:N(K),url:N(Z),referrer:N(F),tag:A||void 0}),_=(t,e,n)=>{n&&(F=Z,Z=(t=>j?t.split("?")[0]:t)(n.toString()),Z!==F&&setTimeout(R,k))},H=()=>i&&i.getItem("pt.disabled")||E&&!x.includes(d),M=async(t,e="analytics")=>{if(H())return;const n={"content-type":"application/json",Authorization:"Bearer "+m,"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"GET,HEAD,POST,OPTIONS"};void 0!==B&&(n["x-pt-m2cache"]=B);try{const e={project:S,token:m,screen:t.screen,query:C,user_id:t.userId,vp_size:t.vp_size,path:z,title:t.title,language:r,utm_source:I,ua:w,url:N(Z)??t.url,referrer:N(F)??P,tag:A||void 0};n["Content-Length"]=JSON.stringify(e).length.toString();const a={method:"POST",headers:n,body:JSON.stringify(e),mode:"no-cors",redirect:"follow"},o=O+"track",c=await fetch(o,a),i=await c.text();return B=i}catch{}},J=async t=>{const e=t.dataset.event;if(!e)return;const n={};for(const[e,r]of Object.entries(t.dataset))if(e.startsWith("tag")){n[D(e).replace("tag-","")]=r}const r={};for(const e of Object.keys(t)){const n=t[e];n&&!e.toLowerCase().includes("password")&&(r[D(e)]=n.toString())}const a={channel:t.dataset.channel||L,project:S,event:e,description:t.dataset.description,icon:t.dataset.icon||"👆",notify:t.dataset.notify||!1,user_id:t.dataset.userid,tags:{...W(n),...W(r)}};return q(a,"event")};function W(t){return Object.fromEntries(Object.entries(t).filter((([t,e])=>void 0!==e)))}const q=async(t,e="event")=>{if(H())return;const n={"content-type":"application/json",Authorization:"Bearer "+m,"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"GET,HEAD,POST,OPTIONS"};void 0!==B&&(n["x-pt-m1cache"]=B);try{const e=O+"log";n["Content-Length"]=JSON.stringify(t).length.toString();const r={method:"POST",headers:n,body:JSON.stringify(t),mode:"no-cors",redirect:"follow"},a=await fetch(e,r),o=await a.text();return B=o}catch{}};function D(t){return t.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase()}const R=(t,e)=>M("string"==typeof t?{...$(),name:t,tags:"object"==typeof e?e:void 0}:"object"==typeof t?t:"function"==typeof t?t($()):$()),U=t=>M({...$(),data:t},"identify");t.pt||(t.pt={track:R,identify:U});let B,G,Z=`${h}${u}`,F=f!==d?f:"",K=s.title;if(b&&!H()){(()=>{const t=(t,e,n)=>{const r=t[e];return(...e)=>(n.apply(null,e),r.apply(t,e))};l.pushState=t(l,"pushState",_),l.replaceState=t(l,"replaceState",_)})(),(()=>{const t=new MutationObserver((([t])=>{K=t&&t.target?t.target.text:void 0})),e=s.querySelector("head > title");e&&t.observe(e,{subtree:!0,characterData:!0,childList:!0})})(),s.addEventListener("click",(async t=>{let e=t.target;for(;e&&e!==s.body&&!e.matches("[data-event]");)e=e.parentElement;e&&e.matches("[data-event]")&&J(e)}),!0);const t=()=>{"complete"!==s.readyState||G||(R(),G=!0)};s.addEventListener("readystatechange",t,!0),t()}})(window)}();