userlens-analytics-sdk 0.1.88 → 0.1.89

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/dist/main.cjs.js CHANGED
@@ -5,5 +5,5 @@
5
5
  * MIT License | (c) Dustin Diaz 2012-2015
6
6
  * MIT License | (c) Denis Demchenko 2015-2019
7
7
  */class g{static getParser(e,t=!1){if("string"!=typeof e)throw new Error("UserAgent should be a string");return new p(e,t)}static parse(e){return new p(e).getResult()}static get BROWSER_MAP(){return s}static get ENGINE_MAP(){return o}static get OS_MAP(){return n}static get PLATFORMS_MAP(){return i}}var f="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function m(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var b,w={exports:{}},v=w.exports;
8
- /*! https://mths.be/cssescape v1.5.1 by @mathias | MIT license */function y(){return b||(b=1,w.exports=function(e){if(e.CSS&&e.CSS.escape)return e.CSS.escape;var t=function(e){if(0==arguments.length)throw new TypeError("`CSS.escape` requires an argument.");for(var t,r=String(e),s=r.length,i=-1,n="",o=r.charCodeAt(0);++i<s;)0!=(t=r.charCodeAt(i))?n+=t>=1&&t<=31||127==t||0==i&&t>=48&&t<=57||1==i&&t>=48&&t<=57&&45==o?"\\"+t.toString(16)+" ":0==i&&1==s&&45==t||!(t>=128||45==t||95==t||t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122)?"\\"+r.charAt(i):r.charAt(i):n+="�";return n};return e.CSS||(e.CSS={}),e.CSS.escape=t,t}(void 0!==f?f:v)),w.exports}var M,S,E,_={};var T=m(function(){if(E)return S;E=1,y();const{ShadowRootTypes:e,nodeNameInCorrectCase:t,NodeType:r}=(M||(M=1,_.nodeNameInCorrectCase=function(e){const t=e.shadowRoot&&e.shadowRoot.mode;return t?"#shadow-root ("+t+")":e.localName?e.localName.length!==e.nodeName.length?e.nodeName:e.localName:e.nodeName},_.shadowRootType=function(e){const t=e.ancestorShadowRoot();return t?t.mode:null},_.NodeType={ELEMENT_NODE:1,ATTRIBUTE_NODE:2,TEXT_NODE:3,CDATA_SECTION_NODE:4,PROCESSING_INSTRUCTION_NODE:7,COMMENT_NODE:8,DOCUMENT_NODE:9},_.ShadowRootTypes={UserAgent:"user-agent",Open:"open",Closed:"closed"}),_);let s={DOMPath:{}};return s.DOMPath.fullQualifiedSelector=function(e,t){try{return e.nodeType!==r.ELEMENT_NODE?e.localName||e.nodeName.toLowerCase():s.DOMPath.cssPath(e,t)}catch(e){return null}},s.DOMPath.cssPath=function(e,t){if(e.nodeType!==r.ELEMENT_NODE)return"";const i=[];let n=e;for(;n;){const r=s.DOMPath._cssPathStep(n,!!t,n===e);if(!r)break;if(i.push(r),r.optimized)break;n=n.parentNode}return i.reverse(),i.join(" > ")},s.DOMPath.canGetJSPath=function(t){let r=t;for(;r;){if(r.shadowRoot&&r.shadowRoot.mode!==e.Open)return!1;r=r.shadowRoot&&r.shadowRoot.host}return!0},s.DOMPath.jsPath=function(e,t){if(e.nodeType!==r.ELEMENT_NODE)return"";const i=[];let n=e;for(;n;)i.push(s.DOMPath.cssPath(n,t)),n=n.shadowRoot&&n.shadowRoot.host;i.reverse();let o="";for(let e=0;e<i.length;++e){const t=JSON.stringify(i[e]);o+=e?`.shadowRoot.querySelector(${t})`:`document.querySelector(${t})`}return o},s.DOMPath._cssPathStep=function(e,i,n){if(e.nodeType!==r.ELEMENT_NODE)return null;const o=e.getAttribute("id");if(i){if(o)return new s.DOMPath.Step(d(o),!0);const r=e.nodeName.toLowerCase();if("body"===r||"head"===r||"html"===r)return new s.DOMPath.Step(t(e),!0)}const a=t(e);if(o)return new s.DOMPath.Step(a+d(o),!0);const c=e.parentNode;if(!c||c.nodeType===r.DOCUMENT_NODE)return new s.DOMPath.Step(a,!0);function l(e){const t=e.getAttribute("class");return t?t.split(/\s+/g).filter(Boolean).map(function(e){return"$"+e}):[]}function d(e){return"#"+CSS.escape(e)}const u=l(e);let h=!1,p=!1,g=-1,f=-1;const m=c.children;for(let s=0;(-1===g||!p)&&s<m.length;++s){const i=m[s];if(i.nodeType!==r.ELEMENT_NODE)continue;if(f+=1,i===e){g=f;continue}if(p)continue;if(t(i)!==a)continue;h=!0;const n=new Set(u);if(!n.size){p=!0;continue}const o=l(i);for(let e=0;e<o.length;++e){const t=o[e];if(n.has(t)&&(n.delete(t),!n.size)){p=!0;break}}}let b=a;if(n&&"input"===a.toLowerCase()&&e.getAttribute("type")&&!e.getAttribute("id")&&!e.getAttribute("class")&&(b+="[type="+CSS.escape(e.getAttribute("type"))+"]"),p)b+=":nth-child("+(g+1)+")";else if(h)for(const e of u)b+="."+CSS.escape(e.slice(1));return new s.DOMPath.Step(b,!1)},s.DOMPath.xPath=function(e,t){if(e.nodeType===r.DOCUMENT_NODE)return"/";const i=[];let n=e;for(;n;){const e=s.DOMPath._xPathValue(n,t);if(!e)break;if(i.push(e),e.optimized)break;n=n.parentNode}return i.reverse(),(i.length&&i[0].optimized?"":"/")+i.join("/")},s.DOMPath._xPathValue=function(e,t){let i;const n=s.DOMPath._xPathIndex(e);if(-1===n)return null;switch(e.nodeType){case r.ELEMENT_NODE:if(t&&e.getAttribute("id"))return new s.DOMPath.Step('//*[@id="'+e.getAttribute("id")+'"]',!0);i=e.localName;break;case r.ATTRIBUTE_NODE:i="@"+e.nodeName;break;case r.TEXT_NODE:case r.CDATA_SECTION_NODE:i="text()";break;case r.PROCESSING_INSTRUCTION_NODE:i="processing-instruction()";break;case r.COMMENT_NODE:i="comment()";break;case r.DOCUMENT_NODE:default:i=""}return n>0&&(i+="["+n+"]"),new s.DOMPath.Step(i,e.nodeType===r.DOCUMENT_NODE)},s.DOMPath._xPathIndex=function(e){function t(e,t){if(e===t)return!0;if(e.nodeType===r.ELEMENT_NODE&&t.nodeType===r.ELEMENT_NODE)return e.localName===t.localName;if(e.nodeType===t.nodeType)return!0;return(e.nodeType===r.CDATA_SECTION_NODE?r.TEXT_NODE:e.nodeType)===(t.nodeType===r.CDATA_SECTION_NODE?r.TEXT_NODE:t.nodeType)}const s=e.parentNode?e.parentNode.children:null;if(!s)return 0;let i;for(let r=0;r<s.length;++r)if(t(e,s[r])&&s[r]!==e){i=!0;break}if(!i)return 0;let n=1;for(let r=0;r<s.length;++r)if(t(e,s[r])){if(s[r]===e)return n;++n}return-1},s.DOMPath.Step=class{constructor(e,t){this.value=e,this.optimized=t||!1}toString(){return this.value}},S=s.DOMPath}());const O="https://events.userlens.io",N=()=>{try{const e=window.localStorage.getItem("$ul_WRITE_CODE");if(null==e)return null;const t=e.trim();if(!t)return null;const r=t.toLowerCase();return"null"===r||"undefined"===r?null:t}catch{return null}},k=async(e,t=!1)=>{if(!(null==e?void 0:e.userId))return;const r=N();if(!r)return void(t&&console.error("Failed to identify user: Userlens SDK error: WRITE_CODE is not set"));const s={type:"identify",userId:e.userId,source:"userlens-js-analytics-sdk",traits:null==e?void 0:e.traits};if(!(await fetch(`${O}/event`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(s)})).ok)throw new Error("Userlens HTTP error: failed to identify");return"ok"},C=async(e,t=!1)=>{if(!(null==e?void 0:e.groupId))return;const r=N();if(!r)return void(t&&console.error("Failed to group identify: Userlens SDK error: WRITE_CODE is not set"));const{groupId:s,userId:i,traits:n}=e,o={type:"group",groupId:s,source:"userlens-js-analytics-sdk",...i&&{userId:i},...n&&{traits:n}};if(!(await fetch(`${O}/event`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(o)})).ok)throw new Error("Userlens HTTP error: failed to identify");return"ok"},F=async(e,t=!1)=>{const r=N();if(!r)return void(t&&console.error("Failed to group identify: Userlens SDK error: WRITE_CODE is not set"));const s={events:e};if(!(await fetch("https://raw.userlens.io/raw/event",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(s)})).ok)throw new Error("Userlens HTTP error: failed to track");return"ok"};var P,B,A,D,I,x,R,L,U,W,$,z,j,V,q,G,H,Q,K;B=new WeakMap,A=new WeakMap,D=new WeakMap,I=new WeakMap,x=new WeakMap,q=new WeakMap,P=new WeakSet,R=function(){this.debug&&console.log("Userlens EventCollector: adding click event listener"),document.addEventListener("click",e(this,I,"f"),!0),this.debug&&console.log("Userlens EventCollector: click event listener added")},L=function(t){try{const r=t.target;if(!(r instanceof HTMLElement))return;const s=T.xPath(r,!0),i=this.useLighterSnapshot?e(this,P,"m",U).call(this,r):e(this,P,"m",W).call(this,r),n={event:s,is_raw:!0,snapshot:i?[i]:[],properties:{...this.getUserContext(),...this.getPageMetadata()}};this.userId&&(n.userId=this.userId),this.events.push(n),this.events.length>100&&(this.events=this.events.slice(-100))}catch(e){console.warn("Userlens EventCollector error: click event handling failed",e)}},U=function(t){if(!(t instanceof HTMLElement))return null;const r=document.body;if(!r)return null;const s=[];let i=t;for(;i&&1===i.nodeType&&i!==r;)s.unshift(i),i=i.parentElement;const n=[r,...s.filter(e=>e!==r)];let o=null,a=null;for(let t=0;t<n.length;t++){const r=n[t],s=t===n.length-1,i=e(this,P,"m",$).call(this,r,{isTarget:s,leadsToTarget:!s});o||(o=i),a&&(a.children||(a.children=[]),a.children.push(i)),a=i}const c=a;return c&&t.children.length>0&&(c.children=Array.from(t.children).filter(e=>e instanceof HTMLElement).map(t=>e(this,P,"m",$).call(this,t,{includeChildren:!0})).filter(Boolean)),o},W=function(t){if(!(t instanceof HTMLElement))return null;const r=[];let s=t;for(;s&&1===s.nodeType;)r.unshift(s),s=s.parentElement;let i=null,n=null;for(let t=0;t<r.length;t++){const s=r[t],o=t>=r.length-3,a=t===r.length-1,c=e(this,P,"m",$).call(this,s,{isTarget:a,leadsToTarget:!0});let l=[c];if(o&&s.parentElement){l=[c,...Array.from(s.parentElement.children).filter(e=>e!==s&&e instanceof HTMLElement).map(t=>e(this,P,"m",$).call(this,t,{includeChildren:!0})).filter(Boolean)]}i||(i=c),n&&(n.children||(n.children=[]),n.children.push(...l)),n=c}return i},$=function t(r,{isTarget:s=!1,includeChildren:i=!1,leadsToTarget:n=!1}={}){var o,a;const c=r.tagName.toLowerCase(),l=r.classList.length?Array.from(r.classList):null,d=r.id||null,u=r.getAttribute("href")||null,h=Array.from((null===(o=r.parentNode)||void 0===o?void 0:o.children)||[]).indexOf(r)+1,p=Array.from((null===(a=r.parentNode)||void 0===a?void 0:a.children)||[]).filter(e=>e instanceof HTMLElement&&e.tagName===r.tagName).indexOf(r)+1,g={};for(let e of Array.from(r.attributes))g[`attr__${e.name}`]=e.value;const f=Array.from(r.childNodes).filter(e=>e.nodeType===Node.TEXT_NODE).map(e=>{var t;return null===(t=e.textContent)||void 0===t?void 0:t.trim()}).filter(Boolean).join(" ")||null,m={tag_name:c,nth_child:h,nth_of_type:p,attributes:g,...l?{attr_class:l}:{},...d?{attr_id:d}:{},...u?{href:u}:{},...f?{text:f}:{},...s?{is_target:!0}:{},...n&&!s?{leads_to_target:!0}:{}};return(i&&r.children.length>0||s)&&(m.children=Array.from(r.children).filter(e=>e instanceof HTMLElement).map(r=>e(this,P,"m",t).call(this,r,{includeChildren:!0})).filter(Boolean)),m},z=function(){t(this,B,setInterval(()=>{e(this,q,"f").call(this)},this.intervalTime),"f")},j=function(){t(this,A,history.pushState,"f"),t(this,D,history.replaceState,"f"),history.pushState=(...t)=>{e(this,A,"f").apply(history,t),e(this,P,"m",V).call(this)},history.replaceState=(...t)=>{e(this,D,"f").apply(history,t),e(this,P,"m",V).call(this)},window.addEventListener("popstate",e(this,x,"f"))},V=function(){if(function(){if("undefined"==typeof window)return!1;const e=window.location.hostname;return"localhost"===e||"127.0.0.1"===e||"::1"===e||e.endsWith(".localhost")}())return;const e={event:"$ul_pageview",properties:this.getPageMetadata()};this.userId&&(e.userId=this.userId),this.events.push(e)},G=function(){this.events=[]},H=function(){document.removeEventListener("click",e(this,I,"f"),!0)},Q=function(){e(this,q,"f").call(this),clearInterval(e(this,B,"f")),e(this,P,"m",G).call(this)},K=function(){history.pushState=e(this,A,"f"),history.replaceState=e(this,D,"f"),window.removeEventListener("popstate",e(this,x,"f"))},exports.EventCollector=class{constructor(t){if(P.add(this),this.useLighterSnapshot=!1,this.userContext=null,B.set(this,void 0),A.set(this,void 0),D.set(this,void 0),I.set(this,e(this,P,"m",L).bind(this)),x.set(this,e(this,P,"m",V).bind(this)),q.set(this,()=>{if(0===this.events.length)return;const t=[...this.events];if(this.callback){try{this.callback(t)}catch(e){}e(this,P,"m",G).call(this)}else Promise.allSettled([this.userId&&this.userTraits?k({userId:this.userId,traits:this.userTraits},this.debug):null,this.groupId?C({groupId:this.groupId,traits:this.groupTraits,userId:this.userId},this.debug):null,F(t,this.debug)]),e(this,P,"m",G).call(this)}),"undefined"==typeof window)return void console.error("Userlens EventCollector error: unavailable outside of browser environment.");const{userId:r,WRITE_CODE:s,callback:i,intervalTime:n=5e3,skipRawEvents:o=!1,useLighterSnapshot:a=!1,debug:c=!1}=t,l=t.userTraits,d=t.groupId,u=t.groupTraits;var h;(this.autoUploadModeEnabled=!i,!this.autoUploadModeEnabled||(null==r?void 0:r.length))?!this.autoUploadModeEnabled||(null==s?void 0:s.length)?(this.autoUploadModeEnabled&&(h=s,window.localStorage.setItem("$ul_WRITE_CODE",btoa(`${h}:`))),this.autoUploadModeEnabled||"function"==typeof i?(this.userId=r,this.userTraits="object"==typeof l&&null!==l?l:{},this.callback=i,this.intervalTime=n,this.events=[],this.debug=c,d&&(this.groupId=d,this.groupTraits="object"==typeof u&&null!==u?u:{}),o||(this.useLighterSnapshot=a,e(this,P,"m",R).call(this),e(this,P,"m",j).call(this)),e(this,P,"m",z).call(this),this.userContext=this.getUserContext()):this.debug&&console.error("Userlens EventCollector error: callback is not a function.")):this.debug&&console.error("Userlens EventCollector error: WRITE_CODE is missing."):this.debug&&console.error("Userlens EventCollector error: userId is missing.")}pushEvent(e){const t={is_raw:!1,...e,properties:{...null==e?void 0:e.properties,...this.getUserContext(),...this.getPageMetadata()}};this.userId&&(t.userId=this.userId),this.events.push(t)}identify(e,t){return k({userId:e,traits:t})}group(e,t){return C({groupId:e,traits:t,userId:this.userId})}updateUserTraits(e){this.userTraits=e}updateGroupTraits(e){this.groupTraits=e}stop(){e(this,P,"m",H).call(this),e(this,P,"m",Q).call(this),e(this,P,"m",K).call(this)}getUserContext(){var e,t,r,s,i,n,o;if(this.userContext)return this.userContext;const a=g.getParser(window.navigator.userAgent),c=a.getBrowser(),l=a.getOS(),d={$ul_browser:null!==(e=c.name)&&void 0!==e?e:"Unknown",$ul_browser_version:null!==(t=c.version)&&void 0!==t?t:"Unknown",$ul_os:null!==(r=l.name)&&void 0!==r?r:"Unknown",$ul_os_version:null!==(s=l.versionName)&&void 0!==s?s:"Unknown",$ul_browser_language:null!==(i=navigator.language)&&void 0!==i?i:"en-US",$ul_browser_language_prefix:null!==(o=null===(n=navigator.language)||void 0===n?void 0:n.split("-")[0])&&void 0!==o?o:"en",$ul_screen_width:window.screen.width,$ul_screen_height:window.screen.height,$ul_viewport_width:window.innerWidth,$ul_viewport_height:window.innerHeight,$ul_lib:"userlens.js",$ul_lib_version:"0.1.88",$ul_device_type:/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop",$ul_timezone:Intl.DateTimeFormat().resolvedOptions().timeZone};return this.userContext=d,d}getPageMetadata(){try{const e=new URL(window.location.href);let t=document.referrer||"$direct",r="$direct";try{t&&/^https?:\/\//.test(t)&&(r=new URL(t).hostname)}catch(e){}const s=e.search.slice(1);return{$ul_page:e.origin+e.pathname,$ul_pathname:e.pathname,$ul_host:e.host,$ul_referrer:t,$ul_referring_domain:r,$ul_query:s}}catch(e){return{$ul_page:"",$ul_pathname:"",$ul_host:"",$ul_referrer:"",$ul_referring_domain:"",$ul_query:""}}}};
8
+ /*! https://mths.be/cssescape v1.5.1 by @mathias | MIT license */function y(){return b||(b=1,w.exports=function(e){if(e.CSS&&e.CSS.escape)return e.CSS.escape;var t=function(e){if(0==arguments.length)throw new TypeError("`CSS.escape` requires an argument.");for(var t,r=String(e),s=r.length,i=-1,n="",o=r.charCodeAt(0);++i<s;)0!=(t=r.charCodeAt(i))?n+=t>=1&&t<=31||127==t||0==i&&t>=48&&t<=57||1==i&&t>=48&&t<=57&&45==o?"\\"+t.toString(16)+" ":0==i&&1==s&&45==t||!(t>=128||45==t||95==t||t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122)?"\\"+r.charAt(i):r.charAt(i):n+="�";return n};return e.CSS||(e.CSS={}),e.CSS.escape=t,t}(void 0!==f?f:v)),w.exports}var M,S,E,_={};var T=m(function(){if(E)return S;E=1,y();const{ShadowRootTypes:e,nodeNameInCorrectCase:t,NodeType:r}=(M||(M=1,_.nodeNameInCorrectCase=function(e){const t=e.shadowRoot&&e.shadowRoot.mode;return t?"#shadow-root ("+t+")":e.localName?e.localName.length!==e.nodeName.length?e.nodeName:e.localName:e.nodeName},_.shadowRootType=function(e){const t=e.ancestorShadowRoot();return t?t.mode:null},_.NodeType={ELEMENT_NODE:1,ATTRIBUTE_NODE:2,TEXT_NODE:3,CDATA_SECTION_NODE:4,PROCESSING_INSTRUCTION_NODE:7,COMMENT_NODE:8,DOCUMENT_NODE:9},_.ShadowRootTypes={UserAgent:"user-agent",Open:"open",Closed:"closed"}),_);let s={DOMPath:{}};return s.DOMPath.fullQualifiedSelector=function(e,t){try{return e.nodeType!==r.ELEMENT_NODE?e.localName||e.nodeName.toLowerCase():s.DOMPath.cssPath(e,t)}catch(e){return null}},s.DOMPath.cssPath=function(e,t){if(e.nodeType!==r.ELEMENT_NODE)return"";const i=[];let n=e;for(;n;){const r=s.DOMPath._cssPathStep(n,!!t,n===e);if(!r)break;if(i.push(r),r.optimized)break;n=n.parentNode}return i.reverse(),i.join(" > ")},s.DOMPath.canGetJSPath=function(t){let r=t;for(;r;){if(r.shadowRoot&&r.shadowRoot.mode!==e.Open)return!1;r=r.shadowRoot&&r.shadowRoot.host}return!0},s.DOMPath.jsPath=function(e,t){if(e.nodeType!==r.ELEMENT_NODE)return"";const i=[];let n=e;for(;n;)i.push(s.DOMPath.cssPath(n,t)),n=n.shadowRoot&&n.shadowRoot.host;i.reverse();let o="";for(let e=0;e<i.length;++e){const t=JSON.stringify(i[e]);o+=e?`.shadowRoot.querySelector(${t})`:`document.querySelector(${t})`}return o},s.DOMPath._cssPathStep=function(e,i,n){if(e.nodeType!==r.ELEMENT_NODE)return null;const o=e.getAttribute("id");if(i){if(o)return new s.DOMPath.Step(d(o),!0);const r=e.nodeName.toLowerCase();if("body"===r||"head"===r||"html"===r)return new s.DOMPath.Step(t(e),!0)}const a=t(e);if(o)return new s.DOMPath.Step(a+d(o),!0);const c=e.parentNode;if(!c||c.nodeType===r.DOCUMENT_NODE)return new s.DOMPath.Step(a,!0);function l(e){const t=e.getAttribute("class");return t?t.split(/\s+/g).filter(Boolean).map(function(e){return"$"+e}):[]}function d(e){return"#"+CSS.escape(e)}const u=l(e);let h=!1,p=!1,g=-1,f=-1;const m=c.children;for(let s=0;(-1===g||!p)&&s<m.length;++s){const i=m[s];if(i.nodeType!==r.ELEMENT_NODE)continue;if(f+=1,i===e){g=f;continue}if(p)continue;if(t(i)!==a)continue;h=!0;const n=new Set(u);if(!n.size){p=!0;continue}const o=l(i);for(let e=0;e<o.length;++e){const t=o[e];if(n.has(t)&&(n.delete(t),!n.size)){p=!0;break}}}let b=a;if(n&&"input"===a.toLowerCase()&&e.getAttribute("type")&&!e.getAttribute("id")&&!e.getAttribute("class")&&(b+="[type="+CSS.escape(e.getAttribute("type"))+"]"),p)b+=":nth-child("+(g+1)+")";else if(h)for(const e of u)b+="."+CSS.escape(e.slice(1));return new s.DOMPath.Step(b,!1)},s.DOMPath.xPath=function(e,t){if(e.nodeType===r.DOCUMENT_NODE)return"/";const i=[];let n=e;for(;n;){const e=s.DOMPath._xPathValue(n,t);if(!e)break;if(i.push(e),e.optimized)break;n=n.parentNode}return i.reverse(),(i.length&&i[0].optimized?"":"/")+i.join("/")},s.DOMPath._xPathValue=function(e,t){let i;const n=s.DOMPath._xPathIndex(e);if(-1===n)return null;switch(e.nodeType){case r.ELEMENT_NODE:if(t&&e.getAttribute("id"))return new s.DOMPath.Step('//*[@id="'+e.getAttribute("id")+'"]',!0);i=e.localName;break;case r.ATTRIBUTE_NODE:i="@"+e.nodeName;break;case r.TEXT_NODE:case r.CDATA_SECTION_NODE:i="text()";break;case r.PROCESSING_INSTRUCTION_NODE:i="processing-instruction()";break;case r.COMMENT_NODE:i="comment()";break;case r.DOCUMENT_NODE:default:i=""}return n>0&&(i+="["+n+"]"),new s.DOMPath.Step(i,e.nodeType===r.DOCUMENT_NODE)},s.DOMPath._xPathIndex=function(e){function t(e,t){if(e===t)return!0;if(e.nodeType===r.ELEMENT_NODE&&t.nodeType===r.ELEMENT_NODE)return e.localName===t.localName;if(e.nodeType===t.nodeType)return!0;return(e.nodeType===r.CDATA_SECTION_NODE?r.TEXT_NODE:e.nodeType)===(t.nodeType===r.CDATA_SECTION_NODE?r.TEXT_NODE:t.nodeType)}const s=e.parentNode?e.parentNode.children:null;if(!s)return 0;let i;for(let r=0;r<s.length;++r)if(t(e,s[r])&&s[r]!==e){i=!0;break}if(!i)return 0;let n=1;for(let r=0;r<s.length;++r)if(t(e,s[r])){if(s[r]===e)return n;++n}return-1},s.DOMPath.Step=class{constructor(e,t){this.value=e,this.optimized=t||!1}toString(){return this.value}},S=s.DOMPath}());const O="https://events.userlens.io",N=()=>{try{const e=window.localStorage.getItem("$ul_WRITE_CODE");if(null==e)return null;const t=e.trim();if(!t)return null;const r=t.toLowerCase();return"null"===r||"undefined"===r?null:t}catch{return null}},k=async(e,t=!1)=>{if(!(null==e?void 0:e.userId))return;const r=N();if(!r)return void(t&&console.error("Failed to identify user: Userlens SDK error: WRITE_CODE is not set"));const s={type:"identify",userId:e.userId,source:"userlens-js-analytics-sdk",traits:null==e?void 0:e.traits};if(!(await fetch(`${O}/event`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(s)})).ok)throw new Error("Userlens HTTP error: failed to identify");return"ok"},C=async(e,t=!1)=>{if(!(null==e?void 0:e.groupId))return;const r=N();if(!r)return void(t&&console.error("Failed to group identify: Userlens SDK error: WRITE_CODE is not set"));const{groupId:s,userId:i,traits:n}=e,o={type:"group",groupId:s,source:"userlens-js-analytics-sdk",...i&&{userId:i},...n&&{traits:n}};if(!(await fetch(`${O}/event`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(o)})).ok)throw new Error("Userlens HTTP error: failed to identify");return"ok"},F=async(e,t=!1)=>{const r=N();if(!r)return void(t&&console.error("Failed to group identify: Userlens SDK error: WRITE_CODE is not set"));const s={events:e};if(!(await fetch("https://raw.userlens.io/raw/event",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(s)})).ok)throw new Error("Userlens HTTP error: failed to track");return"ok"};var P,B,A,D,I,x,R,L,U,W,$,z,j,V,q,G,H,Q,K;B=new WeakMap,A=new WeakMap,D=new WeakMap,I=new WeakMap,x=new WeakMap,q=new WeakMap,P=new WeakSet,R=function(){this.debug&&console.log("Userlens EventCollector: adding click event listener"),document.addEventListener("click",e(this,I,"f"),!0),this.debug&&console.log("Userlens EventCollector: click event listener added")},L=function(t){try{const r=t.target;if(!(r instanceof HTMLElement))return;const s=T.xPath(r,!0),i=this.useLighterSnapshot?e(this,P,"m",U).call(this,r):e(this,P,"m",W).call(this,r),n={event:s,is_raw:!0,snapshot:i?[i]:[],properties:{...this.getUserContext(),...this.getPageMetadata()}};this.userId&&(n.userId=this.userId),this.events.push(n),this.events.length>100&&(this.events=this.events.slice(-100))}catch(e){console.warn("Userlens EventCollector error: click event handling failed",e)}},U=function(t){if(!(t instanceof HTMLElement))return null;const r=document.body;if(!r)return null;const s=[];let i=t;for(;i&&1===i.nodeType&&i!==r;)s.unshift(i),i=i.parentElement;const n=[r,...s.filter(e=>e!==r)];let o=null,a=null;for(let t=0;t<n.length;t++){const r=n[t],s=t===n.length-1,i=e(this,P,"m",$).call(this,r,{isTarget:s,leadsToTarget:!s});o||(o=i),a&&(a.children||(a.children=[]),a.children.push(i)),a=i}const c=a;return c&&t.children.length>0&&(c.children=Array.from(t.children).filter(e=>e instanceof HTMLElement).map(t=>e(this,P,"m",$).call(this,t,{includeChildren:!0})).filter(Boolean)),o},W=function(t){if(!(t instanceof HTMLElement))return null;const r=[];let s=t;for(;s&&1===s.nodeType;)r.unshift(s),s=s.parentElement;let i=null,n=null;for(let t=0;t<r.length;t++){const s=r[t],o=t>=r.length-3,a=t===r.length-1,c=e(this,P,"m",$).call(this,s,{isTarget:a,leadsToTarget:!0});let l=[c];if(o&&s.parentElement){l=[c,...Array.from(s.parentElement.children).filter(e=>e!==s&&e instanceof HTMLElement).map(t=>e(this,P,"m",$).call(this,t,{includeChildren:!0})).filter(Boolean)]}i||(i=c),n&&(n.children||(n.children=[]),n.children.push(...l)),n=c}return i},$=function t(r,{isTarget:s=!1,includeChildren:i=!1,leadsToTarget:n=!1}={}){var o,a;const c=r.tagName.toLowerCase(),l=r.classList.length?Array.from(r.classList):null,d=r.id||null,u=r.getAttribute("href")||null,h=Array.from((null===(o=r.parentNode)||void 0===o?void 0:o.children)||[]).indexOf(r)+1,p=Array.from((null===(a=r.parentNode)||void 0===a?void 0:a.children)||[]).filter(e=>e instanceof HTMLElement&&e.tagName===r.tagName).indexOf(r)+1,g={};for(let e of Array.from(r.attributes))g[`attr__${e.name}`]=e.value;const f=Array.from(r.childNodes).filter(e=>e.nodeType===Node.TEXT_NODE).map(e=>{var t;return null===(t=e.textContent)||void 0===t?void 0:t.trim()}).filter(Boolean).join(" ")||null,m={tag_name:c,nth_child:h,nth_of_type:p,attributes:g,...l?{attr_class:l}:{},...d?{attr_id:d}:{},...u?{href:u}:{},...f?{text:f}:{},...s?{is_target:!0}:{},...n&&!s?{leads_to_target:!0}:{}};return(i&&r.children.length>0||s)&&(m.children=Array.from(r.children).filter(e=>e instanceof HTMLElement).map(r=>e(this,P,"m",t).call(this,r,{includeChildren:!0})).filter(Boolean)),m},z=function(){t(this,B,setInterval(()=>{e(this,q,"f").call(this)},this.intervalTime),"f")},j=function(){t(this,A,history.pushState,"f"),t(this,D,history.replaceState,"f"),history.pushState=(...t)=>{e(this,A,"f").apply(history,t),e(this,P,"m",V).call(this)},history.replaceState=(...t)=>{e(this,D,"f").apply(history,t),e(this,P,"m",V).call(this)},window.addEventListener("popstate",e(this,x,"f"))},V=function(){if(function(){if("undefined"==typeof window)return!1;const e=window.location.hostname;return"localhost"===e||"127.0.0.1"===e||"::1"===e||e.endsWith(".localhost")}())return;const e={event:"$ul_pageview",properties:this.getPageMetadata()};this.userId&&(e.userId=this.userId),this.events.push(e)},G=function(){this.events=[]},H=function(){document.removeEventListener("click",e(this,I,"f"),!0)},Q=function(){e(this,q,"f").call(this),clearInterval(e(this,B,"f")),e(this,P,"m",G).call(this)},K=function(){history.pushState=e(this,A,"f"),history.replaceState=e(this,D,"f"),window.removeEventListener("popstate",e(this,x,"f"))},exports.EventCollector=class{constructor(t){if(P.add(this),this.useLighterSnapshot=!1,this.userContext=null,B.set(this,void 0),A.set(this,void 0),D.set(this,void 0),I.set(this,e(this,P,"m",L).bind(this)),x.set(this,e(this,P,"m",V).bind(this)),q.set(this,()=>{if(0===this.events.length)return;const t=[...this.events];if(this.callback){try{this.callback(t)}catch(e){}e(this,P,"m",G).call(this)}else Promise.allSettled([this.userId&&this.userTraits?k({userId:this.userId,traits:this.userTraits},this.debug):null,this.groupId?C({groupId:this.groupId,traits:this.groupTraits,userId:this.userId},this.debug):null,F(t,this.debug)]),e(this,P,"m",G).call(this)}),"undefined"==typeof window)return void console.error("Userlens EventCollector error: unavailable outside of browser environment.");const{userId:r,WRITE_CODE:s,callback:i,intervalTime:n=5e3,skipRawEvents:o=!1,useLighterSnapshot:a=!1,debug:c=!1}=t,l=t.userTraits,d=t.groupId,u=t.groupTraits;var h;(this.autoUploadModeEnabled=!i,!this.autoUploadModeEnabled||(null==r?void 0:r.length))?!this.autoUploadModeEnabled||(null==s?void 0:s.length)?(this.autoUploadModeEnabled&&(h=s,window.localStorage.setItem("$ul_WRITE_CODE",btoa(`${h}:`))),this.autoUploadModeEnabled||"function"==typeof i?(this.userId=r,this.userTraits="object"==typeof l&&null!==l?l:{},this.callback=i,this.intervalTime=n,this.events=[],this.debug=c,d&&(this.groupId=d,this.groupTraits="object"==typeof u&&null!==u?u:{}),o||(this.useLighterSnapshot=a,e(this,P,"m",R).call(this),e(this,P,"m",j).call(this)),e(this,P,"m",z).call(this),this.userContext=this.getUserContext()):this.debug&&console.error("Userlens EventCollector error: callback is not a function.")):this.debug&&console.error("Userlens EventCollector error: WRITE_CODE is missing."):this.debug&&console.error("Userlens EventCollector error: userId is missing.")}pushEvent(e){const t={is_raw:!1,...e,properties:{...null==e?void 0:e.properties,...this.getUserContext(),...this.getPageMetadata()}};this.userId&&(t.userId=this.userId),this.events.push(t)}identify(e,t){return k({userId:e,traits:t})}group(e,t){return C({groupId:e,traits:t,userId:this.userId})}updateUserTraits(e){this.userTraits=e}updateGroupTraits(e){this.groupTraits=e}stop(){e(this,P,"m",H).call(this),e(this,P,"m",Q).call(this),e(this,P,"m",K).call(this)}getUserContext(){var e,t,r,s,i,n,o;if(this.userContext)return this.userContext;const a=g.getParser(window.navigator.userAgent),c=a.getBrowser(),l=a.getOS(),d={$ul_browser:null!==(e=c.name)&&void 0!==e?e:"Unknown",$ul_browser_version:null!==(t=c.version)&&void 0!==t?t:"Unknown",$ul_os:null!==(r=l.name)&&void 0!==r?r:"Unknown",$ul_os_version:null!==(s=l.versionName)&&void 0!==s?s:"Unknown",$ul_browser_language:null!==(i=navigator.language)&&void 0!==i?i:"en-US",$ul_browser_language_prefix:null!==(o=null===(n=navigator.language)||void 0===n?void 0:n.split("-")[0])&&void 0!==o?o:"en",$ul_screen_width:window.screen.width,$ul_screen_height:window.screen.height,$ul_viewport_width:window.innerWidth,$ul_viewport_height:window.innerHeight,$ul_lib:"userlens.js",$ul_lib_version:"0.1.89",$ul_device_type:/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop",$ul_timezone:Intl.DateTimeFormat().resolvedOptions().timeZone};return this.userContext=d,d}getPageMetadata(){try{const e=new URL(window.location.href);let t=document.referrer||"$direct",r="$direct";try{t&&/^https?:\/\//.test(t)&&(r=new URL(t).hostname)}catch(e){}const s=e.search.slice(1);return{$ul_page:e.origin+e.pathname,$ul_pathname:e.pathname,$ul_host:e.host,$ul_referrer:t,$ul_referring_domain:r,$ul_query:s}}catch(e){return{$ul_page:"",$ul_pathname:"",$ul_host:"",$ul_referrer:"",$ul_referring_domain:"",$ul_query:""}}}};
9
9
  //# sourceMappingURL=main.cjs.js.map
package/dist/main.esm.js CHANGED
@@ -5,5 +5,5 @@ function e(e,t,r,s){if("a"===r&&!s)throw new TypeError("Private accessor was def
5
5
  * MIT License | (c) Dustin Diaz 2012-2015
6
6
  * MIT License | (c) Denis Demchenko 2015-2019
7
7
  */class g{static getParser(e,t=!1){if("string"!=typeof e)throw new Error("UserAgent should be a string");return new p(e,t)}static parse(e){return new p(e).getResult()}static get BROWSER_MAP(){return s}static get ENGINE_MAP(){return o}static get OS_MAP(){return n}static get PLATFORMS_MAP(){return i}}var f="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function m(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var b,w={exports:{}},v=w.exports;
8
- /*! https://mths.be/cssescape v1.5.1 by @mathias | MIT license */function y(){return b||(b=1,w.exports=function(e){if(e.CSS&&e.CSS.escape)return e.CSS.escape;var t=function(e){if(0==arguments.length)throw new TypeError("`CSS.escape` requires an argument.");for(var t,r=String(e),s=r.length,i=-1,n="",o=r.charCodeAt(0);++i<s;)0!=(t=r.charCodeAt(i))?n+=t>=1&&t<=31||127==t||0==i&&t>=48&&t<=57||1==i&&t>=48&&t<=57&&45==o?"\\"+t.toString(16)+" ":0==i&&1==s&&45==t||!(t>=128||45==t||95==t||t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122)?"\\"+r.charAt(i):r.charAt(i):n+="�";return n};return e.CSS||(e.CSS={}),e.CSS.escape=t,t}(void 0!==f?f:v)),w.exports}var M,S,E,_={};var T=m(function(){if(E)return S;E=1,y();const{ShadowRootTypes:e,nodeNameInCorrectCase:t,NodeType:r}=(M||(M=1,_.nodeNameInCorrectCase=function(e){const t=e.shadowRoot&&e.shadowRoot.mode;return t?"#shadow-root ("+t+")":e.localName?e.localName.length!==e.nodeName.length?e.nodeName:e.localName:e.nodeName},_.shadowRootType=function(e){const t=e.ancestorShadowRoot();return t?t.mode:null},_.NodeType={ELEMENT_NODE:1,ATTRIBUTE_NODE:2,TEXT_NODE:3,CDATA_SECTION_NODE:4,PROCESSING_INSTRUCTION_NODE:7,COMMENT_NODE:8,DOCUMENT_NODE:9},_.ShadowRootTypes={UserAgent:"user-agent",Open:"open",Closed:"closed"}),_);let s={DOMPath:{}};return s.DOMPath.fullQualifiedSelector=function(e,t){try{return e.nodeType!==r.ELEMENT_NODE?e.localName||e.nodeName.toLowerCase():s.DOMPath.cssPath(e,t)}catch(e){return null}},s.DOMPath.cssPath=function(e,t){if(e.nodeType!==r.ELEMENT_NODE)return"";const i=[];let n=e;for(;n;){const r=s.DOMPath._cssPathStep(n,!!t,n===e);if(!r)break;if(i.push(r),r.optimized)break;n=n.parentNode}return i.reverse(),i.join(" > ")},s.DOMPath.canGetJSPath=function(t){let r=t;for(;r;){if(r.shadowRoot&&r.shadowRoot.mode!==e.Open)return!1;r=r.shadowRoot&&r.shadowRoot.host}return!0},s.DOMPath.jsPath=function(e,t){if(e.nodeType!==r.ELEMENT_NODE)return"";const i=[];let n=e;for(;n;)i.push(s.DOMPath.cssPath(n,t)),n=n.shadowRoot&&n.shadowRoot.host;i.reverse();let o="";for(let e=0;e<i.length;++e){const t=JSON.stringify(i[e]);o+=e?`.shadowRoot.querySelector(${t})`:`document.querySelector(${t})`}return o},s.DOMPath._cssPathStep=function(e,i,n){if(e.nodeType!==r.ELEMENT_NODE)return null;const o=e.getAttribute("id");if(i){if(o)return new s.DOMPath.Step(l(o),!0);const r=e.nodeName.toLowerCase();if("body"===r||"head"===r||"html"===r)return new s.DOMPath.Step(t(e),!0)}const a=t(e);if(o)return new s.DOMPath.Step(a+l(o),!0);const c=e.parentNode;if(!c||c.nodeType===r.DOCUMENT_NODE)return new s.DOMPath.Step(a,!0);function d(e){const t=e.getAttribute("class");return t?t.split(/\s+/g).filter(Boolean).map(function(e){return"$"+e}):[]}function l(e){return"#"+CSS.escape(e)}const u=d(e);let h=!1,p=!1,g=-1,f=-1;const m=c.children;for(let s=0;(-1===g||!p)&&s<m.length;++s){const i=m[s];if(i.nodeType!==r.ELEMENT_NODE)continue;if(f+=1,i===e){g=f;continue}if(p)continue;if(t(i)!==a)continue;h=!0;const n=new Set(u);if(!n.size){p=!0;continue}const o=d(i);for(let e=0;e<o.length;++e){const t=o[e];if(n.has(t)&&(n.delete(t),!n.size)){p=!0;break}}}let b=a;if(n&&"input"===a.toLowerCase()&&e.getAttribute("type")&&!e.getAttribute("id")&&!e.getAttribute("class")&&(b+="[type="+CSS.escape(e.getAttribute("type"))+"]"),p)b+=":nth-child("+(g+1)+")";else if(h)for(const e of u)b+="."+CSS.escape(e.slice(1));return new s.DOMPath.Step(b,!1)},s.DOMPath.xPath=function(e,t){if(e.nodeType===r.DOCUMENT_NODE)return"/";const i=[];let n=e;for(;n;){const e=s.DOMPath._xPathValue(n,t);if(!e)break;if(i.push(e),e.optimized)break;n=n.parentNode}return i.reverse(),(i.length&&i[0].optimized?"":"/")+i.join("/")},s.DOMPath._xPathValue=function(e,t){let i;const n=s.DOMPath._xPathIndex(e);if(-1===n)return null;switch(e.nodeType){case r.ELEMENT_NODE:if(t&&e.getAttribute("id"))return new s.DOMPath.Step('//*[@id="'+e.getAttribute("id")+'"]',!0);i=e.localName;break;case r.ATTRIBUTE_NODE:i="@"+e.nodeName;break;case r.TEXT_NODE:case r.CDATA_SECTION_NODE:i="text()";break;case r.PROCESSING_INSTRUCTION_NODE:i="processing-instruction()";break;case r.COMMENT_NODE:i="comment()";break;case r.DOCUMENT_NODE:default:i=""}return n>0&&(i+="["+n+"]"),new s.DOMPath.Step(i,e.nodeType===r.DOCUMENT_NODE)},s.DOMPath._xPathIndex=function(e){function t(e,t){if(e===t)return!0;if(e.nodeType===r.ELEMENT_NODE&&t.nodeType===r.ELEMENT_NODE)return e.localName===t.localName;if(e.nodeType===t.nodeType)return!0;return(e.nodeType===r.CDATA_SECTION_NODE?r.TEXT_NODE:e.nodeType)===(t.nodeType===r.CDATA_SECTION_NODE?r.TEXT_NODE:t.nodeType)}const s=e.parentNode?e.parentNode.children:null;if(!s)return 0;let i;for(let r=0;r<s.length;++r)if(t(e,s[r])&&s[r]!==e){i=!0;break}if(!i)return 0;let n=1;for(let r=0;r<s.length;++r)if(t(e,s[r])){if(s[r]===e)return n;++n}return-1},s.DOMPath.Step=class{constructor(e,t){this.value=e,this.optimized=t||!1}toString(){return this.value}},S=s.DOMPath}());const O="https://events.userlens.io",N=()=>{try{const e=window.localStorage.getItem("$ul_WRITE_CODE");if(null==e)return null;const t=e.trim();if(!t)return null;const r=t.toLowerCase();return"null"===r||"undefined"===r?null:t}catch{return null}},k=async(e,t=!1)=>{if(!(null==e?void 0:e.userId))return;const r=N();if(!r)return void(t&&console.error("Failed to identify user: Userlens SDK error: WRITE_CODE is not set"));const s={type:"identify",userId:e.userId,source:"userlens-js-analytics-sdk",traits:null==e?void 0:e.traits};if(!(await fetch(`${O}/event`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(s)})).ok)throw new Error("Userlens HTTP error: failed to identify");return"ok"},C=async(e,t=!1)=>{if(!(null==e?void 0:e.groupId))return;const r=N();if(!r)return void(t&&console.error("Failed to group identify: Userlens SDK error: WRITE_CODE is not set"));const{groupId:s,userId:i,traits:n}=e,o={type:"group",groupId:s,source:"userlens-js-analytics-sdk",...i&&{userId:i},...n&&{traits:n}};if(!(await fetch(`${O}/event`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(o)})).ok)throw new Error("Userlens HTTP error: failed to identify");return"ok"},F=async(e,t=!1)=>{const r=N();if(!r)return void(t&&console.error("Failed to group identify: Userlens SDK error: WRITE_CODE is not set"));const s={events:e};if(!(await fetch("https://raw.userlens.io/raw/event",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(s)})).ok)throw new Error("Userlens HTTP error: failed to track");return"ok"};var P,B,A,D,I,x,R,L,U,W,$,z,j,V,q,G,H,Q,K;class J{constructor(t){if(P.add(this),this.useLighterSnapshot=!1,this.userContext=null,B.set(this,void 0),A.set(this,void 0),D.set(this,void 0),I.set(this,e(this,P,"m",L).bind(this)),x.set(this,e(this,P,"m",V).bind(this)),q.set(this,()=>{if(0===this.events.length)return;const t=[...this.events];if(this.callback){try{this.callback(t)}catch(e){}e(this,P,"m",G).call(this)}else Promise.allSettled([this.userId&&this.userTraits?k({userId:this.userId,traits:this.userTraits},this.debug):null,this.groupId?C({groupId:this.groupId,traits:this.groupTraits,userId:this.userId},this.debug):null,F(t,this.debug)]),e(this,P,"m",G).call(this)}),"undefined"==typeof window)return void console.error("Userlens EventCollector error: unavailable outside of browser environment.");const{userId:r,WRITE_CODE:s,callback:i,intervalTime:n=5e3,skipRawEvents:o=!1,useLighterSnapshot:a=!1,debug:c=!1}=t,d=t.userTraits,l=t.groupId,u=t.groupTraits;var h;(this.autoUploadModeEnabled=!i,!this.autoUploadModeEnabled||(null==r?void 0:r.length))?!this.autoUploadModeEnabled||(null==s?void 0:s.length)?(this.autoUploadModeEnabled&&(h=s,window.localStorage.setItem("$ul_WRITE_CODE",btoa(`${h}:`))),this.autoUploadModeEnabled||"function"==typeof i?(this.userId=r,this.userTraits="object"==typeof d&&null!==d?d:{},this.callback=i,this.intervalTime=n,this.events=[],this.debug=c,l&&(this.groupId=l,this.groupTraits="object"==typeof u&&null!==u?u:{}),o||(this.useLighterSnapshot=a,e(this,P,"m",R).call(this),e(this,P,"m",j).call(this)),e(this,P,"m",z).call(this),this.userContext=this.getUserContext()):this.debug&&console.error("Userlens EventCollector error: callback is not a function.")):this.debug&&console.error("Userlens EventCollector error: WRITE_CODE is missing."):this.debug&&console.error("Userlens EventCollector error: userId is missing.")}pushEvent(e){const t={is_raw:!1,...e,properties:{...null==e?void 0:e.properties,...this.getUserContext(),...this.getPageMetadata()}};this.userId&&(t.userId=this.userId),this.events.push(t)}identify(e,t){return k({userId:e,traits:t})}group(e,t){return C({groupId:e,traits:t,userId:this.userId})}updateUserTraits(e){this.userTraits=e}updateGroupTraits(e){this.groupTraits=e}stop(){e(this,P,"m",H).call(this),e(this,P,"m",Q).call(this),e(this,P,"m",K).call(this)}getUserContext(){var e,t,r,s,i,n,o;if(this.userContext)return this.userContext;const a=g.getParser(window.navigator.userAgent),c=a.getBrowser(),d=a.getOS(),l={$ul_browser:null!==(e=c.name)&&void 0!==e?e:"Unknown",$ul_browser_version:null!==(t=c.version)&&void 0!==t?t:"Unknown",$ul_os:null!==(r=d.name)&&void 0!==r?r:"Unknown",$ul_os_version:null!==(s=d.versionName)&&void 0!==s?s:"Unknown",$ul_browser_language:null!==(i=navigator.language)&&void 0!==i?i:"en-US",$ul_browser_language_prefix:null!==(o=null===(n=navigator.language)||void 0===n?void 0:n.split("-")[0])&&void 0!==o?o:"en",$ul_screen_width:window.screen.width,$ul_screen_height:window.screen.height,$ul_viewport_width:window.innerWidth,$ul_viewport_height:window.innerHeight,$ul_lib:"userlens.js",$ul_lib_version:"0.1.88",$ul_device_type:/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop",$ul_timezone:Intl.DateTimeFormat().resolvedOptions().timeZone};return this.userContext=l,l}getPageMetadata(){try{const e=new URL(window.location.href);let t=document.referrer||"$direct",r="$direct";try{t&&/^https?:\/\//.test(t)&&(r=new URL(t).hostname)}catch(e){}const s=e.search.slice(1);return{$ul_page:e.origin+e.pathname,$ul_pathname:e.pathname,$ul_host:e.host,$ul_referrer:t,$ul_referring_domain:r,$ul_query:s}}catch(e){return{$ul_page:"",$ul_pathname:"",$ul_host:"",$ul_referrer:"",$ul_referring_domain:"",$ul_query:""}}}}B=new WeakMap,A=new WeakMap,D=new WeakMap,I=new WeakMap,x=new WeakMap,q=new WeakMap,P=new WeakSet,R=function(){this.debug&&console.log("Userlens EventCollector: adding click event listener"),document.addEventListener("click",e(this,I,"f"),!0),this.debug&&console.log("Userlens EventCollector: click event listener added")},L=function(t){try{const r=t.target;if(!(r instanceof HTMLElement))return;const s=T.xPath(r,!0),i=this.useLighterSnapshot?e(this,P,"m",U).call(this,r):e(this,P,"m",W).call(this,r),n={event:s,is_raw:!0,snapshot:i?[i]:[],properties:{...this.getUserContext(),...this.getPageMetadata()}};this.userId&&(n.userId=this.userId),this.events.push(n),this.events.length>100&&(this.events=this.events.slice(-100))}catch(e){console.warn("Userlens EventCollector error: click event handling failed",e)}},U=function(t){if(!(t instanceof HTMLElement))return null;const r=document.body;if(!r)return null;const s=[];let i=t;for(;i&&1===i.nodeType&&i!==r;)s.unshift(i),i=i.parentElement;const n=[r,...s.filter(e=>e!==r)];let o=null,a=null;for(let t=0;t<n.length;t++){const r=n[t],s=t===n.length-1,i=e(this,P,"m",$).call(this,r,{isTarget:s,leadsToTarget:!s});o||(o=i),a&&(a.children||(a.children=[]),a.children.push(i)),a=i}const c=a;return c&&t.children.length>0&&(c.children=Array.from(t.children).filter(e=>e instanceof HTMLElement).map(t=>e(this,P,"m",$).call(this,t,{includeChildren:!0})).filter(Boolean)),o},W=function(t){if(!(t instanceof HTMLElement))return null;const r=[];let s=t;for(;s&&1===s.nodeType;)r.unshift(s),s=s.parentElement;let i=null,n=null;for(let t=0;t<r.length;t++){const s=r[t],o=t>=r.length-3,a=t===r.length-1,c=e(this,P,"m",$).call(this,s,{isTarget:a,leadsToTarget:!0});let d=[c];if(o&&s.parentElement){d=[c,...Array.from(s.parentElement.children).filter(e=>e!==s&&e instanceof HTMLElement).map(t=>e(this,P,"m",$).call(this,t,{includeChildren:!0})).filter(Boolean)]}i||(i=c),n&&(n.children||(n.children=[]),n.children.push(...d)),n=c}return i},$=function t(r,{isTarget:s=!1,includeChildren:i=!1,leadsToTarget:n=!1}={}){var o,a;const c=r.tagName.toLowerCase(),d=r.classList.length?Array.from(r.classList):null,l=r.id||null,u=r.getAttribute("href")||null,h=Array.from((null===(o=r.parentNode)||void 0===o?void 0:o.children)||[]).indexOf(r)+1,p=Array.from((null===(a=r.parentNode)||void 0===a?void 0:a.children)||[]).filter(e=>e instanceof HTMLElement&&e.tagName===r.tagName).indexOf(r)+1,g={};for(let e of Array.from(r.attributes))g[`attr__${e.name}`]=e.value;const f=Array.from(r.childNodes).filter(e=>e.nodeType===Node.TEXT_NODE).map(e=>{var t;return null===(t=e.textContent)||void 0===t?void 0:t.trim()}).filter(Boolean).join(" ")||null,m={tag_name:c,nth_child:h,nth_of_type:p,attributes:g,...d?{attr_class:d}:{},...l?{attr_id:l}:{},...u?{href:u}:{},...f?{text:f}:{},...s?{is_target:!0}:{},...n&&!s?{leads_to_target:!0}:{}};return(i&&r.children.length>0||s)&&(m.children=Array.from(r.children).filter(e=>e instanceof HTMLElement).map(r=>e(this,P,"m",t).call(this,r,{includeChildren:!0})).filter(Boolean)),m},z=function(){t(this,B,setInterval(()=>{e(this,q,"f").call(this)},this.intervalTime),"f")},j=function(){t(this,A,history.pushState,"f"),t(this,D,history.replaceState,"f"),history.pushState=(...t)=>{e(this,A,"f").apply(history,t),e(this,P,"m",V).call(this)},history.replaceState=(...t)=>{e(this,D,"f").apply(history,t),e(this,P,"m",V).call(this)},window.addEventListener("popstate",e(this,x,"f"))},V=function(){if(function(){if("undefined"==typeof window)return!1;const e=window.location.hostname;return"localhost"===e||"127.0.0.1"===e||"::1"===e||e.endsWith(".localhost")}())return;const e={event:"$ul_pageview",properties:this.getPageMetadata()};this.userId&&(e.userId=this.userId),this.events.push(e)},G=function(){this.events=[]},H=function(){document.removeEventListener("click",e(this,I,"f"),!0)},Q=function(){e(this,q,"f").call(this),clearInterval(e(this,B,"f")),e(this,P,"m",G).call(this)},K=function(){history.pushState=e(this,A,"f"),history.replaceState=e(this,D,"f"),window.removeEventListener("popstate",e(this,x,"f"))};export{J as EventCollector};
8
+ /*! https://mths.be/cssescape v1.5.1 by @mathias | MIT license */function y(){return b||(b=1,w.exports=function(e){if(e.CSS&&e.CSS.escape)return e.CSS.escape;var t=function(e){if(0==arguments.length)throw new TypeError("`CSS.escape` requires an argument.");for(var t,r=String(e),s=r.length,i=-1,n="",o=r.charCodeAt(0);++i<s;)0!=(t=r.charCodeAt(i))?n+=t>=1&&t<=31||127==t||0==i&&t>=48&&t<=57||1==i&&t>=48&&t<=57&&45==o?"\\"+t.toString(16)+" ":0==i&&1==s&&45==t||!(t>=128||45==t||95==t||t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122)?"\\"+r.charAt(i):r.charAt(i):n+="�";return n};return e.CSS||(e.CSS={}),e.CSS.escape=t,t}(void 0!==f?f:v)),w.exports}var M,S,E,_={};var T=m(function(){if(E)return S;E=1,y();const{ShadowRootTypes:e,nodeNameInCorrectCase:t,NodeType:r}=(M||(M=1,_.nodeNameInCorrectCase=function(e){const t=e.shadowRoot&&e.shadowRoot.mode;return t?"#shadow-root ("+t+")":e.localName?e.localName.length!==e.nodeName.length?e.nodeName:e.localName:e.nodeName},_.shadowRootType=function(e){const t=e.ancestorShadowRoot();return t?t.mode:null},_.NodeType={ELEMENT_NODE:1,ATTRIBUTE_NODE:2,TEXT_NODE:3,CDATA_SECTION_NODE:4,PROCESSING_INSTRUCTION_NODE:7,COMMENT_NODE:8,DOCUMENT_NODE:9},_.ShadowRootTypes={UserAgent:"user-agent",Open:"open",Closed:"closed"}),_);let s={DOMPath:{}};return s.DOMPath.fullQualifiedSelector=function(e,t){try{return e.nodeType!==r.ELEMENT_NODE?e.localName||e.nodeName.toLowerCase():s.DOMPath.cssPath(e,t)}catch(e){return null}},s.DOMPath.cssPath=function(e,t){if(e.nodeType!==r.ELEMENT_NODE)return"";const i=[];let n=e;for(;n;){const r=s.DOMPath._cssPathStep(n,!!t,n===e);if(!r)break;if(i.push(r),r.optimized)break;n=n.parentNode}return i.reverse(),i.join(" > ")},s.DOMPath.canGetJSPath=function(t){let r=t;for(;r;){if(r.shadowRoot&&r.shadowRoot.mode!==e.Open)return!1;r=r.shadowRoot&&r.shadowRoot.host}return!0},s.DOMPath.jsPath=function(e,t){if(e.nodeType!==r.ELEMENT_NODE)return"";const i=[];let n=e;for(;n;)i.push(s.DOMPath.cssPath(n,t)),n=n.shadowRoot&&n.shadowRoot.host;i.reverse();let o="";for(let e=0;e<i.length;++e){const t=JSON.stringify(i[e]);o+=e?`.shadowRoot.querySelector(${t})`:`document.querySelector(${t})`}return o},s.DOMPath._cssPathStep=function(e,i,n){if(e.nodeType!==r.ELEMENT_NODE)return null;const o=e.getAttribute("id");if(i){if(o)return new s.DOMPath.Step(l(o),!0);const r=e.nodeName.toLowerCase();if("body"===r||"head"===r||"html"===r)return new s.DOMPath.Step(t(e),!0)}const a=t(e);if(o)return new s.DOMPath.Step(a+l(o),!0);const c=e.parentNode;if(!c||c.nodeType===r.DOCUMENT_NODE)return new s.DOMPath.Step(a,!0);function d(e){const t=e.getAttribute("class");return t?t.split(/\s+/g).filter(Boolean).map(function(e){return"$"+e}):[]}function l(e){return"#"+CSS.escape(e)}const u=d(e);let h=!1,p=!1,g=-1,f=-1;const m=c.children;for(let s=0;(-1===g||!p)&&s<m.length;++s){const i=m[s];if(i.nodeType!==r.ELEMENT_NODE)continue;if(f+=1,i===e){g=f;continue}if(p)continue;if(t(i)!==a)continue;h=!0;const n=new Set(u);if(!n.size){p=!0;continue}const o=d(i);for(let e=0;e<o.length;++e){const t=o[e];if(n.has(t)&&(n.delete(t),!n.size)){p=!0;break}}}let b=a;if(n&&"input"===a.toLowerCase()&&e.getAttribute("type")&&!e.getAttribute("id")&&!e.getAttribute("class")&&(b+="[type="+CSS.escape(e.getAttribute("type"))+"]"),p)b+=":nth-child("+(g+1)+")";else if(h)for(const e of u)b+="."+CSS.escape(e.slice(1));return new s.DOMPath.Step(b,!1)},s.DOMPath.xPath=function(e,t){if(e.nodeType===r.DOCUMENT_NODE)return"/";const i=[];let n=e;for(;n;){const e=s.DOMPath._xPathValue(n,t);if(!e)break;if(i.push(e),e.optimized)break;n=n.parentNode}return i.reverse(),(i.length&&i[0].optimized?"":"/")+i.join("/")},s.DOMPath._xPathValue=function(e,t){let i;const n=s.DOMPath._xPathIndex(e);if(-1===n)return null;switch(e.nodeType){case r.ELEMENT_NODE:if(t&&e.getAttribute("id"))return new s.DOMPath.Step('//*[@id="'+e.getAttribute("id")+'"]',!0);i=e.localName;break;case r.ATTRIBUTE_NODE:i="@"+e.nodeName;break;case r.TEXT_NODE:case r.CDATA_SECTION_NODE:i="text()";break;case r.PROCESSING_INSTRUCTION_NODE:i="processing-instruction()";break;case r.COMMENT_NODE:i="comment()";break;case r.DOCUMENT_NODE:default:i=""}return n>0&&(i+="["+n+"]"),new s.DOMPath.Step(i,e.nodeType===r.DOCUMENT_NODE)},s.DOMPath._xPathIndex=function(e){function t(e,t){if(e===t)return!0;if(e.nodeType===r.ELEMENT_NODE&&t.nodeType===r.ELEMENT_NODE)return e.localName===t.localName;if(e.nodeType===t.nodeType)return!0;return(e.nodeType===r.CDATA_SECTION_NODE?r.TEXT_NODE:e.nodeType)===(t.nodeType===r.CDATA_SECTION_NODE?r.TEXT_NODE:t.nodeType)}const s=e.parentNode?e.parentNode.children:null;if(!s)return 0;let i;for(let r=0;r<s.length;++r)if(t(e,s[r])&&s[r]!==e){i=!0;break}if(!i)return 0;let n=1;for(let r=0;r<s.length;++r)if(t(e,s[r])){if(s[r]===e)return n;++n}return-1},s.DOMPath.Step=class{constructor(e,t){this.value=e,this.optimized=t||!1}toString(){return this.value}},S=s.DOMPath}());const O="https://events.userlens.io",N=()=>{try{const e=window.localStorage.getItem("$ul_WRITE_CODE");if(null==e)return null;const t=e.trim();if(!t)return null;const r=t.toLowerCase();return"null"===r||"undefined"===r?null:t}catch{return null}},k=async(e,t=!1)=>{if(!(null==e?void 0:e.userId))return;const r=N();if(!r)return void(t&&console.error("Failed to identify user: Userlens SDK error: WRITE_CODE is not set"));const s={type:"identify",userId:e.userId,source:"userlens-js-analytics-sdk",traits:null==e?void 0:e.traits};if(!(await fetch(`${O}/event`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(s)})).ok)throw new Error("Userlens HTTP error: failed to identify");return"ok"},C=async(e,t=!1)=>{if(!(null==e?void 0:e.groupId))return;const r=N();if(!r)return void(t&&console.error("Failed to group identify: Userlens SDK error: WRITE_CODE is not set"));const{groupId:s,userId:i,traits:n}=e,o={type:"group",groupId:s,source:"userlens-js-analytics-sdk",...i&&{userId:i},...n&&{traits:n}};if(!(await fetch(`${O}/event`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(o)})).ok)throw new Error("Userlens HTTP error: failed to identify");return"ok"},F=async(e,t=!1)=>{const r=N();if(!r)return void(t&&console.error("Failed to group identify: Userlens SDK error: WRITE_CODE is not set"));const s={events:e};if(!(await fetch("https://raw.userlens.io/raw/event",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(s)})).ok)throw new Error("Userlens HTTP error: failed to track");return"ok"};var P,B,A,D,I,x,R,L,U,W,$,z,j,V,q,G,H,Q,K;class J{constructor(t){if(P.add(this),this.useLighterSnapshot=!1,this.userContext=null,B.set(this,void 0),A.set(this,void 0),D.set(this,void 0),I.set(this,e(this,P,"m",L).bind(this)),x.set(this,e(this,P,"m",V).bind(this)),q.set(this,()=>{if(0===this.events.length)return;const t=[...this.events];if(this.callback){try{this.callback(t)}catch(e){}e(this,P,"m",G).call(this)}else Promise.allSettled([this.userId&&this.userTraits?k({userId:this.userId,traits:this.userTraits},this.debug):null,this.groupId?C({groupId:this.groupId,traits:this.groupTraits,userId:this.userId},this.debug):null,F(t,this.debug)]),e(this,P,"m",G).call(this)}),"undefined"==typeof window)return void console.error("Userlens EventCollector error: unavailable outside of browser environment.");const{userId:r,WRITE_CODE:s,callback:i,intervalTime:n=5e3,skipRawEvents:o=!1,useLighterSnapshot:a=!1,debug:c=!1}=t,d=t.userTraits,l=t.groupId,u=t.groupTraits;var h;(this.autoUploadModeEnabled=!i,!this.autoUploadModeEnabled||(null==r?void 0:r.length))?!this.autoUploadModeEnabled||(null==s?void 0:s.length)?(this.autoUploadModeEnabled&&(h=s,window.localStorage.setItem("$ul_WRITE_CODE",btoa(`${h}:`))),this.autoUploadModeEnabled||"function"==typeof i?(this.userId=r,this.userTraits="object"==typeof d&&null!==d?d:{},this.callback=i,this.intervalTime=n,this.events=[],this.debug=c,l&&(this.groupId=l,this.groupTraits="object"==typeof u&&null!==u?u:{}),o||(this.useLighterSnapshot=a,e(this,P,"m",R).call(this),e(this,P,"m",j).call(this)),e(this,P,"m",z).call(this),this.userContext=this.getUserContext()):this.debug&&console.error("Userlens EventCollector error: callback is not a function.")):this.debug&&console.error("Userlens EventCollector error: WRITE_CODE is missing."):this.debug&&console.error("Userlens EventCollector error: userId is missing.")}pushEvent(e){const t={is_raw:!1,...e,properties:{...null==e?void 0:e.properties,...this.getUserContext(),...this.getPageMetadata()}};this.userId&&(t.userId=this.userId),this.events.push(t)}identify(e,t){return k({userId:e,traits:t})}group(e,t){return C({groupId:e,traits:t,userId:this.userId})}updateUserTraits(e){this.userTraits=e}updateGroupTraits(e){this.groupTraits=e}stop(){e(this,P,"m",H).call(this),e(this,P,"m",Q).call(this),e(this,P,"m",K).call(this)}getUserContext(){var e,t,r,s,i,n,o;if(this.userContext)return this.userContext;const a=g.getParser(window.navigator.userAgent),c=a.getBrowser(),d=a.getOS(),l={$ul_browser:null!==(e=c.name)&&void 0!==e?e:"Unknown",$ul_browser_version:null!==(t=c.version)&&void 0!==t?t:"Unknown",$ul_os:null!==(r=d.name)&&void 0!==r?r:"Unknown",$ul_os_version:null!==(s=d.versionName)&&void 0!==s?s:"Unknown",$ul_browser_language:null!==(i=navigator.language)&&void 0!==i?i:"en-US",$ul_browser_language_prefix:null!==(o=null===(n=navigator.language)||void 0===n?void 0:n.split("-")[0])&&void 0!==o?o:"en",$ul_screen_width:window.screen.width,$ul_screen_height:window.screen.height,$ul_viewport_width:window.innerWidth,$ul_viewport_height:window.innerHeight,$ul_lib:"userlens.js",$ul_lib_version:"0.1.89",$ul_device_type:/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop",$ul_timezone:Intl.DateTimeFormat().resolvedOptions().timeZone};return this.userContext=l,l}getPageMetadata(){try{const e=new URL(window.location.href);let t=document.referrer||"$direct",r="$direct";try{t&&/^https?:\/\//.test(t)&&(r=new URL(t).hostname)}catch(e){}const s=e.search.slice(1);return{$ul_page:e.origin+e.pathname,$ul_pathname:e.pathname,$ul_host:e.host,$ul_referrer:t,$ul_referring_domain:r,$ul_query:s}}catch(e){return{$ul_page:"",$ul_pathname:"",$ul_host:"",$ul_referrer:"",$ul_referring_domain:"",$ul_query:""}}}}B=new WeakMap,A=new WeakMap,D=new WeakMap,I=new WeakMap,x=new WeakMap,q=new WeakMap,P=new WeakSet,R=function(){this.debug&&console.log("Userlens EventCollector: adding click event listener"),document.addEventListener("click",e(this,I,"f"),!0),this.debug&&console.log("Userlens EventCollector: click event listener added")},L=function(t){try{const r=t.target;if(!(r instanceof HTMLElement))return;const s=T.xPath(r,!0),i=this.useLighterSnapshot?e(this,P,"m",U).call(this,r):e(this,P,"m",W).call(this,r),n={event:s,is_raw:!0,snapshot:i?[i]:[],properties:{...this.getUserContext(),...this.getPageMetadata()}};this.userId&&(n.userId=this.userId),this.events.push(n),this.events.length>100&&(this.events=this.events.slice(-100))}catch(e){console.warn("Userlens EventCollector error: click event handling failed",e)}},U=function(t){if(!(t instanceof HTMLElement))return null;const r=document.body;if(!r)return null;const s=[];let i=t;for(;i&&1===i.nodeType&&i!==r;)s.unshift(i),i=i.parentElement;const n=[r,...s.filter(e=>e!==r)];let o=null,a=null;for(let t=0;t<n.length;t++){const r=n[t],s=t===n.length-1,i=e(this,P,"m",$).call(this,r,{isTarget:s,leadsToTarget:!s});o||(o=i),a&&(a.children||(a.children=[]),a.children.push(i)),a=i}const c=a;return c&&t.children.length>0&&(c.children=Array.from(t.children).filter(e=>e instanceof HTMLElement).map(t=>e(this,P,"m",$).call(this,t,{includeChildren:!0})).filter(Boolean)),o},W=function(t){if(!(t instanceof HTMLElement))return null;const r=[];let s=t;for(;s&&1===s.nodeType;)r.unshift(s),s=s.parentElement;let i=null,n=null;for(let t=0;t<r.length;t++){const s=r[t],o=t>=r.length-3,a=t===r.length-1,c=e(this,P,"m",$).call(this,s,{isTarget:a,leadsToTarget:!0});let d=[c];if(o&&s.parentElement){d=[c,...Array.from(s.parentElement.children).filter(e=>e!==s&&e instanceof HTMLElement).map(t=>e(this,P,"m",$).call(this,t,{includeChildren:!0})).filter(Boolean)]}i||(i=c),n&&(n.children||(n.children=[]),n.children.push(...d)),n=c}return i},$=function t(r,{isTarget:s=!1,includeChildren:i=!1,leadsToTarget:n=!1}={}){var o,a;const c=r.tagName.toLowerCase(),d=r.classList.length?Array.from(r.classList):null,l=r.id||null,u=r.getAttribute("href")||null,h=Array.from((null===(o=r.parentNode)||void 0===o?void 0:o.children)||[]).indexOf(r)+1,p=Array.from((null===(a=r.parentNode)||void 0===a?void 0:a.children)||[]).filter(e=>e instanceof HTMLElement&&e.tagName===r.tagName).indexOf(r)+1,g={};for(let e of Array.from(r.attributes))g[`attr__${e.name}`]=e.value;const f=Array.from(r.childNodes).filter(e=>e.nodeType===Node.TEXT_NODE).map(e=>{var t;return null===(t=e.textContent)||void 0===t?void 0:t.trim()}).filter(Boolean).join(" ")||null,m={tag_name:c,nth_child:h,nth_of_type:p,attributes:g,...d?{attr_class:d}:{},...l?{attr_id:l}:{},...u?{href:u}:{},...f?{text:f}:{},...s?{is_target:!0}:{},...n&&!s?{leads_to_target:!0}:{}};return(i&&r.children.length>0||s)&&(m.children=Array.from(r.children).filter(e=>e instanceof HTMLElement).map(r=>e(this,P,"m",t).call(this,r,{includeChildren:!0})).filter(Boolean)),m},z=function(){t(this,B,setInterval(()=>{e(this,q,"f").call(this)},this.intervalTime),"f")},j=function(){t(this,A,history.pushState,"f"),t(this,D,history.replaceState,"f"),history.pushState=(...t)=>{e(this,A,"f").apply(history,t),e(this,P,"m",V).call(this)},history.replaceState=(...t)=>{e(this,D,"f").apply(history,t),e(this,P,"m",V).call(this)},window.addEventListener("popstate",e(this,x,"f"))},V=function(){if(function(){if("undefined"==typeof window)return!1;const e=window.location.hostname;return"localhost"===e||"127.0.0.1"===e||"::1"===e||e.endsWith(".localhost")}())return;const e={event:"$ul_pageview",properties:this.getPageMetadata()};this.userId&&(e.userId=this.userId),this.events.push(e)},G=function(){this.events=[]},H=function(){document.removeEventListener("click",e(this,I,"f"),!0)},Q=function(){e(this,q,"f").call(this),clearInterval(e(this,B,"f")),e(this,P,"m",G).call(this)},K=function(){history.pushState=e(this,A,"f"),history.replaceState=e(this,D,"f"),window.removeEventListener("popstate",e(this,x,"f"))};export{J as EventCollector};
9
9
  //# sourceMappingURL=main.esm.js.map
@@ -81,7 +81,7 @@ type UserlensProviderConfig = {
81
81
  userTraits: Record<string, any>;
82
82
  groupTraits?: Record<string, any>;
83
83
  groupId?: string;
84
- eventCollector: EventCollectorConfig;
84
+ eventCollector?: EventCollectorConfig;
85
85
  };
86
86
 
87
87
  declare class EventCollector {
@@ -5,5 +5,5 @@
5
5
  * MIT License | (c) Dustin Diaz 2012-2015
6
6
  * MIT License | (c) Denis Demchenko 2015-2019
7
7
  */class f{static getParser(e,t=!1){if("string"!=typeof e)throw new Error("UserAgent should be a string");return new g(e,t)}static parse(e){return new g(e).getResult()}static get BROWSER_MAP(){return i}static get ENGINE_MAP(){return a}static get OS_MAP(){return o}static get PLATFORMS_MAP(){return n}}var m="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function b(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var w,v={exports:{}},y=v.exports;
8
- /*! https://mths.be/cssescape v1.5.1 by @mathias | MIT license */function M(){return w||(w=1,function(e){var t;t=void 0!==m?m:y,e.exports=function(e){if(e.CSS&&e.CSS.escape)return e.CSS.escape;var t=function(e){if(0==arguments.length)throw new TypeError("`CSS.escape` requires an argument.");for(var t,r=String(e),s=r.length,i=-1,n="",o=r.charCodeAt(0);++i<s;)0!=(t=r.charCodeAt(i))?n+=t>=1&&t<=31||127==t||0==i&&t>=48&&t<=57||1==i&&t>=48&&t<=57&&45==o?"\\"+t.toString(16)+" ":0==i&&1==s&&45==t||!(t>=128||45==t||95==t||t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122)?"\\"+r.charAt(i):r.charAt(i):n+="�";return n};return e.CSS||(e.CSS={}),e.CSS.escape=t,t}(t)}(v)),v.exports}var S,E,_,T={};var O=b(function(){if(_)return E;_=1,M();const{ShadowRootTypes:e,nodeNameInCorrectCase:t,NodeType:r}=(S||(S=1,T.nodeNameInCorrectCase=function(e){const t=e.shadowRoot&&e.shadowRoot.mode;return t?"#shadow-root ("+t+")":e.localName?e.localName.length!==e.nodeName.length?e.nodeName:e.localName:e.nodeName},T.shadowRootType=function(e){const t=e.ancestorShadowRoot();return t?t.mode:null},T.NodeType={ELEMENT_NODE:1,ATTRIBUTE_NODE:2,TEXT_NODE:3,CDATA_SECTION_NODE:4,PROCESSING_INSTRUCTION_NODE:7,COMMENT_NODE:8,DOCUMENT_NODE:9},T.ShadowRootTypes={UserAgent:"user-agent",Open:"open",Closed:"closed"}),T);let s={DOMPath:{}};return s.DOMPath.fullQualifiedSelector=function(e,t){try{return e.nodeType!==r.ELEMENT_NODE?e.localName||e.nodeName.toLowerCase():s.DOMPath.cssPath(e,t)}catch(e){return null}},s.DOMPath.cssPath=function(e,t){if(e.nodeType!==r.ELEMENT_NODE)return"";const i=[];let n=e;for(;n;){const r=s.DOMPath._cssPathStep(n,!!t,n===e);if(!r)break;if(i.push(r),r.optimized)break;n=n.parentNode}return i.reverse(),i.join(" > ")},s.DOMPath.canGetJSPath=function(t){let r=t;for(;r;){if(r.shadowRoot&&r.shadowRoot.mode!==e.Open)return!1;r=r.shadowRoot&&r.shadowRoot.host}return!0},s.DOMPath.jsPath=function(e,t){if(e.nodeType!==r.ELEMENT_NODE)return"";const i=[];let n=e;for(;n;)i.push(s.DOMPath.cssPath(n,t)),n=n.shadowRoot&&n.shadowRoot.host;i.reverse();let o="";for(let e=0;e<i.length;++e){const t=JSON.stringify(i[e]);o+=e?`.shadowRoot.querySelector(${t})`:`document.querySelector(${t})`}return o},s.DOMPath._cssPathStep=function(e,i,n){if(e.nodeType!==r.ELEMENT_NODE)return null;const o=e.getAttribute("id");if(i){if(o)return new s.DOMPath.Step(l(o),!0);const r=e.nodeName.toLowerCase();if("body"===r||"head"===r||"html"===r)return new s.DOMPath.Step(t(e),!0)}const a=t(e);if(o)return new s.DOMPath.Step(a+l(o),!0);const c=e.parentNode;if(!c||c.nodeType===r.DOCUMENT_NODE)return new s.DOMPath.Step(a,!0);function d(e){const t=e.getAttribute("class");return t?t.split(/\s+/g).filter(Boolean).map(function(e){return"$"+e}):[]}function l(e){return"#"+CSS.escape(e)}const u=d(e);let h=!1,p=!1,g=-1,f=-1;const m=c.children;for(let s=0;(-1===g||!p)&&s<m.length;++s){const i=m[s];if(i.nodeType!==r.ELEMENT_NODE)continue;if(f+=1,i===e){g=f;continue}if(p)continue;if(t(i)!==a)continue;h=!0;const n=new Set(u);if(!n.size){p=!0;continue}const o=d(i);for(let e=0;e<o.length;++e){const t=o[e];if(n.has(t)&&(n.delete(t),!n.size)){p=!0;break}}}let b=a;if(n&&"input"===a.toLowerCase()&&e.getAttribute("type")&&!e.getAttribute("id")&&!e.getAttribute("class")&&(b+="[type="+CSS.escape(e.getAttribute("type"))+"]"),p)b+=":nth-child("+(g+1)+")";else if(h)for(const e of u)b+="."+CSS.escape(e.slice(1));return new s.DOMPath.Step(b,!1)},s.DOMPath.xPath=function(e,t){if(e.nodeType===r.DOCUMENT_NODE)return"/";const i=[];let n=e;for(;n;){const e=s.DOMPath._xPathValue(n,t);if(!e)break;if(i.push(e),e.optimized)break;n=n.parentNode}return i.reverse(),(i.length&&i[0].optimized?"":"/")+i.join("/")},s.DOMPath._xPathValue=function(e,t){let i;const n=s.DOMPath._xPathIndex(e);if(-1===n)return null;switch(e.nodeType){case r.ELEMENT_NODE:if(t&&e.getAttribute("id"))return new s.DOMPath.Step('//*[@id="'+e.getAttribute("id")+'"]',!0);i=e.localName;break;case r.ATTRIBUTE_NODE:i="@"+e.nodeName;break;case r.TEXT_NODE:case r.CDATA_SECTION_NODE:i="text()";break;case r.PROCESSING_INSTRUCTION_NODE:i="processing-instruction()";break;case r.COMMENT_NODE:i="comment()";break;case r.DOCUMENT_NODE:default:i=""}return n>0&&(i+="["+n+"]"),new s.DOMPath.Step(i,e.nodeType===r.DOCUMENT_NODE)},s.DOMPath._xPathIndex=function(e){function t(e,t){if(e===t)return!0;if(e.nodeType===r.ELEMENT_NODE&&t.nodeType===r.ELEMENT_NODE)return e.localName===t.localName;if(e.nodeType===t.nodeType)return!0;return(e.nodeType===r.CDATA_SECTION_NODE?r.TEXT_NODE:e.nodeType)===(t.nodeType===r.CDATA_SECTION_NODE?r.TEXT_NODE:t.nodeType)}const s=e.parentNode?e.parentNode.children:null;if(!s)return 0;let i;for(let r=0;r<s.length;++r)if(t(e,s[r])&&s[r]!==e){i=!0;break}if(!i)return 0;let n=1;for(let r=0;r<s.length;++r)if(t(e,s[r])){if(s[r]===e)return n;++n}return-1},s.DOMPath.Step=class{constructor(e,t){this.value=e,this.optimized=t||!1}toString(){return this.value}},E=s.DOMPath}());const N="https://events.userlens.io",k=()=>{try{const e=window.localStorage.getItem("$ul_WRITE_CODE");if(null==e)return null;const t=e.trim();if(!t)return null;const r=t.toLowerCase();return"null"===r||"undefined"===r?null:t}catch{return null}},C=async(e,t=!1)=>{if(!(null==e?void 0:e.userId))return;const r=k();if(!r)return void(t&&console.error("Failed to identify user: Userlens SDK error: WRITE_CODE is not set"));const s={type:"identify",userId:e.userId,source:"userlens-js-analytics-sdk",traits:null==e?void 0:e.traits};if(!(await fetch(`${N}/event`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(s)})).ok)throw new Error("Userlens HTTP error: failed to identify");return"ok"},F=async(e,t=!1)=>{if(!(null==e?void 0:e.groupId))return;const r=k();if(!r)return void(t&&console.error("Failed to group identify: Userlens SDK error: WRITE_CODE is not set"));const{groupId:s,userId:i,traits:n}=e,o={type:"group",groupId:s,source:"userlens-js-analytics-sdk",...i&&{userId:i},...n&&{traits:n}};if(!(await fetch(`${N}/event`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(o)})).ok)throw new Error("Userlens HTTP error: failed to identify");return"ok"},P=async(e,t=!1)=>{const r=k();if(!r)return void(t&&console.error("Failed to group identify: Userlens SDK error: WRITE_CODE is not set"));const s={events:e};if(!(await fetch("https://raw.userlens.io/raw/event",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(s)})).ok)throw new Error("Userlens HTTP error: failed to track");return"ok"};var B,A,D,x,I,R,L,U,W,$,z,j,V,q,G,H,Q,K,J;A=new WeakMap,D=new WeakMap,x=new WeakMap,I=new WeakMap,R=new WeakMap,G=new WeakMap,B=new WeakSet,L=function(){this.debug&&console.log("Userlens EventCollector: adding click event listener"),document.addEventListener("click",t(this,I,"f"),!0),this.debug&&console.log("Userlens EventCollector: click event listener added")},U=function(e){try{const r=e.target;if(!(r instanceof HTMLElement))return;const s=O.xPath(r,!0),i=this.useLighterSnapshot?t(this,B,"m",W).call(this,r):t(this,B,"m",$).call(this,r),n={event:s,is_raw:!0,snapshot:i?[i]:[],properties:{...this.getUserContext(),...this.getPageMetadata()}};this.userId&&(n.userId=this.userId),this.events.push(n),this.events.length>100&&(this.events=this.events.slice(-100))}catch(e){console.warn("Userlens EventCollector error: click event handling failed",e)}},W=function(e){if(!(e instanceof HTMLElement))return null;const r=document.body;if(!r)return null;const s=[];let i=e;for(;i&&1===i.nodeType&&i!==r;)s.unshift(i),i=i.parentElement;const n=[r,...s.filter(e=>e!==r)];let o=null,a=null;for(let e=0;e<n.length;e++){const r=n[e],s=e===n.length-1,i=t(this,B,"m",z).call(this,r,{isTarget:s,leadsToTarget:!s});o||(o=i),a&&(a.children||(a.children=[]),a.children.push(i)),a=i}const c=a;return c&&e.children.length>0&&(c.children=Array.from(e.children).filter(e=>e instanceof HTMLElement).map(e=>t(this,B,"m",z).call(this,e,{includeChildren:!0})).filter(Boolean)),o},$=function(e){if(!(e instanceof HTMLElement))return null;const r=[];let s=e;for(;s&&1===s.nodeType;)r.unshift(s),s=s.parentElement;let i=null,n=null;for(let e=0;e<r.length;e++){const s=r[e],o=e>=r.length-3,a=e===r.length-1,c=t(this,B,"m",z).call(this,s,{isTarget:a,leadsToTarget:!0});let d=[c];if(o&&s.parentElement){d=[c,...Array.from(s.parentElement.children).filter(e=>e!==s&&e instanceof HTMLElement).map(e=>t(this,B,"m",z).call(this,e,{includeChildren:!0})).filter(Boolean)]}i||(i=c),n&&(n.children||(n.children=[]),n.children.push(...d)),n=c}return i},z=function e(r,{isTarget:s=!1,includeChildren:i=!1,leadsToTarget:n=!1}={}){var o,a;const c=r.tagName.toLowerCase(),d=r.classList.length?Array.from(r.classList):null,l=r.id||null,u=r.getAttribute("href")||null,h=Array.from((null===(o=r.parentNode)||void 0===o?void 0:o.children)||[]).indexOf(r)+1,p=Array.from((null===(a=r.parentNode)||void 0===a?void 0:a.children)||[]).filter(e=>e instanceof HTMLElement&&e.tagName===r.tagName).indexOf(r)+1,g={};for(let e of Array.from(r.attributes))g[`attr__${e.name}`]=e.value;const f=Array.from(r.childNodes).filter(e=>e.nodeType===Node.TEXT_NODE).map(e=>{var t;return null===(t=e.textContent)||void 0===t?void 0:t.trim()}).filter(Boolean).join(" ")||null,m={tag_name:c,nth_child:h,nth_of_type:p,attributes:g,...d?{attr_class:d}:{},...l?{attr_id:l}:{},...u?{href:u}:{},...f?{text:f}:{},...s?{is_target:!0}:{},...n&&!s?{leads_to_target:!0}:{}};return(i&&r.children.length>0||s)&&(m.children=Array.from(r.children).filter(e=>e instanceof HTMLElement).map(r=>t(this,B,"m",e).call(this,r,{includeChildren:!0})).filter(Boolean)),m},j=function(){r(this,A,setInterval(()=>{t(this,G,"f").call(this)},this.intervalTime),"f")},V=function(){r(this,D,history.pushState,"f"),r(this,x,history.replaceState,"f"),history.pushState=(...e)=>{t(this,D,"f").apply(history,e),t(this,B,"m",q).call(this)},history.replaceState=(...e)=>{t(this,x,"f").apply(history,e),t(this,B,"m",q).call(this)},window.addEventListener("popstate",t(this,R,"f"))},q=function(){if(function(){if("undefined"==typeof window)return!1;const e=window.location.hostname;return"localhost"===e||"127.0.0.1"===e||"::1"===e||e.endsWith(".localhost")}())return;const e={event:"$ul_pageview",properties:this.getPageMetadata()};this.userId&&(e.userId=this.userId),this.events.push(e)},H=function(){this.events=[]},Q=function(){document.removeEventListener("click",t(this,I,"f"),!0)},K=function(){t(this,G,"f").call(this),clearInterval(t(this,A,"f")),t(this,B,"m",H).call(this)},J=function(){history.pushState=t(this,D,"f"),history.replaceState=t(this,x,"f"),window.removeEventListener("popstate",t(this,R,"f"))},e.EventCollector=class{constructor(e){if(B.add(this),this.useLighterSnapshot=!1,this.userContext=null,A.set(this,void 0),D.set(this,void 0),x.set(this,void 0),I.set(this,t(this,B,"m",U).bind(this)),R.set(this,t(this,B,"m",q).bind(this)),G.set(this,()=>{if(0===this.events.length)return;const e=[...this.events];if(this.callback){try{this.callback(e)}catch(e){}t(this,B,"m",H).call(this)}else Promise.allSettled([this.userId&&this.userTraits?C({userId:this.userId,traits:this.userTraits},this.debug):null,this.groupId?F({groupId:this.groupId,traits:this.groupTraits,userId:this.userId},this.debug):null,P(e,this.debug)]),t(this,B,"m",H).call(this)}),"undefined"==typeof window)return void console.error("Userlens EventCollector error: unavailable outside of browser environment.");const{userId:r,WRITE_CODE:s,callback:i,intervalTime:n=5e3,skipRawEvents:o=!1,useLighterSnapshot:a=!1,debug:c=!1}=e,d=e.userTraits,l=e.groupId,u=e.groupTraits;var h;(this.autoUploadModeEnabled=!i,!this.autoUploadModeEnabled||(null==r?void 0:r.length))?!this.autoUploadModeEnabled||(null==s?void 0:s.length)?(this.autoUploadModeEnabled&&(h=s,window.localStorage.setItem("$ul_WRITE_CODE",btoa(`${h}:`))),this.autoUploadModeEnabled||"function"==typeof i?(this.userId=r,this.userTraits="object"==typeof d&&null!==d?d:{},this.callback=i,this.intervalTime=n,this.events=[],this.debug=c,l&&(this.groupId=l,this.groupTraits="object"==typeof u&&null!==u?u:{}),o||(this.useLighterSnapshot=a,t(this,B,"m",L).call(this),t(this,B,"m",V).call(this)),t(this,B,"m",j).call(this),this.userContext=this.getUserContext()):this.debug&&console.error("Userlens EventCollector error: callback is not a function.")):this.debug&&console.error("Userlens EventCollector error: WRITE_CODE is missing."):this.debug&&console.error("Userlens EventCollector error: userId is missing.")}pushEvent(e){const t={is_raw:!1,...e,properties:{...null==e?void 0:e.properties,...this.getUserContext(),...this.getPageMetadata()}};this.userId&&(t.userId=this.userId),this.events.push(t)}identify(e,t){return C({userId:e,traits:t})}group(e,t){return F({groupId:e,traits:t,userId:this.userId})}updateUserTraits(e){this.userTraits=e}updateGroupTraits(e){this.groupTraits=e}stop(){t(this,B,"m",Q).call(this),t(this,B,"m",K).call(this),t(this,B,"m",J).call(this)}getUserContext(){var e,t,r,s,i,n,o;if(this.userContext)return this.userContext;const a=f.getParser(window.navigator.userAgent),c=a.getBrowser(),d=a.getOS(),l={$ul_browser:null!==(e=c.name)&&void 0!==e?e:"Unknown",$ul_browser_version:null!==(t=c.version)&&void 0!==t?t:"Unknown",$ul_os:null!==(r=d.name)&&void 0!==r?r:"Unknown",$ul_os_version:null!==(s=d.versionName)&&void 0!==s?s:"Unknown",$ul_browser_language:null!==(i=navigator.language)&&void 0!==i?i:"en-US",$ul_browser_language_prefix:null!==(o=null===(n=navigator.language)||void 0===n?void 0:n.split("-")[0])&&void 0!==o?o:"en",$ul_screen_width:window.screen.width,$ul_screen_height:window.screen.height,$ul_viewport_width:window.innerWidth,$ul_viewport_height:window.innerHeight,$ul_lib:"userlens.js",$ul_lib_version:"0.1.88",$ul_device_type:/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop",$ul_timezone:Intl.DateTimeFormat().resolvedOptions().timeZone};return this.userContext=l,l}getPageMetadata(){try{const e=new URL(window.location.href);let t=document.referrer||"$direct",r="$direct";try{t&&/^https?:\/\//.test(t)&&(r=new URL(t).hostname)}catch(e){}const s=e.search.slice(1);return{$ul_page:e.origin+e.pathname,$ul_pathname:e.pathname,$ul_host:e.host,$ul_referrer:t,$ul_referring_domain:r,$ul_query:s}}catch(e){return{$ul_page:"",$ul_pathname:"",$ul_host:"",$ul_referrer:"",$ul_referring_domain:"",$ul_query:""}}}}});
8
+ /*! https://mths.be/cssescape v1.5.1 by @mathias | MIT license */function M(){return w||(w=1,function(e){var t;t=void 0!==m?m:y,e.exports=function(e){if(e.CSS&&e.CSS.escape)return e.CSS.escape;var t=function(e){if(0==arguments.length)throw new TypeError("`CSS.escape` requires an argument.");for(var t,r=String(e),s=r.length,i=-1,n="",o=r.charCodeAt(0);++i<s;)0!=(t=r.charCodeAt(i))?n+=t>=1&&t<=31||127==t||0==i&&t>=48&&t<=57||1==i&&t>=48&&t<=57&&45==o?"\\"+t.toString(16)+" ":0==i&&1==s&&45==t||!(t>=128||45==t||95==t||t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122)?"\\"+r.charAt(i):r.charAt(i):n+="�";return n};return e.CSS||(e.CSS={}),e.CSS.escape=t,t}(t)}(v)),v.exports}var S,E,_,T={};var O=b(function(){if(_)return E;_=1,M();const{ShadowRootTypes:e,nodeNameInCorrectCase:t,NodeType:r}=(S||(S=1,T.nodeNameInCorrectCase=function(e){const t=e.shadowRoot&&e.shadowRoot.mode;return t?"#shadow-root ("+t+")":e.localName?e.localName.length!==e.nodeName.length?e.nodeName:e.localName:e.nodeName},T.shadowRootType=function(e){const t=e.ancestorShadowRoot();return t?t.mode:null},T.NodeType={ELEMENT_NODE:1,ATTRIBUTE_NODE:2,TEXT_NODE:3,CDATA_SECTION_NODE:4,PROCESSING_INSTRUCTION_NODE:7,COMMENT_NODE:8,DOCUMENT_NODE:9},T.ShadowRootTypes={UserAgent:"user-agent",Open:"open",Closed:"closed"}),T);let s={DOMPath:{}};return s.DOMPath.fullQualifiedSelector=function(e,t){try{return e.nodeType!==r.ELEMENT_NODE?e.localName||e.nodeName.toLowerCase():s.DOMPath.cssPath(e,t)}catch(e){return null}},s.DOMPath.cssPath=function(e,t){if(e.nodeType!==r.ELEMENT_NODE)return"";const i=[];let n=e;for(;n;){const r=s.DOMPath._cssPathStep(n,!!t,n===e);if(!r)break;if(i.push(r),r.optimized)break;n=n.parentNode}return i.reverse(),i.join(" > ")},s.DOMPath.canGetJSPath=function(t){let r=t;for(;r;){if(r.shadowRoot&&r.shadowRoot.mode!==e.Open)return!1;r=r.shadowRoot&&r.shadowRoot.host}return!0},s.DOMPath.jsPath=function(e,t){if(e.nodeType!==r.ELEMENT_NODE)return"";const i=[];let n=e;for(;n;)i.push(s.DOMPath.cssPath(n,t)),n=n.shadowRoot&&n.shadowRoot.host;i.reverse();let o="";for(let e=0;e<i.length;++e){const t=JSON.stringify(i[e]);o+=e?`.shadowRoot.querySelector(${t})`:`document.querySelector(${t})`}return o},s.DOMPath._cssPathStep=function(e,i,n){if(e.nodeType!==r.ELEMENT_NODE)return null;const o=e.getAttribute("id");if(i){if(o)return new s.DOMPath.Step(l(o),!0);const r=e.nodeName.toLowerCase();if("body"===r||"head"===r||"html"===r)return new s.DOMPath.Step(t(e),!0)}const a=t(e);if(o)return new s.DOMPath.Step(a+l(o),!0);const c=e.parentNode;if(!c||c.nodeType===r.DOCUMENT_NODE)return new s.DOMPath.Step(a,!0);function d(e){const t=e.getAttribute("class");return t?t.split(/\s+/g).filter(Boolean).map(function(e){return"$"+e}):[]}function l(e){return"#"+CSS.escape(e)}const u=d(e);let h=!1,p=!1,g=-1,f=-1;const m=c.children;for(let s=0;(-1===g||!p)&&s<m.length;++s){const i=m[s];if(i.nodeType!==r.ELEMENT_NODE)continue;if(f+=1,i===e){g=f;continue}if(p)continue;if(t(i)!==a)continue;h=!0;const n=new Set(u);if(!n.size){p=!0;continue}const o=d(i);for(let e=0;e<o.length;++e){const t=o[e];if(n.has(t)&&(n.delete(t),!n.size)){p=!0;break}}}let b=a;if(n&&"input"===a.toLowerCase()&&e.getAttribute("type")&&!e.getAttribute("id")&&!e.getAttribute("class")&&(b+="[type="+CSS.escape(e.getAttribute("type"))+"]"),p)b+=":nth-child("+(g+1)+")";else if(h)for(const e of u)b+="."+CSS.escape(e.slice(1));return new s.DOMPath.Step(b,!1)},s.DOMPath.xPath=function(e,t){if(e.nodeType===r.DOCUMENT_NODE)return"/";const i=[];let n=e;for(;n;){const e=s.DOMPath._xPathValue(n,t);if(!e)break;if(i.push(e),e.optimized)break;n=n.parentNode}return i.reverse(),(i.length&&i[0].optimized?"":"/")+i.join("/")},s.DOMPath._xPathValue=function(e,t){let i;const n=s.DOMPath._xPathIndex(e);if(-1===n)return null;switch(e.nodeType){case r.ELEMENT_NODE:if(t&&e.getAttribute("id"))return new s.DOMPath.Step('//*[@id="'+e.getAttribute("id")+'"]',!0);i=e.localName;break;case r.ATTRIBUTE_NODE:i="@"+e.nodeName;break;case r.TEXT_NODE:case r.CDATA_SECTION_NODE:i="text()";break;case r.PROCESSING_INSTRUCTION_NODE:i="processing-instruction()";break;case r.COMMENT_NODE:i="comment()";break;case r.DOCUMENT_NODE:default:i=""}return n>0&&(i+="["+n+"]"),new s.DOMPath.Step(i,e.nodeType===r.DOCUMENT_NODE)},s.DOMPath._xPathIndex=function(e){function t(e,t){if(e===t)return!0;if(e.nodeType===r.ELEMENT_NODE&&t.nodeType===r.ELEMENT_NODE)return e.localName===t.localName;if(e.nodeType===t.nodeType)return!0;return(e.nodeType===r.CDATA_SECTION_NODE?r.TEXT_NODE:e.nodeType)===(t.nodeType===r.CDATA_SECTION_NODE?r.TEXT_NODE:t.nodeType)}const s=e.parentNode?e.parentNode.children:null;if(!s)return 0;let i;for(let r=0;r<s.length;++r)if(t(e,s[r])&&s[r]!==e){i=!0;break}if(!i)return 0;let n=1;for(let r=0;r<s.length;++r)if(t(e,s[r])){if(s[r]===e)return n;++n}return-1},s.DOMPath.Step=class{constructor(e,t){this.value=e,this.optimized=t||!1}toString(){return this.value}},E=s.DOMPath}());const N="https://events.userlens.io",k=()=>{try{const e=window.localStorage.getItem("$ul_WRITE_CODE");if(null==e)return null;const t=e.trim();if(!t)return null;const r=t.toLowerCase();return"null"===r||"undefined"===r?null:t}catch{return null}},C=async(e,t=!1)=>{if(!(null==e?void 0:e.userId))return;const r=k();if(!r)return void(t&&console.error("Failed to identify user: Userlens SDK error: WRITE_CODE is not set"));const s={type:"identify",userId:e.userId,source:"userlens-js-analytics-sdk",traits:null==e?void 0:e.traits};if(!(await fetch(`${N}/event`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(s)})).ok)throw new Error("Userlens HTTP error: failed to identify");return"ok"},F=async(e,t=!1)=>{if(!(null==e?void 0:e.groupId))return;const r=k();if(!r)return void(t&&console.error("Failed to group identify: Userlens SDK error: WRITE_CODE is not set"));const{groupId:s,userId:i,traits:n}=e,o={type:"group",groupId:s,source:"userlens-js-analytics-sdk",...i&&{userId:i},...n&&{traits:n}};if(!(await fetch(`${N}/event`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(o)})).ok)throw new Error("Userlens HTTP error: failed to identify");return"ok"},P=async(e,t=!1)=>{const r=k();if(!r)return void(t&&console.error("Failed to group identify: Userlens SDK error: WRITE_CODE is not set"));const s={events:e};if(!(await fetch("https://raw.userlens.io/raw/event",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Basic ${r}`},body:JSON.stringify(s)})).ok)throw new Error("Userlens HTTP error: failed to track");return"ok"};var B,A,D,x,I,R,L,U,W,$,z,j,V,q,G,H,Q,K,J;A=new WeakMap,D=new WeakMap,x=new WeakMap,I=new WeakMap,R=new WeakMap,G=new WeakMap,B=new WeakSet,L=function(){this.debug&&console.log("Userlens EventCollector: adding click event listener"),document.addEventListener("click",t(this,I,"f"),!0),this.debug&&console.log("Userlens EventCollector: click event listener added")},U=function(e){try{const r=e.target;if(!(r instanceof HTMLElement))return;const s=O.xPath(r,!0),i=this.useLighterSnapshot?t(this,B,"m",W).call(this,r):t(this,B,"m",$).call(this,r),n={event:s,is_raw:!0,snapshot:i?[i]:[],properties:{...this.getUserContext(),...this.getPageMetadata()}};this.userId&&(n.userId=this.userId),this.events.push(n),this.events.length>100&&(this.events=this.events.slice(-100))}catch(e){console.warn("Userlens EventCollector error: click event handling failed",e)}},W=function(e){if(!(e instanceof HTMLElement))return null;const r=document.body;if(!r)return null;const s=[];let i=e;for(;i&&1===i.nodeType&&i!==r;)s.unshift(i),i=i.parentElement;const n=[r,...s.filter(e=>e!==r)];let o=null,a=null;for(let e=0;e<n.length;e++){const r=n[e],s=e===n.length-1,i=t(this,B,"m",z).call(this,r,{isTarget:s,leadsToTarget:!s});o||(o=i),a&&(a.children||(a.children=[]),a.children.push(i)),a=i}const c=a;return c&&e.children.length>0&&(c.children=Array.from(e.children).filter(e=>e instanceof HTMLElement).map(e=>t(this,B,"m",z).call(this,e,{includeChildren:!0})).filter(Boolean)),o},$=function(e){if(!(e instanceof HTMLElement))return null;const r=[];let s=e;for(;s&&1===s.nodeType;)r.unshift(s),s=s.parentElement;let i=null,n=null;for(let e=0;e<r.length;e++){const s=r[e],o=e>=r.length-3,a=e===r.length-1,c=t(this,B,"m",z).call(this,s,{isTarget:a,leadsToTarget:!0});let d=[c];if(o&&s.parentElement){d=[c,...Array.from(s.parentElement.children).filter(e=>e!==s&&e instanceof HTMLElement).map(e=>t(this,B,"m",z).call(this,e,{includeChildren:!0})).filter(Boolean)]}i||(i=c),n&&(n.children||(n.children=[]),n.children.push(...d)),n=c}return i},z=function e(r,{isTarget:s=!1,includeChildren:i=!1,leadsToTarget:n=!1}={}){var o,a;const c=r.tagName.toLowerCase(),d=r.classList.length?Array.from(r.classList):null,l=r.id||null,u=r.getAttribute("href")||null,h=Array.from((null===(o=r.parentNode)||void 0===o?void 0:o.children)||[]).indexOf(r)+1,p=Array.from((null===(a=r.parentNode)||void 0===a?void 0:a.children)||[]).filter(e=>e instanceof HTMLElement&&e.tagName===r.tagName).indexOf(r)+1,g={};for(let e of Array.from(r.attributes))g[`attr__${e.name}`]=e.value;const f=Array.from(r.childNodes).filter(e=>e.nodeType===Node.TEXT_NODE).map(e=>{var t;return null===(t=e.textContent)||void 0===t?void 0:t.trim()}).filter(Boolean).join(" ")||null,m={tag_name:c,nth_child:h,nth_of_type:p,attributes:g,...d?{attr_class:d}:{},...l?{attr_id:l}:{},...u?{href:u}:{},...f?{text:f}:{},...s?{is_target:!0}:{},...n&&!s?{leads_to_target:!0}:{}};return(i&&r.children.length>0||s)&&(m.children=Array.from(r.children).filter(e=>e instanceof HTMLElement).map(r=>t(this,B,"m",e).call(this,r,{includeChildren:!0})).filter(Boolean)),m},j=function(){r(this,A,setInterval(()=>{t(this,G,"f").call(this)},this.intervalTime),"f")},V=function(){r(this,D,history.pushState,"f"),r(this,x,history.replaceState,"f"),history.pushState=(...e)=>{t(this,D,"f").apply(history,e),t(this,B,"m",q).call(this)},history.replaceState=(...e)=>{t(this,x,"f").apply(history,e),t(this,B,"m",q).call(this)},window.addEventListener("popstate",t(this,R,"f"))},q=function(){if(function(){if("undefined"==typeof window)return!1;const e=window.location.hostname;return"localhost"===e||"127.0.0.1"===e||"::1"===e||e.endsWith(".localhost")}())return;const e={event:"$ul_pageview",properties:this.getPageMetadata()};this.userId&&(e.userId=this.userId),this.events.push(e)},H=function(){this.events=[]},Q=function(){document.removeEventListener("click",t(this,I,"f"),!0)},K=function(){t(this,G,"f").call(this),clearInterval(t(this,A,"f")),t(this,B,"m",H).call(this)},J=function(){history.pushState=t(this,D,"f"),history.replaceState=t(this,x,"f"),window.removeEventListener("popstate",t(this,R,"f"))},e.EventCollector=class{constructor(e){if(B.add(this),this.useLighterSnapshot=!1,this.userContext=null,A.set(this,void 0),D.set(this,void 0),x.set(this,void 0),I.set(this,t(this,B,"m",U).bind(this)),R.set(this,t(this,B,"m",q).bind(this)),G.set(this,()=>{if(0===this.events.length)return;const e=[...this.events];if(this.callback){try{this.callback(e)}catch(e){}t(this,B,"m",H).call(this)}else Promise.allSettled([this.userId&&this.userTraits?C({userId:this.userId,traits:this.userTraits},this.debug):null,this.groupId?F({groupId:this.groupId,traits:this.groupTraits,userId:this.userId},this.debug):null,P(e,this.debug)]),t(this,B,"m",H).call(this)}),"undefined"==typeof window)return void console.error("Userlens EventCollector error: unavailable outside of browser environment.");const{userId:r,WRITE_CODE:s,callback:i,intervalTime:n=5e3,skipRawEvents:o=!1,useLighterSnapshot:a=!1,debug:c=!1}=e,d=e.userTraits,l=e.groupId,u=e.groupTraits;var h;(this.autoUploadModeEnabled=!i,!this.autoUploadModeEnabled||(null==r?void 0:r.length))?!this.autoUploadModeEnabled||(null==s?void 0:s.length)?(this.autoUploadModeEnabled&&(h=s,window.localStorage.setItem("$ul_WRITE_CODE",btoa(`${h}:`))),this.autoUploadModeEnabled||"function"==typeof i?(this.userId=r,this.userTraits="object"==typeof d&&null!==d?d:{},this.callback=i,this.intervalTime=n,this.events=[],this.debug=c,l&&(this.groupId=l,this.groupTraits="object"==typeof u&&null!==u?u:{}),o||(this.useLighterSnapshot=a,t(this,B,"m",L).call(this),t(this,B,"m",V).call(this)),t(this,B,"m",j).call(this),this.userContext=this.getUserContext()):this.debug&&console.error("Userlens EventCollector error: callback is not a function.")):this.debug&&console.error("Userlens EventCollector error: WRITE_CODE is missing."):this.debug&&console.error("Userlens EventCollector error: userId is missing.")}pushEvent(e){const t={is_raw:!1,...e,properties:{...null==e?void 0:e.properties,...this.getUserContext(),...this.getPageMetadata()}};this.userId&&(t.userId=this.userId),this.events.push(t)}identify(e,t){return C({userId:e,traits:t})}group(e,t){return F({groupId:e,traits:t,userId:this.userId})}updateUserTraits(e){this.userTraits=e}updateGroupTraits(e){this.groupTraits=e}stop(){t(this,B,"m",Q).call(this),t(this,B,"m",K).call(this),t(this,B,"m",J).call(this)}getUserContext(){var e,t,r,s,i,n,o;if(this.userContext)return this.userContext;const a=f.getParser(window.navigator.userAgent),c=a.getBrowser(),d=a.getOS(),l={$ul_browser:null!==(e=c.name)&&void 0!==e?e:"Unknown",$ul_browser_version:null!==(t=c.version)&&void 0!==t?t:"Unknown",$ul_os:null!==(r=d.name)&&void 0!==r?r:"Unknown",$ul_os_version:null!==(s=d.versionName)&&void 0!==s?s:"Unknown",$ul_browser_language:null!==(i=navigator.language)&&void 0!==i?i:"en-US",$ul_browser_language_prefix:null!==(o=null===(n=navigator.language)||void 0===n?void 0:n.split("-")[0])&&void 0!==o?o:"en",$ul_screen_width:window.screen.width,$ul_screen_height:window.screen.height,$ul_viewport_width:window.innerWidth,$ul_viewport_height:window.innerHeight,$ul_lib:"userlens.js",$ul_lib_version:"0.1.89",$ul_device_type:/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop",$ul_timezone:Intl.DateTimeFormat().resolvedOptions().timeZone};return this.userContext=l,l}getPageMetadata(){try{const e=new URL(window.location.href);let t=document.referrer||"$direct",r="$direct";try{t&&/^https?:\/\//.test(t)&&(r=new URL(t).hostname)}catch(e){}const s=e.search.slice(1);return{$ul_page:e.origin+e.pathname,$ul_pathname:e.pathname,$ul_host:e.host,$ul_referrer:t,$ul_referring_domain:r,$ul_query:s}}catch(e){return{$ul_page:"",$ul_pathname:"",$ul_host:"",$ul_referrer:"",$ul_referring_domain:"",$ul_query:""}}}}});
9
9
  //# sourceMappingURL=userlens.umd.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "userlens-analytics-sdk",
3
- "version": "0.1.88",
3
+ "version": "0.1.89",
4
4
  "main": "./dist/main.cjs.js",
5
5
  "module": "./dist/main.esm.js",
6
6
  "types": "./dist/types/index.d.ts",