@amplitude/engagement-browser
Version:
Official Amplitude SDK for Web
2 lines (1 loc) • 8.59 kB
JavaScript
var M=function(){function n(){}return n.prototype.getApplicationContext=function(){return{versionName:this.versionName,language:$(),platform:"Web",os:void 0,deviceModel:void 0}},n}(),$=function(){return typeof navigator<"u"&&(navigator.languages&&navigator.languages[0]||navigator.language)||""},R=function(){function n(){this.queue=[]}return n.prototype.logEvent=function(e){this.receiver?this.receiver(e):this.queue.length<512&&this.queue.push(e)},n.prototype.setEventReceiver=function(e){this.receiver=e,this.queue.length>0&&(this.queue.forEach(function(t){e(t)}),this.queue=[])},n}(),D=function(){return D=Object.assign||function(e){for(var t,r=1,i=arguments.length;r<i;r++){t=arguments[r];for(var a in t)Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a])}return e},D.apply(this,arguments)};function A(n){var e=typeof Symbol=="function"&&Symbol.iterator,t=e&&n[e],r=0;if(t)return t.call(n);if(n&&typeof n.length=="number")return{next:function(){return n&&r>=n.length&&(n=void 0),{value:n&&n[r++],done:!n}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function T(n,e){var t=typeof Symbol=="function"&&n[Symbol.iterator];if(!t)return n;var r=t.call(n),i,a=[],p;try{for(;(e===void 0||e-- >0)&&!(i=r.next()).done;)a.push(i.value)}catch(g){p={error:g}}finally{try{i&&!i.done&&(t=r.return)&&t.call(r)}finally{if(p)throw p.error}}return a}var U=function(n,e){var t,r,i=["string","number","boolean","undefined"],a=typeof n,p=typeof e;if(a!==p)return!1;try{for(var g=A(i),y=g.next();!y.done;y=g.next()){var E=y.value;if(E===a)return n===e}}catch(d){t={error:d}}finally{try{y&&!y.done&&(r=g.return)&&r.call(g)}finally{if(t)throw t.error}}if(n==null&&e==null)return!0;if(n==null||e==null||n.length!==e.length)return!1;var S=Array.isArray(n),v=Array.isArray(e);if(S!==v)return!1;if(S&&v){for(var c=0;c<n.length;c++)if(!U(n[c],e[c]))return!1}else{var o=Object.keys(n).sort(),s=Object.keys(e).sort();if(!U(o,s))return!1;var u=!0;return Object.keys(n).forEach(function(d){U(n[d],e[d])||(u=!1)}),u}return!0},G="$set",F="$unset",Z="$clearAll";Object.entries||(Object.entries=function(n){for(var e=Object.keys(n),t=e.length,r=new Array(t);t--;)r[t]=[e[t],n[e[t]]];return r});var W=function(){function n(){this.identity={userProperties:{}},this.listeners=new Set}return n.prototype.editIdentity=function(){var e=this,t=D({},this.identity.userProperties),r=D(D({},this.identity),{userProperties:t});return{setUserId:function(i){return r.userId=i,this},setDeviceId:function(i){return r.deviceId=i,this},setUserProperties:function(i){return r.userProperties=i,this},setOptOut:function(i){return r.optOut=i,this},updateUserProperties:function(i){var a,p,g,y,E,S,v=r.userProperties||{};try{for(var c=A(Object.entries(i)),o=c.next();!o.done;o=c.next()){var s=T(o.value,2),u=s[0],d=s[1];switch(u){case G:try{for(var f=(g=void 0,A(Object.entries(d))),m=f.next();!m.done;m=f.next()){var _=T(m.value,2),I=_[0],l=_[1];v[I]=l}}catch(P){g={error:P}}finally{try{m&&!m.done&&(y=f.return)&&y.call(f)}finally{if(g)throw g.error}}break;case F:try{for(var h=(E=void 0,A(Object.keys(d))),w=h.next();!w.done;w=h.next()){var I=w.value;delete v[I]}}catch(P){E={error:P}}finally{try{w&&!w.done&&(S=h.return)&&S.call(h)}finally{if(E)throw E.error}}break;case Z:v={};break}}}catch(P){a={error:P}}finally{try{o&&!o.done&&(p=c.return)&&p.call(c)}finally{if(a)throw a.error}}return r.userProperties=v,this},commit:function(){return e.setIdentity(r),this}}},n.prototype.getIdentity=function(){return D({},this.identity)},n.prototype.setIdentity=function(e){var t=D({},this.identity);this.identity=D({},e),U(t,this.identity)||this.listeners.forEach(function(r){r(e)})},n.prototype.addIdentityListener=function(e){this.listeners.add(e)},n.prototype.removeIdentityListener=function(e){this.listeners.delete(e)},n}(),b=typeof globalThis<"u"?globalThis:typeof window<"u"?window:self,C=function(){function n(){this.identityStore=new W,this.eventBridge=new R,this.applicationContextProvider=new M}return n.getInstance=function(e){return b.analyticsConnectorInstances||(b.analyticsConnectorInstances={}),b.analyticsConnectorInstances[e]||(b.analyticsConnectorInstances[e]=new n),b.analyticsConnectorInstances[e]},n}();var L=["boot"];var z="$default_instance";function k(n){var i,a,p,g,y,E,S,v;let e=window.engagement,t={_q:(i=e==null?void 0:e._q)!=null?i:[],_configuration:{apiKey:(p=(a=e==null?void 0:e._configuration)==null?void 0:a.apiKey)!=null?p:"",serverUrl:(g=e==null?void 0:e._configuration)==null?void 0:g.serverUrl,chatUrl:(y=e==null?void 0:e._configuration)==null?void 0:y.chatUrl,serverZone:(S=(E=e==null?void 0:e._configuration)==null?void 0:E.serverZone)!=null?S:"US",options:{...(v=e==null?void 0:e._configuration)==null?void 0:v.options}},_sentry:void 0,init(c,o){var m,_,I;if(t._configuration.apiKey){console.log("Engagement SDK has already been initialized. Ignoring additional init call.");return}t._configuration={...t._configuration,...o,apiKey:c,options:{splitting:!0,...t._configuration.options,...o==null?void 0:o.options}},(_=(m=t._configuration)==null?void 0:m.options)!=null&&_.logger&&t._configuration.options.logger.enable((I=t._configuration.options.logLevel)!=null?I:2);let s;if(o!=null&&o.cdnUrl)t._configuration.options.splitting?s=`${o.cdnUrl}/engagement-browser/prod/split/index.js`:s=o.cdnUrl+"/engagement-browser/prod/index.min.js.gz";else{let l=t._configuration.serverZone==="EU"?"https://cdn.eu.amplitude.com":"https://cdn.amplitude.com";t._configuration.options.splitting?s=`${l}/engagement-browser/prod/split/index.js`:s=`${l}/engagement-browser/prod/index.min.js.gz`}let u=null,d=()=>{if(u&&(clearTimeout(u),u=null),t._q&&t._q.length>0)for(console.warn(`Engagement SDK failed to load within ${f}ms. Resolving pending calls gracefully.`);t._q.length>0;){let l=t._q.shift();if(!l)continue;let h=l[0];if(L.includes(h)&&l[1]instanceof Function&&l[2]instanceof Function){let w=l[1];console.warn(`Engagement SDK method '${h}' resolved as no-op due to script loading failure`),w(void 0)}}};n(s,t._configuration.options.splitting?"module":void 0,o==null?void 0:o.nonce,d);let f=1e4;u=setTimeout(()=>{d()},f)},plugin(c){let o=t.init;return{name:"@amplitude/engagement-browser",type:"enrichment",async setup(s,u){var _,I;let d=(_=s.instanceName)!=null?_:z,f=C.getInstance(d).identityStore;o(s.apiKey,{serverZone:s.serverZone,serverUrl:(I=s.serverUrl)!=null&&I.includes("amplitude.com")?void 0:s.serverUrl,...c,options:{logLevel:s.logLevel,logger:s.loggerProvider,...c==null?void 0:c.options}});let m=[{track:l=>{u.track(l)}}];await window.engagement.boot({user:()=>{let l=f.getIdentity();return{user_id:u.getUserId(),device_id:u.getDeviceId(),user_properties:l.userProperties,getSessionId:u.getSessionId}},integrations:m}),f.addIdentityListener(l=>{var h,w,P,K;(((w=(h=window.engagement)==null?void 0:h._.user)==null?void 0:w.user_id)!==l.userId||((K=(P=window.engagement)==null?void 0:P._.user)==null?void 0:K.device_id)!==l.deviceId)&&window.engagement.boot({user:()=>{let B=f.getIdentity();return{user_id:u.getUserId(),device_id:u.getDeviceId(),user_properties:B.userProperties,getSessionId:u.getSessionId}},integrations:m}),window.engagement._setUserProperties(l.userProperties)})},async execute(s){return window.engagement.forwardEvent(s),s}}}},r=t;return new Proxy(t,{get:function(c,o){if(o in r)return r[o];if(o!=="then")return o==="gs"||o==="rc"?new Proxy({},{get:function(s,u){return function(){let d=Array.from(arguments),f=`${o}.${u}`;d.unshift(f),t._q.push(d)}}}):L.includes(o)?function(){let s=Array.prototype.slice.call(arguments);return new Promise((u,d)=>{s.unshift(o,u,d),t._q.push(s)})}:function(){let s=Array.prototype.slice.call(arguments);s.unshift(o),t._q.push(s)}}})}var N=(i=>(i.Local="https://local.amplitude.com:3010/",i.Staging="https://apps.stag2.amplitude.com/",i.Production="https://app.amplitude.com/",i.ProductionEU="https://app.eu.amplitude.com/",i))(N||{}),O=n=>{if(window.opener)for(let e of Object.values(N))window.opener.postMessage({message:n},e)};function H(n,e,t,r){for(let i of n){let a=document.createElement("script");a.src=i,a.id="engagement-sdk-bundle",t&&a.setAttribute("nonce",t),e&&a.setAttribute("type",e),r&&(a.onerror=r),document.getElementsByTagName("head")[0].appendChild(a)}}var q=()=>(O("ENGAGEMENT_SNIPPET"),k((n,e,t,r)=>H([n||"https://cdn.amplitude.com/engagement-browser/prod/index.min.js.gz"],e,t,r)));var x=q(),ie=(n,e)=>{typeof window<"u"&&!window.engagement&&(window.engagement=x),x.init(n,e)},se=n=>(typeof window<"u"&&!window.engagement&&(window.engagement=x),x.plugin(n));export{ie as init,se as plugin};