UNPKG

@amplitude/engagement-browser

Version:

Official Amplitude SDK for Web

2 lines (1 loc) 9.05 kB
"use strict";var K=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var G=Object.prototype.hasOwnProperty;var F=(n,e)=>{for(var t in e)K(n,t,{get:e[t],enumerable:!0})},Z=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of R(e))!G.call(n,o)&&o!==t&&K(n,o,{get:()=>e[o],enumerable:!(r=$(e,o))||r.enumerable});return n};var W=n=>Z(K({},"__esModule",{value:!0}),n);var re={};F(re,{init:()=>ne,plugin:()=>te});module.exports=W(re);var z=function(){function n(){}return n.prototype.getApplicationContext=function(){return{versionName:this.versionName,language:H(),platform:"Web",os:void 0,deviceModel:void 0}},n}(),H=function(){return typeof navigator<"u"&&(navigator.languages&&navigator.languages[0]||navigator.language)||""},V=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,o=arguments.length;r<o;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 C(n,e){var t=typeof Symbol=="function"&&n[Symbol.iterator];if(!t)return n;var r=t.call(n),o,a=[],p;try{for(;(e===void 0||e-- >0)&&!(o=r.next()).done;)a.push(o.value)}catch(g){p={error:g}}finally{try{o&&!o.done&&(t=r.return)&&t.call(r)}finally{if(p)throw p.error}}return a}var U=function(n,e){var t,r,o=["string","number","boolean","undefined"],a=typeof n,p=typeof e;if(a!==p)return!1;try{for(var g=A(o),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 i=Object.keys(n).sort(),s=Object.keys(e).sort();if(!U(i,s))return!1;var u=!0;return Object.keys(n).forEach(function(d){U(n[d],e[d])||(u=!1)}),u}return!0},Y="$set",Q="$unset",J="$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 X=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(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 a,p,g,y,E,S,v=r.userProperties||{};try{for(var c=A(Object.entries(o)),i=c.next();!i.done;i=c.next()){var s=C(i.value,2),u=s[0],d=s[1];switch(u){case Y:try{for(var f=(g=void 0,A(Object.entries(d))),m=f.next();!m.done;m=f.next()){var _=C(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 Q: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 J:v={};break}}}catch(P){a={error:P}}finally{try{i&&!i.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,L=function(){function n(){this.identityStore=new X,this.eventBridge=new V,this.applicationContextProvider=new z}return n.getInstance=function(e){return b.analyticsConnectorInstances||(b.analyticsConnectorInstances={}),b.analyticsConnectorInstances[e]||(b.analyticsConnectorInstances[e]=new n),b.analyticsConnectorInstances[e]},n}();var k=["boot"];var j="$default_instance";function N(n){var o,a,p,g,y,E,S,v;let e=window.engagement,t={_q:(o=e==null?void 0:e._q)!=null?o:[],_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,i){var m,_,I;if(t._configuration.apiKey){console.log("Engagement SDK has already been initialized. Ignoring additional init call.");return}t._configuration={...t._configuration,...i,apiKey:c,options:{splitting:!0,...t._configuration.options,...i==null?void 0:i.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(i!=null&&i.cdnUrl)t._configuration.options.splitting?s=`${i.cdnUrl}/engagement-browser/prod/split/index.js`:s=i.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(k.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,i==null?void 0:i.nonce,d);let f=1e4;u=setTimeout(()=>{d()},f)},plugin(c){let i=t.init;return{name:"@amplitude/engagement-browser",type:"enrichment",async setup(s,u){var _,I;let d=(_=s.instanceName)!=null?_:j,f=L.getInstance(d).identityStore;i(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,T;(((w=(h=window.engagement)==null?void 0:h._.user)==null?void 0:w.user_id)!==l.userId||((T=(P=window.engagement)==null?void 0:P._.user)==null?void 0:T.device_id)!==l.deviceId)&&window.engagement.boot({user:()=>{let M=f.getIdentity();return{user_id:u.getUserId(),device_id:u.getDeviceId(),user_properties:M.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,i){if(i in r)return r[i];if(i!=="then")return i==="gs"||i==="rc"?new Proxy({},{get:function(s,u){return function(){let d=Array.from(arguments),f=`${i}.${u}`;d.unshift(f),t._q.push(d)}}}):k.includes(i)?function(){let s=Array.prototype.slice.call(arguments);return new Promise((u,d)=>{s.unshift(i,u,d),t._q.push(s)})}:function(){let s=Array.prototype.slice.call(arguments);s.unshift(i),t._q.push(s)}}})}var O=(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))(O||{}),q=n=>{if(window.opener)for(let e of Object.values(O))window.opener.postMessage({message:n},e)};function ee(n,e,t,r){for(let o of n){let a=document.createElement("script");a.src=o,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 B=()=>(q("ENGAGEMENT_SNIPPET"),N((n,e,t,r)=>ee([n||"https://cdn.amplitude.com/engagement-browser/prod/index.min.js.gz"],e,t,r)));var x=B(),ne=(n,e)=>{typeof window<"u"&&!window.engagement&&(window.engagement=x),x.init(n,e)},te=n=>(typeof window<"u"&&!window.engagement&&(window.engagement=x),x.plugin(n));