@mparticle/web-sdk 2.23.9 → 2.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/mparticle.js CHANGED
@@ -608,7 +608,7 @@ var mParticle = (function () {
608
608
  Environment: Environment
609
609
  };
610
610
 
611
- var version = "2.23.9";
611
+ var version = "2.25.0";
612
612
 
613
613
  var Constants = {
614
614
  sdkVersion: version,
@@ -721,10 +721,10 @@ var mParticle = (function () {
721
721
  integrationDelayTimeout: 5000,
722
722
  maxCookieSize: 3000,
723
723
  aliasMaxWindow: 90,
724
- uploadInterval: 0 // Maximum milliseconds in between batch uploads, below 500 will mean immediate upload
724
+ uploadInterval: 0 // Maximum milliseconds in between batch uploads, below 500 will mean immediate upload. The server returns this as a string, but we are using it as a number internally
725
725
  },
726
726
 
727
- DefaultUrls: {
727
+ DefaultBaseUrls: {
728
728
  v1SecureServiceUrl: 'jssdks.mparticle.com/v1/JS/',
729
729
  v2SecureServiceUrl: 'jssdks.mparticle.com/v2/JS/',
730
730
  v3SecureServiceUrl: 'jssdks.mparticle.com/v3/JS/',
@@ -762,7 +762,9 @@ var mParticle = (function () {
762
762
  FeatureFlags: {
763
763
  ReportBatching: 'reportBatching',
764
764
  EventBatchingIntervalMillis: 'eventBatchingIntervalMillis',
765
- OfflineStorage: 'offlineStorage'
765
+ OfflineStorage: 'offlineStorage',
766
+ DirectUrlRouting: 'directURLRouting',
767
+ CacheIdentity: 'cacheIdentity'
766
768
  },
767
769
  DefaultInstance: 'default_instance',
768
770
  CCPAPurpose: 'data_sale_opt_out',
@@ -773,6 +775,9 @@ var mParticle = (function () {
773
775
  Identify: 'identify'
774
776
  }
775
777
  };
778
+ // https://go.mparticle.com/work/SQDSDKS-6080
779
+ var ONE_DAY_IN_SECONDS = 60 * 60 * 24;
780
+ var MILLIS_IN_ONE_SEC = 1000;
776
781
 
777
782
  /******************************************************************************
778
783
  Copyright (c) Microsoft Corporation.
@@ -854,6 +859,16 @@ var mParticle = (function () {
854
859
  }
855
860
  }
856
861
 
862
+ function __spreadArray(to, from, pack) {
863
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
864
+ if (ar || !(i in from)) {
865
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
866
+ ar[i] = from[i];
867
+ }
868
+ }
869
+ return to.concat(ar || Array.prototype.slice.call(from));
870
+ }
871
+
857
872
  typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
858
873
  var e = new Error(message);
859
874
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
@@ -2373,7 +2388,7 @@ var mParticle = (function () {
2373
2388
  var self = this;
2374
2389
  this.queueEventForBatchUpload = function (event) {
2375
2390
  if (!this.uploader) {
2376
- var millis = mpInstance._Helpers.getFeatureFlag(Constants.FeatureFlags.EventBatchingIntervalMillis);
2391
+ var millis = parseNumber(mpInstance._Helpers.getFeatureFlag(Constants.FeatureFlags.EventBatchingIntervalMillis));
2377
2392
  this.uploader = new BatchUploader(mpInstance, millis);
2378
2393
  }
2379
2394
  this.uploader.queueEvent(event);
@@ -2509,7 +2524,7 @@ var mParticle = (function () {
2509
2524
  };
2510
2525
  }
2511
2526
 
2512
- var Modify$3 = Constants.IdentityMethods.Modify;
2527
+ var Modify$4 = Constants.IdentityMethods.Modify;
2513
2528
  var Validators = {
2514
2529
  // From ./utils
2515
2530
  // Utility Functions for backwards compatability
@@ -2532,7 +2547,7 @@ var mParticle = (function () {
2532
2547
  copyUserAttributes: 1
2533
2548
  };
2534
2549
  if (identityApiData) {
2535
- if (method === Modify$3) {
2550
+ if (method === Modify$4) {
2536
2551
  if (isObject(identityApiData.userIdentities) && !Object.keys(identityApiData.userIdentities).length || !isObject(identityApiData.userIdentities)) {
2537
2552
  return {
2538
2553
  valid: false,
@@ -3757,13 +3772,13 @@ var mParticle = (function () {
3757
3772
  }
3758
3773
  }
3759
3774
  }
3760
- for (var prop in Constants.DefaultUrls) {
3761
- sdkConfig[prop] = Constants.DefaultUrls[prop];
3775
+ for (var prop in Constants.DefaultBaseUrls) {
3776
+ sdkConfig[prop] = Constants.DefaultBaseUrls[prop];
3762
3777
  }
3763
3778
  return sdkConfig;
3764
3779
  }
3765
3780
  // TODO: Merge this with SDKStoreApi in sdkRuntimeModels
3766
- function Store(config, mpInstance) {
3781
+ function Store(config, mpInstance, apiKey) {
3767
3782
  var defaultStore = {
3768
3783
  isEnabled: true,
3769
3784
  sessionAttributes: {},
@@ -3811,10 +3826,15 @@ var mParticle = (function () {
3811
3826
  for (var key in defaultStore) {
3812
3827
  this[key] = defaultStore[key];
3813
3828
  }
3829
+ this.devToken = apiKey || null;
3814
3830
  this.integrationDelayTimeoutStart = Date.now();
3815
- this.SDKConfig = createSDKConfig(config);
3816
3831
  // Set configuration to default settings
3832
+ this.SDKConfig = createSDKConfig(config);
3817
3833
  if (config) {
3834
+ if (!config.hasOwnProperty('flags')) {
3835
+ this.SDKConfig.flags = {};
3836
+ }
3837
+ this.SDKConfig.flags = processFlags(config, this.SDKConfig);
3818
3838
  if (config.deviceId) {
3819
3839
  this.deviceId = config.deviceId;
3820
3840
  }
@@ -3823,23 +3843,9 @@ var mParticle = (function () {
3823
3843
  } else {
3824
3844
  this.SDKConfig.isDevelopmentMode = false;
3825
3845
  }
3826
- if (config.hasOwnProperty('v1SecureServiceUrl')) {
3827
- this.SDKConfig.v1SecureServiceUrl = config.v1SecureServiceUrl;
3828
- }
3829
- if (config.hasOwnProperty('v2SecureServiceUrl')) {
3830
- this.SDKConfig.v2SecureServiceUrl = config.v2SecureServiceUrl;
3831
- }
3832
- if (config.hasOwnProperty('v3SecureServiceUrl')) {
3833
- this.SDKConfig.v3SecureServiceUrl = config.v3SecureServiceUrl;
3834
- }
3835
- if (config.hasOwnProperty('identityUrl')) {
3836
- this.SDKConfig.identityUrl = config.identityUrl;
3837
- }
3838
- if (config.hasOwnProperty('aliasUrl')) {
3839
- this.SDKConfig.aliasUrl = config.aliasUrl;
3840
- }
3841
- if (config.hasOwnProperty('configUrl')) {
3842
- this.SDKConfig.configUrl = config.configUrl;
3846
+ var baseUrls = processBaseUrls(config, this.SDKConfig.flags, apiKey);
3847
+ for (var baseUrlKeys in baseUrls) {
3848
+ this.SDKConfig[baseUrlKeys] = baseUrls[baseUrlKeys];
3843
3849
  }
3844
3850
  if (config.hasOwnProperty('logLevel')) {
3845
3851
  this.SDKConfig.logLevel = config.logLevel;
@@ -3953,19 +3959,79 @@ var mParticle = (function () {
3953
3959
  this.SDKConfig.onCreateBatch = undefined;
3954
3960
  }
3955
3961
  }
3956
- if (!config.hasOwnProperty('flags')) {
3957
- this.SDKConfig.flags = {};
3958
- }
3959
- if (!this.SDKConfig.flags.hasOwnProperty(Constants.FeatureFlags.EventBatchingIntervalMillis)) {
3960
- this.SDKConfig.flags[Constants.FeatureFlags.EventBatchingIntervalMillis] = Constants.DefaultConfig.uploadInterval;
3961
- }
3962
- if (!this.SDKConfig.flags.hasOwnProperty(Constants.FeatureFlags.ReportBatching)) {
3963
- this.SDKConfig.flags[Constants.FeatureFlags.ReportBatching] = false;
3964
- }
3965
- if (!this.SDKConfig.flags.hasOwnProperty(Constants.FeatureFlags.OfflineStorage)) {
3966
- this.SDKConfig.flags[Constants.FeatureFlags.OfflineStorage] = 0;
3962
+ }
3963
+ }
3964
+ function processFlags(config, SDKConfig) {
3965
+ var flags = {};
3966
+ var _a = Constants.FeatureFlags,
3967
+ ReportBatching = _a.ReportBatching,
3968
+ EventBatchingIntervalMillis = _a.EventBatchingIntervalMillis,
3969
+ OfflineStorage = _a.OfflineStorage,
3970
+ DirectUrlRouting = _a.DirectUrlRouting,
3971
+ CacheIdentity = _a.CacheIdentity;
3972
+ if (!config.flags) {
3973
+ return {};
3974
+ }
3975
+ // Passed in config flags take priority over defaults
3976
+ flags[ReportBatching] = config.flags[ReportBatching] || false;
3977
+ // The server returns stringified numbers, sowe need to parse
3978
+ flags[EventBatchingIntervalMillis] = parseNumber(config.flags[EventBatchingIntervalMillis]) || Constants.DefaultConfig.uploadInterval;
3979
+ flags[OfflineStorage] = config.flags[OfflineStorage] || '0';
3980
+ flags[DirectUrlRouting] = config.flags[DirectUrlRouting] === 'True';
3981
+ flags[CacheIdentity] = config.flags[CacheIdentity] === 'True';
3982
+ return flags;
3983
+ }
3984
+ function processBaseUrls(config, flags, apiKey) {
3985
+ // an API key is not present in a webview only mode. In this case, no baseUrls are needed
3986
+ if (!apiKey) {
3987
+ return {};
3988
+ }
3989
+ // When direct URL routing is false, update baseUrls based custom urls
3990
+ // passed to the config
3991
+ if (flags.directURLRouting) {
3992
+ return processDirectBaseUrls(config, apiKey);
3993
+ } else {
3994
+ return processCustomBaseUrls(config);
3995
+ }
3996
+ }
3997
+ function processCustomBaseUrls(config) {
3998
+ var defaultBaseUrls = Constants.DefaultBaseUrls;
3999
+ var newBaseUrls = {};
4000
+ // If there is no custo base url, we use the default base url
4001
+ for (var baseUrlKey in defaultBaseUrls) {
4002
+ newBaseUrls[baseUrlKey] = config[baseUrlKey] || defaultBaseUrls[baseUrlKey];
4003
+ }
4004
+ return newBaseUrls;
4005
+ }
4006
+ function processDirectBaseUrls(config, apiKey) {
4007
+ var defaultBaseUrls = Constants.DefaultBaseUrls;
4008
+ var directBaseUrls = {};
4009
+ // When Direct URL Routing is true, we create a new set of baseUrls that
4010
+ // include the silo in the urls. mParticle API keys are prefixed with the
4011
+ // silo and a hyphen (ex. "us1-", "us2-", "eu1-"). us1 was the first silo,
4012
+ // and before other silos existed, there were no prefixes and all apiKeys
4013
+ // were us1. As such, if we split on a '-' and the resulting array length
4014
+ // is 1, then it is an older APIkey that should route to us1.
4015
+ // When splitKey.length is greater than 1, then splitKey[0] will be
4016
+ // us1, us2, eu1, au1, or st1, etc as new silos are added
4017
+ var DEFAULT_SILO = 'us1';
4018
+ var splitKey = apiKey.split('-');
4019
+ var routingPrefix = splitKey.length <= 1 ? DEFAULT_SILO : splitKey[0];
4020
+ for (var baseUrlKey in defaultBaseUrls) {
4021
+ // Any custom endpoints passed to mpConfig will take priority over direct
4022
+ // mapping to the silo. The most common use case is a customer provided CNAME.
4023
+ if (baseUrlKey === 'configUrl') {
4024
+ directBaseUrls[baseUrlKey] = config[baseUrlKey] || defaultBaseUrls[baseUrlKey];
4025
+ continue;
4026
+ }
4027
+ if (config.hasOwnProperty(baseUrlKey)) {
4028
+ directBaseUrls[baseUrlKey] = config[baseUrlKey];
4029
+ } else {
4030
+ var urlparts = defaultBaseUrls[baseUrlKey].split('.');
4031
+ directBaseUrls[baseUrlKey] = __spreadArray([urlparts[0], routingPrefix], urlparts.slice(1), true).join('.');
3967
4032
  }
3968
4033
  }
4034
+ return directBaseUrls;
3969
4035
  }
3970
4036
 
3971
4037
  function Logger(config) {
@@ -5215,10 +5281,10 @@ var mParticle = (function () {
5215
5281
  }
5216
5282
 
5217
5283
  var _Constants$IdentityMe$1 = Constants.IdentityMethods,
5218
- Modify$2 = _Constants$IdentityMe$1.Modify,
5219
- Identify$1 = _Constants$IdentityMe$1.Identify,
5220
- Login = _Constants$IdentityMe$1.Login,
5221
- Logout = _Constants$IdentityMe$1.Logout;
5284
+ Modify$3 = _Constants$IdentityMe$1.Modify,
5285
+ Identify$2 = _Constants$IdentityMe$1.Identify,
5286
+ Login$2 = _Constants$IdentityMe$1.Login,
5287
+ Logout$2 = _Constants$IdentityMe$1.Logout;
5222
5288
  function Forwarders(mpInstance, kitBlocker) {
5223
5289
  var self = this;
5224
5290
  var UserAttributeActionTypes = {
@@ -5480,28 +5546,28 @@ var mParticle = (function () {
5480
5546
  mpInstance._Store.activeForwarders.forEach(function (forwarder) {
5481
5547
  var filteredUser = filteredMparticleUser(user.getMPID(), forwarder, mpInstance, kitBlocker);
5482
5548
  var filteredUserIdentities = filteredUser.getUserIdentities();
5483
- if (identityMethod === Identify$1) {
5549
+ if (identityMethod === Identify$2) {
5484
5550
  if (forwarder.onIdentifyComplete) {
5485
5551
  result = forwarder.onIdentifyComplete(filteredUser, filteredUserIdentities);
5486
5552
  if (result) {
5487
5553
  mpInstance.Logger.verbose(result);
5488
5554
  }
5489
5555
  }
5490
- } else if (identityMethod === Login) {
5556
+ } else if (identityMethod === Login$2) {
5491
5557
  if (forwarder.onLoginComplete) {
5492
5558
  result = forwarder.onLoginComplete(filteredUser, filteredUserIdentities);
5493
5559
  if (result) {
5494
5560
  mpInstance.Logger.verbose(result);
5495
5561
  }
5496
5562
  }
5497
- } else if (identityMethod === Logout) {
5563
+ } else if (identityMethod === Logout$2) {
5498
5564
  if (forwarder.onLogoutComplete) {
5499
5565
  result = forwarder.onLogoutComplete(filteredUser, filteredUserIdentities);
5500
5566
  if (result) {
5501
5567
  mpInstance.Logger.verbose(result);
5502
5568
  }
5503
5569
  }
5504
- } else if (identityMethod === Modify$2) {
5570
+ } else if (identityMethod === Modify$3) {
5505
5571
  if (forwarder.onModifyComplete) {
5506
5572
  result = forwarder.onModifyComplete(filteredUser, filteredUserIdentities);
5507
5573
  if (result) {
@@ -6041,13 +6107,149 @@ var mParticle = (function () {
6041
6107
  }
6042
6108
  }
6043
6109
 
6110
+ var _a = Constants.IdentityMethods,
6111
+ Identify$1 = _a.Identify,
6112
+ Modify$2 = _a.Modify,
6113
+ Login$1 = _a.Login,
6114
+ Logout$1 = _a.Logout;
6115
+ var cacheOrClearIdCache = function cacheOrClearIdCache(method, knownIdentities, idCache, xhr, parsingCachedResponse) {
6116
+ // when parsing a response that has already been cached, simply return instead of attempting another cache
6117
+ if (parsingCachedResponse) {
6118
+ return;
6119
+ }
6120
+ var CACHE_HEADER = 'x-mp-max-age';
6121
+ // default the expire timestamp to one day in milliseconds unless a header comes back
6122
+ var now = new Date().getTime();
6123
+ var expireTimestamp = now + ONE_DAY_IN_SECONDS * MILLIS_IN_ONE_SEC;
6124
+ if (xhr.getAllResponseHeaders().includes(CACHE_HEADER)) {
6125
+ expireTimestamp = now + parseNumber(xhr.getResponseHeader(CACHE_HEADER)) * MILLIS_IN_ONE_SEC;
6126
+ }
6127
+ switch (method) {
6128
+ case Login$1:
6129
+ case Identify$1:
6130
+ cacheIdentityRequest(method, knownIdentities, expireTimestamp, idCache, xhr);
6131
+ break;
6132
+ case Modify$2:
6133
+ case Logout$1:
6134
+ idCache.purge();
6135
+ break;
6136
+ }
6137
+ };
6138
+ var cacheIdentityRequest = function cacheIdentityRequest(method, identities, expireTimestamp, idCache, xhr) {
6139
+ var cache = idCache.retrieve() || {};
6140
+ var cacheKey = concatenateIdentities(method, identities);
6141
+ var hashedKey = generateHash(cacheKey);
6142
+ cache[hashedKey] = {
6143
+ responseText: xhr.responseText,
6144
+ status: xhr.status,
6145
+ expireTimestamp: expireTimestamp
6146
+ };
6147
+ idCache.store(cache);
6148
+ };
6149
+ // We need to ensure that identities are concatenated in a deterministic way, so
6150
+ // we sort the identities based on their enum.
6151
+ // we create an array, set the user identity at the index of the user identity type
6152
+ var concatenateIdentities = function concatenateIdentities(method, userIdentities) {
6153
+ var DEVICE_APPLICATION_STAMP = 'device_application_stamp';
6154
+ // set DAS first since it is not an official identity type
6155
+ var cacheKey = "".concat(method, ":").concat(DEVICE_APPLICATION_STAMP, "=").concat(userIdentities.device_application_stamp, ";");
6156
+ var idLength = Object.keys(userIdentities).length;
6157
+ var concatenatedIdentities = '';
6158
+ if (idLength) {
6159
+ var userIDArray = new Array();
6160
+ // create an array where each index is equal to the user identity type
6161
+ for (var key in userIdentities) {
6162
+ if (key === DEVICE_APPLICATION_STAMP) {
6163
+ continue;
6164
+ } else {
6165
+ userIDArray[Types.IdentityType.getIdentityType(key)] = userIdentities[key];
6166
+ }
6167
+ }
6168
+ concatenatedIdentities = userIDArray.reduce(function (prevValue, currentValue, index) {
6169
+ var idName = Types.IdentityType.getIdentityName(index);
6170
+ return "".concat(prevValue).concat(idName, "=").concat(currentValue, ";");
6171
+ }, cacheKey);
6172
+ }
6173
+ return concatenatedIdentities;
6174
+ };
6175
+ var hasValidCachedIdentity = function hasValidCachedIdentity(method, proposedUserIdentities, idCache) {
6176
+ // There is an edge case where multiple identity calls are taking place
6177
+ // before identify fires, so there may not be a cache. See what happens when
6178
+ // the ? in idCache is removed to the following test
6179
+ // "queued events contain login mpid instead of identify mpid when calling
6180
+ // login immediately after mParticle initializes"
6181
+ var cache = idCache === null || idCache === void 0 ? void 0 : idCache.retrieve();
6182
+ // if there is no cache, then there is no valid cached identity
6183
+ if (!cache) {
6184
+ return false;
6185
+ }
6186
+ var cacheKey = concatenateIdentities(method, proposedUserIdentities);
6187
+ var hashedKey = generateHash(cacheKey);
6188
+ // if cache doesn't have the cacheKey, there is no valid cached identity
6189
+ if (!cache.hasOwnProperty(hashedKey)) {
6190
+ return false;
6191
+ }
6192
+ // If there is a valid cache key, compare the expireTimestamp to the current time.
6193
+ // If the current time is greater than the expireTimestamp, it is not a valid
6194
+ // cached identity.
6195
+ var expireTimestamp = cache[hashedKey].expireTimestamp;
6196
+ if (expireTimestamp < new Date().getTime()) {
6197
+ return false;
6198
+ } else {
6199
+ return true;
6200
+ }
6201
+ };
6202
+ var getCachedIdentity = function getCachedIdentity(method, proposedUserIdentities, idCache) {
6203
+ var cacheKey = concatenateIdentities(method, proposedUserIdentities);
6204
+ var hashedKey = generateHash(cacheKey);
6205
+ var cache = idCache.retrieve();
6206
+ var cachedIdentity = cache ? cache[hashedKey] : null;
6207
+ return cachedIdentity;
6208
+ };
6209
+ // https://go.mparticle.com/work/SQDSDKS-6079
6210
+ var createKnownIdentities = function createKnownIdentities(identityApiData, deviceId) {
6211
+ var identitiesResult = {};
6212
+ if (isObject(identityApiData === null || identityApiData === void 0 ? void 0 : identityApiData.userIdentities)) {
6213
+ for (var identity in identityApiData.userIdentities) {
6214
+ identitiesResult[identity] = identityApiData.userIdentities[identity];
6215
+ }
6216
+ }
6217
+ identitiesResult.device_application_stamp = deviceId;
6218
+ return identitiesResult;
6219
+ };
6220
+ var removeExpiredIdentityCacheDates = function removeExpiredIdentityCacheDates(idCache) {
6221
+ var cache = idCache.retrieve() || {};
6222
+ var currentTime = new Date().getTime();
6223
+ // Iterate over the cache and remove any key/value pairs that are expired
6224
+ for (var key in cache) {
6225
+ if (cache[key].expireTimestamp < currentTime) {
6226
+ delete cache[key];
6227
+ }
6228
+ }
6229
+ idCache.store(cache);
6230
+ };
6231
+ var tryCacheIdentity = function tryCacheIdentity(knownIdentities, idCache, parseIdentityResponse, mpid, callback, identityApiData, identityMethod) {
6232
+ // https://go.mparticle.com/work/SQDSDKS-6095
6233
+ var shouldReturnCachedIdentity = hasValidCachedIdentity(identityMethod, knownIdentities, idCache);
6234
+ // If Identity is cached, then immediately parse the identity response
6235
+ if (shouldReturnCachedIdentity) {
6236
+ var cachedIdentity = getCachedIdentity(identityMethod, knownIdentities, idCache);
6237
+ parseIdentityResponse(cachedIdentity, mpid, callback, identityApiData, identityMethod, knownIdentities, true);
6238
+ return true;
6239
+ }
6240
+ return false;
6241
+ };
6242
+
6044
6243
  var Messages$2 = Constants.Messages,
6045
6244
  HTTPCodes$2 = Constants.HTTPCodes;
6046
6245
  var _Constants$IdentityMe = Constants.IdentityMethods,
6047
6246
  Identify = _Constants$IdentityMe.Identify,
6048
- Modify$1 = _Constants$IdentityMe.Modify;
6247
+ Modify$1 = _Constants$IdentityMe.Modify,
6248
+ Login = _Constants$IdentityMe.Login,
6249
+ Logout = _Constants$IdentityMe.Logout;
6049
6250
  function Identity(mpInstance) {
6050
6251
  var self = this;
6252
+ this.idCache = null;
6051
6253
  this.checkIdentitySwap = function (previousMPID, currentMPID, currentSessionMPIDs) {
6052
6254
  if (previousMPID && currentMPID && previousMPID !== currentMPID) {
6053
6255
  var persistence = mpInstance._Persistence.getPersistence();
@@ -6059,16 +6261,6 @@ var mParticle = (function () {
6059
6261
  }
6060
6262
  };
6061
6263
  this.IdentityRequest = {
6062
- createKnownIdentities: function createKnownIdentities(identityApiData, deviceId) {
6063
- var identitiesResult = {};
6064
- if (identityApiData && identityApiData.userIdentities && mpInstance._Helpers.isObject(identityApiData.userIdentities)) {
6065
- for (var identity in identityApiData.userIdentities) {
6066
- identitiesResult[identity] = identityApiData.userIdentities[identity];
6067
- }
6068
- }
6069
- identitiesResult.device_application_stamp = deviceId;
6070
- return identitiesResult;
6071
- },
6072
6264
  preProcessIdentityRequest: function preProcessIdentityRequest(identityApiData, callback, method) {
6073
6265
  mpInstance.Logger.verbose(Messages$2.InformationMessages.StartingLogEvent + ': ' + method);
6074
6266
  var identityValidationResult = mpInstance._Helpers.Validators.validateIdentities(identityApiData, method);
@@ -6103,7 +6295,7 @@ var mParticle = (function () {
6103
6295
  request_id: mpInstance._Helpers.generateUniqueId(),
6104
6296
  request_timestamp_ms: new Date().getTime(),
6105
6297
  previous_mpid: mpid || null,
6106
- known_identities: this.createKnownIdentities(identityApiData, deviceId)
6298
+ known_identities: createKnownIdentities(identityApiData, deviceId)
6107
6299
  };
6108
6300
  return APIRequest;
6109
6301
  },
@@ -6205,18 +6397,24 @@ var mParticle = (function () {
6205
6397
  identify: function identify(identityApiData, callback) {
6206
6398
  var mpid,
6207
6399
  currentUser = mpInstance.Identity.getCurrentUser(),
6208
- preProcessResult = mpInstance._Identity.IdentityRequest.preProcessIdentityRequest(identityApiData, callback, 'identify');
6400
+ preProcessResult = mpInstance._Identity.IdentityRequest.preProcessIdentityRequest(identityApiData, callback, Identify);
6209
6401
  if (currentUser) {
6210
6402
  mpid = currentUser.getMPID();
6211
6403
  }
6212
6404
  if (preProcessResult.valid) {
6213
6405
  var identityApiRequest = mpInstance._Identity.IdentityRequest.createIdentityRequest(identityApiData, Constants.platform, Constants.sdkVendor, Constants.sdkVersion, mpInstance._Store.deviceId, mpInstance._Store.context, mpid);
6406
+ if (mpInstance._Helpers.getFeatureFlag(Constants.FeatureFlags.CacheIdentity)) {
6407
+ var successfullyCachedIdentity = tryCacheIdentity(identityApiRequest.known_identities, self.idCache, self.parseIdentityResponse, mpid, callback, identityApiData, Identify);
6408
+ if (successfullyCachedIdentity) {
6409
+ return;
6410
+ }
6411
+ }
6214
6412
  if (mpInstance._Helpers.canLog()) {
6215
6413
  if (mpInstance._Store.webviewBridgeEnabled) {
6216
6414
  mpInstance._NativeSdkHelpers.sendToNative(Constants.NativeSdkPaths.Identify, JSON.stringify(mpInstance._Identity.IdentityRequest.convertToNative(identityApiData)));
6217
6415
  mpInstance._Helpers.invokeCallback(callback, HTTPCodes$2.nativeIdentityRequest, 'Identify request sent to native sdk');
6218
6416
  } else {
6219
- mpInstance._IdentityAPIClient.sendIdentityRequest(identityApiRequest, 'identify', callback, identityApiData, self.parseIdentityResponse, mpid);
6417
+ mpInstance._IdentityAPIClient.sendIdentityRequest(identityApiRequest, Identify, callback, identityApiData, self.parseIdentityResponse, mpid, identityApiRequest.known_identities);
6220
6418
  }
6221
6419
  } else {
6222
6420
  mpInstance._Helpers.invokeCallback(callback, HTTPCodes$2.loggingDisabledOrMissingAPIKey, Messages$2.InformationMessages.AbandonLogEvent);
@@ -6236,7 +6434,7 @@ var mParticle = (function () {
6236
6434
  logout: function logout(identityApiData, callback) {
6237
6435
  var mpid,
6238
6436
  currentUser = mpInstance.Identity.getCurrentUser(),
6239
- preProcessResult = mpInstance._Identity.IdentityRequest.preProcessIdentityRequest(identityApiData, callback, 'logout');
6437
+ preProcessResult = mpInstance._Identity.IdentityRequest.preProcessIdentityRequest(identityApiData, callback, Logout);
6240
6438
  if (currentUser) {
6241
6439
  mpid = currentUser.getMPID();
6242
6440
  }
@@ -6248,7 +6446,7 @@ var mParticle = (function () {
6248
6446
  mpInstance._NativeSdkHelpers.sendToNative(Constants.NativeSdkPaths.Logout, JSON.stringify(mpInstance._Identity.IdentityRequest.convertToNative(identityApiData)));
6249
6447
  mpInstance._Helpers.invokeCallback(callback, HTTPCodes$2.nativeIdentityRequest, 'Logout request sent to native sdk');
6250
6448
  } else {
6251
- mpInstance._IdentityAPIClient.sendIdentityRequest(identityApiRequest, 'logout', callback, identityApiData, self.parseIdentityResponse, mpid);
6449
+ mpInstance._IdentityAPIClient.sendIdentityRequest(identityApiRequest, Logout, callback, identityApiData, self.parseIdentityResponse, mpid);
6252
6450
  evt = mpInstance._ServerModel.createEventObject({
6253
6451
  messageType: Types.MessageType.Profile
6254
6452
  });
@@ -6279,18 +6477,24 @@ var mParticle = (function () {
6279
6477
  login: function login(identityApiData, callback) {
6280
6478
  var mpid,
6281
6479
  currentUser = mpInstance.Identity.getCurrentUser(),
6282
- preProcessResult = mpInstance._Identity.IdentityRequest.preProcessIdentityRequest(identityApiData, callback, 'login');
6480
+ preProcessResult = mpInstance._Identity.IdentityRequest.preProcessIdentityRequest(identityApiData, callback, Login);
6283
6481
  if (currentUser) {
6284
6482
  mpid = currentUser.getMPID();
6285
6483
  }
6286
6484
  if (preProcessResult.valid) {
6287
6485
  var identityApiRequest = mpInstance._Identity.IdentityRequest.createIdentityRequest(identityApiData, Constants.platform, Constants.sdkVendor, Constants.sdkVersion, mpInstance._Store.deviceId, mpInstance._Store.context, mpid);
6486
+ if (mpInstance._Helpers.getFeatureFlag(Constants.FeatureFlags.CacheIdentity)) {
6487
+ var successfullyCachedIdentity = tryCacheIdentity(identityApiRequest.known_identities, self.idCache, self.parseIdentityResponse, mpid, callback, identityApiData, Login);
6488
+ if (successfullyCachedIdentity) {
6489
+ return;
6490
+ }
6491
+ }
6288
6492
  if (mpInstance._Helpers.canLog()) {
6289
6493
  if (mpInstance._Store.webviewBridgeEnabled) {
6290
6494
  mpInstance._NativeSdkHelpers.sendToNative(Constants.NativeSdkPaths.Login, JSON.stringify(mpInstance._Identity.IdentityRequest.convertToNative(identityApiData)));
6291
6495
  mpInstance._Helpers.invokeCallback(callback, HTTPCodes$2.nativeIdentityRequest, 'Login request sent to native sdk');
6292
6496
  } else {
6293
- mpInstance._IdentityAPIClient.sendIdentityRequest(identityApiRequest, 'login', callback, identityApiData, self.parseIdentityResponse, mpid);
6497
+ mpInstance._IdentityAPIClient.sendIdentityRequest(identityApiRequest, Login, callback, identityApiData, self.parseIdentityResponse, mpid, identityApiRequest.known_identities);
6294
6498
  }
6295
6499
  } else {
6296
6500
  mpInstance._Helpers.invokeCallback(callback, HTTPCodes$2.loggingDisabledOrMissingAPIKey, Messages$2.InformationMessages.AbandonLogEvent);
@@ -6310,7 +6514,7 @@ var mParticle = (function () {
6310
6514
  modify: function modify(identityApiData, callback) {
6311
6515
  var mpid,
6312
6516
  currentUser = mpInstance.Identity.getCurrentUser(),
6313
- preProcessResult = mpInstance._Identity.IdentityRequest.preProcessIdentityRequest(identityApiData, callback, 'modify');
6517
+ preProcessResult = mpInstance._Identity.IdentityRequest.preProcessIdentityRequest(identityApiData, callback, Modify$1);
6314
6518
  if (currentUser) {
6315
6519
  mpid = currentUser.getMPID();
6316
6520
  }
@@ -6322,7 +6526,7 @@ var mParticle = (function () {
6322
6526
  mpInstance._NativeSdkHelpers.sendToNative(Constants.NativeSdkPaths.Modify, JSON.stringify(mpInstance._Identity.IdentityRequest.convertToNative(identityApiData)));
6323
6527
  mpInstance._Helpers.invokeCallback(callback, HTTPCodes$2.nativeIdentityRequest, 'Modify request sent to native sdk');
6324
6528
  } else {
6325
- mpInstance._IdentityAPIClient.sendIdentityRequest(identityApiRequest, 'modify', callback, identityApiData, self.parseIdentityResponse, mpid);
6529
+ mpInstance._IdentityAPIClient.sendIdentityRequest(identityApiRequest, Modify$1, callback, identityApiData, self.parseIdentityResponse, mpid, identityApiRequest.known_identities);
6326
6530
  }
6327
6531
  } else {
6328
6532
  mpInstance._Helpers.invokeCallback(callback, HTTPCodes$2.loggingDisabledOrMissingAPIKey, Messages$2.InformationMessages.AbandonLogEvent);
@@ -6922,7 +7126,7 @@ var mParticle = (function () {
6922
7126
  }
6923
7127
  };
6924
7128
  };
6925
- this.parseIdentityResponse = function (xhr, previousMPID, callback, identityApiData, method) {
7129
+ this.parseIdentityResponse = function (xhr, previousMPID, callback, identityApiData, method, knownIdentities, parsingCachedResponse) {
6926
7130
  var prevUser = mpInstance.Identity.getUser(previousMPID),
6927
7131
  newUser,
6928
7132
  mpidIsNotInCookies,
@@ -6951,6 +7155,9 @@ var mParticle = (function () {
6951
7155
  mpInstance._Persistence.setFirstSeenTime(identityApiResult.mpid);
6952
7156
  }
6953
7157
  if (xhr.status === 200) {
7158
+ if (mpInstance._Helpers.getFeatureFlag(Constants.FeatureFlags.CacheIdentity)) {
7159
+ cacheOrClearIdCache(method, knownIdentities, self.idCache, xhr, parsingCachedResponse);
7160
+ }
6954
7161
  if (method === Modify$1) {
6955
7162
  newIdentitiesByType = mpInstance._Identity.IdentityRequest.combineUserIdentities(previousUIByName, identityApiData.userIdentities);
6956
7163
  mpInstance._Persistence.saveUserIdentitiesToPersistence(previousMPID, newIdentitiesByType);
@@ -7040,7 +7247,7 @@ var mParticle = (function () {
7040
7247
  this.sendUserIdentityChangeEvent = function (newUserIdentities, method, mpid, prevUserIdentities) {
7041
7248
  var currentUserInMemory, userIdentityChangeEvent;
7042
7249
  if (!mpid) {
7043
- if (method !== 'modify') {
7250
+ if (method !== Modify$1) {
7044
7251
  return;
7045
7252
  }
7046
7253
  }
@@ -7931,13 +8138,13 @@ var mParticle = (function () {
7931
8138
  }
7932
8139
  }
7933
8140
  };
7934
- this.sendIdentityRequest = function (identityApiRequest, method, callback, originalIdentityApiData, parseIdentityResponse, mpid) {
8141
+ this.sendIdentityRequest = function (identityApiRequest, method, callback, originalIdentityApiData, parseIdentityResponse, mpid, knownIdentities) {
7935
8142
  var xhr,
7936
8143
  previousMPID,
7937
8144
  xhrCallback = function xhrCallback() {
7938
8145
  if (xhr.readyState === 4) {
7939
8146
  mpInstance.Logger.verbose('Received ' + xhr.statusText + ' from server');
7940
- parseIdentityResponse(xhr, previousMPID, callback, originalIdentityApiData, method);
8147
+ parseIdentityResponse(xhr, previousMPID, callback, originalIdentityApiData, method, knownIdentities, false);
7941
8148
  }
7942
8149
  };
7943
8150
  mpInstance.Logger.verbose(Messages$1.InformationMessages.SendIdentityBegin);
@@ -8918,8 +9125,19 @@ var mParticle = (function () {
8918
9125
  mpInstance._Store.SDKConfig.flags[Constants.FeatureFlags.EventBatchingIntervalMillis] = config.flags[Constants.FeatureFlags.EventBatchingIntervalMillis];
8919
9126
  }
8920
9127
  }
9128
+
9129
+ // add a new function to apply items to the store that require config to be returned
8921
9130
  mpInstance._Store.storageName = mpInstance._Helpers.createMainStorageName(config.workspaceToken);
8922
9131
  mpInstance._Store.prodStorageName = mpInstance._Helpers.createProductStorageName(config.workspaceToken);
9132
+
9133
+ // idCache is instantiated here as opposed to when _Identity is instantiated
9134
+ // because it depends on _Store.storageName, which is not sent until above
9135
+ // because it is a setting on config which returns asyncronously
9136
+ // in self hosted mode
9137
+ mpInstance._Identity.idCache = new LocalStorageVault("".concat(mpInstance._Store.storageName, "-id-cache"), {
9138
+ logger: mpInstance.Logger
9139
+ });
9140
+ removeExpiredIdentityCacheDates(mpInstance._Identity.idCache);
8923
9141
  if (config.hasOwnProperty('workspaceToken')) {
8924
9142
  mpInstance._Store.SDKConfig.workspaceToken = config.workspaceToken;
8925
9143
  } else {
@@ -9080,9 +9298,8 @@ var mParticle = (function () {
9080
9298
  }
9081
9299
  function runPreConfigFetchInitialization(mpInstance, apiKey, config) {
9082
9300
  mpInstance.Logger = new Logger(config);
9083
- mpInstance._Store = new Store(config, mpInstance);
9301
+ mpInstance._Store = new Store(config, mpInstance, apiKey);
9084
9302
  window.mParticle.Store = mpInstance._Store;
9085
- mpInstance._Store.devToken = apiKey || null;
9086
9303
  mpInstance.Logger.verbose(Messages.InformationMessages.StartingInitialization);
9087
9304
 
9088
9305
  // Check to see if localStorage is available before main configuration runs
@@ -9566,7 +9783,7 @@ var mParticle = (function () {
9566
9783
  return self.getInstance().getIntegrationAttributes(moduleId);
9567
9784
  };
9568
9785
  this.Identity = {
9569
- HTTPCodes: self.getInstance().Identity.HTTPCodes,
9786
+ HTTPCodes: Constants.HTTPCodes,
9570
9787
  aliasUsers: function aliasUsers(aliasRequest, callback) {
9571
9788
  self.getInstance().Identity.aliasUsers(aliasRequest, callback);
9572
9789
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mparticle/web-sdk",
3
- "version": "2.23.9",
3
+ "version": "2.25.0",
4
4
  "description": "mParticle core SDK for web applications",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
package/src/apiClient.ts CHANGED
@@ -3,7 +3,7 @@ import Types from './types';
3
3
  import { BatchUploader } from './batchUploader';
4
4
  import { MParticleUser, MParticleWebSDK, SDKEvent } from './sdkRuntimeModels';
5
5
  import KitBlocker from './kitBlocking';
6
- import { Dictionary, isEmpty } from './utils';
6
+ import { Dictionary, getRampNumber, isEmpty, parseNumber } from './utils';
7
7
  import { IUploadObject } from './serverModel';
8
8
  import { MPForwarder } from './forwarders.interfaces';
9
9
 
@@ -38,9 +38,9 @@ export default function APIClient(
38
38
  const self = this;
39
39
  this.queueEventForBatchUpload = function(event: SDKEvent) {
40
40
  if (!this.uploader) {
41
- const millis = mpInstance._Helpers.getFeatureFlag(
41
+ const millis: number = parseNumber(mpInstance._Helpers.getFeatureFlag(
42
42
  Constants.FeatureFlags.EventBatchingIntervalMillis
43
- );
43
+ ));
44
44
  this.uploader = new BatchUploader(mpInstance, millis);
45
45
  }
46
46
  this.uploader.queueEvent(event);