@superfeedback/widget 0.0.15 → 0.0.16
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/index.iife.js +1 -1
- package/dist/index.js +2 -2
- package/package.json +1 -1
package/dist/index.iife.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var superfeedback=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=`sf_visitor_id`,n=class{appId;apiUrl;visitorId;sessionId;segmentStart;accumulatedMs=0;isVisible=!0;constructor(e,t){this.appId=e,this.apiUrl=t,this.segmentStart=Date.now(),this.visitorId=this.getOrCreateVisitorId(),this.sessionId=crypto.randomUUID(),this.setupListeners()}getOrCreateVisitorId(){try{let e=
|
|
1
|
+
var superfeedback=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=`sf_visitor_id`,n=class{appId;apiUrl;visitorId;sessionId;segmentStart;accumulatedMs=0;isVisible=!0;constructor(e,t){this.appId=e,this.apiUrl=t,this.segmentStart=Date.now(),this.visitorId=this.getOrCreateVisitorId(),this.sessionId=crypto.randomUUID(),this.setupListeners()}getOrCreateVisitorId(){try{let e=localStorage.getItem(t);if(e)return e;let n=crypto.randomUUID();return localStorage.setItem(t,n),n}catch{return crypto.randomUUID()}}getOS(){let e=navigator.userAgent;return e.includes(`Windows`)?`Windows`:e.includes(`Mac OS`)?`macOS`:e.includes(`Linux`)?`Linux`:e.includes(`Android`)?`Android`:e.includes(`iOS`)||e.includes(`iPhone`)||e.includes(`iPad`)?`iOS`:`Unknown`}getDeviceType(){let e=navigator.userAgent;return/Tablet|iPad/i.test(e)?`tablet`:/Mobile|iPhone|Android.*Mobile/i.test(e)?`mobile`:`desktop`}getTotalDurationMs(){let e=this.isVisible?Date.now()-this.segmentStart:0;return this.accumulatedMs+e}getSessionData(){return{appId:this.appId,visitorId:this.visitorId,sessionId:this.sessionId,sessionDurationMs:this.getTotalDurationMs(),userAgent:navigator.userAgent,os:this.getOS(),deviceType:this.getDeviceType()}}sendSession(){if(this.getTotalDurationMs()<1e3)return;let e=this.getSessionData(),t=`${this.apiUrl}/api/external/v1/apps/${this.appId}/sessions`,n=JSON.stringify(e);fetch(t,{method:`POST`,headers:{"Content-Type":`application/json`},body:n,keepalive:!0,credentials:`omit`}).catch(()=>{}),this.accumulatedMs=0,this.segmentStart=Date.now()}onVisibilityChange(){document.visibilityState===`hidden`?(this.accumulatedMs+=Date.now()-this.segmentStart,this.isVisible=!1,this.sendSession()):(this.isVisible=!0,this.segmentStart=Date.now())}setupListeners(){window.addEventListener(`beforeunload`,()=>{this.sendSession()}),document.addEventListener(`visibilitychange`,()=>{this.onVisibilityChange()}),window.addEventListener(`pagehide`,()=>{this.sendSession()})}destroy(){this.sendSession()}},r=null;typeof document<`u`&&document.currentScript&&(r=document.currentScript.src);function i(e){return new Promise((t,n)=>{let r=document.createElement(`script`);r.src=e,r.onload=()=>t(),r.onerror=()=>n(Error(`Failed to load: ${e}`)),document.head.appendChild(r)})}async function a(){if(window.__superfeedback_widget)return window.__superfeedback_widget;if(!r)throw Error(`Cannot determine widget chunk URL`);await i(`${r}/dist/widget.iife.js`);let e=window.__superfeedback_widget;if(!e)throw Error(`Widget chunk failed to register`);return e}var o=class e{static instance=null;static CONTAINER_ID=`superfeedback-widget`;app=null;container=null;sessionTracker=null;isLoading=!1;constructor(t){this.container=document.createElement(`div`),this.container.id=e.CONTAINER_ID,this.container.style.position=`fixed`,this.container.style.bottom=`20px`,this.container.style.right=`20px`,this.container.style.zIndex=`9999`,document.body.appendChild(this.container),t.appId&&(this.sessionTracker=new n(t.appId,`https://app.superfeedback.ai`)),!t.appId&&!t.apiKey&&console.error(`Superfeedback: appId or apiKey is required`),t.apiKey&&this.loadWidget(t)}async loadWidget(e){if(!this.isLoading){this.isLoading=!0;try{let{createApp:t,Root:n}=await a(),r=t(n,{apiKey:e.apiKey,readonly:e.readonly,defaultOpen:e.defaultOpen,config:e.config,strategy:e.strategy});this.app=r,r.mount(this.container)}catch(e){console.error(`Superfeedback: Failed to load widget`,e)}finally{this.isLoading=!1}}}static init(t){return e.instance||=new e(typeof t==`string`?{apiKey:t}:t),e.instance}destroy(){this.sessionTracker&&=(this.sessionTracker.destroy(),null),this.app&&=(this.app.unmount(),null),this.container&&=(this.container.remove(),null),e.instance=null}};function s(){let e=document.currentScript;if(!e?.src)return null;try{return new URL(e.src,window.location.href).searchParams.get(`app_id`)}catch{return null}}function c(){let e=s();return e?(o.init({appId:e}),!0):!1}return typeof window<`u`&&(window.Superfeedback=o,c()),e.Superfeedback=o,e})({});
|
package/dist/index.js
CHANGED
|
@@ -11,10 +11,10 @@ var VISITOR_ID_KEY = "sf_visitor_id", SessionTracker = class {
|
|
|
11
11
|
}
|
|
12
12
|
getOrCreateVisitorId() {
|
|
13
13
|
try {
|
|
14
|
-
let a =
|
|
14
|
+
let a = localStorage.getItem(VISITOR_ID_KEY);
|
|
15
15
|
if (a) return a;
|
|
16
16
|
let o = crypto.randomUUID();
|
|
17
|
-
return
|
|
17
|
+
return localStorage.setItem(VISITOR_ID_KEY, o), o;
|
|
18
18
|
} catch {
|
|
19
19
|
return crypto.randomUUID();
|
|
20
20
|
}
|