@amplitude/engagement-browser
Version:
Official Amplitude SDK for Web
2 lines (1 loc) • 7.69 kB
JavaScript
var T=function(){function n(){}return n.prototype.getApplicationContext=function(){return{versionName:this.versionName,language:B(),platform:"Web",os:void 0,deviceModel:void 0}},n}(),B=function(){return typeof navigator<"u"&&(navigator.languages&&navigator.languages[0]||navigator.language)||""},q=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}(),w=function(){return w=Object.assign||function(e){for(var t,r=1,i=arguments.length;r<i;r++){t=arguments[r];for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&(e[s]=t[s])}return e},w.apply(this,arguments)};function b(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 U(n,e){var t=typeof Symbol=="function"&&n[Symbol.iterator];if(!t)return n;var r=t.call(n),i,s=[],f;try{for(;(e===void 0||e-- >0)&&!(i=r.next()).done;)s.push(i.value)}catch(g){f={error:g}}finally{try{i&&!i.done&&(t=r.return)&&t.call(r)}finally{if(f)throw f.error}}return s}var D=function(n,e){var t,r,i=["string","number","boolean","undefined"],s=typeof n,f=typeof e;if(s!==f)return!1;try{for(var g=b(i),p=g.next();!p.done;p=g.next()){var m=p.value;if(m===s)return n===e}}catch(c){t={error:c}}finally{try{p&&!p.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 v=Array.isArray(n),l=Array.isArray(e);if(v!==l)return!1;if(v&&l){for(var a=0;a<n.length;a++)if(!D(n[a],e[a]))return!1}else{var o=Object.keys(n).sort(),u=Object.keys(e).sort();if(!D(o,u))return!1;var d=!0;return Object.keys(n).forEach(function(c){D(n[c],e[c])||(d=!1)}),d}return!0},M="$set",$="$unset",G="$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 R=function(){function n(){this.identity={userProperties:{}},this.listeners=new Set}return n.prototype.editIdentity=function(){var e=this,t=w({},this.identity.userProperties),r=w(w({},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 s,f,g,p,m,v,l=r.userProperties||{};try{for(var a=b(Object.entries(i)),o=a.next();!o.done;o=a.next()){var u=U(o.value,2),d=u[0],c=u[1];switch(d){case M:try{for(var y=(g=void 0,b(Object.entries(c))),h=y.next();!h.done;h=y.next()){var E=U(h.value,2),x=E[0],O=E[1];l[x]=O}}catch(S){g={error:S}}finally{try{h&&!h.done&&(p=y.return)&&p.call(y)}finally{if(g)throw g.error}}break;case $:try{for(var _=(m=void 0,b(Object.keys(c))),I=_.next();!I.done;I=_.next()){var x=I.value;delete l[x]}}catch(S){m={error:S}}finally{try{I&&!I.done&&(v=_.return)&&v.call(_)}finally{if(m)throw m.error}}break;case G:l={};break}}}catch(S){s={error:S}}finally{try{o&&!o.done&&(f=a.return)&&f.call(a)}finally{if(s)throw s.error}}return r.userProperties=l,this},commit:function(){return e.setIdentity(r),this}}},n.prototype.getIdentity=function(){return w({},this.identity)},n.prototype.setIdentity=function(e){var t=w({},this.identity);this.identity=w({},e),D(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}(),P=typeof globalThis<"u"?globalThis:typeof window<"u"?window:self,C=function(){function n(){this.identityStore=new R,this.eventBridge=new q,this.applicationContextProvider=new T}return n.getInstance=function(e){return P.analyticsConnectorInstances||(P.analyticsConnectorInstances={}),P.analyticsConnectorInstances[e]||(P.analyticsConnectorInstances[e]=new n),P.analyticsConnectorInstances[e]},n}();var Z=["boot"];var F="$default_instance";function K(n){var i,s,f,g,p,m,v;let e=window.engagement,t={_q:(i=e==null?void 0:e._q)!=null?i:[],_configuration:{apiKey:(f=(s=e==null?void 0:e._configuration)==null?void 0:s.apiKey)!=null?f:"",serverUrl:(g=e==null?void 0:e._configuration)==null?void 0:g.serverUrl,serverZone:(m=(p=e==null?void 0:e._configuration)==null?void 0:p.serverZone)!=null?m:"US",options:{...(v=e==null?void 0:e._configuration)==null?void 0:v.options}},_sentry:void 0,init(l,a){var u,d,c;if(t._configuration.apiKey){console.log("Engagement SDK has already been initialized. Ignoring additional init call.");return}t._configuration={...t._configuration,...a,apiKey:l,options:{splitting:!0,...t._configuration.options,...a==null?void 0:a.options}},(d=(u=t._configuration)==null?void 0:u.options)!=null&&d.logger&&t._configuration.options.logger.enable((c=t._configuration.options.logLevel)!=null?c:2);let o;if(a!=null&&a.cdnUrl)t._configuration.options.splitting?o=`${a.cdnUrl}/engagement-browser/prod/split/index.js`:o=a.cdnUrl+"/engagement-browser/prod/index.min.js.gz";else{let y=t._configuration.serverZone==="EU"?"https://cdn.eu.amplitude.com":"https://cdn.amplitude.com";t._configuration.options.splitting?o=`${y}/engagement-browser/prod/split/index.js`:o=`${y}/engagement-browser/prod/index.min.js.gz`}window.engagement||(window.engagement=t),n(o,t._configuration.options.splitting?"module":void 0)},plugin(l){let a=t.init;return{name:"@amplitude/engagement-browser",type:"enrichment",async setup(o,u){var y,h;let d=(y=o.instanceName)!=null?y:F,c=C.getInstance(d).identityStore;a(o.apiKey,{serverZone:o.serverZone,serverUrl:(h=o.serverUrl)!=null&&h.includes("amplitude.com")?void 0:o.serverUrl,...l,options:{logLevel:o.logLevel,logger:o.loggerProvider,...l==null?void 0:l.options}}),await window.engagement.boot({user:()=>{let E=c.getIdentity();return{user_id:u.getUserId(),device_id:u.getDeviceId(),user_properties:E.userProperties,getSessionId:u.getSessionId}},integrations:[{track:E=>{u.track(E)}}]}),c.addIdentityListener(E=>{window.engagement._setUserProperties(E.userProperties)})},async execute(o){return window.engagement.forwardEvent(o),o}}}},r=t;return new Proxy(t,{get:function(l,a){if(a in r)return r[a];if(a!=="then")return a==="gs"?new Proxy({},{get:function(o,u){return function(){let d=Array.from(arguments),c=`${a}.${u}`;d.unshift(c),t._q.push(d)}}}):Z.includes(a)?function(){let o=Array.prototype.slice.call(arguments);return new Promise((u,d)=>{o.unshift(a,u,d),t._q.push(o)})}:function(){let o=Array.prototype.slice.call(arguments);o.unshift(a),t._q.push(o)}}})}var L=(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))(L||{}),k=n=>{if(window.opener)for(let e of Object.values(L))window.opener.postMessage({message:n},e)};function W(n,e){for(let t of n){let r=document.createElement("script");r.src=t,r.id="engagement-sdk-bundle";let i=document.querySelector("[nonce]");i&&r.setAttribute("nonce",i.nonce||i.getAttribute("nonce")),e&&r.setAttribute("type",e),document.getElementsByTagName("head")[0].appendChild(r)}}var N=()=>(k("ENGAGEMENT_SNIPPET"),K((n,e)=>W([n||"https://cdn.amplitude.com/engagement-browser/prod/index.min.js.gz"],e)));var A=N(),re=(n,e)=>{window.engagement||(window.engagement=A),A.init(n,e)},oe=n=>(window.engagement||(window.engagement=A),A.plugin(n));export{re as init,oe as plugin};