@kameleoon/javascript-sdk-core 5.14.5 → 5.16.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.
@@ -2267,25 +2267,22 @@ class PageView {
2267
2267
  * CustomData - a class for creating an instance for user's custom data
2268
2268
  * */
2269
2269
  let CustomData$1 = class CustomData {
2270
- /**
2271
- * @param {number} index - an index of custom data to be stored under in a state, an index of custom data can be specified in `Advanced Tools` section of Kameleoon Application
2272
- * @param {string[]} value - custom value to store under the specified id, value can be anything but has to be stringified to match the `string` type. *Note* value is variadic parameter and can be used as follows
2273
- * @example
2274
- * ```ts
2275
- * // - Single value
2276
- * const customData = new CustomData(0, 'value_1');
2277
- * // - Variadic number of values
2278
- * const customData = new CustomData(0, 'value_1', 'value_2', 'value_3');
2279
- * // - Array of values
2280
- * const values = ['value_1', 'value_2', 'value_3'];
2281
- * const customData = new CustomData(0, ...values);
2282
- * ```
2283
- * */
2284
- constructor(index, ...value) {
2285
- this.index = index;
2286
- this.value = value;
2270
+ constructor(first, second, ...rest) {
2287
2271
  this._status = exports.TrackingStatus.Unsent;
2288
2272
  this.isIdentifier = false;
2273
+ const isNumber = typeof first === 'number';
2274
+ const isBoolean = typeof second === 'boolean';
2275
+ if (isNumber) {
2276
+ this.index = first;
2277
+ }
2278
+ else {
2279
+ this.name = first;
2280
+ this.index = -1;
2281
+ }
2282
+ this.overwrite = isBoolean ? second : true;
2283
+ this.value = isBoolean
2284
+ ? rest
2285
+ : [second, ...rest].filter((v) => v != null);
2289
2286
  }
2290
2287
  get url() {
2291
2288
  // --- Note ---
@@ -2312,17 +2309,11 @@ let CustomData$1 = class CustomData {
2312
2309
  UrlParameter.ValuesCountMap +
2313
2310
  encodeURIComponent(JSON.stringify(resultValue)) +
2314
2311
  UrlParameter.Overwrite +
2315
- String(true) +
2312
+ String(this.overwrite) +
2316
2313
  identifierParameter);
2317
2314
  }
2318
2315
  get data() {
2319
- return {
2320
- index: this.index,
2321
- value: this.value,
2322
- type: exports.KameleoonData.CustomData,
2323
- isIdentifier: this.isIdentifier,
2324
- status: this.status,
2325
- };
2316
+ return Object.assign(Object.assign({ index: this.index }, (this.name !== undefined ? { name: this.name } : {})), { value: this.value, type: exports.KameleoonData.CustomData, isIdentifier: this.isIdentifier, status: this.status, overwrite: this.overwrite });
2326
2317
  }
2327
2318
  get status() {
2328
2319
  if (this._isMappingIdentifier) {
@@ -2337,8 +2328,15 @@ let CustomData$1 = class CustomData {
2337
2328
  * @return {CustomData} a CustomData instance
2338
2329
  * */
2339
2330
  static _fromRaw(data) {
2340
- const { index, value, status, isIdentifier } = data;
2341
- const customData = new CustomData(index, ...value);
2331
+ const { index, value, status, isIdentifier, name, overwrite } = data;
2332
+ let customData;
2333
+ if (name) {
2334
+ customData = new CustomData(name, overwrite !== null && overwrite !== void 0 ? overwrite : true, ...value);
2335
+ customData._index = index;
2336
+ }
2337
+ else {
2338
+ customData = new CustomData(index, overwrite !== null && overwrite !== void 0 ? overwrite : true, ...value);
2339
+ }
2342
2340
  customData.status = status;
2343
2341
  customData._isMappingIdentifier = isIdentifier;
2344
2342
  return customData;
@@ -2369,6 +2367,14 @@ let CustomData$1 = class CustomData {
2369
2367
  }
2370
2368
  }
2371
2369
  }
2370
+ /**
2371
+ * @private
2372
+ * @method _index - an internal setter for setting index of custom data
2373
+ * @param {number} value - an index value
2374
+ * */
2375
+ set _index(value) {
2376
+ this.index = value;
2377
+ }
2372
2378
  };
2373
2379
 
2374
2380
  /**
@@ -5114,7 +5120,7 @@ class ConditionFactory {
5114
5120
  case TargetingType.HEAT_SLICE:
5115
5121
  return buildExports.Ok(new ConversionLikelihood(data));
5116
5122
  default:
5117
- KameleoonLogger.error `Unsupported targeted condition type found: ${targetingType}`;
5123
+ KameleoonLogger.info `Unsupported targeted condition type found: ${targetingType}`;
5118
5124
  return buildExports.Err(new KameleoonError(exports.KameleoonException.TargetingCondition, targetingType));
5119
5125
  }
5120
5126
  }
@@ -5503,6 +5509,7 @@ class DataManager {
5503
5509
  this.mappingIdentifierCustomDataIndex = null;
5504
5510
  this.persistentCustomDataIndexes = new Set();
5505
5511
  this.localCustomDataIndexes = new Set();
5512
+ this.customDataIndexByName = new Map();
5506
5513
  this.cleanupIntervalId = null;
5507
5514
  KameleoonLogger.debug `CALL: new DataManager(dataStorage, infoStorage, cleanupInterval: ${cleanupInterval})`;
5508
5515
  this.dataStorage = dataStorage;
@@ -5837,12 +5844,15 @@ class DataManager {
5837
5844
  mutUpdateTargetingData({ infoData, visitorCode, kameleoonData, targetingData, }) {
5838
5845
  for (const dataItem of kameleoonData) {
5839
5846
  if (dataItem.data.type === exports.KameleoonData.CustomData) {
5840
- this.processCustomData({
5847
+ const customDataIsValid = this.processCustomData({
5841
5848
  infoData,
5842
5849
  customData: dataItem,
5843
5850
  mutData: targetingData,
5844
5851
  visitorCode,
5845
5852
  });
5853
+ if (!customDataIsValid) {
5854
+ continue;
5855
+ }
5846
5856
  }
5847
5857
  const expirationTime = this.dataProcessor.mutUpdateData({
5848
5858
  infoData,
@@ -5883,6 +5893,14 @@ class DataManager {
5883
5893
  var _a;
5884
5894
  const { data } = customData;
5885
5895
  const isDataValid = Boolean(data.value.length && data.value[0].length);
5896
+ if (data.name) {
5897
+ const cdIndex = this.customDataIndexByName.get(data.name);
5898
+ if (cdIndex === undefined) {
5899
+ return false;
5900
+ }
5901
+ data.index = cdIndex;
5902
+ customData._index = cdIndex;
5903
+ }
5886
5904
  if (this.mappingIdentifierCustomDataIndex === data.index && isDataValid) {
5887
5905
  customData._isMappingIdentifier = true;
5888
5906
  const userId = data.value[0];
@@ -5906,6 +5924,7 @@ class DataManager {
5906
5924
  if (this.localCustomDataIndexes.has(data.index)) {
5907
5925
  customData.status = exports.TrackingStatus.Sent;
5908
5926
  }
5927
+ return true;
5909
5928
  }
5910
5929
  get unsentDataVisitors() {
5911
5930
  const infoResult = this.infoStorage.read();
@@ -5938,6 +5957,7 @@ class DataManager {
5938
5957
  set customDataIndexes(customData) {
5939
5958
  var _a;
5940
5959
  const [customDataLocalOnlyIndexes, persistentCustomDataIndexes] = [[], []];
5960
+ const customDataIndexByName = new Map();
5941
5961
  customData.forEach((customData) => {
5942
5962
  if (customData.localOnly) {
5943
5963
  customDataLocalOnlyIndexes.push(customData.index);
@@ -5945,12 +5965,19 @@ class DataManager {
5945
5965
  if (customData.scope === exports.CustomDataScope.Visitor) {
5946
5966
  persistentCustomDataIndexes.push(customData.index);
5947
5967
  }
5968
+ if (customData.name) {
5969
+ customDataIndexByName.set(customData.name, customData.index);
5970
+ }
5948
5971
  });
5949
5972
  this.mappingIdentifierCustomDataIndex =
5950
5973
  ((_a = customData.find((customData) => customData.isMappingIdentifier)) === null || _a === void 0 ? void 0 : _a.index) ||
5951
5974
  null;
5952
5975
  this.localCustomDataIndexes = new Set(customDataLocalOnlyIndexes);
5953
5976
  this.persistentCustomDataIndexes = new Set(persistentCustomDataIndexes);
5977
+ this.customDataIndexByName = customDataIndexByName;
5978
+ }
5979
+ getCustomDataIndexByName(name) {
5980
+ return this.customDataIndexByName.get(name);
5954
5981
  }
5955
5982
  }
5956
5983
 
@@ -7742,7 +7769,7 @@ class BodyProvider {
7742
7769
  const identifier = isMappingIdentifier
7743
7770
  ? UrlParameter.MappingValue
7744
7771
  : UrlParameter.VisitorCode;
7745
- return identifier + visitorCode;
7772
+ return identifier + encodeURIComponent(visitorCode);
7746
7773
  }
7747
7774
  }
7748
7775
 
@@ -8326,6 +8353,7 @@ class KameleoonClient {
8326
8353
  }
8327
8354
  const resultVariables = variables.map((variable) => Parser.parseFeatureVariable(variable).throw());
8328
8355
  resultFeatureList.set(variation.featureKey, {
8356
+ name: '',
8329
8357
  key: variationKey,
8330
8358
  id: variationId,
8331
8359
  experimentId,
@@ -8546,6 +8574,62 @@ class KameleoonClient {
8546
8574
  this.flush(visitorCode);
8547
8575
  KameleoonLogger.info `RETURN: KameleoonClient.evaluateAudiences(visitorCode: ${visitorCode})`;
8548
8576
  }
8577
+ getDataFile() {
8578
+ const dataFile = {
8579
+ featureFlags: new Map(),
8580
+ };
8581
+ if (this.stubMode) {
8582
+ return dataFile;
8583
+ }
8584
+ KameleoonLogger.debug `CALL: KameleoonClient.getDataFile()`;
8585
+ this.clientConfiguration.featureFlags.forEach((featureFlag, key) => {
8586
+ const variationsMap = new Map();
8587
+ featureFlag.variations.forEach((sourceVariation) => {
8588
+ var _a;
8589
+ const variablesMap = new Map();
8590
+ sourceVariation.variables.forEach((variable) => {
8591
+ try {
8592
+ const parsedVariable = Parser.parseFeatureVariable(variable).throw();
8593
+ variablesMap.set(variable.key, parsedVariable);
8594
+ }
8595
+ catch (err) {
8596
+ KameleoonLogger.error `Error parsing variable ${variable.key} of feature flag ${featureFlag.featureKey}: ${err}`;
8597
+ }
8598
+ });
8599
+ variationsMap.set(sourceVariation.key, {
8600
+ name: (_a = sourceVariation.name) !== null && _a !== void 0 ? _a : '',
8601
+ key: sourceVariation.key,
8602
+ id: null,
8603
+ experimentId: null,
8604
+ variables: variablesMap,
8605
+ });
8606
+ });
8607
+ const rules = featureFlag.rules.map((rule) => {
8608
+ const ruleVariations = new Map();
8609
+ rule.variationByExposition.forEach((varByExp) => {
8610
+ const baseVariation = variationsMap.get(varByExp.variationKey);
8611
+ if (!baseVariation)
8612
+ return;
8613
+ ruleVariations.set(baseVariation.key, {
8614
+ name: baseVariation.name,
8615
+ key: baseVariation.key,
8616
+ id: varByExp.variationId,
8617
+ experimentId: rule.experimentId,
8618
+ variables: baseVariation.variables,
8619
+ });
8620
+ });
8621
+ return { variations: ruleVariations };
8622
+ });
8623
+ dataFile.featureFlags.set(key, {
8624
+ variations: variationsMap,
8625
+ environmentEnabled: featureFlag.environmentEnabled,
8626
+ rules,
8627
+ defaultVariationKey: featureFlag.defaultVariationKey,
8628
+ });
8629
+ });
8630
+ KameleoonLogger.debug `RETURN: KameleoonClient.getDataFile() -> (dataFile: ${dataFile})`;
8631
+ return dataFile;
8632
+ }
8549
8633
  setUserConsent({ visitorCode, consent, setData, }) {
8550
8634
  if (this.stubMode) {
8551
8635
  return;
@@ -8732,6 +8816,7 @@ class KameleoonClient {
8732
8816
  return selected;
8733
8817
  }
8734
8818
  _getFeatureVariation({ visitorCode, featureKey, track, }) {
8819
+ var _a, _b;
8735
8820
  KameleoonLogger.debug `CALL: KameleoonClient._getFeatureVariation(visitorCode: ${visitorCode}, featureKey: ${featureKey}, track: ${track})`;
8736
8821
  if (!this.initialized) {
8737
8822
  return buildExports.Err(new KameleoonError(exports.KameleoonException.Initialization));
@@ -8782,7 +8867,9 @@ class KameleoonClient {
8782
8867
  if (track && !isSimulated) {
8783
8868
  this.tracker.scheduleVisitor(visitorCode, this._isConsentProvided(visitorCode));
8784
8869
  }
8870
+ const variationName = (_b = (_a = featureFlag.variations.find((item) => item.key === variationKey)) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : '';
8785
8871
  const resultData = {
8872
+ name: variationName,
8786
8873
  key: variationKey,
8787
8874
  id: variationId,
8788
8875
  experimentId,