-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclevertap.min.js
1 lines (1 loc) · 36.6 KB
/
clevertap.min.js
1
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.clevertap=t()}(this,function(){"use strict";var e={APP_LAUNCHED:"App Launched",CHARGED_ID:"chargedId",ECOOKIE_PREFIX:"CT_E",GCOOKIE_NAME:"CT_G",VAPID_KEY:"CT_VKEY",VAPID_STATE_KEY:"CT_VKEY_STATE",KCOOKIE_NAME:"CT_K",PCOOKIE_PREFIX:"CT_P",SEQCOOKIE_PREFIX:"CT_SEQ",SCOOKIE_PREFIX:"CT_S",EV_COOKIE:"CT_EV",PR_COOKIE:"CT_PR",ARP_COOKIE:"CT_ARP",UNDEFINED:"undefined",PING_FREQ_IN_MILLIS:12e4,EVENT_TYPES:{EVENT:"event",PROFILE:"profile",PAGE:"page",PING:"ping",DATA:"data"},IDENTITY_TYPES:{IDENTITY:"Identity",EMAIL:"Email",FBID:"FBID",GPID:"GPID"},TOKEN_UPDATE_TS_KEY:"CT_TK_TS",KAIOS_NOTIFICATION_STATE:"CT_KOS_S",SW_UNREGISTER_FOR_VERSION:"CT_SW_VR",APP_VERSION_KEY:"CT_AP_VR"},t="This property has been ignored.",n={init:"Invalid account id",event:"Event structure not valid. Unable to process event",gender:"Gender value should be either M or F. "+t,employed:"Employed value should be either Y or N. "+t,married:"Married value should be either Y or N. "+t,education:"Education value should be either School, College or Graduate. "+t,age:"Age value should be a number. "+t,dob:"DOB value should be a Date Object",objArr:"Expecting Object array in profile",dateFormat:"setDate(number). number should be formatted as yyyymmdd",enumFormat:"setEnum(value). value should be a string or a number",phoneFormat:"Phone number should be formatted as +[country code][number]"},r="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},i=(function(){function e(e){this.value=e}function t(t){var n,r;function i(n,r){try{var s=t[n](r),a=s.value;a instanceof e?Promise.resolve(a.value).then(function(e){i("next",e)},function(e){i("throw",e)}):o(s.done?"return":"normal",s.value)}catch(e){o("throw",e)}}function o(e,t){switch(e){case"return":n.resolve({value:t,done:!0});break;case"throw":n.reject(t);break;default:n.resolve({value:t,done:!1})}(n=n.next)?i(n.key,n.arg):r=null}this._invoke=function(e,t){return new Promise(function(o,s){var a={key:e,arg:t,resolve:o,reject:s,next:null};r?r=r.next=a:(n=r=a,i(e,t))})},"function"!=typeof t.return&&(this.return=void 0)}"function"==typeof Symbol&&Symbol.asyncIterator&&(t.prototype[Symbol.asyncIterator]=function(){return this}),t.prototype.next=function(e){return this._invoke("next",e)},t.prototype.throw=function(e){return this._invoke("throw",e)},t.prototype.return=function(e){return this._invoke("return",e)}}(),function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}),o=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),s=[],a=function(){function e(){i(this,e)}return o(e,null,[{key:"pushError",value:function(e,t){s.push({c:e,d:t})}},{key:"popError",value:function(){return s.shift()}}]),e}();a.MESSAGES=n;new RegExp("^\\s+|\\.|:|\\$|'|\"|\\\\|\\s+$","g"),new RegExp("^\\s+|'|\"|\\\\|\\s+$","g"),new RegExp('"',"g"),new RegExp("'","g");var u,l=function(){try{return window.localStorage.setItem("wzrk_debug","12345678"),window.localStorage.removeItem("wzrk_debug"),"localStorage"in window&&null!==window.localStorage}catch(e){return!1}},c=function(t){if(!t)return null;var n=null;if(l()&&(n=localStorage[t]||null),(void 0===n?"undefined":r(n))!==e.UNDEFINED&&null!==n)try{n=JSON.parse(n)}catch(e){}return n},g=c,f=function(t,n){if(t&&(void 0===n?"undefined":r(n))!==e.UNDEFINED&&null!==n)try{l()&&(localStorage[t]=JSON.stringify(n))}catch(e){}},v=function(e){e&&l()&&delete localStorage[e]},d=function(){function t(){i(this,t)}return o(t,null,[{key:"save",value:function(e,t){e&&t&&f(e,t)}},{key:"read",value:function(e){return e?g(e):null}},{key:"remove",value:function(e){e&&v(e)}},{key:"getVAPIDKey",value:function(){var t=R.getAccountId();return t?e.VAPID_KEY+"_"+t:null}},{key:"getTokenUpdateTsKey",value:function(){var t=R.getAccountId();return t?e.TOKEN_UPDATE_TS_KEY+"_"+t:null}},{key:"getLastUnregisterForVersionKey",value:function(){var t=R.getAccountId();return t?e.SW_UNREGISTER_FOR_VERSION+"_"+t:null}},{key:"getKaiosNotificationStateKey",value:function(){var t=R.getAccountId();return t?e.KAIOS_NOTIFICATION_STATE+"_"+t:null}},{key:"getAppVersionKey",value:function(){var t=R.getAccountId();return t?e.APP_VERSION_KEY+"_"+t:null}},{key:"getGUIDKey",value:function(){var t=R.getAccountId();return t?e.GCOOKIE_NAME+"_"+t:null}},{key:"getSessionKey",value:function(){var t=R.getAccountId(),n=U.getGUID();return t&&n?e.SCOOKIE_PREFIX+"_"+t+"_"+n:null}},{key:"getUserEventsKey",value:function(){var t=R.getAccountId(),n=U.getGUID();return t&&n?e.EV_COOKIE+"_"+t+"_"+n:null}},{key:"getUserProfileKey",value:function(){var t=R.getAccountId(),n=U.getGUID();return t&&n?e.PR_COOKIE+"_"+t+"_"+n:null}},{key:"getSavedEventsKey",value:function(){var t=R.getAccountId(),n=U.getGUID();return t&&n?e.ECOOKIE_PREFIX+"_"+t+"_"+n:null}},{key:"getSavedProfilesKey",value:function(){var t=R.getAccountId(),n=U.getGUID();return t&&n?e.PCOOKIE_PREFIX+"_"+t+"_"+n:null}},{key:"getSavedSequenceNumberKey",value:function(){var t=R.getAccountId();return t?e.SEQCOOKIE_PREFIX+"_"+t:null}},{key:"getARPKey",value:function(){var t=R.getAccountId(),n=U.getGUID();return t&&n?e.ARP_COOKIE+"_"+t+"_"+n:null}},{key:"getMetaKey",value:function(){var t=R.getAccountId(),n=U.getGUID();return t&&n?e.META_COOKIE+"_"+t+"_"+n:null}},{key:"getIdentitiesMapKey",value:function(){var t=R.getAccountId();return t?e.KCOOKIE_NAME+"_"+t:null}},{key:"getChargedIdKey",value:function(){var t=R.getAccountId();return t?e.CHARGED_ID+"_"+t:null}},{key:"getVAPIDStateKey",value:function(){var t=R.getAccountId();return t?e.VAPID_STATE_KEY+"_"+t:null}}]),t}(),h={DISABLE:0,ERROR:1,INFO:2,DEBUG:3},p=h.INFO,y={error:function(e){p>=h.ERROR&&E("error",e)},info:function(e){p>=h.INFO&&E("log",e)},debug:function(e){p>=h.DEBUG&&E("debug",e)}},E=function(e,t){if(window.console)try{var n=(new Date).getTime();console[e]("CleverTap ["+n+"]: "+t)}catch(e){}},_=function(e){var t=/^(\d{4})(\d{2})(\d{2})$/.exec(e);if(null===t)return!1;var n=t[3],r=t[2]-1,i=t[1],o=new Date(i,r,n);return o.getDate()===n&&o.getMonth()===r&&o.getFullYear()===i},S=function(e){for(var t in e)if(e.hasOwnProperty(t))return!1;return!0},I=function(e){return"string"==typeof e||e instanceof String},m=function(e){return/^-?[\d.]+(?:e-?\d+)?$/.test(e)&&"number"==typeof e},A=new RegExp("^\\s+|\\.|:|\\$|'|\"|\\\\|\\s+$","g"),P=new RegExp("^\\s+|'|\"|\\\\|\\s+$","g"),T=new RegExp('"',"g"),D=new RegExp("'","g"),b=function(e,t){return e.replace(t,"")},k=function(){try{return window.localStorage.setItem("wzrk_debug","12345678"),window.localStorage.removeItem("wzrk_debug"),"localStorage"in window&&null!==window.localStorage}catch(e){return!1}},N=function(t){if(!t)return null;var n=null;if(k()&&(n=localStorage[t]||null),(void 0===n?"undefined":r(n))!==e.UNDEFINED&&null!==n)try{n=JSON.parse(n)}catch(e){}return n},O={logLevels:h,setLogLevel:function(e){p=e},getLogLevel:function(){return p},log:y,setDate:function(e){if(_(e))return"$D_"+e;y.error(a.MESSAGES.dateFormat)},isDateObject:function(e){return"object"===(void 0===e?"undefined":r(e))&&e instanceof Date},convertToWZRKDate:function(e){return"$D_"+Math.round(e.getTime()/1e3)},isDateValid:_,isArray:function(e){return"object"===(void 0===e?"undefined":r(e))&&e instanceof Array},isObject:function(e){return"[object Object]"===Object.prototype.toString.call(e)},isObjectEmpty:S,isEmptyString:function(e){return!e||0===e.length},isString:I,isConvertibleToNumber:function(e){return!isNaN(parseFloat(e))&&isFinite(e)},isNumber:m,arrayContains:function(e,t){for(var n=e.length;n--;)if(e[n]===t)return!0;return!1},getDomain:function(e){if(""===e)return"";var t=document.createElement("a");return t.href=e,t.hostname},removeUnsupportedChars:function e(t,n){if("object"!==(void 0===t?"undefined":r(t))){var i;return I(t)?(i=b(t,P)).length>1024&&(i=i.substring(0,1024),n&&n(521,i+"... length exceeded 1024 chars. Trimmed.")):i=t,i}for(var o in t)if(t.hasOwnProperty(o)){var s=e(t[o]),a=I(o)?b(o,A):o;I(o)?(a=b(o,A)).length>1024&&(a=a.substring(0,1024),n&&n(520,a+"... length exceeded 1024 chars. Trimmed.")):a=o,delete t[o],t[a]=s}return t},sanitize:b,isLocalStorageSupported:k,getToday:function(){var e=new Date;return e.getFullYear()+""+e.getMonth()+e.getDay()},getNow:function(){return Math.floor((new Date).getTime()/1e3)},unsupportedKeyCharRegex:A,unsupportedValueCharRegex:P,doubleQuoteRegex:T,singleQuoteRegex:D,readFromStorage:N,saveToStorage:function(t,n){if(t&&(void 0===n?"undefined":r(n))!==e.UNDEFINED&&null!==n)try{k()&&(localStorage[t]=JSON.stringify(n))}catch(e){}},removeFromStorage:function(e){e&&k()&&delete localStorage[e]},setEnum:function(e){if(I(e)||m(e))return"$E_"+e;y.error(a.MESSAGES.enumFormat)},reportError:function(e,t){a.pushError(e,t)},isAnonymousDevice:function(){var e=d.getIdentitiesMapKey(),t=N(e)||{};return S(t)}},w=null,U=function(){function e(){i(this,e)}return o(e,null,[{key:"setGUID",value:function(e){w=e;var t=d.getGUIDKey();null===e?d.remove(t):d.save(t,e)}},{key:"getVAPIDState",value:function(){var e=d.getVAPIDStateKey();return d.read(e)}},{key:"setVAPIDState",value:function(e){var t=d.getVAPIDStateKey();null===e?d.remove(t):d.save(t,e)}},{key:"getGUID",value:function(){if(!w){var t=d.getGUIDKey();w=d.read(t)}return w||e.generateGUID(),w}},{key:"generateGUID",value:function(){var t="fx"+function e(t){return t?(t^window.crypto.getRandomValues(new Uint8Array(1))[0]%16>>t/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,e)}().replace(/[-]/g,"");O.log.debug("Generating Device ID: "+t),e.setGUID(t)}},{key:"setVAPID",value:function(e){var t=d.getVAPIDKey();null===e?d.remove(t):d.save(t,e)}},{key:"getVAPID",value:function(){var e=d.getVAPIDKey();return d.read(e)}},{key:"setLastTokenUpdateTs",value:function(e){var t=d.getTokenUpdateTsKey();d.save(t,e)}},{key:"getLastTokenUpdateTs",value:function(){var e=d.getTokenUpdateTsKey(),t=d.read(e);return O.log.debug("last updated ts : "+t),t}},{key:"setLastSWUnregistrationForVersion",value:function(e){var t=d.getLastUnregisterForVersionKey();d.save(t,e)}},{key:"getLastSWUnregistrationForVersion",value:function(){var t=d.getLastUnregisterForVersionKey(),n=d.read(t);return null===n&&(O.log.debug("no last unregistration has been set, returning current version"),n=e.getAppVersion()),O.log.debug("last unregistered on version : = "+n),n}},{key:"getKaiOsNotificationState",value:function(){var e=d.getKaiosNotificationStateKey(),t=d.read(e);return t||!1}},{key:"setKaiOsNotificationState",value:function(e){var t=d.getKaiosNotificationStateKey();d.save(t,e)}},{key:"getAppVersion",value:function(){var e=d.getAppVersionKey(),t=d.read(e);return O.log.debug("app version : = "+t),t}},{key:"setAppVersion",value:function(e){var t=d.getAppVersionKey();d.save(t,e)}}]),e}(),C=null,K=null,V=null,R=function(){function e(){i(this,e)}return o(e,null,[{key:"setAccountId",value:function(e){C=e}},{key:"getAccountId",value:function(){return C}},{key:"setRegion",value:function(e){K=e}},{key:"getRegion",value:function(){return K}},{key:"setAppVersion",value:function(e){V=e,U.setAppVersion(e)}},{key:"getAppVersion",value:function(){return V}}]),e}(),L=0,F=0,G=0,M=0,j=function(){return d.getSessionKey()},x=function(){var e=j(),t=d.read(e)||{};return O.isObject(t)||(t={}),t},q=function(e){var t=j();d.save(t,e)},Y=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};i(this,e),this.options=t,L=O.getNow();var n=x();F=n.ps||0,u=F<=0,M=n.p||1,G=(n.sc||0)+1,n.ps=L,n.p=M,n.sc=G,q(n)}return o(e,[{key:"isFirstSession",value:function(){return u}},{key:"getPageCount",value:function(){return M}},{key:"getTimeElapsed",value:function(){var t=e.getSessionId();return t<=0?0:Math.floor(O.getNow()-t)}},{key:"getTotalVisits",value:function(){return G}},{key:"getLastVisit",value:function(){return F>0?new Date(1e3*F):0}}],[{key:"getSessionId",value:function(){return L}}]),e}(),z={_f:String.fromCharCode,getKeyStr:function(){var e="",t=0;for(t=0;t<=25;t++)e+=String.fromCharCode(t+65);for(t=0;t<=25;t++)e+=String.fromCharCode(t+97);for(t=0;t<10;t++)e+=t;return e+"+/="},convertToFormattedHex:function(e){var t,n,r,i="";if(!O.isArray(e))return!1;for(n=e.length,t=0;t<n;++t)e[t]<0&&(e[t]=e[t]+256),void 0===e[t]&&(e[t]=0),1===(r=e[t].toString(16)).length&&(r="0"+r),i+=r;return i.trim()},convertStringToHex:function(e){for(var t=[],n=0;n<e.length;n++){var r=e.charCodeAt(n);t.push(255&r),t.push(r>>8&255)}return z.convertToFormattedHex(t)},compressToBase64:function(e){if(null===e)return"";var t,n,r,i,o,s,a,u="",l=0;for(e=z.compress(e);l<2*e.length;)l%2==0?(t=e.charCodeAt(l/2)>>8,n=255&e.charCodeAt(l/2),r=l/2+1<e.length?e.charCodeAt(l/2+1)>>8:NaN):(t=255&e.charCodeAt((l-1)/2),(l+1)/2<e.length?(n=e.charCodeAt((l+1)/2)>>8,r=255&e.charCodeAt((l+1)/2)):n=r=NaN),l+=3,i=t>>2,o=(3&t)<<4|n>>4,s=(15&n)<<2|r>>6,a=63&r,isNaN(n)?s=a=64:isNaN(r)&&(a=64),u=u+z._keyStr.charAt(i)+z._keyStr.charAt(o)+z._keyStr.charAt(s)+z._keyStr.charAt(a);return u},compress:function(e){if(null===e)return"";var t,n,r,i={},o={},s="",a="",u="",l=2,c=3,g=2,f="",v=0,d=0,h=z._f;for(r=0;r<e.length;r+=1)if(s=e.charAt(r),Object.prototype.hasOwnProperty.call(i,s)||(i[s]=c++,o[s]=!0),a=u+s,Object.prototype.hasOwnProperty.call(i,a))u=a;else{if(Object.prototype.hasOwnProperty.call(o,u)){if(u.charCodeAt(0)<256){for(t=0;t<g;t++)v<<=1,15===d?(d=0,f+=h(v),v=0):d++;for(n=u.charCodeAt(0),t=0;t<8;t++)v=v<<1|1&n,15===d?(d=0,f+=h(v),v=0):d++,n>>=1}else{for(n=1,t=0;t<g;t++)v=v<<1|n,15===d?(d=0,f+=h(v),v=0):d++,n=0;for(n=u.charCodeAt(0),t=0;t<16;t++)v=v<<1|1&n,15===d?(d=0,f+=h(v),v=0):d++,n>>=1}0===--l&&(l=Math.pow(2,g),g++),delete o[u]}else for(n=i[u],t=0;t<g;t++)v=v<<1|1&n,15===d?(d=0,f+=h(v),v=0):d++,n>>=1;0===--l&&(l=Math.pow(2,g),g++),i[a]=c++,u=String(s)}if(""!==u){if(Object.prototype.hasOwnProperty.call(o,u)){if(u.charCodeAt(0)<256){for(t=0;t<g;t++)v<<=1,15===d?(d=0,f+=h(v),v=0):d++;for(n=u.charCodeAt(0),t=0;t<8;t++)v=v<<1|1&n,15===d?(d=0,f+=h(v),v=0):d++,n>>=1}else{for(n=1,t=0;t<g;t++)v=v<<1|n,15===d?(d=0,f+=h(v),v=0):d++,n=0;for(n=u.charCodeAt(0),t=0;t<16;t++)v=v<<1|1&n,15===d?(d=0,f+=h(v),v=0):d++,n>>=1}0===--l&&(l=Math.pow(2,g),g++),delete o[u]}else for(n=i[u],t=0;t<g;t++)v=v<<1|1&n,15===d?(d=0,f+=h(v),v=0):d++,n>>=1;0===--l&&(l=Math.pow(2,g),g++)}for(n=2,t=0;t<g;t++)v=v<<1|1&n,15===d?(d=0,f+=h(v),v=0):d++,n>>=1;for(;;){if(v<<=1,15===d){f+=h(v);break}d++}return f}};z._keyStr=z.getKeyStr();var B,W,J={compressToBase64:z.compressToBase64},$=function(e){return O.log.debug("dobj:"+e),J.compressToBase64(e)},H=function(t){var n={};if(r(t.displayName)!==e.UNDEFINED&&(n.Name=t.displayName),r(t.id)!==e.UNDEFINED&&(n.GPID=t.id+""),r(t.gender)!==e.UNDEFINED&&("male"===t.gender?n.Gender="M":"female"===t.gender?n.Gender="F":"other"===t.gender&&(n.Gender="O")),r(t.image)!==e.UNDEFINED&&(t.image.isDefault||(n.Photo=t.image.url.split("?sz")[0])),r(t.emails)!==e.UNDEFINED)for(var i=0;i<t.emails.length;i++){var o=t.emails[i];"account"===o.type&&(n.Email=o.value)}if(t.organizations){n.Employed="N";for(var s=0;s<t.organizations.length;s++){if("work"===t.organizations[s].type){n.Employed="Y";break}}}if(t.birthday){var a=t.birthday.split("-");n.DOB=O.setDate(a[0]+a[1]+a[2])}return t.relationshipStatus&&(n.Married="N","married"===t.relationshipStatus&&(n.Married="Y")),O.log.debug("gplus usr profile "+JSON.stringify(n)),n},Q=function(e){var t={};t.Name=e.name,e.id&&(t.FBID=e.id+""),"male"===e.gender?t.Gender="M":"female"===e.gender?t.Gender="F":t.Gender="O";e.relationship_status&&(t.Married="N","Married"===e.relationship_status&&(t.Married="Y"));var n=function(e){if(e){for(var t="",n="",r=0;r<e.length;r++){var i=e[r];if(i.type){var o=i.type;if("Graduate School"===o)return"Graduate";"College"===o?t="1":"High School"===o&&(n="1")}}if("1"===t)return"College";if("1"===n)return"School"}}(e.education);if(n&&(t.Education=n),t.Employed=e.work?"Y":"N",e.email&&(t.Email=e.email),e.birthday){var r=e.birthday.split("/");t.DOB=O.setDate(r[2]+r[0]+r[1])}return t},X=void 0,Z=function(e){if(O.isObject(e)){for(var t in e)if(e.hasOwnProperty(t)){if(O.isObject(e[t])||O.isArray(e[t]))return!1;O.isDateObject(e[t])&&(e[t]=O.convertToWZRKDate(e[t]))}return!0}return!1},ee=Z,te=function(t){var n=!0;if(O.isObject(t))for(var i in t)if(t.hasOwnProperty(i)){var o=t[i];if((void 0===o?"undefined":r(o))===e.UNDEFINED){delete t[i];continue}"Gender"!==i||o.match(/^M$|^F$/)||(n=!1,O.log.error(a.MESSAGES.gender)),"Employed"!==i||o.match(/^Y$|^N$/)||(n=!1,O.log.error(a.MESSAGES.employed)),"Married"!==i||o.match(/^Y$|^N$/)||(n=!1,O.log.error(a.MESSAGES.married)),"Education"!==i||o.match(/^School$|^College$|^Graduate$/)||(n=!1,O.log.error(a.MESSAGES.education)),"Age"===i&&(void 0===o?"undefined":r(o))!==e.UNDEFINED&&(O.isConvertibleToNumber(o)?t.Age=+o:(n=!1,O.log.error(a.MESSAGES.age))),"DOB"===i?(/^\$D_/.test(o)&&11===(o+"").length||O.isDateObject(o)||(n=!1,O.log.error(a.MESSAGES.dob)),O.isDateObject(o)&&(t[i]=O.convertToWZRKDate(o))):O.isDateObject(o)&&(t[i]=O.convertToWZRKDate(o)),"Phone"!==i||O.isObjectEmpty(o)||(o.length>8&&"+"===o.charAt(0)?(o=o.substring(1,o.length),O.isConvertibleToNumber(o)?t.Phone=+o:(n=!1,O.log.error(a.MESSAGES.phoneFormat+". Removed."))):(n=!1,O.log.error(a.MESSAGES.phoneFormat+". Removed."))),n||delete t[i]}return n},ne=function(t,n){if(O.isObject(t)){for(var i in t)if(t.hasOwnProperty(i))if("Items"===i){if(!O.isArray(t[i]))return!1;t[i].length>16&&n&&n(522,"Charged Items exceed 16 limit. Actual count: "+t[i].length+". Additional items will be dropped.");for(var o in t[i])if(t[i].hasOwnProperty(o)&&(!O.isObject(t[i][o])||!Z(t[i][o])))return!1}else{if(O.isObject(t[i])||O.isArray(t[i]))return!1;O.isDateObject(t[i])&&(t[i]=O.convertToWZRKDate(t[i]))}if(r(t[e.CHARGED_ID])!==e.UNDEFINED){var s=t[e.CHARGED_ID],a=d.getChargedIdKey();if((void 0===X?"undefined":r(X))===e.UNDEFINED&&(X=d.read(a)),(void 0===X?"undefined":r(X))!==e.UNDEFINED&&X===s)return O.log.error("Duplicate charged Id - Dropped"+t),!1;X=s,d.save(a,s)}return!0}return!1},re=[],ie=function(){function t(e,n){if(i(this,t),this.api=e,re.push=function(e){var n=t.generateProfileObj(e),r=t.generateProfileEvent(n);this.api.processEvent(r)}.bind(this),n&&n.length>0)for(var r=0;r<n.length;r++)this.push(n[r])}return o(t,[{key:"push",value:function(){return re.push(Array.prototype.slice.call(arguments)),!0}},{key:"getAttribute",value:function(e){return this.api.getProfileAttribute(e)}}],[{key:"getCachedGUIDForIdentity",value:function(e,t){if(!e||!t)return null;var n=d.getIdentitiesMapKey();if(!n)return null;return(d.read(n)||{})[e+"_"+t]||null}},{key:"extractIdentifiersFromProfileObj",value:function(t){if(!t||O.isObjectEmpty(t))return null;var n=[];return t.Email&&n.push({type:e.IDENTITY_TYPES.EMAIL,id:t.Email}),t.GPID&&n.push({type:e.IDENTITY_TYPES.GPID,id:t.GPID}),t.FBID&&n.push({type:e.IDENTITY_TYPES.FBID,id:t.FBID}),t.Identity&&n.push({type:e.IDENTITY_TYPES.IDENTITY,id:t.Identity}),n}},{key:"generateProfileObj",value:function(e){var t;if(!O.isArray(e)||e.length<=0)return null;for(var n=0;n<e.length;n++){var r=e[n];if(t=null,r.Site){if(t=r.Site,O.isObjectEmpty(t)||!te(t))return null}else if(r.Facebook){var i=r.Facebook;O.isObjectEmpty(i)||i.error||(t=Q(i))}else if(r["Google Plus"]){var o=r["Google Plus"];O.isObjectEmpty(o)||o.error||(t=H(o))}if(t&&!O.isObjectEmpty(t)){t.tz||(t.tz=(new Date).toString().match(/([A-Z]+[\+-][0-9]+)/)[1]);break}}return t}},{key:"generateProfileEvent",value:function(n){if(!n||O.isObjectEmpty(n))return null;var r=t.extractIdentifiersFromProfileObj(n);return r.length>0&&function(e){var t=d.getIdentitiesMapKey(),n=U.getGUID();if(n&&e&&!(e.length<=0)){for(var r=d.read(t)||{},i=0;i<e.length;i++){var o=e[i];o.type&&o.id&&(r[o.type+"_"+o.id]=n)}d.save(t,r)}}(r),{ep:O.getNow(),type:e.EVENT_TYPES.PROFILE,profile:n}}}]),t}(),oe=function(){function e(t,n){i(this,e),this.url=t,this.data=n||[]}return o(e,[{key:"send",value:function(e){var t=new XMLHttpRequest({mozSystem:!0});t.open("post",this.url,!0),t.responseType="json",t.addEventListener("error",function(){e&&e(t.status,t.error)}),t.addEventListener("load",function(){e&&e(t.status,t.response)}),t.setRequestHeader("Accept","application/json"),t.setRequestHeader("Content-Type","application/json"),t.send(JSON.stringify(this.data)),O.log.debug("req snt -> url: "+this.url)}}]),e}(),se=function(e){var t=d.read(e);return O.isArray(t)?t:[]},ae=function(e){var t=d.read(e)||0;return parseInt(t,10)},ue=function(){return d.getSavedEventsKey()},le=function(){return d.getSavedProfilesKey()},ce=function(){return d.getSavedSequenceNumberKey()},ge=function(){return d.getARPKey()},fe=function(){function t(e){i(this,t),this.options=e,this.options.eventUploadRetryInterval=this.options.eventUploadInterval,this._uploading=!1,this._requestScheduled=!1,this._unsentEvents=se(ue()),this._unsentProfiles=se(le()),this._sequenceNumber=ae(ce()),this._scheduleEvents()}return o(t,[{key:"clearEvents",value:function(e){var t=this;this._sendEvents(function(){t._unsentEvents=[],t._unsentProfiles=[],t._saveEvents(),e()})}},{key:"queueEvent",value:function(t){if(t){t._seq=this._nextSequenceNumber();O.log.debug("Queuing event "+JSON.stringify(t)),t.type===e.EVENT_TYPES.PROFILE?this._unsentProfiles.push(t):this._unsentEvents.push(t),this._saveEvents(),this._scheduleEvents()}}},{key:"_getEndPoint",value:function(){var e=this.options.domain;return R.getRegion()&&(e=R.getRegion()+"."+this.options.domain),this.options.protocol+"//"+e+"/a2?t=77"}},{key:"_unsentCount",value:function(){return this._unsentEvents.length+this._unsentProfiles.length}},{key:"_scheduleRetry",value:function(){this.options.eventUploadRetryInterval=2*this.options.eventUploadRetryInterval,this._scheduleEvents(null,!0)}},{key:"_scheduleEvents",value:function(e,t){if(0===this._unsentCount())return O.log.debug("No events in the queue, not scheduling an event update"),!1;if(this._unsentCount()>=this.options.eventUploadThreshold)return this._sendEvents(e),!0;if(this._requestScheduled)return O.log.debug("Event upload already scheduled"),!1;this._requestScheduled=!0;var n=t?this.options.eventUploadRetryInterval:this.options.eventUploadInterval;return setTimeout(function(){this._requestScheduled=!1,this._sendEvents(e)}.bind(this),n),O.log.debug("Scheduling an event upload in "+n/1e3+" seconds"),!1}},{key:"_sendEvents",value:function(t){var n=!1,r="";if(this._uploading&&(n=!0,r="Already Uploading"),this._unsentCount()<=0&&(n=!0,r="No events in the queue"),n){var i="Skipping events upload: "+r;return O.log.debug(i),void("function"==typeof t&&t(0,i))}this._uploading=!0;var o=this._getEndPoint();o=this._addToURL(o,"sn",O.getNow()),o=this._addARPToRequest(o),o=this._addToURL(o,"r",(new Date).getTime());var s={id:R.getAccountId(),"SDK Version":10101,s:""+Y.getSessionId()};R.getAppVersion()&&(s.Version=""+R.getAppVersion());var a=U.getGUID();a&&(o=this._addToURL(o,"gc",a),s.g=a),o=this._addToURL(o,"d",$(JSON.stringify(s)));var u=Math.min(this._unsentCount(),this.options.uploadBatchSize),l=this._mergeEventQueues(u),c=l.maxEventId,g=l.maxProfilesId,f=l.eventsToSend;O.log.debug("Sending events: "+JSON.stringify(f));var v=this;new oe(o,f).send(function(n,r){v._uploading=!1,r=r||{},O.log.debug("handling response with status: "+n+" and data: "+JSON.stringify(r));try{if(200===n){r.g&&U.setGUID(r.g),r.KVAPID&&(O.log.debug("kaios vapid recieved: "+r.KVAPID),U.setVAPID(r.KVAPID));for(var i=0;i<f.length;i++){f[i].evtName===e.APP_LAUNCHED&&U.setVAPIDState(!0)}r.hasOwnProperty("kaiosPush")&&(O.log.debug("kaios notification status: "+r.kaiosPush),U.setKaiOsNotificationState(r.kaiosPush)),v._removeEvents(c,g),v._saveEvents(),v._updateARP(r),v._scheduleEvents(t)||"function"!=typeof t||t(n,r)}else 413===n?(O.log.error("event upload request too large"),1===v.options.uploadBatchSize&&v._removeEvents(c,g),v.options.uploadBatchSize=Math.ceil(u/2),v._sendEvents(t)):(O.log.error("Events upload failed with status "+n+". Will retry."),v._scheduleRetry(),"function"==typeof t&&t(n,r))}catch(e){v._uploading=!1,O.log.error("Events upload failed: "+e.message+". Will retry."),v._scheduleRetry()}})}},{key:"_unregisterTokenApiCall",value:function(e,t){var n=this._getEndPoint();n=this._addToURL(n,"sn",O.getNow()),n=this._addARPToRequest(n),n=this._addToURL(n,"r",(new Date).getTime());var r={id:R.getAccountId(),"SDK Version":10101,s:""+Y.getSessionId()};R.getAppVersion()&&(r.Version=""+R.getAppVersion());var i=e.g;i&&(n=this._addToURL(n,"gc",i),r.g=i),O.log.debug("kaios vapid req with request url : "+n+" and meta: "+r),n=this._addToURL(n,"d",$(JSON.stringify(r))),O.log.debug("Sending kaios-Token unregister request: "+JSON.stringify(e));var o=[];o.push(e),new oe(n,o).send(function(e,n){n=n||{},O.log.debug("kaios req handling response with status: "+e+" and data: "+JSON.stringify(n));try{200===e?"function"==typeof t&&t():O.log.error("kaios vapid request failed with status "+e+".")}catch(e){O.log.error("kaios vapid request failed: "+e.message+".")}})}},{key:"_saveEvents",value:function(){d.save(ue(),this._unsentEvents),d.save(le(),this._unsentProfiles)}},{key:"_trimEventsQueued",value:function(e){e.length>this.options.maxSavedEventCount&&e.splice(0,e.length-this.options.maxSavedEventCount)}},{key:"_removeEvents",value:function(e,t){this._removeEventsFromQueue("_unsentEvents",e),this._removeEventsFromQueue("_unsentProfiles",t)}},{key:"_removeEventsFromQueue",value:function(e,t){if(!(t<0)){for(var n=[],r=0;r<this[e].length;r++)this[e][r]._seq>t&&n.push(this[e][r]);this[e]=n}}},{key:"_nextSequenceNumber",value:function(){return this._sequenceNumber++,d.save(ce(),this._sequenceNumber),this._sequenceNumber}},{key:"_mergeEventQueues",value:function(e){for(var t=[],n=0,r=-1,i=0,o=-1;t.length<e;){var s,a=i>=this._unsentProfiles.length,u=n>=this._unsentEvents.length;if(u&&a){O.log.error("Problem merging event queues, fewer events than expected");break}a?r=(s=this._unsentEvents[n++])._seq:u?o=(s=this._unsentProfiles[i++])._seq:this._unsentEvents[n]._seq<this._unsentProfiles[i]._seq?r=(s=this._unsentEvents[n++])._seq:o=(s=this._unsentProfiles[i++])._seq,t.push(s)}return{eventsToSend:t,maxEventId:r,maxProfilesId:o}}},{key:"_updateARP",value:function(){var e=(arguments.length>0&&void 0!==arguments[0]?arguments[0]:{}).arp;if(e){var t=ge();try{var n=d.read(t)||{};for(var r in e)e.hasOwnProperty(r)&&(-1===e[r]?delete n[r]:n[r]=e[r]);d.save(t,n)}catch(e){O.log.error("Unable to parse ARP JSON: "+e)}}}},{key:"_addARPToRequest",value:function(e){var t=ge();if(!t)return e;var n=d.read(t);return n?this._addToURL(e,"arp",$(JSON.stringify(n))):e}},{key:"_addToURL",value:function(e,t,n){return e+"&"+t+"="+encodeURIComponent(n)}}]),t}(),ve=function(e,t){var n={type:e,ep:O.getNow()};t&&t(n)},de=function(){function t(e){i(this,t),this.options=e,this.queueManager=new fe(Object.assign({},this.options)),this._newSession()}return o(t,[{key:"getCleverTapID",value:function(){return U.getGUID()}},{key:"getTimeElapsed",value:function(){return this._isPersonalizationActive()?this.session.getTimeElapsed():0}},{key:"getPageCount",value:function(){return this._isPersonalizationActive()?this.session.getPageCount():0}},{key:"getTotalVisits",value:function(){return this._isPersonalizationActive()?this.session.getTotalVisits():0}},{key:"getLastVisit",value:function(){return this._isPersonalizationActive()?this.session.getLastVisit():0}},{key:"getEventDetails",value:function(e){if(this._isPersonalizationActive()){var t=this._getEventsMap()[e];if(!t)return null;var n={};return n.firstTime=new Date(1e3*t[1]),n.lastTime=new Date(1e3*t[2]),n.count=t[0],n}}},{key:"getProfileAttribute",value:function(e){if(this._isPersonalizationActive())return this._getProfilesMap()[e]}},{key:"processEvent",value:function(t){O.isObject(t)&&(t.type===e.EVENT_TYPES.EVENT&&this._addToLocalEventMap(t.evtName),t.type===e.EVENT_TYPES.PROFILE&&this._addToLocalProfileMap(t.profile),t=this._addSystemDataToObject(t,void 0),this.queueManager.queueEvent(t))}},{key:"onUserLogin",value:function(e){var t=this,n=ie.generateProfileObj(e),r=U.getGUID();if(r&&n){for(var i,o=ie.extractIdentifiersFromProfileObj(n)||[],s=o.length>0,a=0;a<o.length;a++){var u=o[a];if(u.type&&u.id&&(i=ie.getCachedGUIDForIdentity(u.type,u.id)))break}return!s||O.isAnonymousDevice()?(O.log.debug("onUserLogin: either don't have identifier or device is anonymous, associating profile "+JSON.stringify(n)+" with current user profile"),void this._processProfileEvent(n)):i&&i===r?(O.log.debug("onUserLogin: profile "+JSON.stringify(n)+" maps to current device id "+r+", using current user profile"),void this._processProfileEvent(n)):void(this.processingUserLogin?O.log.debug("Already processing onUserLogin for "+this.processingUserLoginKey+" , will not process for profile: "+JSON.stringify(n)):(this.processingUserLogin=!0,this.processingUserLoginKey=JSON.stringify(n),O.log.debug("Processing onUserLogin for "+this.processingUserLoginKey),this.unregisterToken(r),this.queueManager.clearEvents(function(){i?U.setGUID(i):U.generateGUID(),t._clearDataMaps(),t._newSession(),t._processProfileEvent(n),t.processingUserLogin=!1,t.processingUserLoginKey=null})))}}},{key:"unregisterToken",value:function(t){var n={};n.type=e.EVENT_TYPES.DATA,n.g=t,n.action="unregister",this.queueManager._unregisterTokenApiCall(n)}},{key:"_newSession",value:function(){this.session=new Y(Object.assign({},this.options)),this._generateLaunchEvents()}},{key:"_generateLaunchEvents",value:function(){var t=this;n=function(e){t.processEvent(e)},ve(e.EVENT_TYPES.EVENT,function(t){t.evtName=e.APP_LAUNCHED,t.evtData={"SDK Version":10101},R.getAppVersion()&&(t.evtData.Version=""+R.getAppVersion()),n&&n(t)});var n;this.options.sendPages&&ve(e.EVENT_TYPES.PAGE,function(e){t.processEvent(e)})}},{key:"registerSW",value:function(e,t){var n=this,r=null;O.log.debug("---------\x3eregisterSW--\x3e",navigator),"serviceWorker"in navigator&&"PushManager"in window?(O.log.debug("Service Worker and Push is supported"),navigator.serviceWorker.register(e).then(function(e){r=e,console.log("Subscription data: ",e),console.log("Registration succeeded."),t?r.unregister().then(function(e){O.log.debug("Service worker unregistered attempt, success: "+e)}):n.triggerPushSubscription(r)}).catch(function(e){console.error("Service Worker Error",e)})):console.warn("Push messaging is not supported")}},{key:"triggerPushSubscription",value:function(e){var t=this,n=U.getVAPID(),r={};console.log("Base 64 VAPID Key "+this.urlBase64ToUint8Array(n)),e.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:this.urlBase64ToUint8Array(n)}).then(function(e){console.log("pushManager Subscription data: ",e.toJSON()),console.log("applicationserverykey",e.options.applicationServerKey),(r=JSON.parse(JSON.stringify(e))).endpoint=r.endpoint.split("/").pop(),r.browser="Kaios",t._startuploadPushToken(r);var n=(new Date).getTime();U.setLastTokenUpdateTs(n)}).catch(function(e){console.error("Subcription error ",e)})}},{key:"urlBase64ToUint8Array",value:function(e){for(var t=(e+"=".repeat((4-e.length%4)%4)).replace(/-/g,"+").replace(/_/g,"/"),n=window.atob(t),r=new Uint8Array(n.length),i=0;i<n.length;++i)r[i]=n.charCodeAt(i);return r}},{key:"_startuploadPushToken",value:function(t){t.type=e.EVENT_TYPES.DATA,this.processEvent(t)}},{key:"_getEventsMap",value:function(){if(!B){var e=d.getUserEventsKey();B=d.read(e)||{}}return B}},{key:"_getProfilesMap",value:function(){if(!W){var e=d.getUserProfileKey();W=d.read(e)||{}}return W}},{key:"_clearDataMaps",value:function(){B=null,W=null}},{key:"_startPings",value:function(){if(this.options.sendPings){var t=this;setInterval(function(){ve(e.EVENT_TYPES.PING,function(e){t.processEvent(e)})},e.PING_FREQ_IN_MILLIS)}}},{key:"_isPersonalizationActive",value:function(){return this.options.enablePersonalization}},{key:"_processProfileEvent",value:function(e){var t=ie.generateProfileEvent(e);this.processEvent(t)}},{key:"_addToLocalEventMap",value:function(e){if(e){var t=this._getEventsMap(),n=O.getNow(),r=t[e];r?(r[2]=n,r[0]++):((r=[]).push(1),r.push(n),r.push(n)),t[e]=r;var i=d.getUserEventsKey();d.save(i,t)}}},{key:"_addToLocalProfileMap",value:function(e){if(e){var t=this._getProfilesMap();if(e._custom){var n=e._custom;for(var r in n)n.hasOwnProperty(r)&&(e[r]=n[r]);delete e._custom}for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i]);t._custom&&delete t._custom;var o=d.getUserProfileKey();d.save(o,t)}}},{key:"_addSystemDataToObject",value:function(e,t){t||(e=O.removeUnsupportedChars(e,function(e,t){O.reportError(e,t)}));var n=a.popError();return n&&(e.wzrk_error=n),e.id=R.getAccountId(),U.getGUID()&&(e.g=U.getGUID()),e.s=Y.getSessionId(),e.pg=this.session.getPageCount(),e.f=this.session.isFirstSession(),e}}]),t}(),he=[],pe=function(t){if(O.isArray(t)){for(var n=function(e,t){O.reportError(e,t)},r=null;t.length>0;){var i=t.shift();if(!O.isString(i))return void O.log.error(a.MESSAGES.event);if(i.length>1024&&(i=i.substring(0,1024),O.reportError(510,i+"... length exceeded 1024 chars. Trimmed.")),"Stayed"!==i&&"UTM Visited"!==i&&"App Launched"!==i&&"Notification Sent"!==i&&"Notification Viewed"!==i&&"Notification Clicked"!==i){if(r={type:e.EVENT_TYPES.EVENT,ep:O.getNow(),evtName:O.sanitize(i,O.unsupportedKeyCharRegex)},0!==t.length){var o=t.shift();if(O.isObject(o)){if("Charged"===i){if(ne(o),n){O.reportError(511,"Charged event structure invalid. Not sent.");continue}}else if(!ee(o)){O.reportError(512,i+" event structure invalid. Not sent.");continue}r.evtData=o}else t.unshift(o)}}else O.reportError(513,i+" is a restricted system event. It cannot be used as an event name.")}return r}},ye=function(){function e(t,n){if(i(this,e),this.api=t,he.push=function(e){var t=pe(e);this.api.processEvent(t)}.bind(this),n&&n.length>0)for(var r=0;r<n.length;r++)this.push(n[r])}return o(e,[{key:"push",value:function(){return he.push(Array.prototype.slice.call(arguments)),!0}},{key:"getDetails",value:function(e){return this.api.getEventDetails(e)}}]),e}(),Ee={domain:"wzrkt.com",protocol:"https:",enablePersonalization:!0,eventUploadInterval:1e3,eventUploadThreshold:50,maxSavedEventCount:1e3,uploadBatchSize:50,sendPages:!1,sendPings:!1},_e=function(){function e(t){i(this,e),this.api=t}return o(e,[{key:"getTimeElapsed",value:function(){return this.api.getTimeElapsed()}},{key:"getPageCount",value:function(){return this.api.getPageCount()}}]),e}(),Se=function(){function e(t){i(this,e),this.api=t}return o(e,[{key:"getTotalVisits",value:function(){return this.api.getTotalVisits()}},{key:"getLastVisit",value:function(){return this.api.getLastVisit()}}]),e}(),Ie=[],me=function(){function e(t,n){if(i(this,e),this.api=t,Ie.push=function(e){this.api.onUserLogin(e)}.bind(this),n&&n.length>0)for(var r=0;r<n.length;r++)this.push(n[r])}return o(e,[{key:"push",value:function(){return Ie.push(Array.prototype.slice.call(arguments)),!0}}]),e}();return new(function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};i(this,e),this.options=Object.assign({},Ee),this.event=t.event||[],this.profile=t.profile||[],this.onUserLogin=t.onUserLogin||[],this.logLevels=O.logLevels,this.swpath="/serviceWorker.js"}return o(e,[{key:"init",value:function(e,t){O.isEmptyString(e)?O.log.error(a.MESSAGES.init):(R.setAccountId(e),R.setRegion(t),null===U.getVAPIDState()&&U.setVAPIDState(!1),this.api=new de(Object.assign({},this.options)),this.session=new _e(this.api),this.user=new Se(this.api),this.onUserLogin=new me(this.api,this.onUserLogin),this.event=new ye(this.api,this.event),this.profile=new ie(this.api,this.profile),this._initiateTokenUpdateIfNeeded())}},{key:"setSWPath",value:function(e){this.swpath=e}},{key:"getCleverTapID",value:function(){return this.api.getCleverTapID()}},{key:"setLogLevel",value:function(e){O.setLogLevel(e)}},{key:"getLogLevel",value:function(){return O.getLogLevel()}},{key:"setAppVersion",value:function(e){R.setAppVersion(e)}},{key:"getAppVersion",value:function(){return R.getAppVersion()}},{key:"_registerCTNotifications",value:function(e,t){var n=this;e?this.swpath=e:e=this.swpath,O.log.debug("register initiated, vapid: "+U.getVAPID()),console.log("Device VAPID State "+U.getVAPIDState()),U.getVAPIDState()?U.getVAPID()&&U.getKaiOsNotificationState()?null!==this.api?(O.log.debug("registering SW callled"),this.api.registerSW(e,t)):O.log.debug("clevertap-Api context not available "+this.api):O.log.debug("Service Worker Subscription from client failed: Vapid-key: "+U.getVAPID()+" Notification Enabled:"+U.getKaiOsNotificationState()):(O.log.debug("setting timeout and calling _registerCTNotifications again in 2s"),setTimeout(function(){n._registerCTNotifications(e,t)},2e3))}},{key:"unregisterCTNotifications",value:function(e){this._registerCTNotifications(e,!0)}},{key:"registerCTNotifications",value:function(e){this._registerCTNotifications(e,!1)}},{key:"_initiateTokenUpdateIfNeeded",value:function(){O.log.debug("token updating: for vapid: "+U.getVAPID()+"notification state:"+U.getKaiOsNotificationState());null!==U.getLastTokenUpdateTs()&&(O.log.debug("Updating token initated on app launch"),this._registerCTNotifications(this.swpath,!1))}}]),e}())(window.clevertap)});