UNPKG

@tinytapanalytics/sdk

Version:

Behavioral psychology platform that detects visitor frustration, predicts abandonment, and helps you save at-risk conversions in real-time

1 lines 1.95 kB
var t="3.3.3";class e{constructor(t={}){this.config={endpoint:"https://api.tinytapanalytics.com",debug:!1,...t},this.sessionId=this.generateSessionId()}identify(t){this.userId=t,this.track("identify",{user_id:t})}async track(e,i){if(!this.config.websiteId){const t="TinyTapAnalytics: websiteId is required but not configured";throw this.config.debug,new Error(t)}const n={website_id:this.config.websiteId,event_type:e,user_id:this.userId,session_id:this.sessionId,timestamp:(new Date).toISOString(),user_agent:navigator.userAgent,page_url:window.location.href,metadata:{...i,viewport_width:window.innerWidth,viewport_height:window.innerHeight,sdk_version:`${t}-minimal`}};try{await this.sendEvent(n),this.config.debug}catch(a){throw this.config.debug,a}}async trackConversion(t,e="USD",i){await this.track("conversion",{value:t,currency:e,...i})}async trackPageView(){await this.track("page_view",{url:window.location.href,title:document.title,referrer:document.referrer})}async trackClick(t,e){await this.track("click",{element:t,page_url:window.location.href,...e})}async sendEvent(t){const e=await fetch(`${this.config.endpoint}/api/v1/events`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(t)});if(!e.ok)throw new Error(`HTTP ${e.status}: ${e.statusText}`)}generateSessionId(){return"ciq_"+Date.now().toString(36)+Math.random().toString(36).substring(2)}}if("undefined"!=typeof window){const t=document.currentScript,i=!!window.__tinytapanalytics_config;if(t||i){const i=window.__tinytapanalytics_config||{};t?.dataset.apiKey&&(i.apiKey=t.dataset.apiKey,i.websiteId=t.dataset.websiteId||t.dataset.apiKey);const n=new e(i);window.TinyTapAnalytics=n,i.debug||i.websiteId;const a=async()=>{try{await n.trackPageView()}catch(t){}};"loading"===document.readyState?document.addEventListener("DOMContentLoaded",a):a()}}var i=e;export{e as MinimalTinyTapAnalytics,i as default};