!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).amplitude=t()}(this,function(){"use strict";function t(e){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function l(t){for(var e=1;e>6|192):(t+=String.fromCharCode(i>>12|224),t+=String.fromCharCode(i>>6&63|128)),t+=String.fromCharCode(63&i|128))}return t},R=function(e){for(var t="",n=0,i=0,r=0,o=0;n>2,o=(3&t)<<4|(n=e.charCodeAt(p++))>>4,s=(15&n)<<2|(i=e.charCodeAt(p++))>>6,a=63&i,isNaN(n)?s=a=64:isNaN(i)&&(a=64),u=u+q._keyStr.charAt(r)+q._keyStr.charAt(o)+q._keyStr.charAt(s)+q._keyStr.charAt(a);return u},decode:function(e){try{if(window.btoa&&window.atob)return decodeURIComponent(escape(window.atob(e)))}catch(e){}return q._decode(e)},_decode:function(e){var t,n,i,r,o,s,a="",u=0;for(e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");u>4,n=(15&r)<<4|(o=q._keyStr.indexOf(e.charAt(u++)))>>2,i=(3&o)<<6|(s=q._keyStr.indexOf(e.charAt(u++))),a+=String.fromCharCode(t),64!==o&&(a+=String.fromCharCode(n)),64!==s&&(a+=String.fromCharCode(i));return a=R(a)}},j=Object.prototype.toString;function C(e){switch(j.call(e)){case"[object Date]":return"date";case"[object RegExp]":return"regexp";case"[object Arguments]":return"arguments";case"[object Array]":return"array";case"[object Error]":return"error"}return null===e?"null":void 0===e?"undefined":e!=e?"nan":e&&1===e.nodeType?"element":"undefined"!=typeof Buffer&&"function"==typeof Buffer.isBuffer&&Buffer.isBuffer(e)?"buffer":t(e=e.valueOf?e.valueOf():Object.prototype.valueOf.apply(e))}var e,U={DISABLE:0,ERROR:1,WARN:2,INFO:3},D=U.WARN,M={error:function(e){D>=U.ERROR&&z(e)},warn:function(e){D>=U.WARN&&z(e)},info:function(e){D>=U.INFO&&z(e)}},z=function(e){try{console.log("[Amplitude] "+e)}catch(e){}},B=function(e){return"string"===C(e)&&e.length>n?e.substring(0,n):e},G=function(e){var t=C(e);if("object"!==t)return M.error("Error: invalid properties format. Expecting Javascript object, received "+t+", ignoring"),{};if(Object.keys(e).length>a)return M.error("Error: too many properties (more than 1000), ignoring"),{};var n={};for(var i in e)if(e.hasOwnProperty(i)){var r=i,o=C(r);"string"!==o&&(r=String(r),M.warn("WARNING: Non-string property key, received type "+o+', coercing to string "'+r+'"'));var s=F(r,e[i]);null!==s&&(n[r]=s)}return n},L=["nan","function","arguments","regexp","element"],F=function e(t,n){var i=C(n);if(-1!==L.indexOf(i))M.warn('WARNING: Property key "'+t+'" with invalid value type '+i+", ignoring"),n=null;else if("undefined"===i)n=null;else if("error"===i)n=String(n),M.warn('WARNING: Property key "'+t+'" with value type error, coercing to '+n);else if("array"===i){for(var r=[],o=0;o>16)+(t>>16)+(n>>16)<<16|65535&n}function a(e,t,n,i,r,o){return d((s=d(d(t,e),d(i,o)))<<(a=r)|s>>>32-a,n);var s,a}function l(e,t,n,i,r,o,s){return a(t&n|~t&i,e,t,r,o,s)}function f(e,t,n,i,r,o,s){return a(t&i|n&~i,e,t,r,o,s)}function h(e,t,n,i,r,o,s){return a(t^n^i,e,t,r,o,s)}function g(e,t,n,i,r,o,s){return a(n^(t|~i),e,t,r,o,s)}function u(e,t){var n,i,r,o,s;e[t>>5]|=128<>>9<<4)]=t;var a=1732584193,u=-271733879,p=-1732584194,c=271733878;for(n=0;n>5]>>>t%32&255);return n}function c(e){var t,n=[];for(n[(e.length>>2)-1]=void 0,t=0;t>5]|=(255&e.charCodeAt(t/8))<>>4&15)+i.charAt(15&t);return r}function n(e){return unescape(encodeURIComponent(e))}function r(e){return p(u(c(t=n(e)),8*t.length));var t}function o(e,t){return function(e,t){var n,i,r=c(e),o=[],s=[];for(o[15]=s[15]=void 0,16>t/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,e)}),Te="5.2.2",Pe={apiEndpoint:"api.amplitude.com",batchEvents:!1,cookieExpiration:3650,cookieName:"amplitude_id",deviceIdFromUrlParam:!1,domain:"",eventUploadPeriodMillis:3e4,eventUploadThreshold:30,forceHttps:!0,includeGclid:!1,includeReferrer:!1,includeUtm:!1,language:{language:navigator&&(navigator.languages&&navigator.languages[0]||navigator.language||navigator.userLanguage)||void 0}.language,logLevel:"WARN",optOut:!1,platform:"Web",savedMaxCount:1e3,saveEvents:!0,saveParamsReferrerOncePerSession:!0,secureCookie:!1,sessionTimeout:18e5,trackingOptions:{city:!0,country:!0,device_model:!0,dma:!0,ip_address:!0,language:!0,os_name:!0,os_version:!0,platform:!0,region:!0,version_name:!0},unsetParamsReferrerOnNewSession:!1,unsentKey:"amplitude_unsent",unsentIdentifyKey:"amplitude_unsent_identify",uploadBatchSize:100},Ae=function(e){this._instanceName=$(e)?f:e.toLowerCase(),this._legacyStorageSuffix=this._instanceName===f?"":"_"+this._instanceName,this._unsentEvents=[],this._unsentIdentifys=[],this._ua=new Ne(navigator.userAgent).getResult(),this.options=l({},Pe,{trackingOptions:l({},Pe.trackingOptions)}),this.cookieStorage=(new de).getStorage(),this._q=[],this._sending=!1,this._updateScheduled=!1,this._onInit=[],this._eventId=0,this._identifyId=0,this._lastEventTime=null,this._newSession=!1,this._sequenceNumber=0,this._sessionId=null,this._isInitialized=!1,this._userAgent=navigator&&navigator.userAgent||null};Ae.prototype.Identify=fe,Ae.prototype.Revenue=xe,Ae.prototype.init=function(e,t,n,i){if("string"!==C(e)||$(e))W.error("Invalid apiKey. Please re-initialize with a valid apiKey");else{try{this.options.apiKey=e,this._storageSuffix="_"+e+this._legacyStorageSuffix,Re(this.options,n),"string"===C(this.options.logLevel)&&V(this.options.logLevel);var r=Be(this);if(this._apiPropertiesTrackingOptions=0this.options.sessionTimeout)&&(this.options.unsetParamsReferrerOnNewSession&&this._unsetUTMParams(),this._newSession=!0,this._sessionId=d,this.options.saveParamsReferrerOncePerSession&&this._trackParamsAndReferrer()),this.options.saveParamsReferrerOncePerSession||this._trackParamsAndReferrer(),this._lastEventTime=d,Ue(this),this._sendEventsIfReady()}catch(e){W.error(e)}finally{"function"===C(i)&&i(this)}for(var l=0;l=this.options.eventUploadThreshold?(this.sendEvents(e),!0):(this._updateScheduled||(this._updateScheduled=!0,setTimeout(function(){this._updateScheduled=!1,this.sendEvents()}.bind(this),this.options.eventUploadPeriodMillis)),!1):(this.sendEvents(e),!0))},Ae.prototype._getFromStorage=function(e,t){return e.getItem(t+this._storageSuffix)},Ae.prototype._getFromStorageLegacy=function(e,t){return e.getItem(t+this._legacyStorageSuffix)},Ae.prototype._setInStorage=function(e,t,n){e.setItem(t+this._storageSuffix,n)};var qe=function(e){var t=e.cookieStorage.get(e.options.cookieName+e._storageSuffix);if("object"!==C(t)&&!("object"===C(t=e.cookieStorage.get(e.options.cookieName+e._legacyStorageSuffix))&&t.deviceId&&t.sessionId&&t.lastEventTime)){var n=function(e){var t=ce.getItem(e);return ce.removeItem(e),t},i="string"===C(e.options.apiKey)&&"_"+e.options.apiKey.slice(0,6)||"",r=n(b+i),o=n(S+i),s=n(I+i);null!=s&&(s="true"===String(s));var a=parseInt(n(w)),u=parseInt(n(m)),p=parseInt(n(v)),c=parseInt(n(y)),d=parseInt(n(_)),l=function(e){return"object"===C(t)&&t[e]};e.options.deviceId=l("deviceId")||r,e.options.userId=l("userId")||o,e._sessionId=l("sessionId")||a||e._sessionId,e._lastEventTime=l("lastEventTime")||u||e._lastEventTime,e._eventId=l("eventId")||p||e._eventId,e._identifyId=l("identifyId")||c||e._identifyId,e._sequenceNumber=l("sequenceNumber")||d||e._sequenceNumber,e.options.optOut=s||!1,t&&void 0!==t.optOut&&null!==t.optOut&&(e.options.optOut="true"===String(t.optOut)),Ue(e)}},je=function(e){var t=e.cookieStorage.get(e.options.cookieName+e._storageSuffix);if("object"===C(t))Ce(e,t);else{var n=e.cookieStorage.get(e.options.cookieName+e._legacyStorageSuffix);"object"===C(n)&&(e.cookieStorage.remove(e.options.cookieName+e._legacyStorageSuffix),Ce(e,n))}},Ce=function(e,t){t.deviceId&&(e.options.deviceId=t.deviceId),t.userId&&(e.options.userId=t.userId),null!==t.optOut&&void 0!==t.optOut&&(e.options.optOut=t.optOut),t.sessionId&&(e._sessionId=parseInt(t.sessionId)),t.lastEventTime&&(e._lastEventTime=parseInt(t.lastEventTime)),t.eventId&&(e._eventId=parseInt(t.eventId)),t.identifyId&&(e._identifyId=parseInt(t.identifyId)),t.sequenceNumber&&(e._sequenceNumber=parseInt(t.sequenceNumber))},Ue=function(e){e.cookieStorage.set(e.options.cookieName+e._storageSuffix,{deviceId:e.options.deviceId,userId:e.options.userId,optOut:e.options.optOut,sessionId:e._sessionId,lastEventTime:e._lastEventTime,eventId:e._eventId,identifyId:e._identifyId,sequenceNumber:e._sequenceNumber})};Ae.prototype._initUtmData=function(e,t){e=e||this._getUrlParams(),t=t||this.cookieStorage.get("__utmz");var n,i,r,o,s,a,u,p,c,d,l,f=(i=e,r=(n=t)?"?"+n.split(".").slice(-1)[0].replace(/\|/g,"&"):"",s=(o=function(e,t,n,i){return J(e,t)||J(n,i)})(x,i,"utmcsr",r),a=o(N,i,"utmcmd",r),u=o(k,i,"utmccn",r),p=o(T,i,"utmctr",r),c=o(P,i,"utmcct",r),d={},(l=function(e,t){$(t)||(d[e]=t)})(x,s),l(N,a),l(k,u),l(T,p),l(P,c),d);De(this,f)},Ae.prototype._unsetUTMParams=function(){var e=new fe;e.unset(O),e.unset(x),e.unset(N),e.unset(k),e.unset(T),e.unset(P),this.identify(e)};var De=function(e,t){if("object"===C(t)&&0!==Object.keys(t).length){var n=new fe;for(var i in t)t.hasOwnProperty(i)&&(n.setOnce("initial_"+i,t[i]),n.set(i,t[i]));e.identify(n)}};Ae.prototype._getReferrer=function(){return document.referrer},Ae.prototype._getUrlParams=function(){return location.search},Ae.prototype._saveGclid=function(e){var t=J("gclid",e);$(t)||De(this,{gclid:t})},Ae.prototype._getDeviceIdFromUrlParam=function(e){return J(E,e)},Ae.prototype._getReferringDomain=function(e){if($(e))return null;var t=e.split("/");return 3<=t.length?t[2]:null},Ae.prototype._saveReferrer=function(e){if(!$(e)){var t={referrer:e,referring_domain:this._getReferringDomain(e)};De(this,t)}},Ae.prototype.saveEvents=function(){try{this._setInStorage(ce,this.options.unsentKey,JSON.stringify(this._unsentEvents))}catch(e){}try{this._setInStorage(ce,this.options.unsentIdentifyKey,JSON.stringify(this._unsentIdentifys))}catch(e){}},Ae.prototype.setDomain=function(e){if(H(e,"domain","string"))try{this.cookieStorage.options({domain:e}),this.options.domain=this.cookieStorage.options().domain,je(this),Ue(this)}catch(e){W.error(e)}},Ae.prototype.setUserId=function(e){try{this.options.userId=null!=e&&""+e||null,Ue(this)}catch(e){W.error(e)}},Ae.prototype.setGroup=function(e,t){if(this._apiKeySet("setGroup()")&&H(e,"groupType","string")&&!$(e)){var n={};n[e]=t;var i=(new fe).set(e,t);this._logEvent(h,null,null,i.userPropertiesOperations,n,null,null,null)}},Ae.prototype.setOptOut=function(e){if(H(e,"enable","boolean"))try{this.options.optOut=e,Ue(this)}catch(e){W.error(e)}},Ae.prototype.setSessionId=function(e){if(H(e,"sessionId","number"))try{this._sessionId=e,Ue(this)}catch(e){W.error(e)}},Ae.prototype.resetSessionId=function(){this.setSessionId((new Date).getTime())},Ae.prototype.regenerateDeviceId=function(){this.setDeviceId(ke()+"R")},Ae.prototype.setDeviceId=function(e){if(H(e,"deviceId","string"))try{$(e)||(this.options.deviceId=""+e,Ue(this))}catch(e){W.error(e)}},Ae.prototype.setUserProperties=function(e){if(this._apiKeySet("setUserProperties()")&&H(e,"userProperties","object")){var t=Q(Z(e));if(0!==Object.keys(t).length){var n=new fe;for(var i in t)t.hasOwnProperty(i)&&n.set(i,t[i]);this.identify(n)}}},Ae.prototype.clearUserProperties=function(){if(this._apiKeySet("clearUserProperties()")){var e=new fe;e.clearAll(),this.identify(e)}};var Me=function(e,t){for(var n=0;nthis.options.sessionTimeout)&&(this._sessionId=c),this._lastEventTime=c,Ue(this),i=i||{},n=l({},n||{},l({},this._apiPropertiesTrackingOptions)),t=t||{},r=r||{},o=o||{};var d={device_id:this.options.deviceId,user_id:this.options.userId,timestamp:c,event_id:u,session_id:this._sessionId||-1,event_type:e,version_name:ze(this,"version_name")&&this.options.versionName||null,platform:ze(this,"platform")?this.options.platform:null,os_name:ze(this,"os_name")&&this._ua.browser.name||null,os_version:ze(this,"os_version")&&this._ua.browser.major||null,device_model:ze(this,"device_model")&&this._ua.os.name||null,language:ze(this,"language")?this.options.language:null,api_properties:n,event_properties:Q(Z(t)),user_properties:Q(Z(i)),uuid:ke(),library:{name:"amplitude-js",version:Te},sequence_number:p,groups:Q(X(r)),group_properties:Q(Z(o)),user_agent:this._userAgent};return e===h||e===g?(this._unsentIdentifys.push(d),this._limitEventsQueued(this._unsentIdentifys)):(this._unsentEvents.push(d),this._limitEventsQueued(this._unsentEvents)),this.options.saveEvents&&this.saveEvents(),this._sendEventsIfReady(a)||"function"!==C(a)||a(0,"No request sent",{reason:"No events to send or upload queued"}),u}catch(e){W.error(e)}else"function"===C(a)&&a(0,"No request sent",{reason:"Missing eventType"})};var ze=function(e,t){return!!e.options.trackingOptions[t]},Be=function(e){for(var t=["city","country","dma","ip_address","region"],n={},i=0;ithis.options.savedMaxCount&&e.splice(0,e.length-this.options.savedMaxCount)},Ae.prototype.logEvent=function(e,t,n){return this.logEventWithTimestamp(e,t,null,n)},Ae.prototype.logEventWithTimestamp=function(e,t,n,i){return this._apiKeySet("logEvent()")?H(e,"eventType","string")?$(e)?("function"===C(i)&&i(0,"No request sent",{reason:"Missing eventType"}),-1):this._logEvent(e,t,null,null,null,null,n,i):("function"===C(i)&&i(0,"No request sent",{reason:"Invalid type for eventType"}),-1):("function"===C(i)&&i(0,"No request sent",{reason:"API key not set"}),-1)},Ae.prototype.logEventWithGroups=function(e,t,n,i){return this._apiKeySet("logEventWithGroups()")?H(e,"eventType","string")?this._logEvent(e,t,null,null,n,null,null,i):("function"===C(i)&&i(0,"No request sent",{reason:"Invalid type for eventType"}),-1):("function"===C(i)&&i(0,"No request sent",{reason:"API key not set"}),-1)};var Ge=function(e){return!isNaN(parseFloat(e))&&isFinite(e)};Ae.prototype.logRevenueV2=function(e){if(this._apiKeySet("logRevenueV2()"))if("object"===C(e)&&e.hasOwnProperty("_q")&&(e=Me(new xe,e)),e instanceof xe){if(e&&e._isValidRevenue())return this.logEvent(o,e._toJSONObject())}else W.error("Invalid revenue input type. Expected Revenue object but saw "+C(e))},Ae.prototype.logRevenue=function(e,t,n){return this._apiKeySet("logRevenue()")&&Ge(e)&&(void 0===t||Ge(t))?this._logEvent(o,{},{productId:n,special:"revenue_amount",quantity:t||1,price:e},null,null,null,null,null):-1},Ae.prototype.removeEvents=function(e,t){Le(this,"_unsentEvents",e),Le(this,"_unsentIdentifys",t)};var Le=function(e,t,n){if(!(n<0)){for(var i=[],r=0;rn&&i.push(e[t][r]);e[t]=i}};Ae.prototype.sendEvents=function(n){if(this._apiKeySet("sendEvents()"))if(this.options.optOut)"function"===C(n)&&n(0,"No request sent",{reason:"optOut is set to true"});else if(0!==this._unsentCount())if(this._sending)"function"===C(n)&&n(0,"No request sent",{reason:"Request already in progress"});else{this._sending=!0;var e=(this.options.forceHttps?"https":"https:"===window.location.protocol?"https":"http")+"://"+this.options.apiEndpoint,i=Math.min(this._unsentCount(),this.options.uploadBatchSize),t=this._mergeEventsAndIdentifys(i),r=t.maxEventId,o=t.maxIdentifyId,s=JSON.stringify(t.eventsToSend),a=(new Date).getTime(),u={client:this.options.apiKey,e:s,v:c,upload_time:a,checksum:ve(c+this.options.apiKey+s+a)},p=this;new Oe(e,u).send(function(e,t){p._sending=!1;try{200===e&&"success"===t?(p.removeEvents(r,o),p.options.saveEvents&&p.saveEvents(),p._sendEventsIfReady(n)||"function"!==C(n)||n(e,t)):413===e?(1===p.options.uploadBatchSize&&p.removeEvents(r,o),p.options.uploadBatchSize=Math.ceil(i/2),p.sendEvents(n)):"function"===C(n)&&n(e,t)}catch(e){}})}else"function"===C(n)&&n(0,"No request sent",{reason:"No events to send"});else"function"===C(n)&&n(0,"No request sent",{reason:"API key not set"})},Ae.prototype._mergeEventsAndIdentifys=function(e){for(var t=[],n=0,i=-1,r=0,o=-1;t.length=this._unsentIdentifys.length,u=n>=this._unsentEvents.length;if(u&&a){W.error("Merging Events and Identifys, less events and identifys than expected");break}a?i=(s=this._unsentEvents[n++]).event_id:u?o=(s=this._unsentIdentifys[r++]).event_id:!("sequence_number"in this._unsentEvents[n])||this._unsentEvents[n].sequence_number