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