humanbehavior-js
Version:
SDK for HumanBehavior session and event recording
1 lines • 74.1 kB
JavaScript
"use strict";var e,t=require("@rrweb/record"),s=require("uuid");exports.LogLevel=void 0,(e=exports.LogLevel||(exports.LogLevel={}))[e.NONE=0]="NONE",e[e.ERROR=1]="ERROR",e[e.WARN=2]="WARN",e[e.INFO=3]="INFO",e[e.DEBUG=4]="DEBUG";const i=new class{constructor(e){this.config={level:exports.LogLevel.ERROR,enableConsole:!0,enableStorage:!1},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(exports.LogLevel.ERROR))return;const s=this.formatMessage("ERROR",e);this.config.enableConsole&&console.error(s,...t),this.config.enableStorage&&this.isBrowser&&this.logToStorage(s,t)}warn(e,...t){if(!this.shouldLog(exports.LogLevel.WARN))return;const s=this.formatMessage("WARN",e);this.config.enableConsole&&console.warn(s,...t),this.config.enableStorage&&this.isBrowser&&this.logToStorage(s,t)}info(e,...t){if(!this.shouldLog(exports.LogLevel.INFO))return;const s=this.formatMessage("INFO",e);this.config.enableConsole&&console.log(s,...t),this.config.enableStorage&&this.isBrowser&&this.logToStorage(s,t)}debug(e,...t){if(!this.shouldLog(exports.LogLevel.DEBUG))return;const s=this.formatMessage("DEBUG",e);this.config.enableConsole&&console.log(s,...t),this.config.enableStorage&&this.isBrowser&&this.logToStorage(s,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=!1;const r=()=>n,o=(e,...t)=>{n=!0;try{i.error(e,...t)}finally{n=!1}},a=(e,...t)=>{n=!0;try{i.warn(e,...t)}finally{n=!1}},d=(e,...t)=>{n=!0;try{i.info(e,...t)}finally{n=!1}},l=(e,...t)=>{n=!0;try{i.debug(e,...t)}finally{n=!1}};class c{constructor(e){this._isPolling=!1,this._pollIntervalMs=3e3,this._queue=[],this._queue=[],this._areWeOnline=!0,this._sendRequest=e,"undefined"!=typeof window&&"onLine"in window.navigator&&(this._areWeOnline=window.navigator.onLine,window.addEventListener("online",()=>{this._areWeOnline=!0,this._flush()}),window.addEventListener("offline",()=>{this._areWeOnline=!1}))}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=!0,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),!1));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)),l(`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(),l("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.69",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 f(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 y{constructor({apiKey:e,ingestionUrl:t}){this.monthlyLimitReached=!1,this.sessionId="",this.endUserId=null,this.cspBlocked=!1,this.requestTimeout=1e4,this.currentBatchSize=100,this.apiKey=e,this.baseUrl=t,this.persistence=new h(e),this.retryQueue=new c(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){l(`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=!0,new Error("429: Monthly video processing limit reached");throw new Error(`Failed to send events: ${n.statusText}`)}!0===(await n.json()).monthlyLimitReached&&(this.monthlyLimitReached=!0,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){l(`[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=f(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 l=w({sessionId:t,events:e.slice(o,o+r),endUserId:s,windowId:i,automaticProperties:n,sdkVersion:u}),c=(new TextEncoder).encode(l).length;try{const h=await this.trackedFetch(`${this.baseUrl}/api/ingestion/events`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:l},c);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:l,estimatedSize:c,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:l,estimatedSize:c,callback:e=>{200===e.statusCode&&e.json&&!0===e.json.monthlyLimitReached&&(this.monthlyLimitReached=!0)}}),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};l("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 l("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 l("[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(l("[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?l("[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(l("[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?l("[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,t,i){const n=Date.now(),r=s.v1(),o=this.shouldSkipNetworkTracking(e);if(this.cspBlocked&&"POST"===t.method&&"undefined"!=typeof navigator&&"function"==typeof navigator.sendBeacon)return this.trackedFetchWithBeaconFallback(e,t,o);try{const s="undefined"!=typeof AbortController?new AbortController:null;let a=null;s&&(a=setTimeout(()=>{s.abort()},this.requestTimeout));const d="POST"===t.method&&void 0!==i&&i<m,l=await fetch(e,{...t,signal:s?.signal,keepalive:d});a&&clearTimeout(a);const c=Date.now()-n;return l.ok||o||await this.sendNetworkError({requestId:r,url:e,method:t.method||"GET",status:l.status,statusText:l.statusText,duration:c,timestampMs:Date.now(),sessionId:this.sessionId,endUserId:this.endUserId,errorType:this.classifyHttpError(l.status),errorMessage:l.statusText,startTimeMs:n,spanName:`${t.method||"GET"} ${e}`,spanStatus:"error",attributes:{"http.status_code":l.status,"http.status_text":l.statusText}}).catch(()=>{}),l}catch(s){const i=Date.now()-n;if("AbortError"===s.name){const e=new Error("Request timeout");e.name="TimeoutError",s=e}if(this.isCSPViolation(s)&&"POST"===t.method&&"undefined"!=typeof navigator&&"function"==typeof navigator.sendBeacon)return this.cspBlocked=!0,a("[SDK] CSP violation detected, falling back to sendBeacon for future requests"),this.trackedFetchWithBeaconFallback(e,t,o);throw o||await this.sendNetworkError({requestId:r,url:e,method:t.method||"GET",status:null,statusText:null,duration:i,timestampMs:Date.now(),sessionId:this.sessionId,endUserId:this.endUserId,errorType:this.classifyNetworkError(s),errorMessage:s.message,errorName:s.name,startTimeMs:n,spanName:`${t.method||"GET"} ${e}`,spanStatus:"error",attributes:{"error.name":s.name,"error.message":s.message}}).catch(()=>{}),s}}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)?(l("[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!1;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?l(`Redaction: Active for ${this.redactedFields.size} field(s):`,Array.from(this.redactedFields)):l("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`),!1));t.forEach(e=>this.unredactedFields.add(e)),t.length>0?l(`Unredaction: Active for ${t.length} field(s):`,t):l("Unredaction: No valid fields to unredact"),this.applyUnredactionClasses()}redactFields(e){e.forEach(e=>{this.unredactedFields.delete(e)}),this.unredactedFields.size>0?l(`Unredaction: Removed ${e.length} field(s), ${this.unredactedFields.size} remaining:`,Array.from(this.unredactedFields)):l("Unredaction: All fields redacted"),this.applyUnredactionClasses()}clearUnredactedFields(){this.unredactedFields.clear(),l("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")}),l(`Added rr-mask class to ${t.length} element(s) for selector: ${e}`)}catch(t){a(`Invalid selector: ${e}`)}}):l("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")}),l(`Removed rr-mask class from ${t.length} element(s) for selector: ${e}`)}catch(t){a(`Invalid selector: ${e}`)}}):l("DOM not ready, deferring unredaction class application"))}removeUnredactionClasses(){l("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!1;for(const t of this.unredactedFields)try{if(e.matches(t))return!0}catch(e){a(`Invalid selector: ${t}`)}return!1}if(0===this.redactedFields.size)return!0;for(const t of this.redactedFields)try{if(e.matches(t))return!1}catch(e){a(`Invalid selector: ${t}`)}return!0}}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 C(){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 U{constructor(e={}){this.sessionProperties={},this.userProperties={},this.initialProperties={},this.isInitialized=!1,this.config={enableAutomaticProperties:!0,enableSessionProperties:!0,enableUserProperties:!0,propertyDenylist:[],...e},this.automaticProperties=T(),this.initialize()}initialize(){this.isInitialized||(this.initialProperties=E(),this.loadSessionProperties(),this.isInitialized=!0)}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",!0)),this.applyDenylist(t),t}getAutomaticProperties(){return{...this.automaticProperties,...C()}}getAutomaticPropertiesWithGeoIP(e={}){return{...this.automaticProperties,...C(),...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=!1,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 P="undefined"!=typeof window;class R{get isTrackerStarted(){return this.isStarted}setupDomReadyHandler(){if(P)if("complete"===document.readyState||"interactive"===document.readyState)this.onDomReady();else if(document.addEventListener){document.addEventListener("DOMContentLoaded",()=>this.onDomReady(),{capture:!1});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=!0,l("🎯 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(l("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(P&&!1!==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(),!1})}if(P&&window.__humanBehaviorGlobalTracker)return l("Tracker already initialized, returning existing instance"),window.__humanBehaviorGlobalTracker;t?.logLevel&&this.configureLogging({level:t.logLevel});const s=new R(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??!1,t?.redactFields&&s.setUnredactedFields(t.redactFields),!1!==t?.enableAutomaticTracking&&s.setupAutomaticTracking(t?.automaticTrackingOptions),s.start(),s}constructor(e,t,i){if(this.eventQueue=[],this.pendingCustomEvents=[],this.pendingLogs=[],this.pendingNetworkErrors=[],this._sessionActivityTimestamp=null,this._sessionStartTimestamp=null,this.userProperties={},this.isProcessing=!1,this.flushInterval=null,this.FLUSH_INTERVAL_MS=3e3,this.endUserId=null,this.initialized=!1,this.initializationPromise=null,this.monthlyLimitReached=!1,this.isDomReady=!1,this.requestQueue=[],this.domReadyHandlers=[],this.originalConsole=null,this.consoleTrackingEnabled=!1,this.originalFetch=null,this.networkTrackingEnabled=!1,this.enableConsoleTrackingFlag=!0,this.enableNetworkTrackingFlag=!0,this.navigationTrackingEnabled=!1,this.currentUrl="",this.previousUrl="",this.originalPushState=null,this.originalReplaceState=null,this.navigationListeners=[],this._connectionBlocked=!1,this.recordInstance=null,this.sessionStartTime=Date.now(),this.rrwebRecord=null,this.fullSnapshotTimeout=null,this.recordCanvas=!1,this.isStarted=!1,this.minimumDurationMilliseconds=5e3,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=t||"https://ingest.humanbehavior.co";if(this.api=new y({apiKey:e,ingestionUrl:n}),this.apiKey=e,this.ingestionUrl=n,this.MAX_QUEUE_SIZE=i?.maxQueueSize??1e3,this.enableConsoleTrackingFlag=!1!==i?.enableConsoleTracking,this.enableNetworkTrackingFlag=!1!==i?.enableNetworkTracking,this.redactionManager=new v({redactionStrategy:i?.redactionStrategy,legacyRedactFields:i?.redactFields}),this.propertyManager=new U({enableAutomaticProperties:!1!==i?.enableAutomaticProperties,propertyDenylist:i?.propertyDenylist||[]}),P){const e="human_behavior_end_user_id",t=this.getCookie(e);this.endUserId=t||s.v1(),t?l(`Reusing existing endUserId: ${this.endUserId}`):(this.setCookie(e,this.endUserId,365),l(`Generated new endUserId: ${this.endUserId}`))}else this.endUserId=s.v1();if(P){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=s.v1(),this.windowId=s.v1();this.api.setTrackingContext(this.sessionId,this.endUserId),this.initializationPromise=this.init().catch(e=>{o("Initialization failed:",e)})}async init(){try{P?(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=!0}}async ensureInitialized(){this.initializationPromise&&await this.initializationPromise}setupNavigationTracking(){if(!P||this.navigationTrackingEnabled)return;this.navigationTrackingEnabled=!0,l("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)}l(`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()}),l(`Pageview tracked: ${t.url}`)}catch(e){o("Failed to track pageview event:",e)}}}async customEvent(e,t){this.endUserId||(a(`endUserId not available, using anonymous ID for event: ${e}`),this.endUserId=s.v1()),P&&this.checkAndRefreshSession();const i=this.propertyManager.getEventProperties(t);if(this.shouldSkipDueToMinimumDuration())return l(`Custom event '${e}' queued due to session duration below minimum`),void this.pendingCustomEvents.push({eventName:e,properties:i,timestamp:Date.now()});await this.flushPendingCustomEvents();try{await this.api.sendCustomEvent(this.sessionId,e,i,this.endUserId),l(`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()}),l(`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(!P)return;const t={trackButtons:!1!==e?.trackButtons,trackLinks:!1,trackForms:!1!==e?.trackForms,includeText:!1!==e?.includeText,includeClasses:e?.includeClasses||!1};l("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(){P&&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!0}else this.rageClickTracker.clicks=[{x:e,y:t,timestamp:s,element:i}];return!1}isInteractiveElement(e){const t=e.tagName.toLowerCase();if("button"===t||"a"===t)return!0;if(["input","select","textarea"].includes(t))return!0;const s=e.getAttribute("role");if(s&&["button","link","tab","menuitem","checkbox","radio"].includes(s))return!0;if(e.onclick||e.getAttribute("onclick"))return!0;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=!1,l("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:!1!==e.enableConsole,enableStorage:e.enableStorage||!1})}enableConsoleTracking(){P&&!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=!0,l("Console tracking enabled"))}enableNetworkTracking(){P&&!this.networkTrackingEnabled&&"undefined"!=typeof fetch&&(this.originalFetch=window.fetch.bind(window),window.fetch=async(e,t)=>{const i=Date.now(),n=s.v1(),r="string"==typeof e?e:e instanceof URL?e.toString():e.url,o=(t?.method||("object"==typeof e&&"method"in e?e.method:void 0)||"GET").toUpperCase(),a=this.shouldSkipNetworkTracking(r),d=1e4;let c=null,h=!1;a||(c=setTimeout(()=>{const e=Date.now()-i;if(!h){h=!0;const t={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}};return this.shouldSkipDueToMinimumDuration()?(l("Long-loading network error queued due to session duration below minimum"),void this.pendingNetworkErrors.push({errorData:t,timestamp:Date.now()})):(this.flushPendingNetworkErrors(),void this.api.sendNetworkError(t).catch(()=>{}))}},d));try{const s=await this.originalFetch(e,t),d=Date.now()-i;if(c&&clearTimeout(c),!s.ok&&!a){const e={requestId:n,url:r,method:o,status:s.status,statusText:s.statusText,duration:d,timestampMs:Date.now(),sessionId:this.sessionId,endUserId:this.endUserId,errorType:this.classifyHttpError(s.status),errorMessage:s.statusText,startTimeMs:i,spanName:`${o} ${r}`,spanStatus:"error",attributes:{"http.status_code":s.status,"http.status_text":s.statusText}};if(this.shouldSkipDueToMinimumDuration())return l("Failed request network error queued due to session duration below minimum"),this.pendingNetworkErrors.push({errorData:e,timestamp:Date.now()}),s;this.flushPendingNetworkErrors(),this.api.sendNetworkError(e).catch(()=>{})}return s}catch(e){const t=Date.now()-i;if(c&&clearTimeout(c),!a){const s={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}};if(this.shouldSkipDueToMinimumDuration())throw l("Network error queued due to session duration below minimum"),this.pendingNetworkErrors.push({errorData:s,timestamp:Date.now()}),e;this.flushPendingNetworkErrors(),this.api.sendNetworkError(s).catch(()=>{})}throw e}},this.networkTrackingEnabled=!0,l("Network tracking enabled"))}async flushPendingCustomEvents(){if(0===this.pendingCustomEvents.length)return;const e=[...this.pendingCustomEvents];this.pendingCustomEvents=[],l(`Flushing ${e.length} pending custom events`);for(const{eventName:t,properties:s}of e)try{await this.api.sendCustomEvent(this.sessionId,t,s,this.endUserId)}catch(e){o("Failed to flush pending custom event:",e)}}async flushPendingLogs(){if(0===this.pendingLogs.length)return;const e=[...this.pendingLogs];this.pendingLogs=[],l(`Flushing ${e.length} pending logs`);for(const{logData:t}of e)try{await this.api.sendLog(t)}catch(e){o("Failed to flush pending log:",e)}}async flushPendingNetworkErrors(){if(0===this.pendingNetworkErrors.length)return;const e=[...this.pendingNetworkErrors];this.pendingNetworkErrors=[],l(`Flushing ${e.length} pending network errors`);for(const{errorData:t}of e)try{await this.api.sendNetworkError(t)}catch(e){o("Failed to flush pending network error:",e)}}enablePageLoadTracking(){P&&"undefined"!=typeof window&&("complete"===document.readyState?this.trackPageLoad():window.addEventListener("load",()=>{this.trackPageLoad()}),l("Page load tracking enabled"))}trackPageLoad(){if(P&&"undefined"!=typeof performance)try{const e=performance.getEntriesByType("navigation")[0];if(!e)return;const t=e.loadEventEnd-e.fetchStart;if(t>3e3){const i=s.v1(),n=e.domContentLoadedEventEnd-e.fetchStart,r=e.domComplete-e.fetchStart,o={requestId:i,url:window.location.href,method:"GET",status:200,statusText:"OK",duration:t,timestampMs:e.loadEventEnd+performance.timeOrigin,sessionId:this.sessionI