@amplitude/engagement-browser
Version:
Official Amplitude SDK for Web
2 lines (1 loc) • 8.15 kB
JavaScript
;var U=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var $=(n,e)=>{for(var t in e)U(n,t,{get:e[t],enumerable:!0})},G=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of q(e))!M.call(n,o)&&o!==t&&U(n,o,{get:()=>e[o],enumerable:!(r=B(e,o))||r.enumerable});return n};var R=n=>G(U({},"__esModule",{value:!0}),n);var ne={};$(ne,{init:()=>j,plugin:()=>ee});module.exports=R(ne);var Z=function(){function n(){}return n.prototype.getApplicationContext=function(){return{versionName:this.versionName,language:F(),platform:"Web",os:void 0,deviceModel:void 0}},n}(),F=function(){return typeof navigator<"u"&&(navigator.languages&&navigator.languages[0]||navigator.language)||""},W=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,o=arguments.length;r<o;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 C(n,e){var t=typeof Symbol=="function"&&n[Symbol.iterator];if(!t)return n;var r=t.call(n),o,s=[],f;try{for(;(e===void 0||e-- >0)&&!(o=r.next()).done;)s.push(o.value)}catch(g){f={error:g}}finally{try{o&&!o.done&&(t=r.return)&&t.call(r)}finally{if(f)throw f.error}}return s}var D=function(n,e){var t,r,o=["string","number","boolean","undefined"],s=typeof n,f=typeof e;if(s!==f)return!1;try{for(var g=b(o),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 i=Object.keys(n).sort(),u=Object.keys(e).sort();if(!D(i,u))return!1;var d=!0;return Object.keys(n).forEach(function(c){D(n[c],e[c])||(d=!1)}),d}return!0},z="$set",H="$unset",V="$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 Y=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(o){return r.userId=o,this},setDeviceId:function(o){return r.deviceId=o,this},setUserProperties:function(o){return r.userProperties=o,this},setOptOut:function(o){return r.optOut=o,this},updateUserProperties:function(o){var s,f,g,p,m,v,l=r.userProperties||{};try{for(var a=b(Object.entries(o)),i=a.next();!i.done;i=a.next()){var u=C(i.value,2),d=u[0],c=u[1];switch(d){case z:try{for(var y=(g=void 0,b(Object.entries(c))),h=y.next();!h.done;h=y.next()){var E=C(h.value,2),x=E[0],T=E[1];l[x]=T}}catch(S){g={error:S}}finally{try{h&&!h.done&&(p=y.return)&&p.call(y)}finally{if(g)throw g.error}}break;case H: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 V:l={};break}}}catch(S){s={error:S}}finally{try{i&&!i.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,K=function(){function n(){this.identityStore=new Y,this.eventBridge=new W,this.applicationContextProvider=new Z}return n.getInstance=function(e){return P.analyticsConnectorInstances||(P.analyticsConnectorInstances={}),P.analyticsConnectorInstances[e]||(P.analyticsConnectorInstances[e]=new n),P.analyticsConnectorInstances[e]},n}();var J=["boot"];var Q="$default_instance";function L(n){var o,s,f,g,p,m,v;let e=window.engagement,t={_q:(o=e==null?void 0:e._q)!=null?o:[],_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 i;if(a!=null&&a.cdnUrl)t._configuration.options.splitting?i=`${a.cdnUrl}/engagement-browser/prod/split/index.js`:i=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?i=`${y}/engagement-browser/prod/split/index.js`:i=`${y}/engagement-browser/prod/index.min.js.gz`}window.engagement||(window.engagement=t),n(i,t._configuration.options.splitting?"module":void 0)},plugin(l){let a=t.init;return{name:"@amplitude/engagement-browser",type:"enrichment",async setup(i,u){var y,h;let d=(y=i.instanceName)!=null?y:Q,c=K.getInstance(d).identityStore;a(i.apiKey,{serverZone:i.serverZone,serverUrl:(h=i.serverUrl)!=null&&h.includes("amplitude.com")?void 0:i.serverUrl,...l,options:{logLevel:i.logLevel,logger:i.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(i){return window.engagement.forwardEvent(i),i}}}},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(i,u){return function(){let d=Array.from(arguments),c=`${a}.${u}`;d.unshift(c),t._q.push(d)}}}):J.includes(a)?function(){let i=Array.prototype.slice.call(arguments);return new Promise((u,d)=>{i.unshift(a,u,d),t._q.push(i)})}:function(){let i=Array.prototype.slice.call(arguments);i.unshift(a),t._q.push(i)}}})}var k=(o=>(o.Local="https://local.amplitude.com:3010/",o.Staging="https://apps.stag2.amplitude.com/",o.Production="https://app.amplitude.com/",o.ProductionEU="https://app.eu.amplitude.com/",o))(k||{}),N=n=>{if(window.opener)for(let e of Object.values(k))window.opener.postMessage({message:n},e)};function X(n,e){for(let t of n){let r=document.createElement("script");r.src=t,r.id="engagement-sdk-bundle";let o=document.querySelector("[nonce]");o&&r.setAttribute("nonce",o.nonce||o.getAttribute("nonce")),e&&r.setAttribute("type",e),document.getElementsByTagName("head")[0].appendChild(r)}}var O=()=>(N("ENGAGEMENT_SNIPPET"),L((n,e)=>X([n||"https://cdn.amplitude.com/engagement-browser/prod/index.min.js.gz"],e)));var A=O(),j=(n,e)=>{window.engagement||(window.engagement=A),A.init(n,e)},ee=n=>(window.engagement||(window.engagement=A),A.plugin(n));