UNPKG

@goplayerzero/js-api

Version:

The official PlayerZero JavaScript SDK

2 lines (1 loc) 3.75 kB
class e{apiToken;static SPAN_COUNTER=Math.round(Math.random()*Number.MAX_SAFE_INTEGER);static CHECKS=new RegExp("\\b\\d{3}-?\\d{2}-?\\d{4}\\b|\\b(?:(?:\\d{3,4})[ -]?){4}\\b","gm");dataset;prod;batchEventsSize;debounceInMs;endpoint;privacy;dequeuedEvents;queuedEvent;insight={Track:{q:Array()},Log:{q:Array()},Span:{q:Array()}};currentIdentification;currentIdentificationMetadata;constructor(e,t){this.apiToken=e,this.dataset=t?.dataset??"default",this.prod=t?.prod??!1,this.batchEventsSize=t?.batchEventsSize??100,this.debounceInMs=t?.debounceInMs??2e3,this.endpoint=`${t?.endpoint??"https://sdk.playerzero.app"}`,this.privacy=t?.privacy,this.queuedEvent=t?.queuedEvent,this.dequeuedEvents=t?.dequeuedEvents,console.assert(this.batchEventsSize>=0,"Negative batch sizes are not supported")}identify(e,t){if(null==e)return void(this.currentIdentification=void 0);let i="string"==typeof e?{userId:e}:{userId:e.userId,email:e.email,anonId:e.anonId};void 0===i.userId&&delete i.userId,void 0===i.email&&delete i.email,void 0===i.anonId&&delete i.anonId,this.currentIdentification=i,this.currentIdentificationMetadata={},void 0===t&&(t={});for(let e in t??{})this.currentIdentificationMetadata[`identity.${e}`]=t[e]}track(e,t){const i=this.currentIdentification;void 0===i||void 0===i.userId&&void 0===i.anonId&&void 0===i.email||this.publishInsights("Track",{id:t?.id,identity:i,value:e,type:t?.type,ts:t?.ts??new Date,properties:{...t?.properties,...this.currentIdentificationMetadata},attributes:t?.attributes})}log(e,t,i){const s={},n=t instanceof Error?t:void 0,a=n?n.message:t,r={...this.currentIdentification};i?.fp&&(s.fp=i.fp),i?.exception?.message?s["exception.message"]=i.exception.message:n?.message&&(s["exception.message"]=n.message),i?.exception?.type?s["exception.type"]=i.exception.type:n?.name&&(s["exception.type"]=n.name),i?.exception?.stacktrace?s["exception.stacktrace"]=i.exception.stacktrace:n?.stack&&(s["exception.stacktrace"]=n.stack),void 0===i?.traceId&&(Object.assign(s,this.currentIdentificationMetadata),r?.userId&&(s["identity.userId"]=r.userId),r?.email&&(s["identity.email"]=r.email),r?.anonId&&(s["identity.anonId"]=r.anonId)),this.publishInsights("Log",{id:i?.id,traceId:i?.traceId,spanId:i?.spanId,type:e,value:this.privatizeText(a),ts:i?.ts||new Date,qty:i?.qty,properties:{...i?.properties,...s},attributes:{...i?.attributes}})}span(t,i,s,n,a){const r=this.transformId(t,16),d=new Date,o=new Date(d.getTime()-n);this.publishInsights("Span",{id:a?.id??e.SPAN_COUNTER.toString(16).padStart(16,"0"),traceId:r,spanId:a?.parentId,type:i,value:s,start:o,end:d,error:a?.error,events:a?.events,properties:a?.properties,attributes:a?.attributes})}flush(){this.flushQ("Track"),this.flushQ("Span"),this.flushQ("Log")}publishInsights(e,t){if(Array.isArray(t)||(t=[t]),t.length>0&&(this.insight[e].q.push(...t),t.forEach((t=>this.queuedEvent?.(e,t)))),this.insight[e].q.length>=this.batchEventsSize)return this.flushQ(e);void 0!==this.insight[e].debounce&&(clearInterval(this.insight[e].debounce),this.insight[e].debounce=void 0),this.insight[e].debounce=setInterval((()=>this.flushQ(e)),this.debounceInMs)}flushQ(e){const t=this.insight[e],i=t.q.splice(0,t.q.length);void 0!==t.debounce&&(clearInterval(t.debounce),t.debounce=void 0),0!==i.length&&fetch(`${this.endpoint}/v2/${e.toLowerCase()}s`,{keepalive:!0,method:"POST",body:JSON.stringify(i),headers:{Authorization:`Bearer ${this.apiToken}`,"X-PzSdk":"JavaScript 0.6.2","X-PzProd":`${this.prod}`,"X-PzBucket":`${this.dataset}`}}).catch((()=>[])).then((()=>this.dequeuedEvents?.(e,i)))}privatizeText(t){if(t?.length>1048576)return"";let i=t;return void 0!==this.privacy&&(i=this.privacy(i)),i.replace(e.CHECKS,"<redact>")}transformId(e,t){return e}}export{e as PzApi};