humanbehavior-js 0.5.67 β 0.5.68
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -8
- package/package.json +1 -1
- package/packages/browser/dist/cjs/index.js +5 -5
- package/packages/browser/dist/cjs/index.js.map +1 -1
- package/packages/browser/dist/esm/index.js +2 -2
- package/packages/browser/dist/esm/index.js.map +1 -1
- package/packages/browser/dist/index.min.js +1 -1
- package/packages/browser/dist/index.min.js.map +1 -1
- package/packages/core/dist/index.js +1 -1
- package/packages/core/dist/index.js.map +1 -1
- package/packages/core/dist/index.mjs +1 -1
- package/packages/core/dist/index.mjs.map +1 -1
- package/packages/core/dist/tracker.d.ts +55 -0
- package/packages/core/dist/tracker.d.ts.map +1 -1
- package/packages/react/dist/index.js +1 -1
- package/packages/react/dist/index.js.map +1 -1
- package/packages/react/dist/index.mjs +1 -1
- package/packages/react/dist/index.mjs.map +1 -1
|
@@ -12171,7 +12171,7 @@ function v1Bytes(rnds, msecs, nsecs, clockseq, node, buf, offset = 0) {
|
|
|
12171
12171
|
return buf;
|
|
12172
12172
|
}
|
|
12173
12173
|
|
|
12174
|
-
var s;!function(e){e[e.NONE=0]="NONE",e[e.ERROR=1]="ERROR",e[e.WARN=2]="WARN",e[e.INFO=3]="INFO",e[e.DEBUG=4]="DEBUG";}(s||(s={}));const i=new class{constructor(e){this.config={level:s.ERROR,enableConsole:true,enableStorage:false},this.isBrowser="undefined"!=typeof window,e&&(this.config={...this.config,...e});}setConfig(e){this.config={...this.config,...e};}shouldLog(e){return e<=this.config.level}formatMessage(e,t,...s){return `[HumanBehavior ${e}] ${(new Date).toISOString()}: ${t}`}error(e,...t){if(!this.shouldLog(s.ERROR))return;const i=this.formatMessage("ERROR",e);this.config.enableConsole&&console.error(i,...t),this.config.enableStorage&&this.isBrowser&&this.logToStorage(i,t);}warn(e,...t){if(!this.shouldLog(s.WARN))return;const i=this.formatMessage("WARN",e);this.config.enableConsole&&console.warn(i,...t),this.config.enableStorage&&this.isBrowser&&this.logToStorage(i,t);}info(e,...t){if(!this.shouldLog(s.INFO))return;const i=this.formatMessage("INFO",e);this.config.enableConsole&&console.log(i,...t),this.config.enableStorage&&this.isBrowser&&this.logToStorage(i,t);}debug(e,...t){if(!this.shouldLog(s.DEBUG))return;const i=this.formatMessage("DEBUG",e);this.config.enableConsole&&console.log(i,...t),this.config.enableStorage&&this.isBrowser&&this.logToStorage(i,t);}logToStorage(e,t){try{const s=JSON.parse(localStorage.getItem("human_behavior_logs")||"[]"),i={message:e,args:t.length>0?t:void 0,timestamp:Date.now()};s.push(i),s.length>1e3&&s.splice(0,s.length-1e3),localStorage.setItem("human_behavior_logs",JSON.stringify(s));}catch(e){}}getLogs(){if(!this.isBrowser)return [];try{return JSON.parse(localStorage.getItem("human_behavior_logs")||"[]")}catch(e){return []}}clearLogs(){this.isBrowser&&localStorage.removeItem("human_behavior_logs");}};let n=false;const r=()=>n,o=(e,...t)=>{n=true;try{i.error(e,...t);}finally{n=false;}},a=(e,...t)=>{n=true;try{i.warn(e,...t);}finally{n=false;}},d=(e,...t)=>{n=true;try{i.info(e,...t);}finally{n=false;}},c=(e,...t)=>{n=true;try{i.debug(e,...t);}finally{n=false;}};class l{constructor(e){this._isPolling=false,this._pollIntervalMs=3e3,this._queue=[],this._queue=[],this._areWeOnline=true,this._sendRequest=e,"undefined"!=typeof window&&"onLine"in window.navigator&&(this._areWeOnline=window.navigator.onLine,window.addEventListener("online",()=>{this._areWeOnline=true,this._flush();}),window.addEventListener("offline",()=>{this._areWeOnline=false;}));}get length(){return this._queue.length}async retriableRequest(e){const t=e.retriesPerformedSoFar||0;if(t>0){const s=new URL(e.url);s.searchParams.set("retry_count",t.toString()),e.url=s.toString();}try{await this._sendRequest(e);}catch(s){if(this._shouldRetry(s,t)&&t<10)return void this._enqueue(e);e.callback&&e.callback({statusCode:s.status||0,text:s.message||"Request failed"});}}_shouldRetry(e,t){return e.status>=400&&e.status<500?408===e.status||429===e.status:e.status>=500||!e.status}_enqueue(e){const t=e.retriesPerformedSoFar||0;e.retriesPerformedSoFar=t+1;const s=function(e){const t=3e3*2**e,s=t/2,i=Math.min(18e5,t),n=(Math.random()-.5)*(i-s);return Math.ceil(i+n)}(t),i=Date.now()+s;this._queue.push({retryAt:i,requestOptions:e});let n=`Enqueued failed request for retry in ${Math.round(s/1e3)}s`;"undefined"==typeof navigator||navigator.onLine||(n+=" (Browser is offline)"),a(n),this._isPolling||(this._isPolling=true,this._poll());}_poll(){this._poller&&clearTimeout(this._poller),this._poller=setTimeout(()=>{this._areWeOnline&&this._queue.length>0&&this._flush(),this._poll();},this._pollIntervalMs);}_flush(){const e=Date.now(),t=[],s=this._queue.filter(s=>s.retryAt<e||(t.push(s),false));if(this._queue=t,s.length>0)for(const{requestOptions:e}of s)this.retriableRequest(e).catch(e=>{o("Failed to retry request:",e);});}unload(){this._poller&&(clearTimeout(this._poller),this._poller=void 0);for(const{requestOptions:e}of this._queue)try{this._sendBeaconRequest(e);}catch(e){o("Failed to send request via sendBeacon on unload:",e);}this._queue=[];}_sendBeaconRequest(e){if("undefined"!=typeof navigator&&navigator.sendBeacon)try{const t=new URL(e.url);t.searchParams.set("beacon","1");let s=null;e.body&&("string"==typeof e.body?s=new Blob([e.body],{type:"application/json"}):e.body instanceof Blob&&(s=e.body));navigator.sendBeacon(t.toString(),s)||a("sendBeacon returned false for unload request");}catch(e){o("Error sending beacon request:",e);}}}class h{constructor(e,t=1e3){this.storageKey="human_behavior_queue",this.maxQueueSize=t;}getQueue(){if("undefined"==typeof window||!window.localStorage)return [];try{const e=window.localStorage.getItem(this.storageKey);if(!e)return [];const t=JSON.parse(e);return Array.isArray(t)?t:[]}catch(e){return a("Failed to read persisted queue:",e),[]}}setQueue(e){if("undefined"!=typeof window&&window.localStorage)try{const t=e.slice(-this.maxQueueSize);window.localStorage.setItem(this.storageKey,JSON.stringify(t)),c(`Persisted ${t.length} events to storage`);}catch(t){if("QuotaExceededError"===t.name||22===t.code){a("Storage quota exceeded, clearing old events");try{const t=e.slice(-Math.floor(this.maxQueueSize/2));window.localStorage.setItem(this.storageKey,JSON.stringify(t));}catch(e){a("Failed to save smaller queue, clearing storage"),this.clearQueue();}}else a("Failed to persist queue:",t);}}addToQueue(e){const t=this.getQueue();t.push(e),t.length>this.maxQueueSize&&(t.shift(),c("Queue is full, the oldest event is dropped.")),this.setQueue(t);}removeFromQueue(e){const t=this.getQueue();t.splice(0,e),this.setQueue(t);}clearQueue(){if("undefined"!=typeof window&&window.localStorage)try{window.localStorage.removeItem(this.storageKey);}catch(e){a("Failed to clear persisted queue:",e);}}getQueueLength(){return this.getQueue().length}}const u="0.5.67",p=1048576,m=52428.8;function g(e,t,s){return (new TextEncoder).encode(w({sessionId:s,events:[...e,t]})).length>p}function w(e){return JSON.stringify(e,(e,t)=>"bigint"==typeof t?t.toString():t)}function y(e,t){if(!e||"object"!=typeof e)return [];if((new TextEncoder).encode(w({sessionId:t,events:[e]})).length<=p)return [e];const s={...e},i=["screenshot","html","dom","fullText","innerHTML","outerHTML"];i.forEach(e=>{s[e]&&delete s[e];});if((new TextEncoder).encode(w({sessionId:t,events:[s]})).length<=p)return [s];return [{type:e.type,timestamp:e.timestamp,url:e.url,pathname:e.pathname,...Object.fromEntries(Object.entries(e).filter(([e,t])=>!i.includes(e)&&"object"!=typeof t&&"string"!=typeof t||"string"==typeof t&&t.length<1e3))}]}class f{constructor({apiKey:e,ingestionUrl:t}){this.monthlyLimitReached=false,this.sessionId="",this.endUserId=null,this.cspBlocked=false,this.requestTimeout=1e4,this.currentBatchSize=100,this.apiKey=e,this.baseUrl=t,this.persistence=new h(e),this.retryQueue=new l(e=>this._sendRequestInternal(e)),this._loadPersistedEvents();}setTrackingContext(e,t){this.sessionId=e,this.endUserId=t;}async _loadPersistedEvents(){const e=this.persistence.getQueue();if(0!==e.length){c(`Loading ${e.length} persisted events from storage`);for(const t of e)try{await this.sendEventsChunked(t.events,t.sessionId,t.endUserId||void 0,t.windowId,t.automaticProperties),this.persistence.removeFromQueue(1);}catch(e){a("Failed to send persisted event, will retry later:",e);}}}async _sendRequestInternal(e){const t="undefined"!=typeof AbortController?new AbortController:null;let s=null;t&&(s=setTimeout(()=>{t.abort();},this.requestTimeout));try{const i=e.estimatedSize||0,n="POST"===e.method&&i<m,r=await fetch(e.url,{method:e.method||"GET",headers:e.headers||{},body:e.body,signal:t?.signal,keepalive:n});s&&clearTimeout(s);const o=await r.text();let a=null;try{a=JSON.parse(o);}catch{}if(e.callback&&e.callback({statusCode:r.status,text:o,json:a}),!r.ok)throw {status:r.status,message:o}}catch(e){if(s&&clearTimeout(s),"AbortError"===e.name)throw {status:0,message:"Request timeout"};throw e}}unload(){this.retryQueue.unload();}checkMonthlyLimit(){return !this.monthlyLimitReached}async init(e,t){if(!this.checkMonthlyLimit())return {sessionId:e,endUserId:t};let s=null,i=null;"undefined"!=typeof window&&(s=window.location.href,i=document.referrer),d("API init called with:",{sessionId:e,userId:t,entryURL:s,referrer:i,baseUrl:this.baseUrl});try{const n=await this.trackedFetch(`${this.baseUrl}/api/ingestion/init`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,Referer:i||""},body:w({sessionId:e,endUserId:t,entryURL:s,referrer:i,sdkVersion:u})});if(d("API init response status:",n.status),!n.ok){if(429===n.status)return this.monthlyLimitReached=!0,{sessionId:e,endUserId:t};const s=await n.text();throw o("API init failed:",n.status,s),new Error(`Failed to initialize ingestion: ${n.statusText} - ${s}`)}const r=await n.json();return !0===r.monthlyLimitReached&&(this.monthlyLimitReached=!0,d("Monthly limit reached detected from server response")),d("API init success:",r),{sessionId:r.sessionId,endUserId:r.endUserId}}catch(e){throw o("API init error:",e),e}}async sendEvents(e,t,s){const i=e.filter(e=>e&&"object"==typeof e),n=await this.trackedFetch(`${this.baseUrl}/api/ingestion/events`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w({sessionId:t,events:i,endUserId:s,sdkVersion:u})});if(!n.ok){if(429===n.status)throw this.monthlyLimitReached=true,new Error("429: Monthly video processing limit reached");throw new Error(`Failed to send events: ${n.statusText}`)} true===(await n.json()).monthlyLimitReached&&(this.monthlyLimitReached=true,d("Monthly limit reached detected from events response"));}async sendEventsChunked(e,t,s,i,n){if(!this.checkMonthlyLimit())return [];try{const r=[];let o=[];for(const a of e)if(a&&"object"==typeof a)if(g(o,a,t)){if(o.length>0){c(`[SDK] Sending chunk with ${o.length} events`);const e=await this.trackedFetch(`${this.baseUrl}/api/ingestion/events`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w({sessionId:t,events:o,endUserId:s,windowId:i,automaticProperties:n,sdkVersion:u})});if(!e.ok){if(429===e.status)return this.monthlyLimitReached=!0,r.flat();throw new Error(`Failed to send events: ${e.statusText}`)}const a=await e.json();!0===a.monthlyLimitReached&&(this.monthlyLimitReached=!0,d("Monthly limit reached detected from chunked events response")),r.push(a),o=[];}o=y(a,t);}else o.push(a);if(o.length>0){const e=await this._sendChunkWithRetry(o,t,s,i,n||o[0]?.automaticProperties);e&&r.push(e);}return r.flat()}catch(r){throw o("Error sending events:",r),this._persistEvents(e,t,s,i,n),r}}async _sendChunkWithRetry(e,t,s,i,n){let r=Math.min(this.currentBatchSize,e.length),o=0;for(;o<e.length;){const c=w({sessionId:t,events:e.slice(o,o+r),endUserId:s,windowId:i,automaticProperties:n,sdkVersion:u}),l=(new TextEncoder).encode(c).length;try{const h=await this.trackedFetch(`${this.baseUrl}/api/ingestion/events`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:c},l);if(!h.ok){if(429===h.status)return this.monthlyLimitReached=!0,this._persistEvents(e.slice(o),t,s,i,n),null;if(413===h.status){a(`413 error: reducing batch size from ${r} to ${Math.max(1,Math.floor(r/2))}`),this.currentBatchSize=Math.max(1,Math.floor(r/2)),r=this.currentBatchSize;continue}return await this.retryQueue.retriableRequest({url:`${this.baseUrl}/api/ingestion/events`,method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:c,estimatedSize:l,callback:e=>{200===e.statusCode&&e.json&&!0===e.json.monthlyLimitReached&&(this.monthlyLimitReached=!0);}}),this._persistEvents(e.slice(o),t,s,i,n),null}const u=await h.json();if(!0===u.monthlyLimitReached&&(this.monthlyLimitReached=!0,d("Monthly limit reached detected from chunked events response")),o+=r,o>=e.length)return u}catch(r){return a("Network error sending chunk, adding to retry queue:",r),await this.retryQueue.retriableRequest({url:`${this.baseUrl}/api/ingestion/events`,method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:c,estimatedSize:l,callback:e=>{200===e.statusCode&&e.json&&true===e.json.monthlyLimitReached&&(this.monthlyLimitReached=true);}}),this._persistEvents(e.slice(o),t,s,i,n),null}}return null}_persistEvents(e,t,s,i,n){0!==e.length&&this.persistence.addToQueue({sessionId:t,events:e,endUserId:s,windowId:i,automaticProperties:n,timestamp:Date.now()});}async sendUserData(e,t,s){try{const i={userId:e,userAttributes:t,sessionId:s,posthogName:t.email||t.name||null};c("Sending user data to server:",i);const n=await this.trackedFetch(`${this.baseUrl}/api/ingestion/user`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w(i)});if(!n.ok)throw new Error(`Failed to send user data: ${n.statusText} with API key: ${this.apiKey}`);const r=await n.json();return c("Server response:",r),r}catch(e){throw o("Error sending user data:",e),e}}async sendUserAuth(e,t,s,i){try{const n=await this.trackedFetch(`${this.baseUrl}/api/ingestion/user/auth`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w({userId:e,userAttributes:t,sessionId:s,authFields:i})});if(!n.ok)throw new Error(`Failed to authenticate user: ${n.statusText} with API key: ${this.apiKey}`);return await n.json()}catch(e){throw o("Error authenticating user:",e),e}}sendBeaconEvents(e,t,s,i,n){const r={sessionId:t,events:e,endUserId:s||null,windowId:i,automaticProperties:n,sdkVersion:u,apiKey:this.apiKey},o=new Blob([w(r)],{type:"application/json"});return navigator.sendBeacon(`${this.baseUrl}/api/ingestion/events`,o)}async sendCustomEvent(e,t,s,i){d("[SDK] Sending custom event",{sessionId:e,eventName:t,eventProperties:s,endUserId:i});try{const n=await this.trackedFetch(`${this.baseUrl}/api/ingestion/customEvent`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w({sessionId:e,eventName:t,eventProperties:s||{},endUserId:i||null})});if(d("[SDK] Custom event response",{status:n.status,statusText:n.statusText}),!n.ok){const e=await n.text();throw o("[SDK] Failed to send custom event",{status:n.status,statusText:n.statusText,errorText:e}),new Error(`Failed to send custom event: ${n.status} ${n.statusText} - ${e}`)}const r=await n.json();return c("[SDK] Custom event success",r),r}catch(i){throw o("[SDK] Error sending custom event",i,{sessionId:e,eventName:t,eventProperties:s}),i}}async sendCustomEventBatch(e,t,s){try{const i=await this.trackedFetch(`${this.baseUrl}/api/ingestion/customEvent/batch`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w({sessionId:e,events:t,endUserId:s||null})});if(!i.ok)throw new Error(`Failed to send custom event batch: ${i.statusText}`);return await i.json()}catch(e){throw o("Error sending custom event batch:",e),e}}async sendLog(e){try{if(c("[SDK] Sending log to server:",{level:e.level,message:e.message.substring(0,50),sessionId:e.sessionId}),!this.baseUrl)return;if(!e.sessionId)return;const t=await fetch(`${this.baseUrl}/api/ingestion/logs`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w(e)});t.ok?c("[SDK] Log sent successfully"):a("[SDK] Failed to send log to server:",t.status,t.statusText);}catch(e){a("[SDK] Failed to send log to server:",e);}}async sendNetworkError(e){try{if(c("[SDK] Sending network error to server:",{errorType:e.errorType,url:e.url.substring(0,50),sessionId:e.sessionId}),!this.baseUrl)return;if(!e.sessionId)return;const t=await fetch(`${this.baseUrl}/api/ingestion/network`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w(e)});t.ok?c("[SDK] Network error sent successfully"):a("[SDK] Failed to send network error to server:",t.status,t.statusText);}catch(e){a("[SDK] Failed to send network error to server:",e);}}async trackedFetch(e,s,i){const n=Date.now(),r=v1(),o=this.shouldSkipNetworkTracking(e);if(this.cspBlocked&&"POST"===s.method&&"undefined"!=typeof navigator&&"function"==typeof navigator.sendBeacon)return this.trackedFetchWithBeaconFallback(e,s,o);try{const t="undefined"!=typeof AbortController?new AbortController:null;let a=null;t&&(a=setTimeout(()=>{t.abort();},this.requestTimeout));const d="POST"===s.method&&void 0!==i&&i<m,c=await fetch(e,{...s,signal:t?.signal,keepalive:d});a&&clearTimeout(a);const l=Date.now()-n;return c.ok||o||await this.sendNetworkError({requestId:r,url:e,method:s.method||"GET",status:c.status,statusText:c.statusText,duration:l,timestampMs:Date.now(),sessionId:this.sessionId,endUserId:this.endUserId,errorType:this.classifyHttpError(c.status),errorMessage:c.statusText,startTimeMs:n,spanName:`${s.method||"GET"} ${e}`,spanStatus:"error",attributes:{"http.status_code":c.status,"http.status_text":c.statusText}}).catch(()=>{}),c}catch(t){const i=Date.now()-n;if("AbortError"===t.name){const e=new Error("Request timeout");e.name="TimeoutError",t=e;}if(this.isCSPViolation(t)&&"POST"===s.method&&"undefined"!=typeof navigator&&"function"==typeof navigator.sendBeacon)return this.cspBlocked=true,a("[SDK] CSP violation detected, falling back to sendBeacon for future requests"),this.trackedFetchWithBeaconFallback(e,s,o);throw o||await this.sendNetworkError({requestId:r,url:e,method:s.method||"GET",status:null,statusText:null,duration:i,timestampMs:Date.now(),sessionId:this.sessionId,endUserId:this.endUserId,errorType:this.classifyNetworkError(t),errorMessage:t.message,errorName:t.name,startTimeMs:n,spanName:`${s.method||"GET"} ${e}`,spanStatus:"error",attributes:{"error.name":t.name,"error.message":t.message}}).catch(()=>{}),t}}async trackedFetchWithBeaconFallback(e,t,s){try{let s=null,i="";if(t.body)if("string"==typeof t.body)try{s=JSON.parse(t.body),i=t.body;}catch{i=t.body;}else {if(t.body instanceof Blob){a("[SDK] Cannot extract apiKey from Blob body for sendBeacon, using URL param"),e=`${e}${e.includes("?")?"&":"?"}apiKey=${encodeURIComponent(this.apiKey)}`;return navigator.sendBeacon(e,t.body)?new Response(null,{status:200,statusText:"OK",headers:new Headers}):new Response(null,{status:500,statusText:"sendBeacon failed",headers:new Headers})}i=w(t.body),s=t.body;}if(e.includes("/api/ingestion/"))if(s&&"object"==typeof s)s.apiKey=this.apiKey,i=w(s);else if(i)try{const e=JSON.parse(i);e.apiKey=this.apiKey,i=w(e);}catch{e=`${e}${e.includes("?")?"&":"?"}apiKey=${encodeURIComponent(this.apiKey)}`;}else e=`${e}${e.includes("?")?"&":"?"}apiKey=${encodeURIComponent(this.apiKey)}`;else e=`${e}${e.includes("?")?"&":"?"}apiKey=${encodeURIComponent(this.apiKey)}`;const n=i?new Blob([i],{type:"application/json"}):null;return navigator.sendBeacon(e,n)?(c("[SDK] Successfully sent request via sendBeacon (CSP fallback)"),new Response(null,{status:200,statusText:"OK",headers:new Headers})):(a("[SDK] sendBeacon returned false - browser may be throttling"),new Response(null,{status:200,statusText:"OK (sendBeacon best-effort)",headers:new Headers}))}catch(e){return o("[SDK] sendBeacon fallback failed:",e),new Response(null,{status:500,statusText:"Failed to send via sendBeacon",headers:new Headers})}}isCSPViolation(e){const t=(e?.message||"").toLowerCase();return "typeerror"===(e?.name||"").toLowerCase()&&t.includes("failed to fetch")||t.includes("content security policy")||t.includes("csp")||t.includes("violates")||t.includes("refused to connect")&&t.includes("violates")}shouldSkipNetworkTracking(e){if(!e||!this.baseUrl)return false;try{const t=new URL(e),s=new URL(this.baseUrl);return !(t.origin!==s.origin||!t.pathname.startsWith("/api/ingestion/"))||!!e.includes(this.baseUrl)}catch(t){return e.includes(this.baseUrl)}}classifyHttpError(e){return e>=400&&e<500?"client_error":e>=500?"server_error":"unknown_error"}classifyNetworkError(e){const t=e.message||"",s=e.name||"";return this.isCSPViolation(e)?"csp_violation":t.includes("ERR_BLOCKED_BY_CLIENT")||t.includes("ERR_BLOCKED_BY_RESPONSE")||t.includes("blocked:other")||t.includes("net::ERR_BLOCKED_BY_CLIENT")||t.includes("net::ERR_BLOCKED_BY_RESPONSE")||"TypeError"===s&&t.includes("Failed to fetch")&&(t.includes("blocked")||t.includes("ERR_BLOCKED"))?"blocked_by_client":t.includes("CORS")||t.includes("Access-Control")?"cors_error":t.includes("timeout")||"TimeoutError"===s?"timeout_error":t.includes("Failed to fetch")||t.includes("NetworkError")?"network_error":"unknown_error"}}class v{constructor(e){if(this.redactedText="[REDACTED]",this.unredactedFields=new Set,this.redactedFields=new Set,this.redactionMode="privacy-first",this.excludeSelectors=['[data-no-redact="true"]',".human-behavior-no-redact"],e?.redactedText&&(this.redactedText=e.redactedText),e?.excludeSelectors&&(this.excludeSelectors=[...this.excludeSelectors,...e.excludeSelectors]),e?.redactionStrategy)if(this.redactionMode=e.redactionStrategy.mode,"privacy-first"===this.redactionMode)e.redactionStrategy.unredactFields&&this.setFieldsToUnredact(e.redactionStrategy.unredactFields);else {const t=['input[type="password"]','[data-hb-redact="true"]'],s=e.redactionStrategy.redactFields&&e.redactionStrategy.redactFields.length>0?e.redactionStrategy.redactFields:t;this.setFieldsToRedact(s);}e?.legacyRedactFields&&this.setFieldsToUnredact(e.legacyRedactFields),e?.userFields&&this.setFieldsToUnredact(e.userFields);}setFieldsToRedact(e){this.redactedFields.clear();['input[type="password"]','input[type="password" i]','[type="password"]','[type="password" i]',...e].forEach(e=>{this.redactedFields.add(e);}),this.redactedFields.size>0?c(`Redaction: Active for ${this.redactedFields.size} field(s):`,Array.from(this.redactedFields)):c("Redaction: No fields to redact"),this.applyRedactionClasses();}setFieldsToUnredact(e){this.unredactedFields.clear();const t=e.filter(e=>!this.isPasswordSelector(e)||(a(`Cannot unredact password field: ${e} - Password fields are always protected`),false));t.forEach(e=>this.unredactedFields.add(e)),t.length>0?c(`Unredaction: Active for ${t.length} field(s):`,t):c("Unredaction: No valid fields to unredact"),this.applyUnredactionClasses();}redactFields(e){e.forEach(e=>{this.unredactedFields.delete(e);}),this.unredactedFields.size>0?c(`Unredaction: Removed ${e.length} field(s), ${this.unredactedFields.size} remaining:`,Array.from(this.unredactedFields)):c("Unredaction: All fields redacted"),this.applyUnredactionClasses();}clearUnredactedFields(){this.unredactedFields.clear(),c("Unredaction: All fields cleared, everything redacted"),this.removeUnredactionClasses();}hasUnredactedFields(){return this.unredactedFields.size>0}getRedactionMode(){return this.redactionMode}getUnredactedFields(){return Array.from(this.unredactedFields)}getMaskTextSelector(){return "privacy-first"===this.redactionMode?0===this.unredactedFields.size?null:Array.from(this.unredactedFields).join(","):0===this.redactedFields.size?null:Array.from(this.redactedFields).join(",")}applyRedactionClasses(){0!==this.redactedFields.size&&("undefined"!=typeof document&&"loading"!==document.readyState?this.redactedFields.forEach(e=>{try{const t=document.querySelectorAll(e);t.forEach(e=>{e&&e.classList&&e.classList.add("rr-mask");}),c(`Added rr-mask class to ${t.length} element(s) for selector: ${e}`);}catch(t){a(`Invalid selector: ${e}`);}}):c("DOM not ready, deferring redaction class application"));}applyUnredactionClasses(){0!==this.unredactedFields.size&&("undefined"!=typeof document&&"loading"!==document.readyState?this.unredactedFields.forEach(e=>{try{const t=document.querySelectorAll(e);t.forEach(e=>{e&&e.classList&&e.classList.remove("rr-mask");}),c(`Removed rr-mask class from ${t.length} element(s) for selector: ${e}`);}catch(t){a(`Invalid selector: ${e}`);}}):c("DOM not ready, deferring unredaction class application"));}removeUnredactionClasses(){c("Unredaction classes removed");}isPasswordSelector(e){return ['input[type="password"]','input[type="password" i]','[type="password"]','[type="password" i]'].some(t=>e.toLowerCase().includes(t.toLowerCase().replace(/[\[\]]/g,"")))}getOriginalValue(e){if(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement)return e.value}isElementUnredacted(e){return this.shouldUnredactElement(e)}shouldUnredactElement(e){if("privacy-first"===this.redactionMode){if(0===this.unredactedFields.size)return false;for(const t of this.unredactedFields)try{if(e.matches(t))return !0}catch(e){a(`Invalid selector: ${t}`);}return false}if(0===this.redactedFields.size)return true;for(const t of this.redactedFields)try{if(e.matches(t))return !1}catch(e){a(`Invalid selector: ${t}`);}return true}}new v;const S="undefined"!=typeof window;function k(){if(!S)return "unknown";const e=navigator.userAgent.toLowerCase(),t=window.screen.width,s=window.screen.height;return /mobile|android|iphone|ipad|ipod|blackberry|windows phone/i.test(e)?/ipad/i.test(e)||t>=768&&s>=1024?"tablet":"mobile":/windows|macintosh|linux/i.test(e)?"desktop":"unknown"}function _(e){try{return new URL(e).hostname}catch{return ""}}function b(){if(!S)return {device_type:"unknown",browser:"unknown",browser_version:"unknown",os:"unknown",os_version:"unknown",screen_resolution:"unknown",viewport_size:"unknown",color_depth:0,timezone:"unknown",language:"unknown",languages:[]};const{browser:e,browser_version:t}=function(){if(!S)return {browser:"unknown",browser_version:"unknown"};const e=navigator.userAgent;if(/chrome/i.test(e)&&!/edge/i.test(e)){const t=e.match(/chrome\/(\d+)/i);return {browser:"chrome",browser_version:t?t[1]:"unknown"}}if(/firefox/i.test(e)){const t=e.match(/firefox\/(\d+)/i);return {browser:"firefox",browser_version:t?t[1]:"unknown"}}if(/safari/i.test(e)&&!/chrome/i.test(e)){const t=e.match(/version\/(\d+)/i);return {browser:"safari",browser_version:t?t[1]:"unknown"}}if(/edge/i.test(e)){const t=e.match(/edge\/(\d+)/i);return {browser:"edge",browser_version:t?t[1]:"unknown"}}if(/msie|trident/i.test(e)){const t=e.match(/msie (\d+)/i)||e.match(/rv:(\d+)/i);return {browser:"ie",browser_version:t?t[1]:"unknown"}}return {browser:"unknown",browser_version:"unknown"}}(),{os:s,os_version:i}=function(){if(!S)return {os:"unknown",os_version:"unknown"};const e=navigator.userAgent;if(/windows/i.test(e)){const t=e.match(/windows nt (\d+\.\d+)/i);let s="unknown";if(t){const e=parseFloat(t[1]);s=10===e?"10":6.3===e?"8.1":6.2===e?"8":6.1===e?"7":t[1];}return {os:"windows",os_version:s}}if(/macintosh|mac os x/i.test(e)){const t=e.match(/mac os x (\d+[._]\d+)/i);return {os:"macos",os_version:t?t[1].replace("_","."):"unknown"}}if(/iphone|ipad|ipod/i.test(e)){const t=e.match(/os (\d+[._]\d+)/i);return {os:"ios",os_version:t?t[1].replace("_","."):"unknown"}}if(/android/i.test(e)){const t=e.match(/android (\d+\.\d+)/i);return {os:"android",os_version:t?t[1]:"unknown"}}return /linux/i.test(e)?{os:"linux",os_version:"unknown"}:{os:"unknown",os_version:"unknown"}}();return {device_type:k(),browser:e,browser_version:t,os:s,os_version:i,screen_resolution:`${window.screen.width}x${window.screen.height}`,viewport_size:`${window.innerWidth}x${window.innerHeight}`,color_depth:window.screen.colorDepth,timezone:Intl.DateTimeFormat().resolvedOptions().timeZone,language:navigator.language,languages:[...navigator.languages||[navigator.language]],raw_user_agent:navigator.userAgent}}function T(){if(!S)return {current_url:"",pathname:"",search:"",hash:"",title:"",referrer:"",referrer_domain:"",initial_referrer:"",initial_referrer_domain:""};const e=window.location.href,t=document.referrer,s=function(e){const t=new URL(e),s={};return ["utm_source","utm_medium","utm_campaign","utm_term","utm_content"].forEach(e=>{const i=t.searchParams.get(e);i&&(s[e]=i);}),s}(e);return {current_url:e,pathname:window.location.pathname,search:window.location.search,hash:window.location.hash,title:document.title,referrer:t,referrer_domain:_(t),initial_referrer:t,initial_referrer_domain:_(t),initial_host:window.location.hostname,...s}}function I(){return {...b(),...T()}}function E(){if(!S)return {};const e=T();return {initial_referrer:e.initial_referrer,initial_referrer_domain:e.initial_referrer_domain,initial_url:e.current_url,initial_pathname:e.pathname,initial_utm_source:e.utm_source,initial_utm_medium:e.utm_medium,initial_utm_campaign:e.utm_campaign,initial_utm_term:e.utm_term,initial_utm_content:e.utm_content}}function U(){if(!S)return {};const e=T();return {current_url:e.current_url,pathname:e.pathname,search:e.search,hash:e.hash,title:e.title,referrer:e.referrer,referrer_domain:e.referrer_domain,utm_source:e.utm_source,utm_medium:e.utm_medium,utm_campaign:e.utm_campaign,utm_term:e.utm_term,utm_content:e.utm_content}}class C{constructor(e={}){this.sessionProperties={},this.userProperties={},this.initialProperties={},this.isInitialized=false,this.config={enableAutomaticProperties:true,enableSessionProperties:true,enableUserProperties:true,propertyDenylist:[],...e},this.automaticProperties=I(),this.initialize();}initialize(){this.isInitialized||(this.initialProperties=E(),this.loadSessionProperties(),this.isInitialized=true);}getEventProperties(e={}){const t={...e};return this.config.enableAutomaticProperties&&Object.assign(t,this.getAutomaticProperties()),this.config.enableSessionProperties&&Object.assign(t,this.sessionProperties),this.config.enableUserProperties&&Object.assign(t,this.userProperties),this.sessionProperties.$initial_properties_captured||(Object.assign(t,this.initialProperties),this.setSessionProperty("$initial_properties_captured",true)),this.applyDenylist(t),t}getAutomaticProperties(){return {...this.automaticProperties,...U()}}getAutomaticPropertiesWithGeoIP(e={}){return {...this.automaticProperties,...U(),...e}}setSessionProperty(e,t){this.sessionProperties[e]=t,this.saveSessionProperties();}setSessionProperties(e){Object.assign(this.sessionProperties,e),this.saveSessionProperties();}getSessionProperty(e){return this.sessionProperties[e]}removeSessionProperty(e){delete this.sessionProperties[e],this.saveSessionProperties();}setUserProperty(e,t){this.userProperties[e]=t;}setUserProperties(e){Object.assign(this.userProperties,e);}getUserProperty(e){return this.userProperties[e]}removeUserProperty(e){delete this.userProperties[e];}setOnce(e,t,s="user"){"session"===s?e in this.sessionProperties||this.setSessionProperty(e,t):e in this.userProperties||this.setUserProperty(e,t);}clearSessionProperties(){this.sessionProperties={},this.saveSessionProperties();}clearUserProperties(){this.userProperties={};}reset(){this.clearSessionProperties(),this.clearUserProperties(),this.initialProperties={},this.isInitialized=false,this.initialize();}loadSessionProperties(){if("undefined"!=typeof sessionStorage)try{const e=sessionStorage.getItem("hb_session_properties");e&&(this.sessionProperties=JSON.parse(e));}catch(e){console.warn("Failed to load session properties:",e);}}saveSessionProperties(){if("undefined"!=typeof sessionStorage)try{sessionStorage.setItem("hb_session_properties",JSON.stringify(this.sessionProperties));}catch(e){console.warn("Failed to save session properties:",e);}}applyDenylist(e){this.config.propertyDenylist&&0!==this.config.propertyDenylist.length&&this.config.propertyDenylist.forEach(t=>{delete e[t];});}updateAutomaticProperties(){this.automaticProperties=I();}getAllProperties(){return {automatic:this.getAutomaticProperties(),session:{...this.sessionProperties},user:{...this.userProperties},initial:{...this.initialProperties}}}}const R="undefined"!=typeof window;class P{get isTrackerStarted(){return this.isStarted}setupDomReadyHandler(){if(R)if("complete"===document.readyState||"interactive"===document.readyState)this.onDomReady();else if(document.addEventListener){document.addEventListener("DOMContentLoaded",()=>this.onDomReady(),{capture:false});const e=setInterval(()=>{"interactive"!==document.readyState&&"complete"!==document.readyState||(clearInterval(e),this.onDomReady());},10);setTimeout(()=>clearInterval(e),5e3);}else this.onDomReady();else this.onDomReady();}onDomReady(){this.isDomReady||(this.isDomReady=true,c("π― DOM is ready, processing queued requests"),this.requestQueue.forEach(e=>{this.processRequest(e);}),this.requestQueue=[],this.domReadyHandlers.forEach(e=>e()),this.domReadyHandlers=[]);}queueRequest(e){this.isDomReady?this.processRequest(e):this.requestQueue.push(e);}async processRequest(e){switch(c("Processing queued request:",e),e.type){case "addEvent":await this.addEvent(e.event);break;case "identifyUser":await this.identifyUser(e.userProperties);break;case "trackPageView":this.trackPageView();break;default:a("Unknown request type:",e.type);}}registerDomReadyHandler(e){this.isDomReady?e():this.domReadyHandlers.push(e);}static init(e,t){if(R&&false!==t?.suppressConsoleErrors){const e=console.error;console.error=(...t)=>{const s=t.join(" ");s.includes("SecurityError: Failed to execute 'toDataURL'")||s.includes("Tainted canvases may not be exported")||s.includes("Cannot inline img src=")||s.includes("Cross-Origin")||s.includes("CORS")||s.includes("Access-Control-Allow-Origin")||s.includes("Failed to load resource")||s.includes("net::ERR_BLOCKED_BY_CLIENT")||s.includes("NetworkError when attempting to fetch resource")||s.includes("Failed to fetch")||s.includes("TypeError: NetworkError")||s.includes("HumanBehavior ERROR")||s.includes("Failed to track custom event")||s.includes("Error sending custom event")||e.apply(console,t);};const t=console.warn;console.warn=(...e)=>{const s=e.join(" ");s.includes("Cannot inline img src=")||s.includes("Cross-Origin")||s.includes("CORS")||s.includes("Access-Control-Allow-Origin")||s.includes("Failed to load resource")||s.includes("net::ERR_BLOCKED_BY_CLIENT")||s.includes("NetworkError when attempting to fetch resource")||s.includes("Failed to fetch")||s.includes("Custom event network error")||s.includes("Request blocked by ad blocker")||t.apply(console,e);},window.addEventListener("error",e=>{const t=e.message||"";if(t.includes("SecurityError")||t.includes("Tainted canvases")||t.includes("toDataURL")||t.includes("Cross-Origin")||t.includes("CORS")||t.includes("NetworkError")||t.includes("Failed to fetch"))return e.preventDefault(),false});}if(R&&window.__humanBehaviorGlobalTracker)return c("Tracker already initialized, returning existing instance"),window.__humanBehaviorGlobalTracker;t?.logLevel&&this.configureLogging({level:t.logLevel});const s=new P(e,t?.ingestionUrl,{enableAutomaticProperties:t?.enableAutomaticProperties,propertyDenylist:t?.propertyDenylist,redactionStrategy:t?.redactionStrategy,redactFields:t?.redactFields,maxQueueSize:t?.maxQueueSize,enableConsoleTracking:t?.enableConsoleTracking,enableNetworkTracking:t?.enableNetworkTracking});return s.recordCanvas=t?.recordCanvas??false,t?.redactFields&&s.setUnredactedFields(t.redactFields),false!==t?.enableAutomaticTracking&&s.setupAutomaticTracking(t?.automaticTrackingOptions),s.start(),s}constructor(e,s,i){if(this.eventQueue=[],this._sessionActivityTimestamp=null,this._sessionStartTimestamp=null,this.userProperties={},this.isProcessing=false,this.flushInterval=null,this.FLUSH_INTERVAL_MS=3e3,this.endUserId=null,this.initialized=false,this.initializationPromise=null,this.monthlyLimitReached=false,this.isDomReady=false,this.requestQueue=[],this.domReadyHandlers=[],this.originalConsole=null,this.consoleTrackingEnabled=false,this.originalFetch=null,this.networkTrackingEnabled=false,this.enableConsoleTrackingFlag=true,this.enableNetworkTrackingFlag=true,this.navigationTrackingEnabled=false,this.currentUrl="",this.previousUrl="",this.originalPushState=null,this.originalReplaceState=null,this.navigationListeners=[],this._connectionBlocked=false,this.recordInstance=null,this.sessionStartTime=Date.now(),this.rrwebRecord=null,this.fullSnapshotTimeout=null,this.recordCanvas=false,this.isStarted=false,this.rageClickTracker={clicks:[]},this.RAGE_CLICK_THRESHOLD_PX=30,this.RAGE_CLICK_TIMEOUT_MS=1e3,this.RAGE_CLICK_CLICK_COUNT=3,this.deadClickTracker={pendingClicks:new Map},this.DEAD_CLICK_SCROLL_THRESHOLD_MS=100,this.DEAD_CLICK_SELECTION_THRESHOLD_MS=100,this.DEAD_CLICK_MUTATION_THRESHOLD_MS=2e3,this.DEAD_CLICK_ABSOLUTE_TIMEOUT_MS=1400,!e)throw new Error("Human Behavior API Key is required");const n=s||"https://ingest.humanbehavior.co";if(this.api=new f({apiKey:e,ingestionUrl:n}),this.apiKey=e,this.ingestionUrl=n,this.MAX_QUEUE_SIZE=i?.maxQueueSize??1e3,this.enableConsoleTrackingFlag=false!==i?.enableConsoleTracking,this.enableNetworkTrackingFlag=false!==i?.enableNetworkTracking,this.redactionManager=new v({redactionStrategy:i?.redactionStrategy,legacyRedactFields:i?.redactFields}),this.propertyManager=new C({enableAutomaticProperties:false!==i?.enableAutomaticProperties,propertyDenylist:i?.propertyDenylist||[]}),R){const e="human_behavior_end_user_id",s=this.getCookie(e);this.endUserId=s||v1(),s?c(`Reusing existing endUserId: ${this.endUserId}`):(this.setCookie(e,this.endUserId,365),c(`Generated new endUserId: ${this.endUserId}`));}else this.endUserId=v1();R?(this.sessionId=this.getOrCreateSessionId(),this.windowId=v1(),this.currentUrl=window.location.href,window.__humanBehaviorGlobalTracker=this):(this.sessionId=v1(),this.windowId=v1()),this.api.setTrackingContext(this.sessionId,this.endUserId),this.initializationPromise=this.init().catch(e=>{o("Initialization failed:",e);});}async init(){try{R?(this.setupPageUnloadHandler(),this.setupNavigationTracking()):d("HumanBehaviorTracker initialized in server environment. Session tracking is disabled."),this.initialized=!0,d(`HumanBehaviorTracker initialized with sessionId: ${this.sessionId}, endUserId: ${this.endUserId}`);}catch(e){o("Failed to initialize HumanBehaviorTracker:",e),this.initialized=true;}}async ensureInitialized(){this.initializationPromise&&await this.initializationPromise;}setupNavigationTracking(){if(!R||this.navigationTrackingEnabled)return;this.navigationTrackingEnabled=true,c("Setting up navigation tracking"),this.originalPushState=history.pushState,this.originalReplaceState=history.replaceState,history.pushState=(...e)=>{this.previousUrl=this.currentUrl,this.currentUrl=window.location.href,this.originalPushState.apply(history,e),this.trackNavigationEvent("pushState",this.previousUrl,this.currentUrl),this.takeFullSnapshot();},history.replaceState=(...e)=>{this.previousUrl=this.currentUrl,this.currentUrl=window.location.href,this.originalReplaceState.apply(history,e),this.trackNavigationEvent("replaceState",this.previousUrl,this.currentUrl),this.takeFullSnapshot();};const e=()=>{this.previousUrl=this.currentUrl,this.currentUrl=window.location.href,this.trackNavigationEvent("popstate",this.previousUrl,this.currentUrl),this.takeFullSnapshot();};window.addEventListener("popstate",e),this.navigationListeners.push(()=>{window.removeEventListener("popstate",e);});const t=()=>{this.previousUrl=this.currentUrl,this.currentUrl=window.location.href,this.trackNavigationEvent("hashchange",this.previousUrl,this.currentUrl);};window.addEventListener("hashchange",t),this.navigationListeners.push(()=>{window.removeEventListener("hashchange",t);}),this.trackNavigationEvent("pageLoad","",this.currentUrl);}async trackNavigationEvent(e,t,s){if(this.initialized)try{const i={type:e,from:t,to:s,timestamp:(new Date).toISOString(),pathname:window.location.pathname,search:window.location.search,hash:window.location.hash,referrer:document.referrer};if(await this.addEvent({type:5,data:{payload:{eventType:"navigation",...i}},timestamp:Date.now()}),"pageLoad"===e||"pushState"===e||"replaceState"===e||"popstate"===e||"hashchange"===e){const s={url:window.location.href,fromUrl:t,navigationType:e,pathname:window.location.pathname,search:window.location.search,hash:window.location.hash,referrer:document.referrer,timestamp:Date.now()};await this.customEvent("$page_viewed",s);}c(`Navigation tracked: ${e} from ${t} to ${s}`);}catch(e){o("Failed to track navigation event:",e);}}async trackPageView(e){if(this.initialized){this.propertyManager.updateAutomaticProperties();try{const t={url:e||window.location.href,pathname:window.location.pathname,search:window.location.search,hash:window.location.hash,referrer:document.referrer,timestamp:(new Date).toISOString()},s=this.propertyManager.getEventProperties(t);await this.addEvent({type:5,data:{payload:{eventType:"pageview",...s}},timestamp:Date.now()}),c(`Pageview tracked: ${t.url}`);}catch(e){o("Failed to track pageview event:",e);}}}async customEvent(e,s){this.endUserId||(a(`endUserId not available, using anonymous ID for event: ${e}`),this.endUserId=v1()),R&&this.checkAndRefreshSession();const i=this.propertyManager.getEventProperties(s);try{await this.api.sendCustomEvent(this.sessionId,e,i,this.endUserId),c(`Custom event tracked: ${e}`,i);}catch(t){o("Failed to track custom event:",t),t.message?.includes("500")||t.message?.includes("Internal Server Error")||t.message?.includes("Failed to send custom event")?a("Custom event endpoint failed, using fallback"):t.message?.includes("ERR_BLOCKED_BY_CLIENT")?a("Custom event request blocked by ad blocker, using fallback"):t.message?.includes("Failed to fetch")&&a("Custom event network error, using fallback");try{const t={eventName:e,properties:i||{},timestamp:(new Date).toISOString(),url:window.location.href,pathname:window.location.pathname};await this.addEvent({type:5,data:{payload:{eventType:"custom",...t}},timestamp:Date.now()}),c(`Custom event added to event stream as fallback: ${e}`);}catch(e){o("Failed to add custom event to event stream as fallback:",e);}}}setupAutomaticTracking(e){if(!R)return;const t={trackButtons:false!==e?.trackButtons,trackLinks:false,trackForms:false!==e?.trackForms,includeText:false!==e?.includeText,includeClasses:e?.includeClasses||false};c("Setting up automatic tracking with config:",t),t.trackButtons&&this.setupAutomaticButtonTracking(t),t.trackForms&&this.setupAutomaticFormTracking(t),this.setupRageClickDetection();}setupAutomaticButtonTracking(e){document.addEventListener("click",async t=>{const s=t.target;if("BUTTON"===s.tagName||s.closest("button")){const t="BUTTON"===s.tagName?s:s.closest("button"),i={buttonId:t.id||null,buttonType:t.type||"button",page:window.location.pathname,timestamp:Date.now()};e.includeText&&(i.buttonText=t.textContent?.trim()||null),e.includeClasses&&(i.buttonClass=t.className||null),Object.keys(i).forEach(e=>{null===i[e]&&delete i[e];}),await this.customEvent("$button_clicked",i);}});}setupRageClickDetection(){R&&document.addEventListener("click",async e=>{const t=e.target,s=e.clientX,i=e.clientY,n=Date.now();if(this.isRageClick(s,i,n,t)){const e=t.closest('button, a, [role="button"], [role="link"]')||t,r={x:s,y:i,page:window.location.pathname,element:e.tagName.toLowerCase(),clickCount:this.RAGE_CLICK_CLICK_COUNT,timestamp:n};e.id&&(r.elementId=e.id),e.className&&(r.elementClass=e.className),e.textContent&&(r.elementText=e.textContent.trim().substring(0,100)),Object.keys(r).forEach(e=>{null!==r[e]&&void 0!==r[e]||delete r[e];}),await this.customEvent("$rageclick",r),this.rageClickTracker.clicks=[];}});}isRageClick(e,t,s,i){const n=this.rageClickTracker.clicks,r=n[n.length-1];if(r&&Math.abs(e-r.x)+Math.abs(t-r.y)<this.RAGE_CLICK_THRESHOLD_PX&&s-r.timestamp<this.RAGE_CLICK_TIMEOUT_MS){if(n.push({x:e,y:t,timestamp:s,element:i}),n.length>=this.RAGE_CLICK_CLICK_COUNT)return true}else this.rageClickTracker.clicks=[{x:e,y:t,timestamp:s,element:i}];return false}isInteractiveElement(e){const t=e.tagName.toLowerCase();if("button"===t||"a"===t)return true;if(["input","select","textarea"].includes(t))return true;const s=e.getAttribute("role");if(s&&["button","link","tab","menuitem","checkbox","radio"].includes(s))return true;if(e.onclick||e.getAttribute("onclick"))return true;try{if("pointer"===window.getComputedStyle(e).cursor)return !0}catch(e){}return !!e.closest('button, a, [role="button"], [role="link"], [role="tab"], [role="menuitem"]')}setupAutomaticLinkTracking(e){}setupAutomaticFormTracking(e){document.addEventListener("submit",async t=>{const s=t.target,i=new FormData(s),n={formId:s.id||null,formAction:s.action||null,formMethod:s.method||"get",fields:Array.from(i.keys()),page:window.location.pathname,timestamp:Date.now()};e.includeClasses&&(n.formClass=s.className||null),Object.keys(n).forEach(e=>{null===n[e]&&delete n[e];}),await this.customEvent("$form_submitted",n);});}cleanupNavigationTracking(){this.navigationTrackingEnabled&&(this.originalPushState&&(history.pushState=this.originalPushState),this.originalReplaceState&&(history.replaceState=this.originalReplaceState),this.navigationListeners.forEach(e=>e()),this.navigationListeners=[],this.navigationTrackingEnabled=false,c("Navigation tracking cleaned up"));}static logToStorage(e){d(e);}static configureLogging(e){i.setConfig({level:{none:0,error:1,warn:2,info:3,debug:4}[e.level||"error"],enableConsole:false!==e.enableConsole,enableStorage:e.enableStorage||false});}enableConsoleTracking(){R&&!this.consoleTrackingEnabled&&(this.originalConsole={log:console.log,warn:console.warn,error:console.error},console.log=(...e)=>{this.trackConsoleEvent("log",e),this.originalConsole.log(...e);},console.warn=(...e)=>{this.trackConsoleEvent("warn",e),this.originalConsole.warn(...e);},console.error=(...e)=>{this.trackConsoleEvent("error",e),this.originalConsole.error(...e);},this.consoleTrackingEnabled=true,c("Console tracking enabled"));}enableNetworkTracking(){R&&!this.networkTrackingEnabled&&"undefined"!=typeof fetch&&(this.originalFetch=window.fetch.bind(window),window.fetch=async(e,s)=>{const i=Date.now(),n=v1(),r="string"==typeof e?e:e instanceof URL?e.toString():e.url,o=(s?.method||("object"==typeof e&&"method"in e?e.method:void 0)||"GET").toUpperCase(),a=this.shouldSkipNetworkTracking(r),d=1e4;let c=null,l=false;a||(c=setTimeout(()=>{const e=Date.now()-i;l||(l=true,this.api.sendNetworkError({requestId:n,url:r,method:o,status:null,statusText:null,duration:e,timestampMs:Date.now(),sessionId:this.sessionId,endUserId:this.endUserId,errorType:"long_loading",errorMessage:`Request took longer than 10000ms (${e}ms elapsed)`,startTimeMs:i,spanName:`${o} ${r}`,spanStatus:"slow",attributes:{"http.method":o,"http.url":r,"request.duration_ms":e,"request.long_loading_threshold_ms":d}}).catch(()=>{}));},d));try{const t=await this.originalFetch(e,s),d=Date.now()-i;return c&&clearTimeout(c),t.ok||a||this.api.sendNetworkError({requestId:n,url:r,method:o,status:t.status,statusText:t.statusText,duration:d,timestampMs:Date.now(),sessionId:this.sessionId,endUserId:this.endUserId,errorType:this.classifyHttpError(t.status),errorMessage:t.statusText,startTimeMs:i,spanName:`${o} ${r}`,spanStatus:"error",attributes:{"http.status_code":t.status,"http.status_text":t.statusText}}).catch(()=>{}),t}catch(e){const t=Date.now()-i;throw c&&clearTimeout(c),a||this.api.sendNetworkError({requestId:n,url:r,method:o,status:null,statusText:null,duration:t,timestampMs:Date.now(),sessionId:this.sessionId,endUserId:this.endUserId,errorType:this.classifyNetworkError(e),errorMessage:e.message,errorName:e.name,startTimeMs:i,spanName:`${o} ${r}`,spanStatus:"error",attributes:{"error.name":e.name,"error.message":e.message}}).catch(()=>{}),e}},this.networkTrackingEnabled=true,c("Network tracking enabled"));}enablePageLoadTracking(){R&&"undefined"!=typeof window&&("complete"===document.readyState?this.trackPageLoad():window.addEventListener("load",()=>{this.trackPageLoad();}),c("Page load tracking enabled"));}trackPageLoad(){if(R&&"undefined"!=typeof performance)try{const e=performance.getEntriesByType("navigation")[0];if(!e)return;const s=e.loadEventEnd-e.fetchStart;if(s>3e3){const i=v1(),n=e.domContentLoadedEventEnd-e.fetchStart,r=e.domComplete-e.fetchStart;this.api.sendNetworkError({requestId:i,url:window.location.href,method:"GET",status:200,statusText:"OK",duration:s,timestampMs:e.loadEventEnd+performance.timeOrigin,sessionId:this.sessionId,endUserId:this.endUserId,errorType:"slow_page_load",errorMessage:`Page load took ${s}ms`,startTimeMs:e.fetchStart+performance.timeOrigin,spanName:"page_load",spanStatus:"slow",attributes:{"page.url":window.location.href,"page.load_time":s,"page.dom_content_loaded":n,"page.dom_complete":r,"page.fetch_start":e.fetchStart,"page.response_start":e.responseStart-e.fetchStart,"page.response_end":e.responseEnd-e.fetchStart}}).catch(()=>{});}}catch(e){a("Failed to track page load:",e);}}shouldSkipNetworkTracking(e){if(!e||!this.ingestionUrl)return false;try{const t=new URL(e),s=new URL(this.ingestionUrl);return !(t.origin!==s.origin||!t.pathname.startsWith("/api/ingestion/"))||!!e.includes(this.ingestionUrl)}catch(t){return e.includes(this.ingestionUrl)}}classifyHttpError(e){return e>=400&&e<500?"client_error":e>=500?"server_error":"unknown_error"}classifyNetworkError(e){const t=e.message||"",s=e.name||"";return t.includes("blocked")||t.includes("ERR_BLOCKED_BY_CLIENT")||t.includes("net::ERR_BLOCKED_BY_CLIENT")||"TypeError"===s&&t.includes("Failed to fetch")?"blocked_by_client":t.includes("CORS")||t.includes("Cross-Origin")||t.includes("Access-Control-Allow-Origin")||"TypeError"===s&&t.includes("CORS")?"cors_error":t.includes("timeout")||t.includes("TIMEOUT")||t.includes("NetworkError")||"NetworkError"===s?"network_error":t.includes("abort")||"AbortError"===s?"aborted":"unknown_error"}disableConsoleTracking(){R&&this.consoleTrackingEnabled&&(this.originalConsole&&(console.log=this.originalConsole.log,console.warn=this.originalConsole.warn,console.error=this.originalConsole.error),this.consoleTrackingEnabled=false,c("Console tracking disabled"));}trackConsoleEvent(e,t){if(this.initialized)if("log"!==e)try{if(r())return void(this.originalConsole&&this.originalConsole[e](...t));const s=(new Error).stack||"";if(this.isSDKStackFrame(s))return void(this.originalConsole&&this.originalConsole[e](...t));const i={level:e,message:t.map(e=>"object"==typeof e?JSON.stringify(e):String(e)).join(" "),timestampMs:Date.now(),url:R?window.location.href:"",userAgent:R?navigator.userAgent:"",stack:s,sessionId:this.sessionId,endUserId:this.endUserId};this.api.sendLog(i).catch(e=>{this.addEvent({type:5,data:{payload:{eventType:"console",...i}},timestamp:Date.now()}).catch(()=>{});});}catch(e){o("Error in trackConsoleEvent:",e);}else this.originalConsole&&this.originalConsole.log(...t);}isSDKStackFrame(e){if(!e)return false;const t=["humanbehavior-js","@humanbehavior/core","@humanbehavior/browser","tracker.ts","api.ts","logger.ts","utils/logger","packages/core","packages/browser","index.mjs","index.js"],s=e.split("\n");e.toLowerCase();let i=false;for(let e=0;e<s.length;e++){const n=s[e].trim().toLowerCase();if(!n||"error"===n||n.startsWith("error:"))continue;if(!t.some(e=>n.includes(e.toLowerCase()))){i=true;break}}return !i}setupPageUnloadHandler(){if(!R)return;c("Setting up page unload handler"),window.addEventListener("visibilitychange",()=>{"hidden"===document.visibilityState&&(c("Page hidden - sending pending events"),this.flushEvents());});const e="onpagehide"in window?"pagehide":"beforeunload";window.addEventListener(e,()=>{c("Page unloading - sending final events via sendBeacon");const e=[...this.eventQueue],t=e.some(e=>e&&2===e.type);if(!t&&R&&window.__hb_pending_snapshots){const t=window.__hb_pending_snapshots;Array.isArray(t)&&t.length>0&&(c("β οΈ Including pending FullSnapshot(s) in sendBeacon for short session"),e.unshift(...t),delete window.__hb_pending_snapshots);}if(!t&&e.length>0&&a("β οΈ No FullSnapshot in queue on unload - if session was very short, initial snapshot may be lost"),e.length>0&&this.api)try{const t=this.propertyManager.getAutomaticProperties();this.api.sendBeaconEvents(e,this.sessionId,this.endUserId||void 0,this.windowId,t),this.eventQueue=[];}catch(e){a("Failed to send events via sendBeacon on unload:",e);}this.api&&this.api.unload();});const t=()=>{localStorage.setItem("human_behavior_last_activity",Date.now().toString());};window.addEventListener("click",t),window.addEventListener("keydown",t),window.addEventListener("scroll",t),window.addEventListener("mousemove",t);}viewLogs(){try{const e=i.getLogs();d("HumanBehavior Logs:",e),i.clearLogs();}catch(e){o("Failed to read logs:",e);}}async identifyUser({userProperties:e}){const t=this.endUserId;this.userProperties=e,c("Identifying user:",{userProperties:e,originalEndUserId:t,sessionId:this.sessionId}),!R||this.propertyManager.getAutomaticProperties();const s=await this.api.sendUserData(t||"",e,this.sessionId);if(s.actualUserId||s.wasExistingUser){const e=s.actualUserId||t;if(e&&e!==t){const s="human_behavior_end_user_id";if(this.setCookie(s,e,365),R)try{localStorage.setItem(s,e);}catch(e){c("Failed to set canonical endUserId in localStorage:",e);}c(`π Preexisting user detected. Future sessions will use canonical ID: ${e} (current session stays: ${t})`);}}return t||""}getUserAttributes(){return {...this.userProperties}}async start(){if(!R)return;if(this.isStarted)return void c("HumanBehaviorTracker already started, skipping start() call.");this.isStarted=true,this.flushInterval=window.setInterval(()=>{this.flushEvents();},this.FLUSH_INTERVAL_MS),this.enableConsoleTrackingFlag&&this.enableConsoleTracking(),this.enableNetworkTrackingFlag&&this.enableNetworkTracking(),this.enablePageLoadTracking();const t=()=>{if(this.recordInstance)return void c("π― Recording already started, skipping duplicate start");c("π― DOM ready, starting session recording"),this.rrwebRecord=record;const t=record({emit:e=>{this.addRecordingEvent(e),2===e.type&&c(`π― FullSnapshot generated at ${(new Date).toISOString()}`);},maskTextSelector:this.redactionManager.getMaskTextSelector()||void 0,maskTextFn:void 0,maskAllInputs:"privacy-first"===this.redactionManager.getRedactionMode(),maskInputOptions:{password:true,text:true,textarea:true,email:true,number:true,tel:true,url:true,search:true,date:true,time:true,month:true,week:true},maskInputFn:(e,t)=>{try{const s=this.redactionManager.getRedactionMode();if(!(t instanceof HTMLElement))return e;if("privacy-first"===s)return "*".repeat(e.length||1);const i=this.redactionManager.shouldUnredactElement(t);t.id,t.name,t.type;return i?e:"*".repeat(e.length||1)}catch{return e}},slimDOMOptions:{},collectFonts:false,inlineStylesheet:true,recordCrossOriginIframes:false,recordCanvas:this.recordCanvas,sampling:this.recordCanvas?{canvas:4}:void 0,dataURLOptions:this.recordCanvas?{type:"image/webp",quality:.4}:void 0,hooks:{input:e=>{try{if("privacy-first"===this.redactionManager.getRedactionMode())return;const t="undefined"!=typeof document?document.querySelector(`[data-rrweb-id="${e.id}"]`):null;if(t&&t instanceof HTMLElement){this.redactionManager.shouldUnredactElement(t)||(void 0!==e.text&&(e.text="*".repeat(e.text?.length||1)),void 0!==e.value&&(e.value="*".repeat(e.value?.length||1)));}}catch{}}}});this.recordInstance=t||null;};if(c(`π― DOM ready state: ${document.readyState}`),"complete"===document.readyState||"interactive"===document.readyState)c(`π― DOM ready (${document.readyState}), starting recording immediately`),t();else {c("π― DOM not ready, waiting for DOMContentLoaded event");const e=()=>("interactive"===document.readyState||"complete"===document.readyState)&&(c(`π― DOM ready (${document.readyState}), starting recording`),t(),true);if(e())return;document.addEventListener("DOMContentLoaded",()=>{c("π― DOMContentLoaded fired, starting recording"),t();},{once:true});const s=setInterval(()=>{e()&&clearInterval(s);},10);setTimeout(()=>clearInterval(s),5e3);}}takeFullSnapshot(){this.fullSnapshotTimeout&&clearTimeout(this.fullSnapshotTimeout),this.fullSnapshotTimeout=window.setTimeout(()=>{try{requestAnimationFrame(()=>{requestAnimationFrame(()=>{this.rrwebRecord&&"function"==typeof this.rrwebRecord.takeFullSnapshot?(this.rrwebRecord.takeFullSnapshot(),c("β
FullSnapshot taken for navigation (delayed for animations)")):a("β οΈ takeFullSnapshot not available on record function");});});}catch(e){o("β Failed to take FullSnapshot for navigation:",e);}},1e3);}async stop(){await this.ensureInitialized(),R&&(this.flushInterval&&(clearInterval(this.flushInterval),this.flushInterval=null),this.recordInstance&&(this.recordInstance(),this.recordInstance=null),this.fullSnapshotTimeout&&(clearTimeout(this.fullSnapshotTimeout),this.fullSnapshotTimeout=null),this.rrwebRecord=null,this.disableConsoleTracking(),this.cleanupNavigationTracking());}async addEvent(e){if(R&&this.checkAndRefreshSession(),e&&"object"==typeof e){if(2===e.type){const t=!!e.data,s=!(!e.data||!e.data.node);c(t&&s?`β
Valid FullSnapshot: hasData=${t}, hasNode=${s}, dataType=${e.data?.node?.type}`:`β οΈ Empty FullSnapshot detected: hasData=${t}, hasNode=${s} - continuing session`);}this.eventQueue.length>=this.MAX_QUEUE_SIZE&&(this.eventQueue.shift(),c("Queue is full, the oldest event is dropped.")),this.eventQueue.push(e),2===e.type?(c("FullSnapshot added, triggering immediate flush"),this.flushEvents()):this.eventQueue.length>=.8*this.MAX_QUEUE_SIZE&&(c(`Queue at ${this.eventQueue.length}/${this.MAX_QUEUE_SIZE}, triggering immediate flush`),this.flushEvents());}else c("β οΈ Skipping invalid event:",e);}async flushEvents(){if(!this.isProcessing&&!this.monthlyLimitReached){this.isProcessing=true;try{const e=this.eventQueue,t=e.filter(e=>e&&2===e.type);if(this.eventQueue=[],t.length>0&&R&&(window.__hb_pending_snapshots=t,setTimeout(()=>{delete window.__hb_pending_snapshots;},5e3)),e.length>0){c("Flushing events:",e);const t=e.filter(e=>2===e.type);t.length>0&&c(`[FIXED] Sending ${t.length} FullSnapshot(s) with valid data`);try{const t=this.propertyManager.getAutomaticProperties();await this.api.sendEventsChunked(e,this.sessionId,this.endUserId,this.windowId,t);}catch(e){if(e.message?.includes("ERROR: Session already completed"))a("Session expired, events will be lost");else if(e.message?.includes("413")||e.message?.includes("Content Too Large"))a("Payload too large, events will be lost");else {if(!(e.message?.includes("ERR_BLOCKED_BY_CLIENT")||e.message?.includes("Failed to fetch")||e.message?.includes("NetworkError")))throw e;a("Request blocked by ad blocker or network issue, events will be lost");}}}}finally{this.isProcessing=false;}}}async addRecordingEvent(e){if(R&&this.checkAndRefreshSession(),e&&"object"==typeof e){if(2===e.type){const t=!!e.data,s=!(!e.data||!e.data.node);c(t&&s?`β
Valid FullSnapshot: hasData=${t}, hasNode=${s}, dataType=${e.data?.node?.type}`:`β οΈ Empty FullSnapshot detected: hasData=${t}, hasNode=${s} - continuing session`);}this.eventQueue.length>=this.MAX_QUEUE_SIZE&&(this.eventQueue.shift(),c("Queue is full, the oldest event is dropped.")),this.eventQueue.push(e),2===e.type?(c("FullSnapshot added, triggering immediate flush"),this.flushEvents()):this.eventQueue.length>=.8*this.MAX_QUEUE_SIZE&&(c(`Queue at ${this.eventQueue.length}/${this.MAX_QUEUE_SIZE}, triggering immediate flush`),this.flushEvents());}else c("β οΈ Skipping invalid recording event:",e);}setCookie(e,t,s){if(R)try{const i=new Date;i.setTime(i.getTime()+24*s*60*60*1e3);const n=`expires=${i.toUTCString()}`;document.cookie=`${e}=${t};${n};path=/;SameSite=Lax`,localStorage.setItem(e,t),c(`Set cookie and localStorage: ${e}`);}catch(s){try{localStorage.setItem(e,t),c(`Cookie blocked, using localStorage: ${e}`);}catch(e){o("Failed to store user ID in both cookie and localStorage:",e);}}}getCookie(e){if(!R)return null;try{const t=e+"=",s=document.cookie.split(";");for(let i=0;i<s.length;i++){let n=s[i];for(;" "===n.charAt(0);)n=n.substring(1,n.length);if(0===n.indexOf(t)){const s=n.substring(t.length,n.length);return c(`Found cookie: ${e}`),s}}const i=localStorage.getItem(e);return i?(c(`Cookie not found, using localStorage: ${e}`),i):null}catch(t){try{const t=localStorage.getItem(e);if(t)return c(`Cookie access failed, using localStorage: ${e}`),t}catch(e){o("Failed to access both cookie and localStorage:",e);}return null}}deleteCookie(e){if(R){try{document.cookie=`${e}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; SameSite=Lax`,c(`Deleted cookie: ${e}`);}catch(t){o(`Failed to delete cookie: ${e}`,t);}try{localStorage.removeItem(e),c(`Removed from localStorage: ${e}`);}catch(t){o(`Failed to remove from localStorage: ${e}`,t);}}}logout(){if(R)try{const e="human_behavior_end_user_id";this.deleteCookie(e);const s="human_behavior_session";localStorage.removeItem(s),this.endUserId=null,this.userProperties={},this.endUserId=v1(),this.setCookie("human_behavior_end_user_id",this.endUserId,365),this.sessionId=this.createNewSession(s),this.windowId=v1(),this.api.setTrackingContext(this.sessionId,this.endUserId),d("User logged out - cleared all user data and started fresh session");}catch(e){o("Error during logout:",e);}}async redact(e){await this.ensureInitialized(),R?this.redactionManager=new v(e):a("Redaction is only available in browser environments");}setRedactedFields(e){this.redactionManager.setFieldsToRedact(e),this.recordInstance&&this.restartWithNewRedaction();}setUnredactedFields(e){this.redactionManager.setFieldsToUnredact(e),this.recordInstance&&this.restartWithNewRedaction();}restartWithNewRedaction(){this.recordInstance&&(this.recordInstance(),this.start());}hasUnredactedFields(){return this.redactionManager.hasUnredactedFields()}getUnredactedFields(){return this.redactionManager.getUnredactedFields()}redactFields(e){this.redactionManager.redactFields(e),this.recordInstance&&this.restartWithNewRedaction();}clearUnredactedFields(){this.redactionManager.clearUnredactedFields(),this.recordInstance&&this.restartWithNewRedaction();}checkAndRefreshSession(){if(!R)return;const e="human_behavior_session",s=Date.now(),i=this.getStoredSession(e);if(!i||!i.sessionId)return this.createNewSession(e),this.windowId=v1(),this.api.setTrackingContext(this.sessionId,this.endUserId),void c(`Created new session (no stored session): ${this.sessionId}`);this.updateSessionActivity(e,s,i.sessionId,i.sessionStartTimestamp);}getOrCreateSessionId(){if(!R)return v1();const e="human_behavior_session",s=Date.now(),i=this.getStoredSession(e);if(!i||!i.sessionId){const t=this.createNewSession(e);return this.api.setTrackingContext(t,this.endUserId),t}const n=s-i.lastActivityTimestamp,r=s-i.sessionStartTimestamp;if(n>9e5||r>864e5){c(`Session expired: idle=${n}ms, age=${r}ms`);const t=this.createNewSession(e);return this.api.setTrackingContext(t,this.endUserId),t}return this.updateSessionActivity(e,s,i.sessionId,i.sessionStartTimestamp),i.sessionId}getStoredSession(e){const s=Date.now(),i=9e5,n=864e5;if(this.sessionId&&null!==this._sessionActivityTimestamp&&null!==this._sessionStartTimestamp){const r=s-this._sessionActivityTimestamp,o=s-this._sessionStartTimestamp;if(!(r>i||o>n))return {sessionId:this.sessionId,lastActivityTimestamp:this._sessionActivityTimestamp,sessionStartTimestamp:this._sessionStartTimestamp};{c("Session in memory expired: creating new session immediately");const s=this.sessionId;if(this.createNewSession(e),this.windowId=v1(),this.api.setTrackingContext(this.sessionId,this.endUserId),d(`π Session timeout (memory): Created new session ${this.sessionId} (previous: ${s})`),null!==this._sessionActivityTimestamp&&null!==this._sessionStartTimestamp)return {sessionId:this.sessionId,lastActivityTimestamp:this._sessionActivityTimestamp,sessionStartTimestamp:this._sessionStartTimestamp}}}try{const r=localStorage.getItem(e);if(!r)return null;const o=JSON.parse(r),a=s-o.lastActivityTimestamp,l=s-o.sessionStartTimestamp;if(a>i||l>n){c(`Session in localStorage expired: idle=${Math.round(a/1e3/60)}min, age=${Math.round(l/1e3/60/60)}hrs`);const s=o.sessionId;if(this.createNewSession(e),this.windowId=v1(),this.api.setTrackingContext(this.sessionId,this.endUserId),d(`π Session timeout (localStorage): Created new session ${this.sessionId} (previous: ${s})`),null!==this._sessionActivityTimestamp&&null!==this._sessionStartTimestamp)return {sessionId:this.sessionId,lastActivityTimestamp:this._sessionActivityTimestamp,sessionStartTimestamp:this._sessionStartTimestamp}}return o.sessionId&&(this.sessionId=o.sessionId,this._sessionActivityTimestamp=o.lastActivityTimestamp,this._sessionStartTimestamp=o.sessionStartTimestamp),o}catch{return null}}createNewSession(e){const s=v1(),i=Date.now();this.sessionId=s,this._sessionActivityTimestamp=i,this._sessionStartTimestamp=i;const n={sessionId:s,lastActivityTimestamp:i,sessionStartTimestamp:i};try{localStorage.setItem(e,JSON.stringify(n));}catch(e){a(`Failed to save session to localStorage: ${e}`);}return c(`Created new session: ${s}`),s}updateSessionActivity(e,t,s,i){this.sessionId=s,this._sessionActivityTimestamp=t,this._sessionStartTimestamp=i;const n={sessionId:s,lastActivityTimestamp:t,sessionStartTimestamp:i};try{localStorage.setItem(e,JSON.stringify(n));}catch(e){a(`Failed to update session in localStorage: ${e}`);}}getSessionId(){return this.sessionId}getCurrentUrl(){return this.currentUrl}getSnapshotFrequencyInfo(){return {sessionDuration:Date.now()-this.sessionStartTime,currentInterval:3e5,currentThreshold:1e3,phase:"configured"}}async testConnection(){try{return await this.api.init(this.sessionId,this.endUserId),{success:!0}}catch(e){return {success:false,error:e.message||"Unknown error"}}}getConnectionStatus(){const e=[];let t=false;return this.eventQueue.length>0&&(t=true,e.push("Some requests may be blocked by ad blockers")),this._connectionBlocked&&(t=true,e.push("Initial connection test failed - ad blocker may be active")),"undefined"==typeof window&&e.push("Not running in browser environment"),void 0===navigator.sendBeacon&&e.push("sendBeacon not available, using fetch fallback"),{blocked:t,recommendations:e}}isPreexistingUser(){if(!R)return false;const e=this.getCookie("human_behavior_end_user_id");return null!==e&&e!==this.endUserId}getUserInfo(){return {endUserId:this.endUserId,sessionId:this.sessionId,isPreexistingUser:this.isPreexistingUser(),initialized:this.initialized}}setSessionProperty(e,t){this.propertyManager.setSessionProperty(e,t);}setSessionProperties(e){this.propertyManager.setSessionProperties(e);}getSessionProperty(e){return this.propertyManager.getSessionProperty(e)}removeSessionProperty(e){this.propertyManager.removeSessionProperty(e);}setUserProperty(e,t){this.propertyManager.setUserProperty(e,t);}setUserProperties(e){this.propertyManager.setUserProperties(e);}getUserProperty(e){return this.propertyManager.getUserProperty(e)}removeUserProperty(e){this.propertyManager.removeUserProperty(e);}setOnce(e,t,s="user"){this.propertyManager.setOnce(e,t,s);}clearSessionProperties(){this.propertyManager.clearSessionProperties();}clearUserProperties(){this.propertyManager.clearUserProperties();}getAllProperties(){return this.propertyManager.getAllProperties()}}function F(e){const t=globalThis.__humanBehaviorGlobalTracker;return t?.identifyUser?t.identifyUser({userProperties:e}):(console.warn("HumanBehavior tracker not found. Make sure the SDK is initialized."),null)}function L(e,t){const s=globalThis.__humanBehaviorGlobalTracker;return s?.track?s.track(e,t):(console.warn("HumanBehavior tracker not found. Make sure the SDK is initialized."),null)}function $(){const e=globalThis.__humanBehaviorGlobalTracker;return !!e?.identifyUser}R&&(window.HumanBehaviorTracker=P);
|
|
12174
|
+
var s;!function(e){e[e.NONE=0]="NONE",e[e.ERROR=1]="ERROR",e[e.WARN=2]="WARN",e[e.INFO=3]="INFO",e[e.DEBUG=4]="DEBUG";}(s||(s={}));const i=new class{constructor(e){this.config={level:s.ERROR,enableConsole:true,enableStorage:false},this.isBrowser="undefined"!=typeof window,e&&(this.config={...this.config,...e});}setConfig(e){this.config={...this.config,...e};}shouldLog(e){return e<=this.config.level}formatMessage(e,t,...s){return `[HumanBehavior ${e}] ${(new Date).toISOString()}: ${t}`}error(e,...t){if(!this.shouldLog(s.ERROR))return;const i=this.formatMessage("ERROR",e);this.config.enableConsole&&console.error(i,...t),this.config.enableStorage&&this.isBrowser&&this.logToStorage(i,t);}warn(e,...t){if(!this.shouldLog(s.WARN))return;const i=this.formatMessage("WARN",e);this.config.enableConsole&&console.warn(i,...t),this.config.enableStorage&&this.isBrowser&&this.logToStorage(i,t);}info(e,...t){if(!this.shouldLog(s.INFO))return;const i=this.formatMessage("INFO",e);this.config.enableConsole&&console.log(i,...t),this.config.enableStorage&&this.isBrowser&&this.logToStorage(i,t);}debug(e,...t){if(!this.shouldLog(s.DEBUG))return;const i=this.formatMessage("DEBUG",e);this.config.enableConsole&&console.log(i,...t),this.config.enableStorage&&this.isBrowser&&this.logToStorage(i,t);}logToStorage(e,t){try{const s=JSON.parse(localStorage.getItem("human_behavior_logs")||"[]"),i={message:e,args:t.length>0?t:void 0,timestamp:Date.now()};s.push(i),s.length>1e3&&s.splice(0,s.length-1e3),localStorage.setItem("human_behavior_logs",JSON.stringify(s));}catch(e){}}getLogs(){if(!this.isBrowser)return [];try{return JSON.parse(localStorage.getItem("human_behavior_logs")||"[]")}catch(e){return []}}clearLogs(){this.isBrowser&&localStorage.removeItem("human_behavior_logs");}};let n=false;const r=()=>n,o=(e,...t)=>{n=true;try{i.error(e,...t);}finally{n=false;}},a=(e,...t)=>{n=true;try{i.warn(e,...t);}finally{n=false;}},d=(e,...t)=>{n=true;try{i.info(e,...t);}finally{n=false;}},c=(e,...t)=>{n=true;try{i.debug(e,...t);}finally{n=false;}};class l{constructor(e){this._isPolling=false,this._pollIntervalMs=3e3,this._queue=[],this._queue=[],this._areWeOnline=true,this._sendRequest=e,"undefined"!=typeof window&&"onLine"in window.navigator&&(this._areWeOnline=window.navigator.onLine,window.addEventListener("online",()=>{this._areWeOnline=true,this._flush();}),window.addEventListener("offline",()=>{this._areWeOnline=false;}));}get length(){return this._queue.length}async retriableRequest(e){const t=e.retriesPerformedSoFar||0;if(t>0){const s=new URL(e.url);s.searchParams.set("retry_count",t.toString()),e.url=s.toString();}try{await this._sendRequest(e);}catch(s){if(this._shouldRetry(s,t)&&t<10)return void this._enqueue(e);e.callback&&e.callback({statusCode:s.status||0,text:s.message||"Request failed"});}}_shouldRetry(e,t){return e.status>=400&&e.status<500?408===e.status||429===e.status:e.status>=500||!e.status}_enqueue(e){const t=e.retriesPerformedSoFar||0;e.retriesPerformedSoFar=t+1;const s=function(e){const t=3e3*2**e,s=t/2,i=Math.min(18e5,t),n=(Math.random()-.5)*(i-s);return Math.ceil(i+n)}(t),i=Date.now()+s;this._queue.push({retryAt:i,requestOptions:e});let n=`Enqueued failed request for retry in ${Math.round(s/1e3)}s`;"undefined"==typeof navigator||navigator.onLine||(n+=" (Browser is offline)"),a(n),this._isPolling||(this._isPolling=true,this._poll());}_poll(){this._poller&&clearTimeout(this._poller),this._poller=setTimeout(()=>{this._areWeOnline&&this._queue.length>0&&this._flush(),this._poll();},this._pollIntervalMs);}_flush(){const e=Date.now(),t=[],s=this._queue.filter(s=>s.retryAt<e||(t.push(s),false));if(this._queue=t,s.length>0)for(const{requestOptions:e}of s)this.retriableRequest(e).catch(e=>{o("Failed to retry request:",e);});}unload(){this._poller&&(clearTimeout(this._poller),this._poller=void 0);for(const{requestOptions:e}of this._queue)try{this._sendBeaconRequest(e);}catch(e){o("Failed to send request via sendBeacon on unload:",e);}this._queue=[];}_sendBeaconRequest(e){if("undefined"!=typeof navigator&&navigator.sendBeacon)try{const t=new URL(e.url);t.searchParams.set("beacon","1");let s=null;e.body&&("string"==typeof e.body?s=new Blob([e.body],{type:"application/json"}):e.body instanceof Blob&&(s=e.body));navigator.sendBeacon(t.toString(),s)||a("sendBeacon returned false for unload request");}catch(e){o("Error sending beacon request:",e);}}}class h{constructor(e,t=1e3){this.storageKey="human_behavior_queue",this.maxQueueSize=t;}getQueue(){if("undefined"==typeof window||!window.localStorage)return [];try{const e=window.localStorage.getItem(this.storageKey);if(!e)return [];const t=JSON.parse(e);return Array.isArray(t)?t:[]}catch(e){return a("Failed to read persisted queue:",e),[]}}setQueue(e){if("undefined"!=typeof window&&window.localStorage)try{const t=e.slice(-this.maxQueueSize);window.localStorage.setItem(this.storageKey,JSON.stringify(t)),c(`Persisted ${t.length} events to storage`);}catch(t){if("QuotaExceededError"===t.name||22===t.code){a("Storage quota exceeded, clearing old events");try{const t=e.slice(-Math.floor(this.maxQueueSize/2));window.localStorage.setItem(this.storageKey,JSON.stringify(t));}catch(e){a("Failed to save smaller queue, clearing storage"),this.clearQueue();}}else a("Failed to persist queue:",t);}}addToQueue(e){const t=this.getQueue();t.push(e),t.length>this.maxQueueSize&&(t.shift(),c("Queue is full, the oldest event is dropped.")),this.setQueue(t);}removeFromQueue(e){const t=this.getQueue();t.splice(0,e),this.setQueue(t);}clearQueue(){if("undefined"!=typeof window&&window.localStorage)try{window.localStorage.removeItem(this.storageKey);}catch(e){a("Failed to clear persisted queue:",e);}}getQueueLength(){return this.getQueue().length}}const u="0.5.67",p=1048576,m=52428.8;function g(e,t,s){return (new TextEncoder).encode(w({sessionId:s,events:[...e,t]})).length>p}function w(e){return JSON.stringify(e,(e,t)=>"bigint"==typeof t?t.toString():t)}function y(e,t){if(!e||"object"!=typeof e)return [];if((new TextEncoder).encode(w({sessionId:t,events:[e]})).length<=p)return [e];const s={...e},i=["screenshot","html","dom","fullText","innerHTML","outerHTML"];i.forEach(e=>{s[e]&&delete s[e];});if((new TextEncoder).encode(w({sessionId:t,events:[s]})).length<=p)return [s];return [{type:e.type,timestamp:e.timestamp,url:e.url,pathname:e.pathname,...Object.fromEntries(Object.entries(e).filter(([e,t])=>!i.includes(e)&&"object"!=typeof t&&"string"!=typeof t||"string"==typeof t&&t.length<1e3))}]}class f{constructor({apiKey:e,ingestionUrl:t}){this.monthlyLimitReached=false,this.sessionId="",this.endUserId=null,this.cspBlocked=false,this.requestTimeout=1e4,this.currentBatchSize=100,this.apiKey=e,this.baseUrl=t,this.persistence=new h(e),this.retryQueue=new l(e=>this._sendRequestInternal(e)),this._loadPersistedEvents();}setTrackingContext(e,t){this.sessionId=e,this.endUserId=t;}async _loadPersistedEvents(){const e=this.persistence.getQueue();if(0!==e.length){c(`Loading ${e.length} persisted events from storage`);for(const t of e)try{await this.sendEventsChunked(t.events,t.sessionId,t.endUserId||void 0,t.windowId,t.automaticProperties),this.persistence.removeFromQueue(1);}catch(e){a("Failed to send persisted event, will retry later:",e);}}}async _sendRequestInternal(e){const t="undefined"!=typeof AbortController?new AbortController:null;let s=null;t&&(s=setTimeout(()=>{t.abort();},this.requestTimeout));try{const i=e.estimatedSize||0,n="POST"===e.method&&i<m,r=await fetch(e.url,{method:e.method||"GET",headers:e.headers||{},body:e.body,signal:t?.signal,keepalive:n});s&&clearTimeout(s);const o=await r.text();let a=null;try{a=JSON.parse(o);}catch{}if(e.callback&&e.callback({statusCode:r.status,text:o,json:a}),!r.ok)throw {status:r.status,message:o}}catch(e){if(s&&clearTimeout(s),"AbortError"===e.name)throw {status:0,message:"Request timeout"};throw e}}unload(){this.retryQueue.unload();}checkMonthlyLimit(){return !this.monthlyLimitReached}async init(e,t){if(!this.checkMonthlyLimit())return {sessionId:e,endUserId:t};let s=null,i=null;"undefined"!=typeof window&&(s=window.location.href,i=document.referrer),d("API init called with:",{sessionId:e,userId:t,entryURL:s,referrer:i,baseUrl:this.baseUrl});try{const n=await this.trackedFetch(`${this.baseUrl}/api/ingestion/init`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,Referer:i||""},body:w({sessionId:e,endUserId:t,entryURL:s,referrer:i,sdkVersion:u})});if(d("API init response status:",n.status),!n.ok){if(429===n.status)return this.monthlyLimitReached=!0,{sessionId:e,endUserId:t};const s=await n.text();throw o("API init failed:",n.status,s),new Error(`Failed to initialize ingestion: ${n.statusText} - ${s}`)}const r=await n.json();return !0===r.monthlyLimitReached&&(this.monthlyLimitReached=!0,d("Monthly limit reached detected from server response")),d("API init success:",r),{sessionId:r.sessionId,endUserId:r.endUserId}}catch(e){throw o("API init error:",e),e}}async sendEvents(e,t,s){const i=e.filter(e=>e&&"object"==typeof e),n=await this.trackedFetch(`${this.baseUrl}/api/ingestion/events`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w({sessionId:t,events:i,endUserId:s,sdkVersion:u})});if(!n.ok){if(429===n.status)throw this.monthlyLimitReached=true,new Error("429: Monthly video processing limit reached");throw new Error(`Failed to send events: ${n.statusText}`)} true===(await n.json()).monthlyLimitReached&&(this.monthlyLimitReached=true,d("Monthly limit reached detected from events response"));}async sendEventsChunked(e,t,s,i,n){if(!this.checkMonthlyLimit())return [];try{const r=[];let o=[];for(const a of e)if(a&&"object"==typeof a)if(g(o,a,t)){if(o.length>0){c(`[SDK] Sending chunk with ${o.length} events`);const e=await this.trackedFetch(`${this.baseUrl}/api/ingestion/events`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w({sessionId:t,events:o,endUserId:s,windowId:i,automaticProperties:n,sdkVersion:u})});if(!e.ok){if(429===e.status)return this.monthlyLimitReached=!0,r.flat();throw new Error(`Failed to send events: ${e.statusText}`)}const a=await e.json();!0===a.monthlyLimitReached&&(this.monthlyLimitReached=!0,d("Monthly limit reached detected from chunked events response")),r.push(a),o=[];}o=y(a,t);}else o.push(a);if(o.length>0){const e=await this._sendChunkWithRetry(o,t,s,i,n||o[0]?.automaticProperties);e&&r.push(e);}return r.flat()}catch(r){throw o("Error sending events:",r),this._persistEvents(e,t,s,i,n),r}}async _sendChunkWithRetry(e,t,s,i,n){let r=Math.min(this.currentBatchSize,e.length),o=0;for(;o<e.length;){const c=w({sessionId:t,events:e.slice(o,o+r),endUserId:s,windowId:i,automaticProperties:n,sdkVersion:u}),l=(new TextEncoder).encode(c).length;try{const h=await this.trackedFetch(`${this.baseUrl}/api/ingestion/events`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:c},l);if(!h.ok){if(429===h.status)return this.monthlyLimitReached=!0,this._persistEvents(e.slice(o),t,s,i,n),null;if(413===h.status){a(`413 error: reducing batch size from ${r} to ${Math.max(1,Math.floor(r/2))}`),this.currentBatchSize=Math.max(1,Math.floor(r/2)),r=this.currentBatchSize;continue}return await this.retryQueue.retriableRequest({url:`${this.baseUrl}/api/ingestion/events`,method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:c,estimatedSize:l,callback:e=>{200===e.statusCode&&e.json&&!0===e.json.monthlyLimitReached&&(this.monthlyLimitReached=!0);}}),this._persistEvents(e.slice(o),t,s,i,n),null}const u=await h.json();if(!0===u.monthlyLimitReached&&(this.monthlyLimitReached=!0,d("Monthly limit reached detected from chunked events response")),o+=r,o>=e.length)return u}catch(r){return a("Network error sending chunk, adding to retry queue:",r),await this.retryQueue.retriableRequest({url:`${this.baseUrl}/api/ingestion/events`,method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:c,estimatedSize:l,callback:e=>{200===e.statusCode&&e.json&&true===e.json.monthlyLimitReached&&(this.monthlyLimitReached=true);}}),this._persistEvents(e.slice(o),t,s,i,n),null}}return null}_persistEvents(e,t,s,i,n){0!==e.length&&this.persistence.addToQueue({sessionId:t,events:e,endUserId:s,windowId:i,automaticProperties:n,timestamp:Date.now()});}async sendUserData(e,t,s){try{const i={userId:e,userAttributes:t,sessionId:s,posthogName:t.email||t.name||null};c("Sending user data to server:",i);const n=await this.trackedFetch(`${this.baseUrl}/api/ingestion/user`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w(i)});if(!n.ok)throw new Error(`Failed to send user data: ${n.statusText} with API key: ${this.apiKey}`);const r=await n.json();return c("Server response:",r),r}catch(e){throw o("Error sending user data:",e),e}}async sendUserAuth(e,t,s,i){try{const n=await this.trackedFetch(`${this.baseUrl}/api/ingestion/user/auth`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w({userId:e,userAttributes:t,sessionId:s,authFields:i})});if(!n.ok)throw new Error(`Failed to authenticate user: ${n.statusText} with API key: ${this.apiKey}`);return await n.json()}catch(e){throw o("Error authenticating user:",e),e}}sendBeaconEvents(e,t,s,i,n){const r={sessionId:t,events:e,endUserId:s||null,windowId:i,automaticProperties:n,sdkVersion:u,apiKey:this.apiKey},o=new Blob([w(r)],{type:"application/json"});return navigator.sendBeacon(`${this.baseUrl}/api/ingestion/events`,o)}async sendCustomEvent(e,t,s,i){d("[SDK] Sending custom event",{sessionId:e,eventName:t,eventProperties:s,endUserId:i});try{const n=await this.trackedFetch(`${this.baseUrl}/api/ingestion/customEvent`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w({sessionId:e,eventName:t,eventProperties:s||{},endUserId:i||null})});if(d("[SDK] Custom event response",{status:n.status,statusText:n.statusText}),!n.ok){const e=await n.text();throw o("[SDK] Failed to send custom event",{status:n.status,statusText:n.statusText,errorText:e}),new Error(`Failed to send custom event: ${n.status} ${n.statusText} - ${e}`)}const r=await n.json();return c("[SDK] Custom event success",r),r}catch(i){throw o("[SDK] Error sending custom event",i,{sessionId:e,eventName:t,eventProperties:s}),i}}async sendCustomEventBatch(e,t,s){try{const i=await this.trackedFetch(`${this.baseUrl}/api/ingestion/customEvent/batch`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w({sessionId:e,events:t,endUserId:s||null})});if(!i.ok)throw new Error(`Failed to send custom event batch: ${i.statusText}`);return await i.json()}catch(e){throw o("Error sending custom event batch:",e),e}}async sendLog(e){try{if(c("[SDK] Sending log to server:",{level:e.level,message:e.message.substring(0,50),sessionId:e.sessionId}),!this.baseUrl)return;if(!e.sessionId)return;const t=await fetch(`${this.baseUrl}/api/ingestion/logs`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w(e)});t.ok?c("[SDK] Log sent successfully"):a("[SDK] Failed to send log to server:",t.status,t.statusText);}catch(e){a("[SDK] Failed to send log to server:",e);}}async sendNetworkError(e){try{if(c("[SDK] Sending network error to server:",{errorType:e.errorType,url:e.url.substring(0,50),sessionId:e.sessionId}),!this.baseUrl)return;if(!e.sessionId)return;const t=await fetch(`${this.baseUrl}/api/ingestion/network`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:w(e)});t.ok?c("[SDK] Network error sent successfully"):a("[SDK] Failed to send network error to server:",t.status,t.statusText);}catch(e){a("[SDK] Failed to send network error to server:",e);}}async trackedFetch(e,s,i){const n=Date.now(),r=v1(),o=this.shouldSkipNetworkTracking(e);if(this.cspBlocked&&"POST"===s.method&&"undefined"!=typeof navigator&&"function"==typeof navigator.sendBeacon)return this.trackedFetchWithBeaconFallback(e,s,o);try{const t="undefined"!=typeof AbortController?new AbortController:null;let a=null;t&&(a=setTimeout(()=>{t.abort();},this.requestTimeout));const d="POST"===s.method&&void 0!==i&&i<m,c=await fetch(e,{...s,signal:t?.signal,keepalive:d});a&&clearTimeout(a);const l=Date.now()-n;return c.ok||o||await this.sendNetworkError({requestId:r,url:e,method:s.method||"GET",status:c.status,statusText:c.statusText,duration:l,timestampMs:Date.now(),sessionId:this.sessionId,endUserId:this.endUserId,errorType:this.classifyHttpError(c.status),errorMessage:c.statusText,startTimeMs:n,spanName:`${s.method||"GET"} ${e}`,spanStatus:"error",attributes:{"http.status_code":c.status,"http.status_text":c.statusText}}).catch(()=>{}),c}catch(t){const i=Date.now()-n;if("AbortError"===t.name){const e=new Error("Request timeout");e.name="TimeoutError",t=e;}if(this.isCSPViolation(t)&&"POST"===s.method&&"undefined"!=typeof navigator&&"function"==typeof navigator.sendBeacon)return this.cspBlocked=true,a("[SDK] CSP violation detected, falling back to sendBeacon for future requests"),this.trackedFetchWithBeaconFallback(e,s,o);throw o||await this.sendNetworkError({requestId:r,url:e,method:s.method||"GET",status:null,statusText:null,duration:i,timestampMs:Date.now(),sessionId:this.sessionId,endUserId:this.endUserId,errorType:this.classifyNetworkError(t),errorMessage:t.message,errorName:t.name,startTimeMs:n,spanName:`${s.method||"GET"} ${e}`,spanStatus:"error",attributes:{"error.name":t.name,"error.message":t.message}}).catch(()=>{}),t}}async trackedFetchWithBeaconFallback(e,t,s){try{let s=null,i="";if(t.body)if("string"==typeof t.body)try{s=JSON.parse(t.body),i=t.body;}catch{i=t.body;}else {if(t.body instanceof Blob){a("[SDK] Cannot extract apiKey from Blob body for sendBeacon, using URL param"),e=`${e}${e.includes("?")?"&":"?"}apiKey=${encodeURIComponent(this.apiKey)}`;return navigator.sendBeacon(e,t.body)?new Response(null,{status:200,statusText:"OK",headers:new Headers}):new Response(null,{status:500,statusText:"sendBeacon failed",headers:new Headers})}i=w(t.body),s=t.body;}if(e.includes("/api/ingestion/"))if(s&&"object"==typeof s)s.apiKey=this.apiKey,i=w(s);else if(i)try{const e=JSON.parse(i);e.apiKey=this.apiKey,i=w(e);}catch{e=`${e}${e.includes("?")?"&":"?"}apiKey=${encodeURIComponent(this.apiKey)}`;}else e=`${e}${e.includes("?")?"&":"?"}apiKey=${encodeURIComponent(this.apiKey)}`;else e=`${e}${e.includes("?")?"&":"?"}apiKey=${encodeURIComponent(this.apiKey)}`;const n=i?new Blob([i],{type:"application/json"}):null;return navigator.sendBeacon(e,n)?(c("[SDK] Successfully sent request via sendBeacon (CSP fallback)"),new Response(null,{status:200,statusText:"OK",headers:new Headers})):(a("[SDK] sendBeacon returned false - browser may be throttling"),new Response(null,{status:200,statusText:"OK (sendBeacon best-effort)",headers:new Headers}))}catch(e){return o("[SDK] sendBeacon fallback failed:",e),new Response(null,{status:500,statusText:"Failed to send via sendBeacon",headers:new Headers})}}isCSPViolation(e){const t=(e?.message||"").toLowerCase();return "typeerror"===(e?.name||"").toLowerCase()&&t.includes("failed to fetch")||t.includes("content security policy")||t.includes("csp")||t.includes("violates")||t.includes("refused to connect")&&t.includes("violates")}shouldSkipNetworkTracking(e){if(!e||!this.baseUrl)return false;try{const t=new URL(e),s=new URL(this.baseUrl);return !(t.origin!==s.origin||!t.pathname.startsWith("/api/ingestion/"))||!!e.includes(this.baseUrl)}catch(t){return e.includes(this.baseUrl)}}classifyHttpError(e){return e>=400&&e<500?"client_error":e>=500?"server_error":"unknown_error"}classifyNetworkError(e){const t=e.message||"",s=e.name||"";return this.isCSPViolation(e)?"csp_violation":t.includes("ERR_BLOCKED_BY_CLIENT")||t.includes("ERR_BLOCKED_BY_RESPONSE")||t.includes("blocked:other")||t.includes("net::ERR_BLOCKED_BY_CLIENT")||t.includes("net::ERR_BLOCKED_BY_RESPONSE")||"TypeError"===s&&t.includes("Failed to fetch")&&(t.includes("blocked")||t.includes("ERR_BLOCKED"))?"blocked_by_client":t.includes("CORS")||t.includes("Access-Control")?"cors_error":t.includes("timeout")||"TimeoutError"===s?"timeout_error":t.includes("Failed to fetch")||t.includes("NetworkError")?"network_error":"unknown_error"}}class v{constructor(e){if(this.redactedText="[REDACTED]",this.unredactedFields=new Set,this.redactedFields=new Set,this.redactionMode="privacy-first",this.excludeSelectors=['[data-no-redact="true"]',".human-behavior-no-redact"],e?.redactedText&&(this.redactedText=e.redactedText),e?.excludeSelectors&&(this.excludeSelectors=[...this.excludeSelectors,...e.excludeSelectors]),e?.redactionStrategy)if(this.redactionMode=e.redactionStrategy.mode,"privacy-first"===this.redactionMode)e.redactionStrategy.unredactFields&&this.setFieldsToUnredact(e.redactionStrategy.unredactFields);else {const t=['input[type="password"]','[data-hb-redact="true"]'],s=e.redactionStrategy.redactFields&&e.redactionStrategy.redactFields.length>0?e.redactionStrategy.redactFields:t;this.setFieldsToRedact(s);}e?.legacyRedactFields&&this.setFieldsToUnredact(e.legacyRedactFields),e?.userFields&&this.setFieldsToUnredact(e.userFields);}setFieldsToRedact(e){this.redactedFields.clear();['input[type="password"]','input[type="password" i]','[type="password"]','[type="password" i]',...e].forEach(e=>{this.redactedFields.add(e);}),this.redactedFields.size>0?c(`Redaction: Active for ${this.redactedFields.size} field(s):`,Array.from(this.redactedFields)):c("Redaction: No fields to redact"),this.applyRedactionClasses();}setFieldsToUnredact(e){this.unredactedFields.clear();const t=e.filter(e=>!this.isPasswordSelector(e)||(a(`Cannot unredact password field: ${e} - Password fields are always protected`),false));t.forEach(e=>this.unredactedFields.add(e)),t.length>0?c(`Unredaction: Active for ${t.length} field(s):`,t):c("Unredaction: No valid fields to unredact"),this.applyUnredactionClasses();}redactFields(e){e.forEach(e=>{this.unredactedFields.delete(e);}),this.unredactedFields.size>0?c(`Unredaction: Removed ${e.length} field(s), ${this.unredactedFields.size} remaining:`,Array.from(this.unredactedFields)):c("Unredaction: All fields redacted"),this.applyUnredactionClasses();}clearUnredactedFields(){this.unredactedFields.clear(),c("Unredaction: All fields cleared, everything redacted"),this.removeUnredactionClasses();}hasUnredactedFields(){return this.unredactedFields.size>0}getRedactionMode(){return this.redactionMode}getUnredactedFields(){return Array.from(this.unredactedFields)}getMaskTextSelector(){return "privacy-first"===this.redactionMode?0===this.unredactedFields.size?null:Array.from(this.unredactedFields).join(","):0===this.redactedFields.size?null:Array.from(this.redactedFields).join(",")}applyRedactionClasses(){0!==this.redactedFields.size&&("undefined"!=typeof document&&"loading"!==document.readyState?this.redactedFields.forEach(e=>{try{const t=document.querySelectorAll(e);t.forEach(e=>{e&&e.classList&&e.classList.add("rr-mask");}),c(`Added rr-mask class to ${t.length} element(s) for selector: ${e}`);}catch(t){a(`Invalid selector: ${e}`);}}):c("DOM not ready, deferring redaction class application"));}applyUnredactionClasses(){0!==this.unredactedFields.size&&("undefined"!=typeof document&&"loading"!==document.readyState?this.unredactedFields.forEach(e=>{try{const t=document.querySelectorAll(e);t.forEach(e=>{e&&e.classList&&e.classList.remove("rr-mask");}),c(`Removed rr-mask class from ${t.length} element(s) for selector: ${e}`);}catch(t){a(`Invalid selector: ${e}`);}}):c("DOM not ready, deferring unredaction class application"));}removeUnredactionClasses(){c("Unredaction classes removed");}isPasswordSelector(e){return ['input[type="password"]','input[type="password" i]','[type="password"]','[type="password" i]'].some(t=>e.toLowerCase().includes(t.toLowerCase().replace(/[\[\]]/g,"")))}getOriginalValue(e){if(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement)return e.value}isElementUnredacted(e){return this.shouldUnredactElement(e)}shouldUnredactElement(e){if("privacy-first"===this.redactionMode){if(0===this.unredactedFields.size)return false;for(const t of this.unredactedFields)try{if(e.matches(t))return !0}catch(e){a(`Invalid selector: ${t}`);}return false}if(0===this.redactedFields.size)return true;for(const t of this.redactedFields)try{if(e.matches(t))return !1}catch(e){a(`Invalid selector: ${t}`);}return true}}new v;const _="undefined"!=typeof window;function S(){if(!_)return "unknown";const e=navigator.userAgent.toLowerCase(),t=window.screen.width,s=window.screen.height;return /mobile|android|iphone|ipad|ipod|blackberry|windows phone/i.test(e)?/ipad/i.test(e)||t>=768&&s>=1024?"tablet":"mobile":/windows|macintosh|linux/i.test(e)?"desktop":"unknown"}function k(e){try{return new URL(e).hostname}catch{return ""}}function I(){if(!_)return {device_type:"unknown",browser:"unknown",browser_version:"unknown",os:"unknown",os_version:"unknown",screen_resolution:"unknown",viewport_size:"unknown",color_depth:0,timezone:"unknown",language:"unknown",languages:[]};const{browser:e,browser_version:t}=function(){if(!_)return {browser:"unknown",browser_version:"unknown"};const e=navigator.userAgent;if(/chrome/i.test(e)&&!/edge/i.test(e)){const t=e.match(/chrome\/(\d+)/i);return {browser:"chrome",browser_version:t?t[1]:"unknown"}}if(/firefox/i.test(e)){const t=e.match(/firefox\/(\d+)/i);return {browser:"firefox",browser_version:t?t[1]:"unknown"}}if(/safari/i.test(e)&&!/chrome/i.test(e)){const t=e.match(/version\/(\d+)/i);return {browser:"safari",browser_version:t?t[1]:"unknown"}}if(/edge/i.test(e)){const t=e.match(/edge\/(\d+)/i);return {browser:"edge",browser_version:t?t[1]:"unknown"}}if(/msie|trident/i.test(e)){const t=e.match(/msie (\d+)/i)||e.match(/rv:(\d+)/i);return {browser:"ie",browser_version:t?t[1]:"unknown"}}return {browser:"unknown",browser_version:"unknown"}}(),{os:s,os_version:i}=function(){if(!_)return {os:"unknown",os_version:"unknown"};const e=navigator.userAgent;if(/windows/i.test(e)){const t=e.match(/windows nt (\d+\.\d+)/i);let s="unknown";if(t){const e=parseFloat(t[1]);s=10===e?"10":6.3===e?"8.1":6.2===e?"8":6.1===e?"7":t[1];}return {os:"windows",os_version:s}}if(/macintosh|mac os x/i.test(e)){const t=e.match(/mac os x (\d+[._]\d+)/i);return {os:"macos",os_version:t?t[1].replace("_","."):"unknown"}}if(/iphone|ipad|ipod/i.test(e)){const t=e.match(/os (\d+[._]\d+)/i);return {os:"ios",os_version:t?t[1].replace("_","."):"unknown"}}if(/android/i.test(e)){const t=e.match(/android (\d+\.\d+)/i);return {os:"android",os_version:t?t[1]:"unknown"}}return /linux/i.test(e)?{os:"linux",os_version:"unknown"}:{os:"unknown",os_version:"unknown"}}();return {device_type:S(),browser:e,browser_version:t,os:s,os_version:i,screen_resolution:`${window.screen.width}x${window.screen.height}`,viewport_size:`${window.innerWidth}x${window.innerHeight}`,color_depth:window.screen.colorDepth,timezone:Intl.DateTimeFormat().resolvedOptions().timeZone,language:navigator.language,languages:[...navigator.languages||[navigator.language]],raw_user_agent:navigator.userAgent}}function b(){if(!_)return {current_url:"",pathname:"",search:"",hash:"",title:"",referrer:"",referrer_domain:"",initial_referrer:"",initial_referrer_domain:""};const e=window.location.href,t=document.referrer,s=function(e){const t=new URL(e),s={};return ["utm_source","utm_medium","utm_campaign","utm_term","utm_content"].forEach(e=>{const i=t.searchParams.get(e);i&&(s[e]=i);}),s}(e);return {current_url:e,pathname:window.location.pathname,search:window.location.search,hash:window.location.hash,title:document.title,referrer:t,referrer_domain:k(t),initial_referrer:t,initial_referrer_domain:k(t),initial_host:window.location.hostname,...s}}function T(){return {...I(),...b()}}function E(){if(!_)return {};const e=b();return {initial_referrer:e.initial_referrer,initial_referrer_domain:e.initial_referrer_domain,initial_url:e.current_url,initial_pathname:e.pathname,initial_utm_source:e.utm_source,initial_utm_medium:e.utm_medium,initial_utm_campaign:e.utm_campaign,initial_utm_term:e.utm_term,initial_utm_content:e.utm_content}}function U(){if(!_)return {};const e=b();return {current_url:e.current_url,pathname:e.pathname,search:e.search,hash:e.hash,title:e.title,referrer:e.referrer,referrer_domain:e.referrer_domain,utm_source:e.utm_source,utm_medium:e.utm_medium,utm_campaign:e.utm_campaign,utm_term:e.utm_term,utm_content:e.utm_content}}class C{constructor(e={}){this.sessionProperties={},this.userProperties={},this.initialProperties={},this.isInitialized=false,this.config={enableAutomaticProperties:true,enableSessionProperties:true,enableUserProperties:true,propertyDenylist:[],...e},this.automaticProperties=T(),this.initialize();}initialize(){this.isInitialized||(this.initialProperties=E(),this.loadSessionProperties(),this.isInitialized=true);}getEventProperties(e={}){const t={...e};return this.config.enableAutomaticProperties&&Object.assign(t,this.getAutomaticProperties()),this.config.enableSessionProperties&&Object.assign(t,this.sessionProperties),this.config.enableUserProperties&&Object.assign(t,this.userProperties),this.sessionProperties.$initial_properties_captured||(Object.assign(t,this.initialProperties),this.setSessionProperty("$initial_properties_captured",true)),this.applyDenylist(t),t}getAutomaticProperties(){return {...this.automaticProperties,...U()}}getAutomaticPropertiesWithGeoIP(e={}){return {...this.automaticProperties,...U(),...e}}setSessionProperty(e,t){this.sessionProperties[e]=t,this.saveSessionProperties();}setSessionProperties(e){Object.assign(this.sessionProperties,e),this.saveSessionProperties();}getSessionProperty(e){return this.sessionProperties[e]}removeSessionProperty(e){delete this.sessionProperties[e],this.saveSessionProperties();}setUserProperty(e,t){this.userProperties[e]=t;}setUserProperties(e){Object.assign(this.userProperties,e);}getUserProperty(e){return this.userProperties[e]}removeUserProperty(e){delete this.userProperties[e];}setOnce(e,t,s="user"){"session"===s?e in this.sessionProperties||this.setSessionProperty(e,t):e in this.userProperties||this.setUserProperty(e,t);}clearSessionProperties(){this.sessionProperties={},this.saveSessionProperties();}clearUserProperties(){this.userProperties={};}reset(){this.clearSessionProperties(),this.clearUserProperties(),this.initialProperties={},this.isInitialized=false,this.initialize();}loadSessionProperties(){if("undefined"!=typeof sessionStorage)try{const e=sessionStorage.getItem("hb_session_properties");e&&(this.sessionProperties=JSON.parse(e));}catch(e){console.warn("Failed to load session properties:",e);}}saveSessionProperties(){if("undefined"!=typeof sessionStorage)try{sessionStorage.setItem("hb_session_properties",JSON.stringify(this.sessionProperties));}catch(e){console.warn("Failed to save session properties:",e);}}applyDenylist(e){this.config.propertyDenylist&&0!==this.config.propertyDenylist.length&&this.config.propertyDenylist.forEach(t=>{delete e[t];});}updateAutomaticProperties(){this.automaticProperties=T();}getAllProperties(){return {automatic:this.getAutomaticProperties(),session:{...this.sessionProperties},user:{...this.userProperties},initial:{...this.initialProperties}}}}const R="undefined"!=typeof window;class P{get isTrackerStarted(){return this.isStarted}setupDomReadyHandler(){if(R)if("complete"===document.readyState||"interactive"===document.readyState)this.onDomReady();else if(document.addEventListener){document.addEventListener("DOMContentLoaded",()=>this.onDomReady(),{capture:false});const e=setInterval(()=>{"interactive"!==document.readyState&&"complete"!==document.readyState||(clearInterval(e),this.onDomReady());},10);setTimeout(()=>clearInterval(e),5e3);}else this.onDomReady();else this.onDomReady();}onDomReady(){this.isDomReady||(this.isDomReady=true,c("π― DOM is ready, processing queued requests"),this.requestQueue.forEach(e=>{this.processRequest(e);}),this.requestQueue=[],this.domReadyHandlers.forEach(e=>e()),this.domReadyHandlers=[]);}queueRequest(e){this.isDomReady?this.processRequest(e):this.requestQueue.push(e);}async processRequest(e){switch(c("Processing queued request:",e),e.type){case "addEvent":await this.addEvent(e.event);break;case "identifyUser":await this.identifyUser(e.userProperties);break;case "trackPageView":this.trackPageView();break;default:a("Unknown request type:",e.type);}}registerDomReadyHandler(e){this.isDomReady?e():this.domReadyHandlers.push(e);}static init(e,t){if(R&&false!==t?.suppressConsoleErrors){const e=console.error;console.error=(...t)=>{const s=t.join(" ");s.includes("SecurityError: Failed to execute 'toDataURL'")||s.includes("Tainted canvases may not be exported")||s.includes("Cannot inline img src=")||s.includes("Cross-Origin")||s.includes("CORS")||s.includes("Access-Control-Allow-Origin")||s.includes("Failed to load resource")||s.includes("net::ERR_BLOCKED_BY_CLIENT")||s.includes("NetworkError when attempting to fetch resource")||s.includes("Failed to fetch")||s.includes("TypeError: NetworkError")||s.includes("HumanBehavior ERROR")||s.includes("Failed to track custom event")||s.includes("Error sending custom event")||e.apply(console,t);};const t=console.warn;console.warn=(...e)=>{const s=e.join(" ");s.includes("Cannot inline img src=")||s.includes("Cross-Origin")||s.includes("CORS")||s.includes("Access-Control-Allow-Origin")||s.includes("Failed to load resource")||s.includes("net::ERR_BLOCKED_BY_CLIENT")||s.includes("NetworkError when attempting to fetch resource")||s.includes("Failed to fetch")||s.includes("Custom event network error")||s.includes("Request blocked by ad blocker")||t.apply(console,e);},window.addEventListener("error",e=>{const t=e.message||"";if(t.includes("SecurityError")||t.includes("Tainted canvases")||t.includes("toDataURL")||t.includes("Cross-Origin")||t.includes("CORS")||t.includes("NetworkError")||t.includes("Failed to fetch"))return e.preventDefault(),false});}if(R&&window.__humanBehaviorGlobalTracker)return c("Tracker already initialized, returning existing instance"),window.__humanBehaviorGlobalTracker;t?.logLevel&&this.configureLogging({level:t.logLevel});const s=new P(e,t?.ingestionUrl,{enableAutomaticProperties:t?.enableAutomaticProperties,propertyDenylist:t?.propertyDenylist,redactionStrategy:t?.redactionStrategy,redactFields:t?.redactFields,maxQueueSize:t?.maxQueueSize,enableConsoleTracking:t?.enableConsoleTracking,enableNetworkTracking:t?.enableNetworkTracking});return s.recordCanvas=t?.recordCanvas??false,t?.redactFields&&s.setUnredactedFields(t.redactFields),false!==t?.enableAutomaticTracking&&s.setupAutomaticTracking(t?.automaticTrackingOptions),s.start(),s}constructor(e,s,i){if(this.eventQueue=[],this._sessionActivityTimestamp=null,this._sessionStartTimestamp=null,this.userProperties={},this.isProcessing=false,this.flushInterval=null,this.FLUSH_INTERVAL_MS=3e3,this.endUserId=null,this.initialized=false,this.initializationPromise=null,this.monthlyLimitReached=false,this.isDomReady=false,this.requestQueue=[],this.domReadyHandlers=[],this.originalConsole=null,this.consoleTrackingEnabled=false,this.originalFetch=null,this.networkTrackingEnabled=false,this.enableConsoleTrackingFlag=true,this.enableNetworkTrackingFlag=true,this.navigationTrackingEnabled=false,this.currentUrl="",this.previousUrl="",this.originalPushState=null,this.originalReplaceState=null,this.navigationListeners=[],this._connectionBlocked=false,this.recordInstance=null,this.sessionStartTime=Date.now(),this.rrwebRecord=null,this.fullSnapshotTimeout=null,this.recordCanvas=false,this.isStarted=false,this._isIdle="unknown",this._lastActivityTimestamp=Date.now(),this.IDLE_THRESHOLD_MS=3e5,this.rageClickTracker={clicks:[]},this.RAGE_CLICK_THRESHOLD_PX=30,this.RAGE_CLICK_TIMEOUT_MS=1e3,this.RAGE_CLICK_CLICK_COUNT=3,this.deadClickTracker={pendingClicks:new Map},this.DEAD_CLICK_SCROLL_THRESHOLD_MS=100,this.DEAD_CLICK_SELECTION_THRESHOLD_MS=100,this.DEAD_CLICK_MUTATION_THRESHOLD_MS=2e3,this.DEAD_CLICK_ABSOLUTE_TIMEOUT_MS=1400,!e)throw new Error("Human Behavior API Key is required");const n=s||"https://ingest.humanbehavior.co";if(this.api=new f({apiKey:e,ingestionUrl:n}),this.apiKey=e,this.ingestionUrl=n,this.MAX_QUEUE_SIZE=i?.maxQueueSize??1e3,this.enableConsoleTrackingFlag=false!==i?.enableConsoleTracking,this.enableNetworkTrackingFlag=false!==i?.enableNetworkTracking,this.redactionManager=new v({redactionStrategy:i?.redactionStrategy,legacyRedactFields:i?.redactFields}),this.propertyManager=new C({enableAutomaticProperties:false!==i?.enableAutomaticProperties,propertyDenylist:i?.propertyDenylist||[]}),R){const e="human_behavior_end_user_id",s=this.getCookie(e);this.endUserId=s||v1(),s?c(`Reusing existing endUserId: ${this.endUserId}`):(this.setCookie(e,this.endUserId,365),c(`Generated new endUserId: ${this.endUserId}`));}else this.endUserId=v1();if(R){const e=this.apiKey||"default";this._window_id_storage_key=`human_behavior_${e}_window_id`,this._primary_window_exists_storage_key=`human_behavior_${e}_primary_window_exists`,this.sessionId=this.getOrCreateSessionId(),this.windowId=this.getOrCreateWindowId(),this.currentUrl=window.location.href,window.__humanBehaviorGlobalTracker=this,this.setupWindowUnloadListener();}else this._window_id_storage_key="",this._primary_window_exists_storage_key="",this.sessionId=v1(),this.windowId=v1();this.api.setTrackingContext(this.sessionId,this.endUserId),this.initializationPromise=this.init().catch(e=>{o("Initialization failed:",e);});}async init(){try{R?(this.setupPageUnloadHandler(),this.setupNavigationTracking()):d("HumanBehaviorTracker initialized in server environment. Session tracking is disabled."),this.initialized=!0,d(`HumanBehaviorTracker initialized with sessionId: ${this.sessionId}, endUserId: ${this.endUserId}`);}catch(e){o("Failed to initialize HumanBehaviorTracker:",e),this.initialized=true;}}async ensureInitialized(){this.initializationPromise&&await this.initializationPromise;}setupNavigationTracking(){if(!R||this.navigationTrackingEnabled)return;this.navigationTrackingEnabled=true,c("Setting up navigation tracking"),this.originalPushState=history.pushState,this.originalReplaceState=history.replaceState,history.pushState=(...e)=>{this.previousUrl=this.currentUrl,this.currentUrl=window.location.href,this.originalPushState.apply(history,e),this.trackNavigationEvent("pushState",this.previousUrl,this.currentUrl),this.takeFullSnapshot();},history.replaceState=(...e)=>{this.previousUrl=this.currentUrl,this.currentUrl=window.location.href,this.originalReplaceState.apply(history,e),this.trackNavigationEvent("replaceState",this.previousUrl,this.currentUrl),this.takeFullSnapshot();};const e=()=>{this.previousUrl=this.currentUrl,this.currentUrl=window.location.href,this.trackNavigationEvent("popstate",this.previousUrl,this.currentUrl),this.takeFullSnapshot();};window.addEventListener("popstate",e),this.navigationListeners.push(()=>{window.removeEventListener("popstate",e);});const t=()=>{this.previousUrl=this.currentUrl,this.currentUrl=window.location.href,this.trackNavigationEvent("hashchange",this.previousUrl,this.currentUrl);};window.addEventListener("hashchange",t),this.navigationListeners.push(()=>{window.removeEventListener("hashchange",t);}),this.trackNavigationEvent("pageLoad","",this.currentUrl);}async trackNavigationEvent(e,t,s){if(this.initialized)try{const i={type:e,from:t,to:s,timestamp:(new Date).toISOString(),pathname:window.location.pathname,search:window.location.search,hash:window.location.hash,referrer:document.referrer};if(await this.addEvent({type:5,data:{payload:{eventType:"navigation",...i}},timestamp:Date.now()}),"pageLoad"===e||"pushState"===e||"replaceState"===e||"popstate"===e||"hashchange"===e){const s={url:window.location.href,fromUrl:t,navigationType:e,pathname:window.location.pathname,search:window.location.search,hash:window.location.hash,referrer:document.referrer,timestamp:Date.now()};await this.customEvent("$page_viewed",s);}c(`Navigation tracked: ${e} from ${t} to ${s}`);}catch(e){o("Failed to track navigation event:",e);}}async trackPageView(e){if(this.initialized){this.propertyManager.updateAutomaticProperties();try{const t={url:e||window.location.href,pathname:window.location.pathname,search:window.location.search,hash:window.location.hash,referrer:document.referrer,timestamp:(new Date).toISOString()},s=this.propertyManager.getEventProperties(t);await this.addEvent({type:5,data:{payload:{eventType:"pageview",...s}},timestamp:Date.now()}),c(`Pageview tracked: ${t.url}`);}catch(e){o("Failed to track pageview event:",e);}}}async customEvent(e,s){this.endUserId||(a(`endUserId not available, using anonymous ID for event: ${e}`),this.endUserId=v1()),R&&this.checkAndRefreshSession();const i=this.propertyManager.getEventProperties(s);try{await this.api.sendCustomEvent(this.sessionId,e,i,this.endUserId),c(`Custom event tracked: ${e}`,i);}catch(t){o("Failed to track custom event:",t),t.message?.includes("500")||t.message?.includes("Internal Server Error")||t.message?.includes("Failed to send custom event")?a("Custom event endpoint failed, using fallback"):t.message?.includes("ERR_BLOCKED_BY_CLIENT")?a("Custom event request blocked by ad blocker, using fallback"):t.message?.includes("Failed to fetch")&&a("Custom event network error, using fallback");try{const t={eventName:e,properties:i||{},timestamp:(new Date).toISOString(),url:window.location.href,pathname:window.location.pathname};await this.addEvent({type:5,data:{payload:{eventType:"custom",...t}},timestamp:Date.now()}),c(`Custom event added to event stream as fallback: ${e}`);}catch(e){o("Failed to add custom event to event stream as fallback:",e);}}}setupAutomaticTracking(e){if(!R)return;const t={trackButtons:false!==e?.trackButtons,trackLinks:false,trackForms:false!==e?.trackForms,includeText:false!==e?.includeText,includeClasses:e?.includeClasses||false};c("Setting up automatic tracking with config:",t),t.trackButtons&&this.setupAutomaticButtonTracking(t),t.trackForms&&this.setupAutomaticFormTracking(t),this.setupRageClickDetection();}setupAutomaticButtonTracking(e){document.addEventListener("click",async t=>{const s=t.target;if("BUTTON"===s.tagName||s.closest("button")){const t="BUTTON"===s.tagName?s:s.closest("button"),i={buttonId:t.id||null,buttonType:t.type||"button",page:window.location.pathname,timestamp:Date.now()};e.includeText&&(i.buttonText=t.textContent?.trim()||null),e.includeClasses&&(i.buttonClass=t.className||null),Object.keys(i).forEach(e=>{null===i[e]&&delete i[e];}),await this.customEvent("$button_clicked",i);}});}setupRageClickDetection(){R&&document.addEventListener("click",async e=>{const t=e.target,s=e.clientX,i=e.clientY,n=Date.now();if(this.isRageClick(s,i,n,t)){const e=t.closest('button, a, [role="button"], [role="link"]')||t,r={x:s,y:i,page:window.location.pathname,element:e.tagName.toLowerCase(),clickCount:this.RAGE_CLICK_CLICK_COUNT,timestamp:n};e.id&&(r.elementId=e.id),e.className&&(r.elementClass=e.className),e.textContent&&(r.elementText=e.textContent.trim().substring(0,100)),Object.keys(r).forEach(e=>{null!==r[e]&&void 0!==r[e]||delete r[e];}),await this.customEvent("$rageclick",r),this.rageClickTracker.clicks=[];}});}isRageClick(e,t,s,i){const n=this.rageClickTracker.clicks,r=n[n.length-1];if(r&&Math.abs(e-r.x)+Math.abs(t-r.y)<this.RAGE_CLICK_THRESHOLD_PX&&s-r.timestamp<this.RAGE_CLICK_TIMEOUT_MS){if(n.push({x:e,y:t,timestamp:s,element:i}),n.length>=this.RAGE_CLICK_CLICK_COUNT)return true}else this.rageClickTracker.clicks=[{x:e,y:t,timestamp:s,element:i}];return false}isInteractiveElement(e){const t=e.tagName.toLowerCase();if("button"===t||"a"===t)return true;if(["input","select","textarea"].includes(t))return true;const s=e.getAttribute("role");if(s&&["button","link","tab","menuitem","checkbox","radio"].includes(s))return true;if(e.onclick||e.getAttribute("onclick"))return true;try{if("pointer"===window.getComputedStyle(e).cursor)return !0}catch(e){}return !!e.closest('button, a, [role="button"], [role="link"], [role="tab"], [role="menuitem"]')}setupAutomaticLinkTracking(e){}setupAutomaticFormTracking(e){document.addEventListener("submit",async t=>{const s=t.target,i=new FormData(s),n={formId:s.id||null,formAction:s.action||null,formMethod:s.method||"get",fields:Array.from(i.keys()),page:window.location.pathname,timestamp:Date.now()};e.includeClasses&&(n.formClass=s.className||null),Object.keys(n).forEach(e=>{null===n[e]&&delete n[e];}),await this.customEvent("$form_submitted",n);});}cleanupNavigationTracking(){this.navigationTrackingEnabled&&(this.originalPushState&&(history.pushState=this.originalPushState),this.originalReplaceState&&(history.replaceState=this.originalReplaceState),this.navigationListeners.forEach(e=>e()),this.navigationListeners=[],this.navigationTrackingEnabled=false,c("Navigation tracking cleaned up"));}static logToStorage(e){d(e);}static configureLogging(e){i.setConfig({level:{none:0,error:1,warn:2,info:3,debug:4}[e.level||"error"],enableConsole:false!==e.enableConsole,enableStorage:e.enableStorage||false});}enableConsoleTracking(){R&&!this.consoleTrackingEnabled&&(this.originalConsole={log:console.log,warn:console.warn,error:console.error},console.log=(...e)=>{this.trackConsoleEvent("log",e),this.originalConsole.log(...e);},console.warn=(...e)=>{this.trackConsoleEvent("warn",e),this.originalConsole.warn(...e);},console.error=(...e)=>{this.trackConsoleEvent("error",e),this.originalConsole.error(...e);},this.consoleTrackingEnabled=true,c("Console tracking enabled"));}enableNetworkTracking(){R&&!this.networkTrackingEnabled&&"undefined"!=typeof fetch&&(this.originalFetch=window.fetch.bind(window),window.fetch=async(e,s)=>{const i=Date.now(),n=v1(),r="string"==typeof e?e:e instanceof URL?e.toString():e.url,o=(s?.method||("object"==typeof e&&"method"in e?e.method:void 0)||"GET").toUpperCase(),a=this.shouldSkipNetworkTracking(r),d=1e4;let c=null,l=false;a||(c=setTimeout(()=>{const e=Date.now()-i;l||(l=true,this.api.sendNetworkError({requestId:n,url:r,method:o,status:null,statusText:null,duration:e,timestampMs:Date.now(),sessionId:this.sessionId,endUserId:this.endUserId,errorType:"long_loading",errorMessage:`Request took longer than 10000ms (${e}ms elapsed)`,startTimeMs:i,spanName:`${o} ${r}`,spanStatus:"slow",attributes:{"http.method":o,"http.url":r,"request.duration_ms":e,"request.long_loading_threshold_ms":d}}).catch(()=>{}));},d));try{const t=await this.originalFetch(e,s),d=Date.now()-i;return c&&clearTimeout(c),t.ok||a||this.api.sendNetworkError({requestId:n,url:r,method:o,status:t.status,statusText:t.statusText,duration:d,timestampMs:Date.now(),sessionId:this.sessionId,endUserId:this.endUserId,errorType:this.classifyHttpError(t.status),errorMessage:t.statusText,startTimeMs:i,spanName:`${o} ${r}`,spanStatus:"error",attributes:{"http.status_code":t.status,"http.status_text":t.statusText}}).catch(()=>{}),t}catch(e){const t=Date.now()-i;throw c&&clearTimeout(c),a||this.api.sendNetworkError({requestId:n,url:r,method:o,status:null,statusText:null,duration:t,timestampMs:Date.now(),sessionId:this.sessionId,endUserId:this.endUserId,errorType:this.classifyNetworkError(e),errorMessage:e.message,errorName:e.name,startTimeMs:i,spanName:`${o} ${r}`,spanStatus:"error",attributes:{"error.name":e.name,"error.message":e.message}}).catch(()=>{}),e}},this.networkTrackingEnabled=true,c("Network tracking enabled"));}enablePageLoadTracking(){R&&"undefined"!=typeof window&&("complete"===document.readyState?this.trackPageLoad():window.addEventListener("load",()=>{this.trackPageLoad();}),c("Page load tracking enabled"));}trackPageLoad(){if(R&&"undefined"!=typeof performance)try{const e=performance.getEntriesByType("navigation")[0];if(!e)return;const s=e.loadEventEnd-e.fetchStart;if(s>3e3){const i=v1(),n=e.domContentLoadedEventEnd-e.fetchStart,r=e.domComplete-e.fetchStart;this.api.sendNetworkError({requestId:i,url:window.location.href,method:"GET",status:200,statusText:"OK",duration:s,timestampMs:e.loadEventEnd+performance.timeOrigin,sessionId:this.sessionId,endUserId:this.endUserId,errorType:"slow_page_load",errorMessage:`Page load took ${s}ms`,startTimeMs:e.fetchStart+performance.timeOrigin,spanName:"page_load",spanStatus:"slow",attributes:{"page.url":window.location.href,"page.load_time":s,"page.dom_content_loaded":n,"page.dom_complete":r,"page.fetch_start":e.fetchStart,"page.response_start":e.responseStart-e.fetchStart,"page.response_end":e.responseEnd-e.fetchStart}}).catch(()=>{});}}catch(e){a("Failed to track page load:",e);}}shouldSkipNetworkTracking(e){if(!e||!this.ingestionUrl)return false;try{const t=new URL(e),s=new URL(this.ingestionUrl);return !(t.origin!==s.origin||!t.pathname.startsWith("/api/ingestion/"))||!!e.includes(this.ingestionUrl)}catch(t){return e.includes(this.ingestionUrl)}}classifyHttpError(e){return e>=400&&e<500?"client_error":e>=500?"server_error":"unknown_error"}classifyNetworkError(e){const t=e.message||"",s=e.name||"";return t.includes("blocked")||t.includes("ERR_BLOCKED_BY_CLIENT")||t.includes("net::ERR_BLOCKED_BY_CLIENT")||"TypeError"===s&&t.includes("Failed to fetch")?"blocked_by_client":t.includes("CORS")||t.includes("Cross-Origin")||t.includes("Access-Control-Allow-Origin")||"TypeError"===s&&t.includes("CORS")?"cors_error":t.includes("timeout")||t.includes("TIMEOUT")||t.includes("NetworkError")||"NetworkError"===s?"network_error":t.includes("abort")||"AbortError"===s?"aborted":"unknown_error"}disableConsoleTracking(){R&&this.consoleTrackingEnabled&&(this.originalConsole&&(console.log=this.originalConsole.log,console.warn=this.originalConsole.warn,console.error=this.originalConsole.error),this.consoleTrackingEnabled=false,c("Console tracking disabled"));}trackConsoleEvent(e,t){if(this.initialized)if("log"!==e)try{if(r())return void(this.originalConsole&&this.originalConsole[e](...t));const s=(new Error).stack||"";if(this.isSDKStackFrame(s))return void(this.originalConsole&&this.originalConsole[e](...t));const i={level:e,message:t.map(e=>"object"==typeof e?JSON.stringify(e):String(e)).join(" "),timestampMs:Date.now(),url:R?window.location.href:"",userAgent:R?navigator.userAgent:"",stack:s,sessionId:this.sessionId,endUserId:this.endUserId};this.api.sendLog(i).catch(e=>{this.addEvent({type:5,data:{payload:{eventType:"console",...i}},timestamp:Date.now()}).catch(()=>{});});}catch(e){o("Error in trackConsoleEvent:",e);}else this.originalConsole&&this.originalConsole.log(...t);}isSDKStackFrame(e){if(!e)return false;const t=["humanbehavior-js","@humanbehavior/core","@humanbehavior/browser","tracker.ts","api.ts","logger.ts","utils/logger","packages/core","packages/browser","index.mjs","index.js"],s=e.split("\n");e.toLowerCase();let i=false;for(let e=0;e<s.length;e++){const n=s[e].trim().toLowerCase();if(!n||"error"===n||n.startsWith("error:"))continue;if(!t.some(e=>n.includes(e.toLowerCase()))){i=true;break}}return !i}setupPageUnloadHandler(){if(!R)return;c("Setting up page unload handler"),window.addEventListener("visibilitychange",()=>{"hidden"===document.visibilityState&&(c("Page hidden - sending pending events"),this.flushEvents());});const e="onpagehide"in window?"pagehide":"beforeunload";window.addEventListener(e,()=>{c("Page unloading - sending final events via sendBeacon");const e=[...this.eventQueue],t=e.some(e=>e&&2===e.type);if(!t&&R&&window.__hb_pending_snapshots){const t=window.__hb_pending_snapshots;Array.isArray(t)&&t.length>0&&(c("β οΈ Including pending FullSnapshot(s) in sendBeacon for short session"),e.unshift(...t),delete window.__hb_pending_snapshots);}if(!t&&e.length>0&&a("β οΈ No FullSnapshot in queue on unload - if session was very short, initial snapshot may be lost"),e.length>0&&this.api)try{const t=this.propertyManager.getAutomaticProperties();this.api.sendBeaconEvents(e,this.sessionId,this.endUserId||void 0,this.windowId,t),this.eventQueue=[];}catch(e){a("Failed to send events via sendBeacon on unload:",e);}this.api&&this.api.unload();});const t=()=>{localStorage.setItem("human_behavior_last_activity",Date.now().toString());};window.addEventListener("click",t),window.addEventListener("keydown",t),window.addEventListener("scroll",t),window.addEventListener("mousemove",t);}viewLogs(){try{const e=i.getLogs();d("HumanBehavior Logs:",e),i.clearLogs();}catch(e){o("Failed to read logs:",e);}}async identifyUser({userProperties:e}){const t=this.endUserId;this.userProperties=e,c("Identifying user:",{userProperties:e,originalEndUserId:t,sessionId:this.sessionId}),!R||this.propertyManager.getAutomaticProperties();const s=await this.api.sendUserData(t||"",e,this.sessionId);if(s.actualUserId||s.wasExistingUser){const e=s.actualUserId||t;if(e&&e!==t){const s="human_behavior_end_user_id";if(this.setCookie(s,e,365),R)try{localStorage.setItem(s,e);}catch(e){c("Failed to set canonical endUserId in localStorage:",e);}c(`π Preexisting user detected. Future sessions will use canonical ID: ${e} (current session stays: ${t})`);}}return t||""}getUserAttributes(){return {...this.userProperties}}async start(){if(!R)return;if(this.isStarted)return void c("HumanBehaviorTracker already started, skipping start() call.");this.isStarted=true,this._lastActivityTimestamp=null!==this._sessionActivityTimestamp?this._sessionActivityTimestamp:Date.now(),this._isIdle="unknown",this.flushInterval=window.setInterval(()=>{this.flushEvents();},this.FLUSH_INTERVAL_MS),this.enableConsoleTrackingFlag&&this.enableConsoleTracking(),this.enableNetworkTrackingFlag&&this.enableNetworkTracking(),this.enablePageLoadTracking();const t=()=>{if(this.recordInstance)return void c("π― Recording already started, skipping duplicate start");c("π― DOM ready, starting session recording"),this.rrwebRecord=record;const t=record({emit:e=>{this.addRecordingEvent(e),2===e.type&&c(`π― FullSnapshot generated at ${(new Date).toISOString()}`);},maskTextSelector:this.redactionManager.getMaskTextSelector()||void 0,maskTextFn:void 0,maskAllInputs:"privacy-first"===this.redactionManager.getRedactionMode(),maskInputOptions:{password:true,text:true,textarea:true,email:true,number:true,tel:true,url:true,search:true,date:true,time:true,month:true,week:true},maskInputFn:(e,t)=>{try{const s=this.redactionManager.getRedactionMode();if(!(t instanceof HTMLElement))return e;if("privacy-first"===s)return "*".repeat(e.length||1);const i=this.redactionManager.shouldUnredactElement(t);t.id,t.name,t.type;return i?e:"*".repeat(e.length||1)}catch{return e}},slimDOMOptions:{},collectFonts:false,inlineStylesheet:true,recordCrossOriginIframes:false,recordCanvas:this.recordCanvas,sampling:this.recordCanvas?{canvas:4}:void 0,dataURLOptions:this.recordCanvas?{type:"image/webp",quality:.4}:void 0,hooks:{input:e=>{try{if("privacy-first"===this.redactionManager.getRedactionMode())return;const t="undefined"!=typeof document?document.querySelector(`[data-rrweb-id="${e.id}"]`):null;if(t&&t instanceof HTMLElement){this.redactionManager.shouldUnredactElement(t)||(void 0!==e.text&&(e.text="*".repeat(e.text?.length||1)),void 0!==e.value&&(e.value="*".repeat(e.value?.length||1)));}}catch{}}}});this.recordInstance=t||null;};if(c(`π― DOM ready state: ${document.readyState}`),"complete"===document.readyState||"interactive"===document.readyState)c(`π― DOM ready (${document.readyState}), starting recording immediately`),t();else {c("π― DOM not ready, waiting for DOMContentLoaded event");const e=()=>("interactive"===document.readyState||"complete"===document.readyState)&&(c(`π― DOM ready (${document.readyState}), starting recording`),t(),true);if(e())return;document.addEventListener("DOMContentLoaded",()=>{c("π― DOMContentLoaded fired, starting recording"),t();},{once:true});const s=setInterval(()=>{e()&&clearInterval(s);},10);setTimeout(()=>clearInterval(s),5e3);}}takeFullSnapshot(){this.fullSnapshotTimeout&&clearTimeout(this.fullSnapshotTimeout),this.fullSnapshotTimeout=window.setTimeout(()=>{try{requestAnimationFrame(()=>{requestAnimationFrame(()=>{this.rrwebRecord&&"function"==typeof this.rrwebRecord.takeFullSnapshot?(this.rrwebRecord.takeFullSnapshot(),c("β
FullSnapshot taken for navigation (delayed for animations)")):a("β οΈ takeFullSnapshot not available on record function");});});}catch(e){o("β Failed to take FullSnapshot for navigation:",e);}},1e3);}async stop(){await this.ensureInitialized(),R&&(this.flushInterval&&(clearInterval(this.flushInterval),this.flushInterval=null),this.recordInstance&&(this.recordInstance(),this.recordInstance=null),this.fullSnapshotTimeout&&(clearTimeout(this.fullSnapshotTimeout),this.fullSnapshotTimeout=null),this.rrwebRecord=null,this.disableConsoleTracking(),this.cleanupNavigationTracking());}async addEvent(e){if(R&&this.checkAndRefreshSession(),e&&"object"==typeof e){if(2===e.type){const t=!!e.data,s=!(!e.data||!e.data.node);c(t&&s?`β
Valid FullSnapshot: hasData=${t}, hasNode=${s}, dataType=${e.data?.node?.type}`:`β οΈ Empty FullSnapshot detected: hasData=${t}, hasNode=${s} - continuing session`);}this.eventQueue.length>=this.MAX_QUEUE_SIZE&&(this.eventQueue.shift(),c("Queue is full, the oldest event is dropped.")),this.eventQueue.push(e),2===e.type?(c("FullSnapshot added, triggering immediate flush"),this.flushEvents()):this.eventQueue.length>=.8*this.MAX_QUEUE_SIZE&&(c(`Queue at ${this.eventQueue.length}/${this.MAX_QUEUE_SIZE}, triggering immediate flush`),this.flushEvents());}else c("β οΈ Skipping invalid event:",e);}async flushEvents(){if(!(this.isProcessing||this.monthlyLimitReached||true===this._isIdle&&0===this.eventQueue.length)){this.isProcessing=true;try{const e=this.eventQueue,t=e.filter(e=>e&&2===e.type);if(this.eventQueue=[],t.length>0&&R&&(window.__hb_pending_snapshots=t,setTimeout(()=>{delete window.__hb_pending_snapshots;},5e3)),e.length>0){c("Flushing events:",e);const t=e.filter(e=>2===e.type);t.length>0&&c(`[FIXED] Sending ${t.length} FullSnapshot(s) with valid data`);try{const t=this.propertyManager.getAutomaticProperties();await this.api.sendEventsChunked(e,this.sessionId,this.endUserId,this.windowId,t);}catch(e){if(e.message?.includes("ERROR: Session already completed"))a("Session expired, events will be lost");else if(e.message?.includes("413")||e.message?.includes("Content Too Large"))a("Payload too large, events will be lost");else {if(!(e.message?.includes("ERR_BLOCKED_BY_CLIENT")||e.message?.includes("Failed to fetch")||e.message?.includes("NetworkError")))throw e;a("Request blocked by ad blocker or network issue, events will be lost");}}}}finally{this.isProcessing=false;}}}isInteractiveEvent(e){if(3!==e.type)return false;const t=e.data?.source;return [1,2,3,4,5,6,11].includes(t)}updateIdleState(e){const t=this.isInteractiveEvent(e),s=e.timestamp||Date.now();if(t){const e=true===this._isIdle;this._lastActivityTimestamp=s,null!==this._sessionActivityTimestamp&&(this._sessionActivityTimestamp=s),e?(c("β
User activity detected, exiting idle state"),this._isIdle=false,this.rrwebRecord&&"function"==typeof this.rrwebRecord.takeFullSnapshot&&(this.rrwebRecord.takeFullSnapshot(),c("β
FullSnapshot taken after returning from idle"))):"unknown"===this._isIdle&&(this._isIdle=false);}else if(true!==this._isIdle){const e=s-this._lastActivityTimestamp;e>this.IDLE_THRESHOLD_MS&&(c(`βΈοΈ Session idle detected (${Math.round(e/1e3)}s since last activity) - stopping background event recording`),c(`βΉοΈ Session will expire after 15 minutes of inactivity (${Math.round((9e5-e)/1e3)}s remaining)`),this._isIdle=true,this.flushEvents());}}async addRecordingEvent(e){if(R&&this.checkAndRefreshSession(),e&&"object"==typeof e){if(this.updateIdleState(e),true!==this._isIdle||3!==e.type||this.isInteractiveEvent(e)){if(2===e.type){const t=!!e.data,s=!(!e.data||!e.data.node);c(t&&s?`β
Valid FullSnapshot: hasData=${t}, hasNode=${s}, dataType=${e.data?.node?.type}`:`β οΈ Empty FullSnapshot detected: hasData=${t}, hasNode=${s} - continuing session`);}this.eventQueue.length>=this.MAX_QUEUE_SIZE&&(this.eventQueue.shift(),c("Queue is full, the oldest event is dropped.")),this.eventQueue.push(e),2===e.type?(c("FullSnapshot added, triggering immediate flush"),this.flushEvents()):true!==this._isIdle&&this.eventQueue.length>=.8*this.MAX_QUEUE_SIZE&&(c(`Queue at ${this.eventQueue.length}/${this.MAX_QUEUE_SIZE}, triggering immediate flush`),this.flushEvents());}}else c("β οΈ Skipping invalid recording event:",e);}_canUseSessionStorage(){if(!R)return false;try{const e="__sessionStorage_test__";return sessionStorage.setItem(e,e),sessionStorage.removeItem(e),!0}catch{return false}}_getWindowIdFromStorage(){if(!this._canUseSessionStorage())return null;try{return sessionStorage.getItem(this._window_id_storage_key)}catch{return null}}_setWindowIdInStorage(e){if(this._canUseSessionStorage())try{sessionStorage.setItem(this._window_id_storage_key,e),c(`Stored windowId in sessionStorage: ${e}`);}catch(e){a("Failed to store windowId in sessionStorage:",e);}}_removeWindowIdFromStorage(){if(this._canUseSessionStorage())try{sessionStorage.removeItem(this._window_id_storage_key);}catch(e){a("Failed to remove windowId from sessionStorage:",e);}}_getPrimaryWindowExists(){if(!this._canUseSessionStorage())return false;try{return "true"===sessionStorage.getItem(this._primary_window_exists_storage_key)}catch{return false}}_setPrimaryWindowExists(e){if(this._canUseSessionStorage())try{e?sessionStorage.setItem(this._primary_window_exists_storage_key,"true"):sessionStorage.removeItem(this._primary_window_exists_storage_key);}catch(e){a("Failed to set primary_window_exists flag:",e);}}getOrCreateWindowId(){if(!R)return v1();const e=this._getWindowIdFromStorage(),s=this._getPrimaryWindowExists();if(e&&!s)return c(`Reusing windowId from previous page load: ${e}`),this._setWindowIdInStorage(e),this._setPrimaryWindowExists(true),e;{const e=v1();return c(`Creating new windowId: ${e} (new window/tab detected)`),this._setWindowIdInStorage(e),this._setPrimaryWindowExists(true),e}}setupWindowUnloadListener(){R&&window.addEventListener("beforeunload",()=>{this._canUseSessionStorage()&&(this._setPrimaryWindowExists(false),c("Cleared primary_window_exists flag on beforeunload"));},{capture:false});}setCookie(e,t,s){if(R)try{const i=new Date;i.setTime(i.getTime()+24*s*60*60*1e3);const n=`expires=${i.toUTCString()}`;document.cookie=`${e}=${t};${n};path=/;SameSite=Lax`,localStorage.setItem(e,t),c(`Set cookie and localStorage: ${e}`);}catch(s){try{localStorage.setItem(e,t),c(`Cookie blocked, using localStorage: ${e}`);}catch(e){o("Failed to store user ID in both cookie and localStorage:",e);}}}getCookie(e){if(!R)return null;try{const t=e+"=",s=document.cookie.split(";");for(let i=0;i<s.length;i++){let n=s[i];for(;" "===n.charAt(0);)n=n.substring(1,n.length);if(0===n.indexOf(t)){const s=n.substring(t.length,n.length);return c(`Found cookie: ${e}`),s}}const i=localStorage.getItem(e);return i?(c(`Cookie not found, using localStorage: ${e}`),i):null}catch(t){try{const t=localStorage.getItem(e);if(t)return c(`Cookie access failed, using localStorage: ${e}`),t}catch(e){o("Failed to access both cookie and localStorage:",e);}return null}}deleteCookie(e){if(R){try{document.cookie=`${e}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; SameSite=Lax`,c(`Deleted cookie: ${e}`);}catch(t){o(`Failed to delete cookie: ${e}`,t);}try{localStorage.removeItem(e),c(`Removed from localStorage: ${e}`);}catch(t){o(`Failed to remove from localStorage: ${e}`,t);}}}logout(){if(R)try{const e="human_behavior_end_user_id";this.deleteCookie(e);const s="human_behavior_session";localStorage.removeItem(s),this.endUserId=null,this.userProperties={},this.endUserId=v1(),this.setCookie("human_behavior_end_user_id",this.endUserId,365),this.sessionId=this.createNewSession(s),this.windowId=v1(),this._setWindowIdInStorage(this.windowId),this.api.setTrackingContext(this.sessionId,this.endUserId),d("User logged out - cleared all user data and started fresh session");}catch(e){o("Error during logout:",e);}}async redact(e){await this.ensureInitialized(),R?this.redactionManager=new v(e):a("Redaction is only available in browser environments");}setRedactedFields(e){this.redactionManager.setFieldsToRedact(e),this.recordInstance&&this.restartWithNewRedaction();}setUnredactedFields(e){this.redactionManager.setFieldsToUnredact(e),this.recordInstance&&this.restartWithNewRedaction();}restartWithNewRedaction(){this.recordInstance&&(this.recordInstance(),this.start());}hasUnredactedFields(){return this.redactionManager.hasUnredactedFields()}getUnredactedFields(){return this.redactionManager.getUnredactedFields()}redactFields(e){this.redactionManager.redactFields(e),this.recordInstance&&this.restartWithNewRedaction();}clearUnredactedFields(){this.redactionManager.clearUnredactedFields(),this.recordInstance&&this.restartWithNewRedaction();}checkAndRefreshSession(){if(!R)return;const e="human_behavior_session",s=Date.now(),i=this.getStoredSession(e);if(!i||!i.sessionId)return this.createNewSession(e),this.windowId=v1(),this._setWindowIdInStorage(this.windowId),this.api.setTrackingContext(this.sessionId,this.endUserId),void c(`Created new session (no stored session): ${this.sessionId}`);this.updateSessionActivity(e,s,i.sessionId,i.sessionStartTimestamp);}getOrCreateSessionId(){if(!R)return v1();const e="human_behavior_session",s=Date.now(),i=this.getStoredSession(e);if(!i||!i.sessionId){const t=this.createNewSession(e);return this.api.setTrackingContext(t,this.endUserId),t}const n=s-i.lastActivityTimestamp,r=s-i.sessionStartTimestamp;if(n>9e5||r>864e5){c(`Session expired: idle=${n}ms, age=${r}ms`);const t=this.createNewSession(e);return this.api.setTrackingContext(t,this.endUserId),t}return this.updateSessionActivity(e,s,i.sessionId,i.sessionStartTimestamp),i.sessionId}getStoredSession(e){const s=Date.now(),i=9e5,n=864e5;if(this.sessionId&&null!==this._sessionActivityTimestamp&&null!==this._sessionStartTimestamp){const r=s-this._sessionActivityTimestamp,o=s-this._sessionStartTimestamp;if(!(r>i||o>n))return {sessionId:this.sessionId,lastActivityTimestamp:this._sessionActivityTimestamp,sessionStartTimestamp:this._sessionStartTimestamp};{c("Session in memory expired: creating new session immediately");const s=this.sessionId;if(this.createNewSession(e),this.windowId=v1(),this._setWindowIdInStorage(this.windowId),this.api.setTrackingContext(this.sessionId,this.endUserId),d(`π Session timeout (memory): Created new session ${this.sessionId} (previous: ${s})`),null!==this._sessionActivityTimestamp&&null!==this._sessionStartTimestamp)return {sessionId:this.sessionId,lastActivityTimestamp:this._sessionActivityTimestamp,sessionStartTimestamp:this._sessionStartTimestamp}}}try{const r=localStorage.getItem(e);if(!r)return null;const o=JSON.parse(r),a=s-o.lastActivityTimestamp,l=s-o.sessionStartTimestamp;if(a>i||l>n){c(`Session in localStorage expired: idle=${Math.round(a/1e3/60)}min, age=${Math.round(l/1e3/60/60)}hrs`);const s=o.sessionId;if(this.createNewSession(e),this.windowId=v1(),this._setWindowIdInStorage(this.windowId),this.api.setTrackingContext(this.sessionId,this.endUserId),d(`π Session timeout (localStorage): Created new session ${this.sessionId} (previous: ${s})`),null!==this._sessionActivityTimestamp&&null!==this._sessionStartTimestamp)return {sessionId:this.sessionId,lastActivityTimestamp:this._sessionActivityTimestamp,sessionStartTimestamp:this._sessionStartTimestamp}}return o.sessionId&&(this.sessionId=o.sessionId,this._sessionActivityTimestamp=o.lastActivityTimestamp,this._sessionStartTimestamp=o.sessionStartTimestamp),o}catch{return null}}createNewSession(e){const s=v1(),i=Date.now();this.sessionId=s,this._sessionActivityTimestamp=i,this._sessionStartTimestamp=i,this._lastActivityTimestamp=i;const n={sessionId:s,lastActivityTimestamp:i,sessionStartTimestamp:i};try{localStorage.setItem(e,JSON.stringify(n));}catch(e){a(`Failed to save session to localStorage: ${e}`);}return c(`Created new session: ${s}`),s}updateSessionActivity(e,t,s,i){this.sessionId=s,this._sessionActivityTimestamp=t,this._sessionStartTimestamp=i;const n={sessionId:s,lastActivityTimestamp:t,sessionStartTimestamp:i};try{localStorage.setItem(e,JSON.stringify(n));}catch(e){a(`Failed to update session in localStorage: ${e}`);}}getSessionId(){return this.sessionId}getCurrentUrl(){return this.currentUrl}getSnapshotFrequencyInfo(){return {sessionDuration:Date.now()-this.sessionStartTime,currentInterval:3e5,currentThreshold:1e3,phase:"configured"}}async testConnection(){try{return await this.api.init(this.sessionId,this.endUserId),{success:!0}}catch(e){return {success:false,error:e.message||"Unknown error"}}}getConnectionStatus(){const e=[];let t=false;return this.eventQueue.length>0&&(t=true,e.push("Some requests may be blocked by ad blockers")),this._connectionBlocked&&(t=true,e.push("Initial connection test failed - ad blocker may be active")),"undefined"==typeof window&&e.push("Not running in browser environment"),void 0===navigator.sendBeacon&&e.push("sendBeacon not available, using fetch fallback"),{blocked:t,recommendations:e}}isPreexistingUser(){if(!R)return false;const e=this.getCookie("human_behavior_end_user_id");return null!==e&&e!==this.endUserId}getUserInfo(){return {endUserId:this.endUserId,sessionId:this.sessionId,isPreexistingUser:this.isPreexistingUser(),initialized:this.initialized}}setSessionProperty(e,t){this.propertyManager.setSessionProperty(e,t);}setSessionProperties(e){this.propertyManager.setSessionProperties(e);}getSessionProperty(e){return this.propertyManager.getSessionProperty(e)}removeSessionProperty(e){this.propertyManager.removeSessionProperty(e);}setUserProperty(e,t){this.propertyManager.setUserProperty(e,t);}setUserProperties(e){this.propertyManager.setUserProperties(e);}getUserProperty(e){return this.propertyManager.getUserProperty(e)}removeUserProperty(e){this.propertyManager.removeUserProperty(e);}setOnce(e,t,s="user"){this.propertyManager.setOnce(e,t,s);}clearSessionProperties(){this.propertyManager.clearSessionProperties();}clearUserProperties(){this.propertyManager.clearUserProperties();}getAllProperties(){return this.propertyManager.getAllProperties()}}function F(e){const t=globalThis.__humanBehaviorGlobalTracker;return t?.identifyUser?t.identifyUser({userProperties:e}):(console.warn("HumanBehavior tracker not found. Make sure the SDK is initialized."),null)}function L(e,t){const s=globalThis.__humanBehaviorGlobalTracker;return s?.track?s.track(e,t):(console.warn("HumanBehavior tracker not found. Make sure the SDK is initialized."),null)}function A(){const e=globalThis.__humanBehaviorGlobalTracker;return !!e?.identifyUser}R&&(window.HumanBehaviorTracker=P);
|
|
12175
12175
|
|
|
12176
12176
|
/**
|
|
12177
12177
|
* Main entry point for the HumanBehavior SDK
|
|
@@ -12187,5 +12187,5 @@ if (typeof window !== 'undefined') {
|
|
|
12187
12187
|
window.humanbehavior = { init };
|
|
12188
12188
|
}
|
|
12189
12189
|
|
|
12190
|
-
export { f as HumanBehaviorAPI, P as HumanBehaviorTracker, s as LogLevel, C as PropertyManager, v as RedactionManager,
|
|
12190
|
+
export { f as HumanBehaviorAPI, P as HumanBehaviorTracker, s as LogLevel, C as PropertyManager, v as RedactionManager, T as getAutomaticProperties, U as getCurrentPageProperties, I as getDeviceInfo, E as getInitialProperties, b as getLocationInfo, F as identifyUserGlobally, init, A as isGlobalTrackerAvailable, r as isSDKLogging, c as logDebug, o as logError, d as logInfo, a as logWarn, i as logger, L as sendEventGlobally };
|
|
12191
12191
|
//# sourceMappingURL=index.js.map
|