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