posthog-js
Version:
Posthog-js allows you to automatically capture usage and send events to PostHog.
2 lines • 17.9 kB
JavaScript
!function(){const t="u">typeof window?window:void 0,n="u">typeof globalThis?globalThis:t;null!=n&&n.XMLHttpRequest&&new n.XMLHttpRequest;const e="undefined"!=typeof globalThis?globalThis:t;e&&"undefined"==typeof self&&(e.self=e),e&&"undefined"==typeof File&&(e.File=function(){});const r=null!=t?t:{},o=Object.prototype.toString,i=Array.isArray||function(t){return"[object Array]"===o.call(t)},c=t=>"function"==typeof t,s=t=>void 0===t,u=t=>"[object String]"==o.call(t),a=t=>u(t)&&0===t.trim().length;function l(t){return null===t||"object"!=typeof t}function f(t,n){return{}.toString.call(t)===`[object ${n}]`}function d(t){switch({}.toString.call(t)){case"[object Error]":case"[object Exception]":case"[object DOMException]":case"[object DOMError]":case"[object WebAssembly.Exception]":return!0;default:return h(t,Error)}}function v(t){return"u">typeof Event&&h(t,Event)}function h(t,n){try{return t instanceof n}catch(t){return!1}}function p(t,n,e,r,o,i,c){try{var s=t[i](c),u=s.value}catch(t){return void e(t)}s.done?n(u):Promise.resolve(u).then(r,o)}function y(t){return function(){var n=this,e=arguments;return new Promise(function(r,o){var i=t.apply(n,e);function c(t){p(i,r,o,c,s,"next",t)}function s(t){p(i,r,o,c,s,"throw",t)}c(void 0)})}}function b(t){return b="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},b(t)}function g(t,n,e){return(n=function(t){var n=function(t){if("object"!=b(t)||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var e=n.call(t,"string");if("object"!=b(e))return e;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==b(n)?n:n+""}(n))in t?Object.defineProperty(t,n,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[n]=e,t}function m(t,n){var e=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);n&&(r=r.filter(function(n){return Object.getOwnPropertyDescriptor(t,n).enumerable})),e.push.apply(e,r)}return e}function w(t){for(var n=1;arguments.length>n;n++){var e=null!=arguments[n]?arguments[n]:{};n%2?m(Object(e),!0).forEach(function(n){g(t,n,e[n])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(e)):m(Object(e)).forEach(function(n){Object.defineProperty(t,n,Object.getOwnPropertyDescriptor(e,n))})}return t}let j,E,x,O;function k(t){var n;const e=globalThis._posthogChunkIds,r=globalThis._debugIds;if(!e&&!r)return;const o=e?Object.keys(e):[],i=r?Object.keys(r):[];if(O&&S(e,o,E)&&S(r,i,x))return O;E=e?w({},e):void 0,x=r?w({},r):void 0,O={},null!==(n=j)&&void 0!==n||(j={});const c=(n,e)=>{for(const o of n){const n=e[o];if(!n)continue;const i=null==j?void 0:j[o];if(i){O[i]=n;continue}const c=t(o);for(let t=c.length-1;t>=0;t--){var r;const e=null===(r=c[t])||void 0===r?void 0:r.filename;if(e){O[e]=n,j[o]=e;break}}}};return r&&c(i,r),e&&c(o,e),O}function S(t,n,e){return t&&e?n.length===Object.keys(e).length&&n.every(n=>t[n]===e[n]):t===e}const _=/^([a-z][a-z0-9.+-]+):/i,M=["http","https","file","blob","app","capacitor","ionic","webpack","webpack-internal","ng"];function T(t){if(!t||"<anonymous>"===t)return!1;const n=_.exec(t);return!n||M.includes(n[1].toLowerCase())}function P(t,n,e,r,o){const i={platform:t,filename:n,function:"<anonymous>"===e?"?":e,in_app:T(n)};return s(r)||(i.lineno=r),s(o)||(i.colno=o),i}const R=(t,n)=>{const e=-1!==t.indexOf("safari-extension"),r=-1!==t.indexOf("safari-web-extension");return e||r?[-1!==t.indexOf("@")?t.split("@")[0]:"?",e?`safari-extension:${n}`:`safari-web-extension:${n}`]:[t,n]},D=/^\s*at (\S+?)(?::(\d+))(?::(\d+))\s*$/i,U=/^\s*at (?:(.+?\)(?: \[.+\])?|.*?) ?\((?:address at )?)?(?:async )?((?:<anonymous>|[-a-z]+:|.*bundle|\/)?.*?)(?::(\d+))?(?::(\d+))?\)?\s*$/i,C=/\((\S*)(?::(\d+))(?::(\d+))\)/,F=(t,n)=>{const e=D.exec(t);if(e){const[,t,r,o]=e;return P(n,t,"?",+r,+o)}const r=U.exec(t);if(r){if(r[2]&&0===r[2].indexOf("eval")){const t=C.exec(r[2]);t&&(r[2]=t[1],r[3]=t[2],r[4]=t[3])}const[t,e]=R(r[1]||"?",r[2]);return P(n,e,t,r[3]?+r[3]:void 0,r[4]?+r[4]:void 0)}},A=/^\s*(.*?)(?:\((.*?)\))?(?:^|@)?((?:[-a-z]+)?:\/.*?|\[native code\]|[^@]*(?:bundle|\d+\.js)|\/[\w\-. /=]+)(?::(\d+))?(?::(\d+))?\s*$/i,z=/(\S+) line (\d+)(?: > eval line \d+)* > eval/i,I=(t,n)=>{const e=A.exec(t);if(e){if(e[3]&&e[3].indexOf(" > eval")>-1){const t=z.exec(e[3]);t&&(e[1]=e[1]||"eval",e[3]=t[1],e[4]=t[2],e[5]="")}let t=e[3],r=e[1]||"?";return[r,t]=R(r,t),P(n,t,r,e[4]?+e[4]:void 0,e[5]?+e[5]:void 0)}},L=/\(error: (.*)\)/;function H(t,n){return t.filename===n.filename&&t.function===n.function&&t.module===n.module&&t.lineno===n.lineno&&t.colno===n.colno}function N(t){for(let n=1;10>=n;n++){const e=t.length-2*n;if(0>e)break;let r=!0;for(let o=0;n>o;o++)if(!H(t[e+o],t[e+n+o])){r=!1;break}if(r){for(let r=0;n>r;r++)t[e+r]=t[e+n+r];return t.length=e+n,{start:e,length:n}}}}function W(t,n){const e=n.start+n.length,r=K(t,n);r&&t.splice(e+r===t.length?e:n.start,r)}function V(t,n){let e=n.start;for(;e>0&&H(t[e-1],t[e-1+n.length]);)e--;return n.start-e}function K(t,n){const e=n.start+n.length;let r=e;for(;t.length>r&&H(t[r],t[r-n.length]);)r++;return r-e}function Y(t,n){const e=n[0];let r=t.length-(e?V(t,e):0);for(const e of n)r-=K(t,e);return r}function q(t,n,e){for(let r=0;t.length>r;r++){const o=t[(n+r)%t.length],i=t[(e+r)%t.length];if(o!==i)return i>o}return!1}const B=/^(?:[Uu]ncaught (?:exception: )?)?(?:((?:Eval|Internal|Range|Reference|Syntax|Type|URI|)Error): )?(.*)$/i;const G=["fatal","error","warning","log","info","debug"];function J(t,n=40){const e=Object.keys(t);if(e.sort(),!e.length)return"[object has no keys]";for(let t=e.length;t>0;t--){const r=e.slice(0,t).join(", ");if(n>=r.length)return t===e.length?r:r.length>n?`${r.slice(0,n)}...`:r}return""}const Q=(t,{debugEnabled:n}={})=>{const e={N(e,...r){let o;try{o=typeof window>"u"?void 0:window}catch(t){return}o&&(o.POSTHOG_DEBUG||n)&&!s(o.console)&&o.console&&("__rrweb_original__"in o.console[e]?o.console[e].__rrweb_original__:o.console[e])(t,...r)},debug(...t){e.N("debug",...t)},info(...t){e.N("log",...t)},warn(...t){e.N("warn",...t)},error(...t){e.N("error",...t)},critical(...n){console.error(t,...n)},uninitializedWarning(t){e.error(`You must initialize PostHog before calling ${t}`)},createLogger:(n,e)=>Q(`${t} ${n}`,e)};return e},X=Q("[PostHog.js]").createLogger;X("[Error tracking]");const Z=X("[ExceptionAutocapture]"),tt=new class{constructor(t,n,e=[]){this.coercers=t,this.stackParser=n,this.modifiers=e}buildFromUnknown(t,n={}){const e=n&&n.mechanism||{handled:!0,type:"generic"},r=this.buildCoercingContext(e,n,0).apply(t),o=this.buildParsingContext(n),i=this.parseStacktrace(r,o);return{$exception_list:this.convertToExceptionList(i,e),$exception_level:"error"}}modifyFrames(t){var n=this;return y(function*(){for(const e of t)e.stacktrace&&e.stacktrace.frames&&i(e.stacktrace.frames)&&(e.stacktrace.frames=yield n.applyModifiers(e.stacktrace.frames));return t})()}coerceFallback(t){var n;return{type:"Error",value:"Unknown error",stack:null===(n=t.syntheticException)||void 0===n?void 0:n.stack,synthetic:!0}}parseStacktrace(t,n){var e;let r,o;if(null!=t.cause&&(r=this.parseStacktrace(t.cause,n)),""!=t.stack&&null!=t.stack)try{o=this.applyChunkIds(this.stackParser(t.stack,t.synthetic?n.skipFirstLines:0),n.chunkIdMap)}catch(t){}return w(w({},t),{},{cause:r,stack:o,errors:null===(e=t.errors)||void 0===e?void 0:e.map(t=>this.parseStacktrace(t,n))})}applyChunkIds(t,n){return t.map(t=>(t.filename&&n&&(t.chunk_id=n[t.filename]),t))}applyCoercers(t,n){for(const e of this.coercers)if(e.match(t))return e.coerce(t,n);return this.coerceFallback(n)}applyModifiers(t){var n=this;return y(function*(){let e=t;for(const t of n.modifiers)e=yield t(e);return e})()}convertToExceptionList(t,n){const e=[],r=(t,o,i)=>{var c;const s=e.length,u=void 0===o?w(w({type:"string"==typeof n.type&&n.type.length>0?n.type:"generic"},"boolean"==typeof n.handled?{handled:n.handled}:void 0===n.handled?{handled:!0}:{}),{},{synthetic:"boolean"==typeof n.synthetic?n.synthetic:t.synthetic,exception_id:s}):{type:"chained",source:i,synthetic:t.synthetic,exception_id:s,parent_id:o},a={type:t.type,value:t.value,mechanism:u};t.stack&&(a.stacktrace={type:"raw",frames:t.stack}),e.push(a),t.cause&&r(t.cause,s,"cause");for(const n of null!==(c=t.errors)&&void 0!==c?c:[])r(n,s,"member")};return r(t),e}buildParsingContext(t){var n;return{chunkIdMap:k(this.stackParser),skipFirstLines:null!==(n=t.skipFirstLines)&&void 0!==n?n:1}}getAggregateErrors(t){try{if(d(t)){let e=Object.getPrototypeOf(t);for(let r=0;e&&100>r;r++){var n;const r=null===(n=Object.getOwnPropertyDescriptor(e,"constructor"))||void 0===n?void 0:n.value;if("function"==typeof r&&"AggregateError"===r.name){const n=t.errors;return i(n)?n:void 0}e=Object.getPrototypeOf(e)}}}catch(t){}}buildCoercingContext(t,n,e=0){let r=0,o=0,i=!1;const c=new Set,s=[],u={},a=(t,n,e=0)=>{const a=l(n,e),f=a.apply;if(a.apply=n=>{s.push(t);try{return f(n)}finally{s.pop()}},e>4||e>0&&-1!==s.indexOf(t))return this.coerceFallback(a);const d="object"==typeof t&&null!==t||"function"==typeof t;if(0===e&&r>=50||d&&c.has(t))return;d&&c.add(t),0===e&&r++;const v=this.getAggregateErrors(t);let h,p;i||(i=!!v);try{if(h=this.applyCoercers(t,a),!h)throw u}catch(t){if(t===u)return void(0===e&&r--);if(!i)throw t;h=this.coerceFallback(a)}if(!v||r>=50)return h;try{p=v.length}catch(t){return h}if(!Number.isInteger(p)||0>p||p>4294967295)return h;const y=[];for(let t=0;50>r&&1e3>o&&p>t;t++){let e;o++;try{e=a.next(v[t])}catch(t){r++,e=this.coerceFallback(l(n+1))}e&&y.push(e)}return w(w({},h),{},{errors:y})},l=(e,r=0)=>w(w({},n),{},{syntheticException:0==e?n.syntheticException:void 0,mechanism:0==e?t:{},apply(t){const n=a(t,e,r+1);if(!n)throw u;return n},next:t=>a(t,e+1)}),f=l(e);return w(w({},f),{},{apply:t=>{var n;return null!==(n=a(t,e))&&void 0!==n?n:this.coerceFallback(f)}})}}([new class{match(t){return this.isDOMException(t)||this.isDOMError(t)}coerce(t,n){const e=u(t.stack);return{type:this.getType(t),value:this.getValue(t),stack:e?t.stack:void 0,cause:t.cause?n.next(t.cause):void 0,synthetic:!1}}getType(t){return this.isDOMError(t)?"DOMError":"DOMException"}getValue(t){const n=t.name||(this.isDOMError(t)?"DOMError":"DOMException");return t.message?`${n}: ${t.message}`:n}isDOMException(t){return f(t,"DOMException")}isDOMError(t){return f(t,"DOMError")}},new class{match(t){return f(t,"PromiseRejectionEvent")||this.isCustomEventWrappingRejection(t)}isCustomEventWrappingRejection(t){if(!v(t))return!1;try{const n=t.detail;return null!=n&&"object"==typeof n&&"reason"in n}catch(t){return!1}}coerce(t,n){var e;const r=this.getUnhandledRejectionReason(t);return l(r)?{type:"UnhandledRejection",value:`Non-Error promise rejection captured with value: ${String(r)}`,stack:null===(e=n.syntheticException)||void 0===e?void 0:e.stack,synthetic:!0}:n.apply(r)}getUnhandledRejectionReason(t){try{if("reason"in t)return t.reason;if("detail"in t&&null!=t.detail&&"object"==typeof t.detail&&"reason"in t.detail)return t.detail.reason}catch(t){}return t}},new class{match(t){return!!f(t,"ErrorEvent")&&(null!=t.error||this.hr(t))}coerce(t,n){var e;if(null!=t.error)return n.apply(t.error);const r=n.apply(t.message);return w(w({},r),{},{stack:null!==(e=this.vr(t))&&void 0!==e?e:r.stack,synthetic:!0})}hr(t){return u(t.message)&&t.message.length>0}vr(t){var n,e;const r=t,o=null!==(n=r.lineno)&&void 0!==n?n:0,i=null!==(e=r.colno)&&void 0!==e?e:0;if(u(r.filename)&&0!==r.filename.length&&0!==o)return`Error\n at ${r.filename}:${o}:${i}`}},new class{match(t){return d(t)}coerce(t,n){var e;const r=this.getStack(t),o=void 0===r?null===(e=n.syntheticException)||void 0===e?void 0:e.stack:void 0,i=!!o;return{type:this.getType(t),value:this.getMessage(t,n),stack:null!=r?r:o,cause:t.cause?n.next(t.cause):void 0,synthetic:i}}getType(t){return t.name||t.constructor.name}getMessage(t,n){const e=t.message;return String(e.error&&"string"==typeof e.error.message?e.error.message:e)}getStack(t){try{const n=t.stacktrace;if("string"==typeof n&&n.length>0)return n;const e=t.stack;return"string"==typeof e&&e.length>0?e:void 0}catch(t){return}}},new class{match(t){return v(t)}coerce(t,n){var e;const r=t.constructor.name;return{type:r,value:`${r} captured as exception with keys: ${J(t)}`,stack:null===(e=n.syntheticException)||void 0===e?void 0:e.stack,synthetic:!0}}},new class{match(t){return"object"==typeof t&&null!==t}coerce(t,n){var e,r;const o=this.getErrorPropertyFromObject(t);return o?n.apply(o):{type:this.getType(t),value:this.getValue(t),stack:null!==(e=this.getStack(t))&&void 0!==e?e:null===(r=n.syntheticException)||void 0===r?void 0:r.stack,level:this.isSeverityLevel(t.level)?t.level:"error",synthetic:!0}}getType(t){if(v(t))return t.constructor.name;const n="name"in t?t.name:void 0;return u(n)&&!a(n)?n:"Error"}getValue(t){if("name"in t&&"string"==typeof t.name){let n=`'${t.name}' captured as exception`;return"message"in t&&"string"==typeof t.message&&(n+=` with message: '${t.message}'`),n}if("message"in t&&"string"==typeof t.message)return t.message;const n=this.getObjectClassName(t);return`${n&&"Object"!==n?`'${n}'`:"Object"} captured as exception with keys: ${J(t)}`}isSeverityLevel(t){return u(t)&&!a(t)&&G.indexOf(t)>=0}getStack(t){try{return u(t.stacktrace)&&t.stacktrace.length>0?t.stacktrace:u(t.stack)&&t.stack.length>0?t.stack:void 0}catch(t){return}}getErrorPropertyFromObject(t){for(const n in t)if({}.hasOwnProperty.call(t,n)){const e=t[n];if(d(e))return e}}getObjectClassName(t){try{const n=Object.getPrototypeOf(t);return n?n.constructor.name:void 0}catch(t){return}}},new class{match(t){return"string"==typeof t}coerce(t,n){var e;const[r,o]=this.getInfos(t);return{type:null!=r?r:"Error",value:null!=o?o:t,stack:null===(e=n.syntheticException)||void 0===e?void 0:e.stack,synthetic:!0}}getInfos(t){let n="Error",e=t;const r=t.match(B);return r&&(n=r[1],e=r[2]),[n,e]}},new class{match(t){return l(t)}coerce(t,n){var e;return{type:"Error",value:`Primitive value captured as exception: ${String(t)}`,stack:null===(e=n.syntheticException)||void 0===e?void 0:e.stack,synthetic:!0}}}],function(t,...n){return(e,r=0)=>{const o=[],i=e.split("\n"),c=Math.min(i.length,r+1e3),s=[];for(let e=r;c>e;e++){const r=i[e];if(r.length>1024)continue;const c=L.test(r)?r.replace(L,"$1"):r;if(!c.match(/\S*Error: /)){for(const e of n){const n=e(c,t);if(n){o.push(n);const t=N(o);if(t){for(let n=s.length-1;n>=0;n--){const e=s[n];if(t.start>=e.start+e.length)break;s.pop()}s.push(t)}break}}if(Y(o,s)>=50)break}}const u=[...s].sort((t,n)=>n.start-t.start),a=u[u.length-1];if(a){const t=V(o,a);o.splice(a.start-t,t);for(const n of u)n.start-=t}for(const t of u)W(o,t);return s.some(t=>0===t.start&&o.length===t.length)&&function(t){const n=t.map(t=>`${t.function}|${t.filename}|${t.lineno}|${t.colno}|${t.module}`);let e=0;for(let t=1;n.length>t;t++)q(n,t,e)&&(e=t);t.push(...t.splice(0,e))}(o),function(t){if(!t.length)return[];const n=Array.from(t);return n.reverse(),n.slice(0,50).map(t=>{return w(w({},t),{},{filename:t.filename||(e=n,e[e.length-1]||{}).filename,function:t.function||"?"});var e})}(o)}}("web:javascript",F,I)),nt=(t,n)=>{try{return t()}catch(t){return n}},et=(t,n)=>nt(()=>t(n()),void 0),rt=t=>nt(()=>c(t)&&c(t.call),!1),ot=(t,n)=>nt(()=>{t[n].__POSTHOG_INSTRUMENTED__=!0},void 0),it=(t,n,e)=>{nt(()=>{var e;null===(e=t[n])||void 0===e||delete e.__POSTHOG_INSTRUMENTED__},void 0),nt(()=>{t[n]=e},void 0)},ct=n=>{const e=t;e||Z.info("window not available, cannot wrap onerror");const r=nt(()=>e.onerror,void 0);return e.onerror=function(...t){var e;return et(n,()=>tt.buildFromUnknown((([t,n,e,r,o])=>{if(null!=o)return o;if(u(t)&&u(n)&&n.length>0&&"undefined"!=typeof ErrorEvent)try{return new ErrorEvent("error",{message:t,filename:n,lineno:e,colno:r})}catch(n){return t}return t})(t),{mechanism:{handled:!1}})),!!rt(r)&&null!==(e=r(...t))&&void 0!==e&&e},ot(e,"onerror"),()=>it(e,"onerror",r)},st=(n,e=!1)=>{const r=t;r||Z.info("window not available, cannot wrap onUnhandledRejection");const o=nt(()=>r.onunhandledrejection,void 0);return r.onunhandledrejection=function(t){var r;return et(n,()=>tt.buildFromUnknown(t,{mechanism:{handled:!1}})),rt(o)?null!==(r=o(t))&&void 0!==r&&r:e},ot(r,"onunhandledrejection"),()=>it(r,"onunhandledrejection",o)},ut={wrapOnError:ct,wrapUnhandledRejection:st,wrapConsoleError(t){const n=console;n||Z.info("console not available, cannot wrap console.error");const e=nt(()=>n.error,void 0);return n.error=function(...n){nt(()=>n.length>1&&u(n[0])&&(/^\[PostHog\.js\](?: |$)/.test(n[0])||"rrweb logger error:"===n[0]),!1)||et(t,()=>{let t;t=1==n.length?n[0]:n.join(" ");const e=n.find(t=>t instanceof Error);return tt.buildFromUnknown(e||t,{mechanism:{handled:!0,type:"onconsole"},syntheticException:new Error("PostHog syntheticException"),skipFirstLines:2})}),rt(e)&&e(...n)},ot(n,"error"),()=>it(n,"error",e)}};r.__PosthogExtensions__=r.__PosthogExtensions__||{},r.__PosthogExtensions__.errorWrappingFunctions=ut,r.posthogErrorWrappingFunctions=ut;const at=(t,n)=>{if(!c(null==t?void 0:t.capture))return;const e=null==n?void 0:n.autocaptureExceptions,r=null!==e&&"object"==typeof e&&i(e.errors_to_ignore)?e.errors_to_ignore.reduce((t,n)=>{try{t.push(new RegExp(n))}catch(t){Z.error("Ignoring invalid legacy exception exclusion rule",n,t)}return t},[]):[],o=n=>{try{var e;null===(e=t.capture)||void 0===e||e.call(t,"$exception",n,{_noTruncate:!0,_batchKey:"exceptionEvent",_noHeatmaps:!0})}catch(t){Z.error("Failed to capture exception for a legacy client",t)}};ct(t=>{r.some(n=>t.$exception_list.some(t=>n.test(t.value||"")))||o(t)}),st(o,!0)};r.extendPostHogWithExceptionAutoCapture=at,r.extendPostHogWithExceptionAutocapture=at}();
//# sourceMappingURL=exception-autocapture.js.map