@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.
@@ -2294,25 +2294,22 @@ class PageView {
2294
2294
  * CustomData - a class for creating an instance for user's custom data
2295
2295
  * */
2296
2296
  let CustomData$1 = class CustomData {
2297
- /**
2298
- * @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
2299
- * @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
2300
- * @example
2301
- * ```ts
2302
- * // - Single value
2303
- * const customData = new CustomData(0, 'value_1');
2304
- * // - Variadic number of values
2305
- * const customData = new CustomData(0, 'value_1', 'value_2', 'value_3');
2306
- * // - Array of values
2307
- * const values = ['value_1', 'value_2', 'value_3'];
2308
- * const customData = new CustomData(0, ...values);
2309
- * ```
2310
- * */
2311
- constructor(index, ...value) {
2312
- this.index = index;
2313
- this.value = value;
2297
+ constructor(first, second, ...rest) {
2314
2298
  this._status = TrackingStatus.Unsent;
2315
2299
  this.isIdentifier = false;
2300
+ const isNumber = typeof first === 'number';
2301
+ const isBoolean = typeof second === 'boolean';
2302
+ if (isNumber) {
2303
+ this.index = first;
2304
+ }
2305
+ else {
2306
+ this.name = first;
2307
+ this.index = -1;
2308
+ }
2309
+ this.overwrite = isBoolean ? second : true;
2310
+ this.value = isBoolean
2311
+ ? rest
2312
+ : [second, ...rest].filter((v) => v != null);
2316
2313
  }
2317
2314
  get url() {
2318
2315
  // --- Note ---
@@ -2339,17 +2336,11 @@ let CustomData$1 = class CustomData {
2339
2336
  UrlParameter.ValuesCountMap +
2340
2337
  encodeURIComponent(JSON.stringify(resultValue)) +
2341
2338
  UrlParameter.Overwrite +
2342
- String(true) +
2339
+ String(this.overwrite) +
2343
2340
  identifierParameter);
2344
2341
  }
2345
2342
  get data() {
2346
- return {
2347
- index: this.index,
2348
- value: this.value,
2349
- type: KameleoonData.CustomData,
2350
- isIdentifier: this.isIdentifier,
2351
- status: this.status,
2352
- };
2343
+ return Object.assign(Object.assign({ index: this.index }, (this.name !== undefined ? { name: this.name } : {})), { value: this.value, type: KameleoonData.CustomData, isIdentifier: this.isIdentifier, status: this.status, overwrite: this.overwrite });
2353
2344
  }
2354
2345
  get status() {
2355
2346
  if (this._isMappingIdentifier) {
@@ -2364,8 +2355,15 @@ let CustomData$1 = class CustomData {
2364
2355
  * @return {CustomData} a CustomData instance
2365
2356
  * */
2366
2357
  static _fromRaw(data) {
2367
- const { index, value, status, isIdentifier } = data;
2368
- const customData = new CustomData(index, ...value);
2358
+ const { index, value, status, isIdentifier, name, overwrite } = data;
2359
+ let customData;
2360
+ if (name) {
2361
+ customData = new CustomData(name, overwrite !== null && overwrite !== void 0 ? overwrite : true, ...value);
2362
+ customData._index = index;
2363
+ }
2364
+ else {
2365
+ customData = new CustomData(index, overwrite !== null && overwrite !== void 0 ? overwrite : true, ...value);
2366
+ }
2369
2367
  customData.status = status;
2370
2368
  customData._isMappingIdentifier = isIdentifier;
2371
2369
  return customData;
@@ -2396,6 +2394,14 @@ let CustomData$1 = class CustomData {
2396
2394
  }
2397
2395
  }
2398
2396
  }
2397
+ /**
2398
+ * @private
2399
+ * @method _index - an internal setter for setting index of custom data
2400
+ * @param {number} value - an index value
2401
+ * */
2402
+ set _index(value) {
2403
+ this.index = value;
2404
+ }
2399
2405
  };
2400
2406
 
2401
2407
  /**
@@ -5141,7 +5147,7 @@ class ConditionFactory {
5141
5147
  case TargetingType.HEAT_SLICE:
5142
5148
  return buildExports.Ok(new ConversionLikelihood(data));
5143
5149
  default:
5144
- KameleoonLogger.error `Unsupported targeted condition type found: ${targetingType}`;
5150
+ KameleoonLogger.info `Unsupported targeted condition type found: ${targetingType}`;
5145
5151
  return buildExports.Err(new KameleoonError(KameleoonException.TargetingCondition, targetingType));
5146
5152
  }
5147
5153
  }
@@ -5530,6 +5536,7 @@ class DataManager {
5530
5536
  this.mappingIdentifierCustomDataIndex = null;
5531
5537
  this.persistentCustomDataIndexes = new Set();
5532
5538
  this.localCustomDataIndexes = new Set();
5539
+ this.customDataIndexByName = new Map();
5533
5540
  this.cleanupIntervalId = null;
5534
5541
  KameleoonLogger.debug `CALL: new DataManager(dataStorage, infoStorage, cleanupInterval: ${cleanupInterval})`;
5535
5542
  this.dataStorage = dataStorage;
@@ -5864,12 +5871,15 @@ class DataManager {
5864
5871
  mutUpdateTargetingData({ infoData, visitorCode, kameleoonData, targetingData, }) {
5865
5872
  for (const dataItem of kameleoonData) {
5866
5873
  if (dataItem.data.type === KameleoonData.CustomData) {
5867
- this.processCustomData({
5874
+ const customDataIsValid = this.processCustomData({
5868
5875
  infoData,
5869
5876
  customData: dataItem,
5870
5877
  mutData: targetingData,
5871
5878
  visitorCode,
5872
5879
  });
5880
+ if (!customDataIsValid) {
5881
+ continue;
5882
+ }
5873
5883
  }
5874
5884
  const expirationTime = this.dataProcessor.mutUpdateData({
5875
5885
  infoData,
@@ -5910,6 +5920,14 @@ class DataManager {
5910
5920
  var _a;
5911
5921
  const { data } = customData;
5912
5922
  const isDataValid = Boolean(data.value.length && data.value[0].length);
5923
+ if (data.name) {
5924
+ const cdIndex = this.customDataIndexByName.get(data.name);
5925
+ if (cdIndex === undefined) {
5926
+ return false;
5927
+ }
5928
+ data.index = cdIndex;
5929
+ customData._index = cdIndex;
5930
+ }
5913
5931
  if (this.mappingIdentifierCustomDataIndex === data.index && isDataValid) {
5914
5932
  customData._isMappingIdentifier = true;
5915
5933
  const userId = data.value[0];
@@ -5933,6 +5951,7 @@ class DataManager {
5933
5951
  if (this.localCustomDataIndexes.has(data.index)) {
5934
5952
  customData.status = TrackingStatus.Sent;
5935
5953
  }
5954
+ return true;
5936
5955
  }
5937
5956
  get unsentDataVisitors() {
5938
5957
  const infoResult = this.infoStorage.read();
@@ -5965,6 +5984,7 @@ class DataManager {
5965
5984
  set customDataIndexes(customData) {
5966
5985
  var _a;
5967
5986
  const [customDataLocalOnlyIndexes, persistentCustomDataIndexes] = [[], []];
5987
+ const customDataIndexByName = new Map();
5968
5988
  customData.forEach((customData) => {
5969
5989
  if (customData.localOnly) {
5970
5990
  customDataLocalOnlyIndexes.push(customData.index);
@@ -5972,12 +5992,19 @@ class DataManager {
5972
5992
  if (customData.scope === CustomDataScope.Visitor) {
5973
5993
  persistentCustomDataIndexes.push(customData.index);
5974
5994
  }
5995
+ if (customData.name) {
5996
+ customDataIndexByName.set(customData.name, customData.index);
5997
+ }
5975
5998
  });
5976
5999
  this.mappingIdentifierCustomDataIndex =
5977
6000
  ((_a = customData.find((customData) => customData.isMappingIdentifier)) === null || _a === void 0 ? void 0 : _a.index) ||
5978
6001
  null;
5979
6002
  this.localCustomDataIndexes = new Set(customDataLocalOnlyIndexes);
5980
6003
  this.persistentCustomDataIndexes = new Set(persistentCustomDataIndexes);
6004
+ this.customDataIndexByName = customDataIndexByName;
6005
+ }
6006
+ getCustomDataIndexByName(name) {
6007
+ return this.customDataIndexByName.get(name);
5981
6008
  }
5982
6009
  }
5983
6010
 
@@ -7778,7 +7805,7 @@ class BodyProvider {
7778
7805
  const identifier = isMappingIdentifier
7779
7806
  ? UrlParameter.MappingValue
7780
7807
  : UrlParameter.VisitorCode;
7781
- return identifier + visitorCode;
7808
+ return identifier + encodeURIComponent(visitorCode);
7782
7809
  }
7783
7810
  }
7784
7811
 
@@ -8362,6 +8389,7 @@ class KameleoonClient {
8362
8389
  }
8363
8390
  const resultVariables = variables.map((variable) => Parser.parseFeatureVariable(variable).throw());
8364
8391
  resultFeatureList.set(variation.featureKey, {
8392
+ name: '',
8365
8393
  key: variationKey,
8366
8394
  id: variationId,
8367
8395
  experimentId,
@@ -8582,6 +8610,62 @@ class KameleoonClient {
8582
8610
  this.flush(visitorCode);
8583
8611
  KameleoonLogger.info `RETURN: KameleoonClient.evaluateAudiences(visitorCode: ${visitorCode})`;
8584
8612
  }
8613
+ getDataFile() {
8614
+ const dataFile = {
8615
+ featureFlags: new Map(),
8616
+ };
8617
+ if (this.stubMode) {
8618
+ return dataFile;
8619
+ }
8620
+ KameleoonLogger.debug `CALL: KameleoonClient.getDataFile()`;
8621
+ this.clientConfiguration.featureFlags.forEach((featureFlag, key) => {
8622
+ const variationsMap = new Map();
8623
+ featureFlag.variations.forEach((sourceVariation) => {
8624
+ var _a;
8625
+ const variablesMap = new Map();
8626
+ sourceVariation.variables.forEach((variable) => {
8627
+ try {
8628
+ const parsedVariable = Parser.parseFeatureVariable(variable).throw();
8629
+ variablesMap.set(variable.key, parsedVariable);
8630
+ }
8631
+ catch (err) {
8632
+ KameleoonLogger.error `Error parsing variable ${variable.key} of feature flag ${featureFlag.featureKey}: ${err}`;
8633
+ }
8634
+ });
8635
+ variationsMap.set(sourceVariation.key, {
8636
+ name: (_a = sourceVariation.name) !== null && _a !== void 0 ? _a : '',
8637
+ key: sourceVariation.key,
8638
+ id: null,
8639
+ experimentId: null,
8640
+ variables: variablesMap,
8641
+ });
8642
+ });
8643
+ const rules = featureFlag.rules.map((rule) => {
8644
+ const ruleVariations = new Map();
8645
+ rule.variationByExposition.forEach((varByExp) => {
8646
+ const baseVariation = variationsMap.get(varByExp.variationKey);
8647
+ if (!baseVariation)
8648
+ return;
8649
+ ruleVariations.set(baseVariation.key, {
8650
+ name: baseVariation.name,
8651
+ key: baseVariation.key,
8652
+ id: varByExp.variationId,
8653
+ experimentId: rule.experimentId,
8654
+ variables: baseVariation.variables,
8655
+ });
8656
+ });
8657
+ return { variations: ruleVariations };
8658
+ });
8659
+ dataFile.featureFlags.set(key, {
8660
+ variations: variationsMap,
8661
+ environmentEnabled: featureFlag.environmentEnabled,
8662
+ rules,
8663
+ defaultVariationKey: featureFlag.defaultVariationKey,
8664
+ });
8665
+ });
8666
+ KameleoonLogger.debug `RETURN: KameleoonClient.getDataFile() -> (dataFile: ${dataFile})`;
8667
+ return dataFile;
8668
+ }
8585
8669
  setUserConsent({ visitorCode, consent, setData, }) {
8586
8670
  if (this.stubMode) {
8587
8671
  return;
@@ -8768,6 +8852,7 @@ class KameleoonClient {
8768
8852
  return selected;
8769
8853
  }
8770
8854
  _getFeatureVariation({ visitorCode, featureKey, track, }) {
8855
+ var _a, _b;
8771
8856
  KameleoonLogger.debug `CALL: KameleoonClient._getFeatureVariation(visitorCode: ${visitorCode}, featureKey: ${featureKey}, track: ${track})`;
8772
8857
  if (!this.initialized) {
8773
8858
  return buildExports.Err(new KameleoonError(KameleoonException.Initialization));
@@ -8818,7 +8903,9 @@ class KameleoonClient {
8818
8903
  if (track && !isSimulated) {
8819
8904
  this.tracker.scheduleVisitor(visitorCode, this._isConsentProvided(visitorCode));
8820
8905
  }
8906
+ const variationName = (_b = (_a = featureFlag.variations.find((item) => item.key === variationKey)) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : '';
8821
8907
  const resultData = {
8908
+ name: variationName,
8822
8909
  key: variationKey,
8823
8910
  id: variationId,
8824
8911
  experimentId,