@kameleoon/javascript-sdk-core 0.0.3 → 1.0.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.
Files changed (261) hide show
  1. package/README.md +7 -3
  2. package/dist/campaignConfiguration/campaignConfiguration.d.ts +46 -0
  3. package/dist/campaignConfiguration/campaignConfiguration.js +217 -0
  4. package/dist/campaignConfiguration/campaignConfiguration.js.map +1 -0
  5. package/dist/campaignConfiguration/constants.d.ts +18 -0
  6. package/dist/campaignConfiguration/constants.js +23 -0
  7. package/dist/campaignConfiguration/constants.js.map +1 -0
  8. package/dist/campaignConfiguration/index.d.ts +2 -0
  9. package/dist/campaignConfiguration/index.js +8 -0
  10. package/dist/campaignConfiguration/index.js.map +1 -0
  11. package/dist/campaignConfiguration/types.d.ts +126 -0
  12. package/dist/campaignConfiguration/types.js +47 -0
  13. package/dist/campaignConfiguration/types.js.map +1 -0
  14. package/dist/clientSettings/clientSettings.d.ts +12 -0
  15. package/dist/clientSettings/clientSettings.js +29 -0
  16. package/dist/clientSettings/clientSettings.js.map +1 -0
  17. package/dist/clientSettings/index.d.ts +2 -0
  18. package/dist/clientSettings/index.js +6 -0
  19. package/dist/clientSettings/index.js.map +1 -0
  20. package/dist/clientSettings/types.d.ts +6 -0
  21. package/dist/{interfaces/interfaces.js → clientSettings/types.js} +1 -1
  22. package/dist/clientSettings/types.js.map +1 -0
  23. package/dist/constants.d.ts +10 -16
  24. package/dist/constants.js +13 -20
  25. package/dist/constants.js.map +1 -1
  26. package/dist/eventSource/constants.d.ts +1 -0
  27. package/dist/eventSource/constants.js +5 -0
  28. package/dist/eventSource/constants.js.map +1 -0
  29. package/dist/eventSource/externalEventSource.d.ts +12 -0
  30. package/dist/eventSource/externalEventSource.js +26 -0
  31. package/dist/eventSource/externalEventSource.js.map +1 -0
  32. package/dist/eventSource/index.d.ts +3 -0
  33. package/dist/eventSource/index.js +8 -0
  34. package/dist/eventSource/index.js.map +1 -0
  35. package/dist/eventSource/types.d.ts +47 -0
  36. package/dist/eventSource/types.js +3 -0
  37. package/dist/eventSource/types.js.map +1 -0
  38. package/dist/hasher/constants.d.ts +1 -0
  39. package/dist/hasher/constants.js +5 -0
  40. package/dist/hasher/constants.js.map +1 -0
  41. package/dist/hasher/hasher.d.ts +6 -0
  42. package/dist/hasher/hasher.js +34 -0
  43. package/dist/hasher/hasher.js.map +1 -0
  44. package/dist/hasher/index.d.ts +1 -0
  45. package/dist/hasher/index.js +6 -0
  46. package/dist/hasher/index.js.map +1 -0
  47. package/dist/hasher/types.d.ts +12 -0
  48. package/dist/hasher/types.js +3 -0
  49. package/dist/hasher/types.js.map +1 -0
  50. package/dist/index.d.ts +10 -3
  51. package/dist/index.js +25 -7
  52. package/dist/index.js.map +1 -1
  53. package/dist/kameleoonClient.d.ts +189 -100
  54. package/dist/kameleoonClient.js +238 -701
  55. package/dist/kameleoonClient.js.map +1 -1
  56. package/dist/kameleoonData/browser.d.ts +21 -0
  57. package/dist/kameleoonData/browser.js +41 -0
  58. package/dist/kameleoonData/browser.js.map +1 -0
  59. package/dist/kameleoonData/constants.d.ts +1 -0
  60. package/dist/kameleoonData/constants.js +5 -0
  61. package/dist/kameleoonData/constants.js.map +1 -0
  62. package/dist/kameleoonData/conversion.d.ts +23 -0
  63. package/dist/kameleoonData/conversion.js +49 -0
  64. package/dist/kameleoonData/conversion.js.map +1 -0
  65. package/dist/kameleoonData/customData.d.ts +33 -0
  66. package/dist/kameleoonData/customData.js +68 -0
  67. package/dist/kameleoonData/customData.js.map +1 -0
  68. package/dist/kameleoonData/device.d.ts +21 -0
  69. package/dist/kameleoonData/device.js +41 -0
  70. package/dist/kameleoonData/device.js.map +1 -0
  71. package/dist/kameleoonData/index.d.ts +7 -0
  72. package/dist/kameleoonData/index.js +20 -0
  73. package/dist/kameleoonData/index.js.map +1 -0
  74. package/dist/kameleoonData/nonce.d.ts +5 -0
  75. package/dist/kameleoonData/nonce.js +20 -0
  76. package/dist/kameleoonData/nonce.js.map +1 -0
  77. package/dist/kameleoonData/pageView.d.ts +24 -0
  78. package/dist/kameleoonData/pageView.js +54 -0
  79. package/dist/kameleoonData/pageView.js.map +1 -0
  80. package/dist/kameleoonData/types.d.ts +100 -0
  81. package/dist/kameleoonData/types.js +36 -0
  82. package/dist/kameleoonData/types.js.map +1 -0
  83. package/dist/kameleoonData/userAgent.d.ts +18 -0
  84. package/dist/kameleoonData/userAgent.js +33 -0
  85. package/dist/kameleoonData/userAgent.js.map +1 -0
  86. package/dist/kameleoonError/constants.d.ts +22 -0
  87. package/dist/kameleoonError/constants.js +25 -0
  88. package/dist/kameleoonError/constants.js.map +1 -0
  89. package/dist/kameleoonError/helpers.d.ts +1 -0
  90. package/dist/kameleoonError/helpers.js +8 -0
  91. package/dist/kameleoonError/helpers.js.map +1 -0
  92. package/dist/kameleoonError/index.d.ts +3 -0
  93. package/dist/kameleoonError/index.js +10 -0
  94. package/dist/kameleoonError/index.js.map +1 -0
  95. package/dist/kameleoonError/kameleoonError.d.ts +24 -0
  96. package/dist/kameleoonError/kameleoonError.js +64 -0
  97. package/dist/kameleoonError/kameleoonError.js.map +1 -0
  98. package/dist/kameleoonError/types.d.ts +20 -0
  99. package/dist/kameleoonError/types.js +25 -0
  100. package/dist/kameleoonError/types.js.map +1 -0
  101. package/dist/kameleoonUtils.d.ts +11 -10
  102. package/dist/kameleoonUtils.js +42 -128
  103. package/dist/kameleoonUtils.js.map +1 -1
  104. package/dist/requester/constants.d.ts +15 -0
  105. package/dist/requester/constants.js +53 -0
  106. package/dist/requester/constants.js.map +1 -0
  107. package/dist/requester/index.d.ts +3 -0
  108. package/dist/requester/index.js +10 -0
  109. package/dist/requester/index.js.map +1 -0
  110. package/dist/requester/requester.d.ts +23 -0
  111. package/dist/requester/requester.js +121 -0
  112. package/dist/requester/requester.js.map +1 -0
  113. package/dist/requester/types.d.ts +43 -0
  114. package/dist/requester/types.js +39 -0
  115. package/dist/requester/types.js.map +1 -0
  116. package/dist/storage/constants.d.ts +9 -0
  117. package/dist/storage/constants.js +14 -0
  118. package/dist/storage/constants.js.map +1 -0
  119. package/dist/storage/externalStorage.d.ts +11 -0
  120. package/dist/storage/externalStorage.js +34 -0
  121. package/dist/storage/externalStorage.js.map +1 -0
  122. package/dist/storage/index.d.ts +3 -0
  123. package/dist/storage/index.js +8 -0
  124. package/dist/storage/index.js.map +1 -0
  125. package/dist/storage/types.d.ts +52 -11
  126. package/dist/targeting/conditions/conditionFactory.d.ts +6 -0
  127. package/dist/targeting/conditions/conditionFactory.js +26 -0
  128. package/dist/targeting/conditions/conditionFactory.js.map +1 -0
  129. package/dist/targeting/conditions/customDataCondition.d.ts +16 -0
  130. package/dist/targeting/conditions/customDataCondition.js +99 -0
  131. package/dist/targeting/conditions/customDataCondition.js.map +1 -0
  132. package/dist/targeting/conditions/exclusiveExperimentCondition.d.ts +9 -0
  133. package/dist/targeting/conditions/exclusiveExperimentCondition.js +21 -0
  134. package/dist/targeting/conditions/exclusiveExperimentCondition.js.map +1 -0
  135. package/dist/targeting/conditions/index.d.ts +5 -0
  136. package/dist/targeting/conditions/index.js +12 -0
  137. package/dist/targeting/conditions/index.js.map +1 -0
  138. package/dist/targeting/conditions/targetExperimentCondition.d.ts +13 -0
  139. package/dist/targeting/conditions/targetExperimentCondition.js +42 -0
  140. package/dist/targeting/conditions/targetExperimentCondition.js.map +1 -0
  141. package/dist/targeting/conditions/types.d.ts +22 -11
  142. package/dist/targeting/index.d.ts +3 -0
  143. package/dist/targeting/index.js +15 -0
  144. package/dist/targeting/index.js.map +1 -0
  145. package/dist/targeting/targetingNode.d.ts +21 -0
  146. package/dist/targeting/targetingNode.js +40 -0
  147. package/dist/targeting/targetingNode.js.map +1 -0
  148. package/dist/targeting/targetingTree.d.ts +16 -8
  149. package/dist/targeting/targetingTree.js +112 -10
  150. package/dist/targeting/targetingTree.js.map +1 -1
  151. package/dist/targeting/types.d.ts +127 -0
  152. package/dist/targeting/types.js +93 -0
  153. package/dist/targeting/types.js.map +1 -0
  154. package/dist/types.d.ts +135 -5
  155. package/dist/types.js +28 -0
  156. package/dist/types.js.map +1 -1
  157. package/dist/utilities/constants.d.ts +1 -0
  158. package/dist/utilities/constants.js +5 -0
  159. package/dist/utilities/constants.js.map +1 -0
  160. package/dist/utilities/index.d.ts +1 -0
  161. package/dist/utilities/index.js +6 -0
  162. package/dist/utilities/index.js.map +1 -0
  163. package/dist/utilities/types.d.ts +18 -0
  164. package/dist/utilities/types.js +3 -0
  165. package/dist/utilities/types.js.map +1 -0
  166. package/dist/utilities/utilities.d.ts +13 -0
  167. package/dist/utilities/utilities.js +119 -0
  168. package/dist/utilities/utilities.js.map +1 -0
  169. package/dist/variationConfiguration/index.d.ts +2 -0
  170. package/dist/variationConfiguration/index.js +6 -0
  171. package/dist/variationConfiguration/index.js.map +1 -0
  172. package/dist/variationConfiguration/types.d.ts +25 -0
  173. package/dist/variationConfiguration/types.js +3 -0
  174. package/dist/variationConfiguration/types.js.map +1 -0
  175. package/dist/variationConfiguration/variation.d.ts +13 -0
  176. package/dist/variationConfiguration/variation.js +38 -0
  177. package/dist/variationConfiguration/variation.js.map +1 -0
  178. package/dist/variationConfiguration/variationConfiguration.d.ts +36 -0
  179. package/dist/variationConfiguration/variationConfiguration.js +248 -0
  180. package/dist/variationConfiguration/variationConfiguration.js.map +1 -0
  181. package/package.json +19 -9
  182. package/dist/configurations/configuration.d.ts +0 -15
  183. package/dist/configurations/configuration.js +0 -31
  184. package/dist/configurations/configuration.js.map +0 -1
  185. package/dist/configurations/experiment.d.ts +0 -13
  186. package/dist/configurations/experiment.js +0 -54
  187. package/dist/configurations/experiment.js.map +0 -1
  188. package/dist/configurations/featureFlag.d.ts +0 -23
  189. package/dist/configurations/featureFlag.js +0 -105
  190. package/dist/configurations/featureFlag.js.map +0 -1
  191. package/dist/configurations/variation.d.ts +0 -15
  192. package/dist/configurations/variation.js +0 -14
  193. package/dist/configurations/variation.js.map +0 -1
  194. package/dist/data.d.ts +0 -67
  195. package/dist/data.js +0 -127
  196. package/dist/data.js.map +0 -1
  197. package/dist/exceptions/credentialsNotFound.d.ts +0 -6
  198. package/dist/exceptions/credentialsNotFound.js +0 -29
  199. package/dist/exceptions/credentialsNotFound.js.map +0 -1
  200. package/dist/exceptions/experimentConfigurationNotFound.d.ts +0 -6
  201. package/dist/exceptions/experimentConfigurationNotFound.js +0 -29
  202. package/dist/exceptions/experimentConfigurationNotFound.js.map +0 -1
  203. package/dist/exceptions/featureConfigurationNotFound.d.ts +0 -6
  204. package/dist/exceptions/featureConfigurationNotFound.js +0 -29
  205. package/dist/exceptions/featureConfigurationNotFound.js.map +0 -1
  206. package/dist/exceptions/notActivated.d.ts +0 -6
  207. package/dist/exceptions/notActivated.js +0 -29
  208. package/dist/exceptions/notActivated.js.map +0 -1
  209. package/dist/exceptions/notTargeted.d.ts +0 -6
  210. package/dist/exceptions/notTargeted.js +0 -29
  211. package/dist/exceptions/notTargeted.js.map +0 -1
  212. package/dist/exceptions/siteCodeDisabled.d.ts +0 -5
  213. package/dist/exceptions/siteCodeDisabled.js +0 -30
  214. package/dist/exceptions/siteCodeDisabled.js.map +0 -1
  215. package/dist/exceptions/variationConfigurationNotFound.d.ts +0 -6
  216. package/dist/exceptions/variationConfigurationNotFound.js +0 -29
  217. package/dist/exceptions/variationConfigurationNotFound.js.map +0 -1
  218. package/dist/exceptions/visitorCodeNotValid.d.ts +0 -5
  219. package/dist/exceptions/visitorCodeNotValid.js +0 -30
  220. package/dist/exceptions/visitorCodeNotValid.js.map +0 -1
  221. package/dist/interfaces/interfaces.d.ts +0 -81
  222. package/dist/interfaces/interfaces.js.map +0 -1
  223. package/dist/nonce.d.ts +0 -1
  224. package/dist/nonce.js +0 -16
  225. package/dist/nonce.js.map +0 -1
  226. package/dist/realTimeConfigurationService.d.ts +0 -12
  227. package/dist/realTimeConfigurationService.js +0 -24
  228. package/dist/realTimeConfigurationService.js.map +0 -1
  229. package/dist/storage/variationStorage.d.ts +0 -11
  230. package/dist/storage/variationStorage.js +0 -159
  231. package/dist/storage/variationStorage.js.map +0 -1
  232. package/dist/storage/visitorVariation.d.ts +0 -9
  233. package/dist/storage/visitorVariation.js +0 -22
  234. package/dist/storage/visitorVariation.js.map +0 -1
  235. package/dist/targeting/conditions/customDatum.d.ts +0 -12
  236. package/dist/targeting/conditions/customDatum.js +0 -129
  237. package/dist/targeting/conditions/customDatum.js.map +0 -1
  238. package/dist/targeting/conditions/exclusiveExperiment.d.ts +0 -9
  239. package/dist/targeting/conditions/exclusiveExperiment.js +0 -45
  240. package/dist/targeting/conditions/exclusiveExperiment.js.map +0 -1
  241. package/dist/targeting/conditions/targetExperiment.d.ts +0 -14
  242. package/dist/targeting/conditions/targetExperiment.js +0 -57
  243. package/dist/targeting/conditions/targetExperiment.js.map +0 -1
  244. package/dist/targeting/targetingCondition.d.ts +0 -7
  245. package/dist/targeting/targetingCondition.js +0 -9
  246. package/dist/targeting/targetingCondition.js.map +0 -1
  247. package/dist/targeting/targetingConditionsFactory.d.ts +0 -7
  248. package/dist/targeting/targetingConditionsFactory.js +0 -30
  249. package/dist/targeting/targetingConditionsFactory.js.map +0 -1
  250. package/dist/targeting/targetingData.d.ts +0 -7
  251. package/dist/targeting/targetingData.js +0 -11
  252. package/dist/targeting/targetingData.js.map +0 -1
  253. package/dist/targeting/targetingEngine.d.ts +0 -6
  254. package/dist/targeting/targetingEngine.js +0 -130
  255. package/dist/targeting/targetingEngine.js.map +0 -1
  256. package/dist/targeting/targetingSegment.d.ts +0 -5
  257. package/dist/targeting/targetingSegment.js +0 -9
  258. package/dist/targeting/targetingSegment.js.map +0 -1
  259. package/dist/targeting/targetingTreeBuilder.d.ts +0 -8
  260. package/dist/targeting/targetingTreeBuilder.js +0 -127
  261. package/dist/targeting/targetingTreeBuilder.js.map +0 -1
@@ -1,23 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -27,700 +8,256 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
27
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
28
9
  });
29
10
  };
30
- var __generator = (this && this.__generator) || function (thisArg, body) {
31
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
32
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
33
- function verb(n) { return function (v) { return step([n, v]); }; }
34
- function step(op) {
35
- if (f) throw new TypeError("Generator is already executing.");
36
- while (_) try {
37
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
38
- if (y = 0, t) op = [op[0] & 2, t.value];
39
- switch (op[0]) {
40
- case 0: case 1: t = op; break;
41
- case 4: _.label++; return { value: op[1], done: false };
42
- case 5: _.label++; y = op[1]; op = [0]; continue;
43
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
44
- default:
45
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
46
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
47
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
48
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
49
- if (t[2]) _.ops.pop();
50
- _.trys.pop(); continue;
51
- }
52
- op = body.call(thisArg, _);
53
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
54
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
55
- }
56
- };
57
- var __importDefault = (this && this.__importDefault) || function (mod) {
58
- return (mod && mod.__esModule) ? mod : { "default": mod };
59
- };
60
11
  Object.defineProperty(exports, "__esModule", { value: true });
61
- exports.KameleoonException = exports.KameleoonClient = void 0;
62
- var async_storage_1 = __importDefault(require("@react-native-async-storage/async-storage"));
63
- var configuration_1 = __importDefault(require("./configurations/configuration"));
64
- var data_1 = __importDefault(require("./data"));
65
- var kameleoonUtils_1 = __importStar(require("./kameleoonUtils"));
66
- var targetingData_1 = __importDefault(require("./targeting/targetingData"));
67
- var experimentConfigurationNotFound_1 = __importDefault(require("./exceptions/experimentConfigurationNotFound"));
68
- var featureConfigurationNotFound_1 = __importDefault(require("./exceptions/featureConfigurationNotFound"));
69
- var notActivated_1 = __importDefault(require("./exceptions/notActivated"));
70
- var notTargeted_1 = __importDefault(require("./exceptions/notTargeted"));
71
- var variationConfigurationNotFound_1 = __importDefault(require("./exceptions/variationConfigurationNotFound"));
72
- var credentialsNotFound_1 = __importDefault(require("./exceptions/credentialsNotFound"));
73
- var visitorCodeNotValid_1 = require("./exceptions/visitorCodeNotValid");
74
- var siteCodeDisabled_1 = require("./exceptions/siteCodeDisabled");
75
- var nonce_1 = require("./nonce");
76
- var realTimeConfigurationService_1 = require("./realTimeConfigurationService");
77
- var variationStorage_1 = require("./storage/variationStorage");
78
- var constants_1 = require("./constants");
79
- var targetingEngine_1 = require("./targeting/targetingEngine");
80
- var KameleoonClient = /** @class */ (function () {
81
- function KameleoonClient(siteCode, configuration) {
82
- var _this = this;
83
- this.ready = false;
84
- this.readyCallbacks = [];
85
- this.minutesBetweenConfigurationUpdate = 30;
86
- this.targetingData = {};
87
- this.unsentData = {};
88
- this.visitorDataMaximumSize = 1;
89
- this.environment = null;
90
- this.realTimeConfigurationService = null;
91
- this.handlerUpdateConfiguration = null;
92
- this.variationStorage = new variationStorage_1.VariationStorage();
93
- this.updateCampaignConfiguration = function (timeStamp) { return __awaiter(_this, void 0, void 0, function () {
94
- var data, savedTargetingData, savedConfiguration, needUpdate, parsedConfiguration, siteCodeStringUrl, environmentStringUrl, timeStampStringUrl, configurationUrl;
95
- var _this = this;
96
- var _a;
97
- return __generator(this, function (_b) {
98
- switch (_b.label) {
99
- case 0:
100
- if (!(typeof document !== 'undefined')) return [3 /*break*/, 1];
101
- data = localStorage.getItem('kameleoonJavaScriptClientData');
102
- return [3 /*break*/, 3];
103
- case 1:
104
- if (!(typeof navigator !== 'undefined' &&
105
- navigator.product === 'ReactNative')) return [3 /*break*/, 3];
106
- return [4 /*yield*/, async_storage_1.default.getItem('kameleoonJavaScriptClientData')];
107
- case 2:
108
- data = _b.sent();
109
- _b.label = 3;
110
- case 3:
111
- if (data) {
112
- try {
113
- data = JSON.parse(data);
114
- }
115
- catch (e) {
116
- console.log(e);
117
- }
118
- }
119
- if (!data) {
120
- data = {
121
- kameleoonTargetingData: {},
122
- kameleoonConfiguration: {},
123
- };
124
- }
125
- savedTargetingData = data.kameleoonTargetingData;
126
- if (savedTargetingData) {
127
- try {
128
- this.targetingData = savedTargetingData;
129
- this.emptyTargetingData();
130
- }
131
- catch (e) {
132
- this.targetingData = {};
133
- }
134
- }
135
- savedConfiguration = data.kameleoonConfiguration;
136
- needUpdate = false;
137
- if (!savedConfiguration || timeStamp != null) {
138
- // timeStamp != null means update by SSE event and requires force update
139
- needUpdate = true;
140
- }
141
- else {
142
- try {
143
- parsedConfiguration = new configuration_1.default(this.siteCode, {
144
- configuration: savedConfiguration.configuration,
145
- experiments: savedConfiguration.experiments,
146
- featureFlags: savedConfiguration.featureFlags,
147
- updateTime: savedConfiguration.updateTime,
148
- });
149
- if (parsedConfiguration.updateTime <
150
- new Date().getTime() -
151
- this.minutesBetweenConfigurationUpdate * 1000 * 60 || //refresh by polling interval
152
- parsedConfiguration.settings.realTimeUpdate) {
153
- //streaming initial refresh, need to update to recent version of configuration
154
- needUpdate = true;
155
- }
156
- else {
157
- this.configurations = parsedConfiguration;
158
- }
159
- }
160
- catch (e) {
161
- needUpdate = true;
162
- }
163
- }
164
- if (needUpdate) {
165
- siteCodeStringUrl = "mobile?siteCode=".concat(this.siteCode);
166
- environmentStringUrl = ((_a = this.environment) === null || _a === void 0 ? void 0 : _a.length)
167
- ? "&environment=".concat(this.environment)
168
- : '';
169
- timeStampStringUrl = timeStamp ? "&ts=".concat(timeStamp) : '';
170
- configurationUrl = constants_1.CONFIGURATION_URL +
171
- siteCodeStringUrl +
172
- environmentStringUrl +
173
- timeStampStringUrl;
174
- kameleoonUtils_1.default.doRequest(configurationUrl, kameleoonUtils_1.HttpMethod.GET, null, function (response) { return __awaiter(_this, void 0, void 0, function () {
175
- return __generator(this, function (_a) {
176
- try {
177
- this.configurations = new configuration_1.default(this.siteCode, JSON.parse(response));
178
- this.ready = true;
179
- this.clearCallbacks(true);
180
- this.saveConfiguration();
181
- this.fireUpdateConfigurationHandler(Boolean(timeStamp)); //fire handler after configuration was updated with SSE event
182
- this.manageConfiguraitonUpdate(this.configurations.settings.realTimeUpdate);
183
- }
184
- catch (e) {
185
- console.log(e);
186
- }
187
- return [2 /*return*/];
188
- });
189
- }); }, false);
190
- }
191
- else {
192
- this.ready = true;
193
- this.clearCallbacks(true);
194
- }
195
- return [2 /*return*/];
196
- }
197
- });
198
- }); };
199
- this.runWhenReady = function (successCallback, errorCallback, timeout) {
200
- if (_this.ready) {
201
- successCallback && successCallback();
202
- }
203
- else {
204
- _this.readyCallbacks.push({
205
- error: errorCallback,
206
- success: successCallback,
207
- timeout: setTimeout(function () {
208
- _this.clearCallbacks(false, errorCallback);
209
- }, timeout),
210
- });
211
- }
212
- };
213
- this.clearCallbacks = function (success, errorCallback) {
214
- var callbacksToKeep = [];
215
- _this.readyCallbacks.forEach(function (c) {
216
- if (success) {
217
- c.success && c.success();
218
- clearTimeout(c.timeout);
219
- }
220
- else {
221
- if (!success && errorCallback == c.error) {
222
- c.error && c.error();
223
- }
224
- else {
225
- callbacksToKeep.push(c);
226
- }
227
- }
228
- });
229
- _this.readyCallbacks = callbacksToKeep;
230
- };
231
- this.trackConversion = function (visitorCode, goalId, revenue) {
232
- _this.validateVisitorCode(visitorCode);
233
- _this.addData(visitorCode, new data_1.default.Conversion(goalId, revenue, false));
234
- _this.flush(visitorCode);
235
- };
236
- this.flush = function (visitorCode) {
237
- if (visitorCode != null) {
238
- var body = _this.getAndClearUnsentData(visitorCode);
239
- kameleoonUtils_1.default.doRequest(constants_1.API_SSX_URL + _this.getDataTrackingURL(visitorCode), kameleoonUtils_1.HttpMethod.POST, body, null);
240
- }
241
- else {
242
- _this.getUnsentUsers().forEach(function (vc) { return _this.flush(vc); });
243
- }
244
- };
245
- this.triggerExperiment = function (visitorCode, experimentId) {
246
- var _a;
247
- _this.validateVisitorCode(visitorCode);
248
- var xpConf = (_a = _this.configurations) === null || _a === void 0 ? void 0 : _a.experiments[experimentId];
249
- _this.checkSiteEnabled(xpConf);
250
- if (_this.checkTargeting(visitorCode, experimentId, false)) {
251
- var variationId = 0;
252
- var noneVariation = true;
253
- var savedVariationId = _this.variationStorage.getVariationId(visitorCode, experimentId);
254
- if (savedVariationId) {
255
- variationId = savedVariationId;
256
- noneVariation = false;
257
- }
258
- else {
259
- if (xpConf) {
260
- var hashDouble = _this.obtainHashDouble(experimentId, visitorCode, xpConf);
261
- var total = 0.0;
262
- for (var key in xpConf.variationConfigurations) {
263
- var variationConfiguration = xpConf.variationConfigurations[key];
264
- total += variationConfiguration.deviation;
265
- if (total >= hashDouble) {
266
- noneVariation = false;
267
- variationId = parseInt(key);
268
- _this.variationStorage.updateVariation(visitorCode, experimentId, variationConfiguration);
269
- break;
270
- }
271
- }
272
- var body = _this.getAndClearUnsentData(visitorCode);
273
- kameleoonUtils_1.default.doRequest(constants_1.API_SSX_URL +
274
- _this.getExperimentRegisterURL(visitorCode, experimentId, variationId, noneVariation), kameleoonUtils_1.HttpMethod.POST, body);
275
- }
276
- else {
277
- throw new experimentConfigurationNotFound_1.default('KameleoonException.ExperimentConfigurationNotFound');
278
- }
279
- }
280
- if (noneVariation) {
281
- throw new notActivated_1.default('KameleoonException.NotActivated');
282
- }
283
- return variationId;
284
- }
285
- else {
286
- throw new notTargeted_1.default('KameleoonException.NotTargeted');
287
- }
288
- };
289
- this.activateFeature = function (visitorCode, featureIdOrName) {
290
- var _a, _b;
291
- _this.checkFeatureKeyOrId(featureIdOrName);
292
- _this.validateVisitorCode(visitorCode);
293
- var featureFlag = null;
294
- var featureId = -1;
295
- for (var key in (_a = _this.configurations) === null || _a === void 0 ? void 0 : _a.featureFlags) {
296
- var ff = (_b = _this.configurations) === null || _b === void 0 ? void 0 : _b.featureFlags[key];
297
- if (key == featureIdOrName || (ff === null || ff === void 0 ? void 0 : ff.identificationKey) == featureIdOrName) {
298
- featureId = parseInt(key);
299
- featureFlag = ff;
300
- }
301
- }
302
- _this.checkSiteEnabled(featureFlag);
303
- if (_this.checkTargeting(visitorCode, featureId, true)) {
304
- if (featureFlag && featureId) {
305
- var result = false;
306
- if (featureFlag.isScheduleActive()) {
307
- var hashDouble = _this.obtainHashDouble(featureId, visitorCode, featureFlag);
308
- var total = 0.0;
309
- var variationId = 0;
310
- var noneVariation = true;
311
- for (var key in featureFlag.variationConfigurations) {
312
- var variationConfiguration = featureFlag.variationConfigurations[key];
313
- total += variationConfiguration.deviation;
314
- if (total >= hashDouble) {
315
- noneVariation = false;
316
- variationId = parseInt(key);
317
- break;
318
- }
319
- }
320
- var body = _this.getAndClearUnsentData(visitorCode);
321
- kameleoonUtils_1.default.doRequest(constants_1.API_SSX_URL +
322
- _this.getExperimentRegisterURL(visitorCode, featureId, variationId, noneVariation), kameleoonUtils_1.HttpMethod.POST, body);
323
- result = variationId != 0;
324
- }
325
- return result;
326
- }
327
- else {
328
- throw new featureConfigurationNotFound_1.default('KameleoonException.FeatureConfigurationNotFound');
329
- }
330
- }
331
- else {
332
- throw new notTargeted_1.default('KameleoonException.NotTargeted');
333
- }
334
- };
335
- this.generateRandomString = function () {
336
- var alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789';
337
- var alphabetLength = alphabet.length;
338
- var randomVisitorCode = '';
339
- for (var i = 0; i < 16; ++i) {
340
- var randomNumber = Math.floor(Math.random() * alphabetLength);
341
- randomVisitorCode += alphabet[randomNumber];
342
- }
343
- return randomVisitorCode;
344
- };
345
- this.obtainAsyncVisitorCode = function (domain, defaultVisitorCode) { return __awaiter(_this, void 0, void 0, function () {
346
- var savedVisitorCode;
347
- return __generator(this, function (_a) {
348
- switch (_a.label) {
349
- case 0:
350
- savedVisitorCode = null;
351
- return [4 /*yield*/, async_storage_1.default.getItem('kameleoonVisitorCode')];
352
- case 1:
353
- savedVisitorCode = _a.sent();
354
- if (!(savedVisitorCode == null)) return [3 /*break*/, 3];
355
- savedVisitorCode = defaultVisitorCode || this.generateRandomString();
356
- return [4 /*yield*/, async_storage_1.default.setItem('kameleoonVisitorCode', savedVisitorCode)];
357
- case 2:
358
- _a.sent();
359
- _a.label = 3;
360
- case 3: return [2 /*return*/, savedVisitorCode];
361
- }
362
- });
363
- }); };
364
- this.obtainVisitorCode = function (domain, defaultVisitorCode) {
365
- var value;
366
- var name = 'kameleoonVisitorCode';
367
- var cookies = document.cookie.split(';');
368
- for (var i = 0, cookiesLength = cookies.length; i < cookiesLength; ++i) {
369
- var cookie = cookies[i];
370
- var valuePos = cookie.indexOf('=');
371
- if (cookie.substring(0, valuePos).trim() == name) {
372
- value = cookie.substring(valuePos + 1);
373
- break;
374
- }
375
- }
376
- if (value) {
377
- return value;
378
- }
379
- value = defaultVisitorCode || _this.generateRandomString();
380
- document.cookie =
381
- 'kameleoonVisitorCode=' +
382
- value +
383
- '; Max-Age=32832000; Path=/; Domain=' +
384
- domain;
385
- return value;
386
- };
387
- this.obtainHashDouble = function (containerId, visitorCode, xpConf) {
388
- var joinedRespoolTime = '';
389
- for (var variationId in xpConf.variationConfigurations) {
390
- var variation = xpConf.variationConfigurations[variationId];
391
- if (variation.respoolTime && variation.respoolTime != 0) {
392
- joinedRespoolTime += variation.respoolTime;
393
- }
394
- }
395
- var identifier = visitorCode + containerId + joinedRespoolTime;
396
- var buff = kameleoonUtils_1.default.computeStringSha256(identifier);
397
- return parseInt(buff, 16) / Math.pow(2, 256);
398
- };
399
- this.getCommonSSXParameters = function (visitorCode) {
400
- return ('&nonce=' +
401
- (0, nonce_1.obtainNonce)() +
402
- '&siteCode=' +
403
- _this.siteCode +
404
- '&visitorCode=' +
405
- visitorCode);
406
- };
407
- this.getDataTrackingURL = function (visitorCode) {
408
- return 'dataTracking?' + _this.getCommonSSXParameters(visitorCode);
409
- };
410
- this.getAPIDataRequestURL = function (key) {
411
- return "".concat(constants_1.API_DATA_URL, "data?siteCode=").concat(_this.siteCode, "&key=").concat(key);
412
- };
413
- this.getEventsRequestURL = function (siteCode) {
414
- return "".concat(constants_1.EVENTS_URL, "sse?siteCode=").concat(siteCode);
415
- };
416
- this.getExperimentRegisterURL = function (visitorCode, experimentId, variationId, noneVariation) {
417
- var baseURL = 'experimentTracking?' +
418
- _this.getCommonSSXParameters(visitorCode) +
419
- '&experimentId=' +
420
- experimentId;
421
- if (variationId == null) {
422
- return baseURL;
423
- }
424
- else {
425
- return (baseURL +
426
- '&variationId=' +
427
- variationId +
428
- (noneVariation ? '&noneVariation=true' : ''));
429
- }
430
- };
431
- this.getUnsentUsers = function () {
432
- return Object.getOwnPropertyNames(_this.unsentData);
433
- };
434
- this.getUnsentData = function (visitorCode) {
435
- if (!_this.unsentData[visitorCode]) {
436
- _this.unsentData[visitorCode] = [];
437
- }
438
- return _this.unsentData[visitorCode];
439
- };
440
- this.getTargetingData = function (visitorCode) {
441
- if (_this.targetingData[visitorCode] == null) {
442
- _this.targetingData[visitorCode] = [];
443
- }
444
- return _this.targetingData[visitorCode];
445
- };
446
- this.emptyUnsentData = function (visitorCode) {
447
- delete _this.unsentData[visitorCode];
448
- };
449
- this.emptyTargetingData = function () {
450
- var currentTime = new Date().getTime();
451
- for (var visitorCode in _this.targetingData) {
452
- _this.targetingData[visitorCode] = _this.targetingData[visitorCode].reduce(function (arr, data) {
453
- if (data.lastActivityTime + constants_1.MINUTES_SESSION_DURATION > currentTime) {
454
- arr.push(data);
455
- }
456
- return arr;
457
- }, []);
458
- }
459
- _this.saveConfiguration();
460
- };
461
- this.addData = function (visitorCode) {
462
- var data = [];
463
- for (var _i = 1; _i < arguments.length; _i++) {
464
- data[_i - 1] = arguments[_i];
465
- }
466
- return __awaiter(_this, void 0, void 0, function () {
467
- var listData, targetingDataList;
468
- var _this = this;
469
- return __generator(this, function (_a) {
470
- this.validateVisitorCode(visitorCode);
471
- listData = this.getUnsentData(visitorCode);
472
- targetingDataList = this.getTargetingData(visitorCode);
473
- data.forEach(function (d) { return __awaiter(_this, void 0, void 0, function () {
474
- return __generator(this, function (_a) {
475
- switch (_a.label) {
476
- case 0:
477
- listData.push(d);
478
- if (targetingDataList.length < this.targetingDataNumberEntriesAuthorized) {
479
- targetingDataList.push(new targetingData_1.default(new Date().getTime(), d));
480
- }
481
- else {
482
- targetingDataList.splice(0, 1);
483
- targetingDataList.push(new targetingData_1.default(new Date().getTime(), d));
484
- }
485
- return [4 /*yield*/, this.saveConfiguration()];
486
- case 1:
487
- _a.sent();
488
- return [2 /*return*/];
489
- }
490
- });
491
- }); });
492
- return [2 /*return*/];
493
- });
494
- });
495
- };
496
- // Targeting Engine
497
- // check targeting
498
- this.checkTargeting = function (visitorCode, campaignId, isFF) {
499
- var _a, _b;
500
- var targeting = true;
501
- var xpConf = null;
502
- // performing targeting
503
- if (isFF) {
504
- xpConf = (_a = _this.configurations) === null || _a === void 0 ? void 0 : _a.featureFlags[campaignId];
505
- }
506
- else {
507
- xpConf = (_b = _this.configurations) === null || _b === void 0 ? void 0 : _b.experiments[campaignId];
508
- }
509
- if (xpConf != null) {
510
- if (true == xpConf.forceTargeting) {
511
- // assigning targeting to container
512
- targeting = true;
513
- }
514
- else {
515
- var targetingTree = void 0;
516
- if (null != xpConf.targetingSegment) {
517
- targetingTree = xpConf.targetingSegment.targetingTree;
518
- }
519
- // obtaining targeting checking result and assigning targeting to container
520
- targeting = targetingEngine_1.TargetingEngine.checkTargetingTree(targetingTree, function (type) {
521
- return _this.getConditionData(type, visitorCode, campaignId);
522
- });
523
- }
524
- }
525
- return targeting;
526
- };
527
- this.getConditionData = function (type, visitorCode, campaignId) {
528
- switch (type) {
529
- case constants_1.ConditionTypes.CUSTOM_DATUM:
530
- return {
531
- targetingData: _this.getTargetingData(visitorCode),
532
- };
533
- case constants_1.ConditionTypes.EXCLUSIVE_EXPERIMENT:
534
- return {
535
- currentExperimentId: campaignId,
536
- variationStorage: _this.variationStorage.getMapVariationId(visitorCode),
537
- };
538
- case constants_1.ConditionTypes.TARGET_EXPERIMENT:
539
- return {
540
- variationStorage: _this.variationStorage.getMapVariationId(visitorCode),
541
- };
542
- default:
543
- return null;
544
- }
545
- };
546
- this.obtainVariationAssociatedData = function (variationId) {
547
- var _a, _b, _c;
548
- for (var vid in (_a = _this.configurations) === null || _a === void 0 ? void 0 : _a.experiments) {
549
- var experiment = (_b = _this.configurations) === null || _b === void 0 ? void 0 : _b.experiments[vid];
550
- if (experiment === null || experiment === void 0 ? void 0 : experiment.variationConfigurations[variationId]) {
551
- return ((_c = experiment === null || experiment === void 0 ? void 0 : experiment.variationConfigurations[variationId].customJson) !== null && _c !== void 0 ? _c : {});
552
- }
553
- }
554
- throw new variationConfigurationNotFound_1.default('KameleoonException.VariationConfigurationNotFound');
555
- };
556
- this.obtainFeatureVariable = function (featureIdOrName, variable) {
557
- var _a, _b, _c, _d;
558
- _this.checkFeatureKeyOrId(featureIdOrName);
559
- for (var ffid in (_a = _this.configurations) === null || _a === void 0 ? void 0 : _a.featureFlags) {
560
- var ff = (_b = _this.configurations) === null || _b === void 0 ? void 0 : _b.featureFlags[ffid];
561
- if (ffid == featureIdOrName ||
562
- (ff === null || ff === void 0 ? void 0 : ff.identificationKey) === featureIdOrName) {
563
- for (var vid in ff === null || ff === void 0 ? void 0 : ff.variationConfigurations) {
564
- if (vid !== '0') {
565
- return _this._parseJsonValue((_d = (_c = ff === null || ff === void 0 ? void 0 : ff.variationConfigurations[vid]) === null || _c === void 0 ? void 0 : _c.customJson) === null || _d === void 0 ? void 0 : _d[variable]);
566
- }
567
- }
568
- }
569
- }
570
- throw new featureConfigurationNotFound_1.default('KameleoonExceptinpmon.FeatureConfigurationNotFound');
571
- };
572
- this.retrieveDataFromRemoteSource = function (key) { return __awaiter(_this, void 0, void 0, function () {
573
- var _this = this;
574
- return __generator(this, function (_a) {
575
- return [2 /*return*/, new Promise(function (resolve) {
576
- kameleoonUtils_1.default.doRequest(_this.getAPIDataRequestURL(key), kameleoonUtils_1.HttpMethod.GET, null, function (response) {
577
- resolve(JSON.parse(response));
578
- });
579
- })];
12
+ exports.KameleoonClient = void 0;
13
+ const ts_res_1 = require("ts-res");
14
+ const campaignConfiguration_1 = require("./campaignConfiguration");
15
+ const clientSettings_1 = require("./clientSettings");
16
+ const constants_1 = require("./constants");
17
+ const kameleoonData_1 = require("./kameleoonData");
18
+ const kameleoonError_1 = require("./kameleoonError");
19
+ const requester_1 = require("./requester");
20
+ const storage_1 = require("./storage");
21
+ const externalStorage_1 = require("./storage/externalStorage");
22
+ const utilities_1 = require("./utilities");
23
+ const variationConfiguration_1 = require("./variationConfiguration");
24
+ /**
25
+ * @class
26
+ * KameleoonClient - a class for creating kameleoon client instance
27
+ * for communicating with Kameleoon JavaScript SDK Core, when new instance is created
28
+ * */
29
+ class KameleoonClient {
30
+ /**
31
+ * @param {SDKCoreParameters} sdkCoreParameters - parameters for initializing sdk core
32
+ */
33
+ constructor({ siteCode, configuration, internalConfiguration, }) {
34
+ const { settings: clientSettings } = new clientSettings_1.ClientSettings(siteCode, configuration);
35
+ const requester = new requester_1.Requester(siteCode, clientSettings.environment);
36
+ this.requester = requester;
37
+ this.settings = clientSettings;
38
+ this.internalConfiguration = internalConfiguration;
39
+ }
40
+ initialize() {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ const { externalStorage, externalEventSource } = this.internalConfiguration;
43
+ const clientDataStorage = new externalStorage_1.ExternalStorage(externalStorage.initialize(storage_1.KameleoonStorageKey.ClientData));
44
+ const variationDataStorage = new externalStorage_1.ExternalStorage(externalStorage.initialize(storage_1.KameleoonStorageKey.VariationData));
45
+ const campaignConfiguration = new campaignConfiguration_1.CampaignConfiguration({
46
+ settings: this.settings,
47
+ storage: clientDataStorage,
48
+ requester: this.requester,
580
49
  });
581
- }); };
582
- this._parseJsonValue = function (obj) {
583
- if (obj == null) {
584
- throw new featureConfigurationNotFound_1.default('KameleoonException.FeatureConfigurationNotFound');
585
- }
586
- else {
587
- switch (obj.type) {
588
- case 'Number':
589
- return parseInt(obj.value);
590
- case 'JSON':
591
- return JSON.parse(obj.value);
592
- default:
593
- return obj.value;
594
- }
595
- }
50
+ const result = yield campaignConfiguration.initialize(externalEventSource);
51
+ result.throw();
52
+ const variationConfiguration = new variationConfiguration_1.VariationConfiguration(campaignConfiguration.experiments, variationDataStorage);
53
+ this.variationConfiguration = variationConfiguration;
54
+ this.campaignConfiguration = campaignConfiguration;
55
+ return result.ok;
56
+ });
57
+ }
58
+ addData(visitorCode, ...data) {
59
+ utilities_1.Utilities.validateVisitorCode(visitorCode).throw();
60
+ if (!this.campaignConfiguration) {
61
+ throw new kameleoonError_1.KameleoonError(kameleoonError_1.KameleoonException.Initialization);
62
+ }
63
+ this.campaignConfiguration.addTargetingData(visitorCode, ...data).throw();
64
+ }
65
+ triggerExperiment(visitorCode, experimentId) {
66
+ utilities_1.Utilities.validateVisitorCode(visitorCode).throw();
67
+ if (!this.campaignConfiguration || !this.variationConfiguration) {
68
+ throw new kameleoonError_1.KameleoonError(kameleoonError_1.KameleoonException.Initialization);
69
+ }
70
+ const experiment = this.campaignConfiguration.experiments.find((item) => item.id === String(experimentId));
71
+ if (!experiment) {
72
+ throw new kameleoonError_1.KameleoonError(kameleoonError_1.KameleoonException.ExperimentConfigurationNotFound, experimentId, visitorCode);
73
+ }
74
+ const targetingData = this.campaignConfiguration.storedTargetingData[visitorCode];
75
+ const targetingResult = utilities_1.Utilities.checkTargeting({
76
+ visitorCode,
77
+ experimentId,
78
+ targetingData,
79
+ segment: experiment.segment,
80
+ variationConfiguration: this.variationConfiguration,
81
+ }).throw();
82
+ if (!targetingResult) {
83
+ throw new kameleoonError_1.KameleoonError(kameleoonError_1.KameleoonException.NotTargeted, visitorCode);
84
+ }
85
+ const dataUrl = utilities_1.Utilities.getDataUrl(visitorCode, this.campaignConfiguration);
86
+ const trackingParameters = {
87
+ body: dataUrl,
88
+ visitorCode,
89
+ experimentId,
596
90
  };
597
- this.saveConfiguration = function () { return __awaiter(_this, void 0, void 0, function () {
598
- var data, e_1;
599
- var _a, _b;
600
- return __generator(this, function (_c) {
601
- switch (_c.label) {
602
- case 0:
603
- if (!this.configurations) return [3 /*break*/, 6];
604
- data = {
605
- kameleoonTargetingData: this.targetingData,
606
- kameleoonConfiguration: {
607
- updateTime: this.configurations.updateTime,
608
- experiments: (_a = this.configurations.configuration) === null || _a === void 0 ? void 0 : _a.experiments,
609
- featureFlags: (_b = this.configurations.configuration) === null || _b === void 0 ? void 0 : _b.featureFlags,
610
- configuration: this.configurations.settings,
611
- },
612
- };
613
- _c.label = 1;
614
- case 1:
615
- _c.trys.push([1, 5, , 6]);
616
- if (!(typeof document !== 'undefined')) return [3 /*break*/, 2];
617
- localStorage.setItem('kameleoonJavaScriptClientData', JSON.stringify(data));
618
- return [3 /*break*/, 4];
619
- case 2:
620
- if (!(typeof navigator !== 'undefined' &&
621
- navigator.product === 'ReactNative')) return [3 /*break*/, 4];
622
- return [4 /*yield*/, async_storage_1.default.setItem('kameleoonJavaScriptClientData', JSON.stringify(data))];
623
- case 3:
624
- _c.sent();
625
- _c.label = 4;
626
- case 4: return [3 /*break*/, 6];
627
- case 5:
628
- e_1 = _c.sent();
629
- console.log(e_1);
630
- return [3 /*break*/, 6];
631
- case 6: return [2 /*return*/];
91
+ const userAgent = utilities_1.Utilities.getUserAgent(visitorCode, this.campaignConfiguration);
92
+ const variationId = this.variationConfiguration
93
+ .getAssociatedVariationId(visitorCode, experimentId)
94
+ .else((error) => {
95
+ if (error.type === kameleoonError_1.KameleoonException.NotAllocated) {
96
+ this.requester.trackExperiment(Object.assign({ variationId: constants_1.UNALLOCATED_VARIATION_ID, isUnallocated: true, userAgent }, trackingParameters));
97
+ return constants_1.UNALLOCATED_VARIATION_ID;
98
+ }
99
+ throw error;
100
+ });
101
+ this.requester.trackExperiment(Object.assign({ variationId, isUnallocated: false, userAgent }, trackingParameters));
102
+ return variationId;
103
+ }
104
+ trackConversion({ visitorCode, goalId, revenue, }) {
105
+ this.addData(visitorCode, new kameleoonData_1.Conversion({ goalId, revenue }));
106
+ this.flushData(visitorCode);
107
+ }
108
+ flushData(visitorCode) {
109
+ if (!this.campaignConfiguration) {
110
+ throw new kameleoonError_1.KameleoonError(kameleoonError_1.KameleoonException.Initialization);
111
+ }
112
+ const dataUrl = utilities_1.Utilities.getDataUrl(visitorCode, this.campaignConfiguration);
113
+ const userAgent = utilities_1.Utilities.getUserAgent(visitorCode, this.campaignConfiguration);
114
+ this.requester.trackData({ visitorCode, body: dataUrl, userAgent });
115
+ }
116
+ getExperiments() {
117
+ if (!this.campaignConfiguration) {
118
+ throw new kameleoonError_1.KameleoonError(kameleoonError_1.KameleoonException.Initialization);
119
+ }
120
+ return this.campaignConfiguration.experiments.map(({ id, name }) => ({
121
+ id: Number(id),
122
+ name,
123
+ }));
124
+ }
125
+ getVisitorExperiments(visitorCode, isActive = true) {
126
+ if (!this.variationConfiguration || !this.campaignConfiguration) {
127
+ throw new kameleoonError_1.KameleoonError(kameleoonError_1.KameleoonException.Initialization);
128
+ }
129
+ utilities_1.Utilities.validateVisitorCode(visitorCode).throw();
130
+ const experiments = this.campaignConfiguration.experiments;
131
+ const result = [];
132
+ const targetingData = this.campaignConfiguration.storedTargetingData[visitorCode];
133
+ for (const experiment of experiments) {
134
+ const { id, name, segment } = experiment;
135
+ const targetingResult = utilities_1.Utilities.checkTargeting({
136
+ targetingData,
137
+ visitorCode,
138
+ segment,
139
+ variationConfiguration: this.variationConfiguration,
140
+ experimentId: Number(id),
141
+ }).throw();
142
+ if (!targetingResult) {
143
+ continue;
144
+ }
145
+ // -- If `isActive=false` then return every experiment
146
+ if (!isActive) {
147
+ result.push({ id: Number(id), name });
148
+ continue;
149
+ }
150
+ // -- Else first check if there are saved variations for the experiment
151
+ // if there is at least one than it's active
152
+ const experimentVariations = this.variationConfiguration.getAssignedVariations(visitorCode);
153
+ if (experimentVariations.ok) {
154
+ const experimentVariation = experimentVariations.data[experiment.id];
155
+ if (experimentVariation) {
156
+ result.push({ id: Number(id), name });
157
+ continue;
632
158
  }
633
- });
634
- }); };
635
- this.validateVisitorCode = function (visitorCode) {
636
- if (visitorCode == null || visitorCode.length == 0) {
637
- throw new visitorCodeNotValid_1.VisitorCodeNotValid('Visitor code is empty');
638
159
  }
639
- else if (visitorCode.length > KameleoonClient.VISITOR_CODE_MAX_LENGTH) {
640
- throw new visitorCodeNotValid_1.VisitorCodeNotValid('Visitor code max length is ' +
641
- KameleoonClient.VISITOR_CODE_MAX_LENGTH +
642
- ' characters');
643
- }
644
- };
645
- this.onUpdateConfiguration = function (handler) {
646
- _this.handlerUpdateConfiguration = handler;
647
- };
648
- this.siteCode = siteCode;
649
- if (configuration) {
650
- if (configuration.visitor_data_maximum_size) {
651
- this.visitorDataMaximumSize = configuration.visitor_data_maximum_size;
652
- }
653
- if (configuration.actions_configuration_refresh_interval) {
654
- this.minutesBetweenConfigurationUpdate =
655
- configuration.actions_configuration_refresh_interval;
656
- }
657
- if (configuration.environment) {
658
- this.environment = configuration.environment;
160
+ // -- Second check if the experiment will have an allocated variation upon triggering
161
+ // if yes than it's active
162
+ const variationResult = this.variationConfiguration.getExperimentVariation(visitorCode, experiment);
163
+ if (variationResult.ok) {
164
+ result.push({ id: Number(id), name });
659
165
  }
660
166
  }
661
- this.targetingDataNumberEntriesAuthorized =
662
- (this.visitorDataMaximumSize * 1000000) /
663
- constants_1.TARGETING_DATA_MAXIMUM_SIZE_BYTES;
664
- this.updateCampaignConfiguration();
665
- setInterval(this.emptyTargetingData, constants_1.DEFAULT_MILLISECONDS_BETWEEN_TARGETING_DATA_UPDATE);
167
+ return result;
168
+ }
169
+ getFeatureFlags() {
170
+ if (!this.campaignConfiguration) {
171
+ throw new kameleoonError_1.KameleoonError(kameleoonError_1.KameleoonException.Initialization);
172
+ }
173
+ return this.campaignConfiguration.featureFlags.map(({ id, featureKey }) => ({ id, key: featureKey }));
666
174
  }
667
- KameleoonClient.prototype.checkSiteEnabled = function (experimentOrFeatureFlag) {
668
- if (experimentOrFeatureFlag != null &&
669
- !experimentOrFeatureFlag.siteEnabled) {
670
- throw new siteCodeDisabled_1.SiteCodeDisabled(this.siteCode);
175
+ getVisitorFeatureFlags(visitorCode) {
176
+ if (!this.variationConfiguration || !this.campaignConfiguration) {
177
+ throw new kameleoonError_1.KameleoonError(kameleoonError_1.KameleoonException.Initialization);
671
178
  }
672
- };
673
- KameleoonClient.prototype.checkFeatureKeyOrId = function (featureIdOrName) {
674
- if (typeof featureIdOrName === 'string') {
675
- console.log('Please use `featureIdOrName` with type of `string`. This is necessary to support multi-environment feature. Supporting of `number` type will be removed in next releases');
179
+ const featureFlags = this.campaignConfiguration.featureFlags;
180
+ const resultFeatureFlags = [];
181
+ for (const featureFlag of featureFlags) {
182
+ const variationData = this.variationConfiguration
183
+ .getFeatureFlagVariation({
184
+ visitorCode,
185
+ featureFlag,
186
+ })
187
+ .throw();
188
+ if (variationData.variationKey !== constants_1.OFF_VARIATION_KEY) {
189
+ const { featureFlagId, featureKey } = variationData;
190
+ resultFeatureFlags.push({ id: featureFlagId, key: featureKey });
191
+ }
192
+ }
193
+ return resultFeatureFlags;
194
+ }
195
+ getFeatureFlagVariationKey(visitorCode, featureKey) {
196
+ const { variationKey } = this._getFeatureFlagVariation(visitorCode, featureKey).throw();
197
+ return variationKey;
198
+ }
199
+ getFeatureFlagVariable({ visitorCode, featureKey, variableKey, }) {
200
+ var _a, _b;
201
+ const { variationKey } = this._getFeatureFlagVariation(visitorCode, featureKey).throw();
202
+ // --- Note ---
203
+ // `_getFeatureFlagVariation` already checks `campaignConfiguration` initialization
204
+ const featureFlags = this.campaignConfiguration
205
+ .featureFlags;
206
+ const variable = (_b = (_a = featureFlags
207
+ .find((item) => item.featureKey === featureKey)) === null || _a === void 0 ? void 0 : _a.variations.find((item) => item.key === variationKey)) === null || _b === void 0 ? void 0 : _b.variables.find((item) => item.key === variableKey);
208
+ if (!variable) {
209
+ throw new kameleoonError_1.KameleoonError(kameleoonError_1.KameleoonException.FeatureFlagConfigurationNotFound, featureKey, visitorCode);
676
210
  }
677
- };
678
- KameleoonClient.prototype.getAndClearUnsentData = function (visitorCode) {
679
- var body = '';
680
- this.getUnsentData(visitorCode).forEach(function (element) {
681
- body += element.obtainFullPostTextLine() + '\n';
211
+ const featureVariable = utilities_1.Utilities.parseFeatureVariable(variable).throw();
212
+ return featureVariable;
213
+ }
214
+ isFeatureFlagActive(visitorCode, featureKey) {
215
+ const variationKey = this.getFeatureFlagVariationKey(visitorCode, featureKey);
216
+ return variationKey !== constants_1.OFF_VARIATION_KEY;
217
+ }
218
+ getRemoteData(key) {
219
+ return __awaiter(this, void 0, void 0, function* () {
220
+ const remoteData = yield this.requester.getRemoteData(key);
221
+ return remoteData.throw();
682
222
  });
683
- this.emptyUnsentData(visitorCode);
684
- return body;
685
- };
686
- KameleoonClient.prototype.manageConfiguraitonUpdate = function (subsciptionRequired) {
687
- var _this = this;
688
- if (subsciptionRequired && !this.realTimeConfigurationService) {
689
- this.realTimeConfigurationService = new realTimeConfigurationService_1.RealTimeConfigurationService(this.getEventsRequestURL(this.siteCode), function (event) { return __awaiter(_this, void 0, void 0, function () {
690
- return __generator(this, function (_a) {
691
- switch (_a.label) {
692
- case 0: return [4 /*yield*/, this.updateCampaignConfiguration(event.ts)];
693
- case 1:
694
- _a.sent();
695
- return [2 /*return*/];
696
- }
697
- });
698
- }); });
223
+ }
224
+ onConfigurationUpdate(callback) {
225
+ if (!this.campaignConfiguration) {
226
+ throw new kameleoonError_1.KameleoonError(kameleoonError_1.KameleoonException.Initialization);
699
227
  }
700
- else if (!subsciptionRequired) {
701
- if (this.realTimeConfigurationService) {
702
- this.realTimeConfigurationService.disconnect();
703
- this.realTimeConfigurationService = null;
704
- }
705
- setTimeout(this.updateCampaignConfiguration, this.minutesBetweenConfigurationUpdate * constants_1.Milliseconds.Minute);
228
+ this.campaignConfiguration.onConfigurationUpdate(callback);
229
+ }
230
+ _getFeatureFlagVariation(visitorCode, featureKey) {
231
+ var _a;
232
+ if (!this.variationConfiguration || !this.campaignConfiguration) {
233
+ return (0, ts_res_1.Err)(new kameleoonError_1.KameleoonError(kameleoonError_1.KameleoonException.Initialization));
706
234
  }
707
- };
708
- KameleoonClient.prototype.fireUpdateConfigurationHandler = function (fire) {
709
- fire &&
710
- this.handlerUpdateConfiguration &&
711
- this.handlerUpdateConfiguration();
712
- };
713
- KameleoonClient.VISITOR_CODE_MAX_LENGTH = 255;
714
- return KameleoonClient;
715
- }());
235
+ const featureFlags = this.campaignConfiguration.featureFlags;
236
+ const featureFlag = featureFlags.find((item) => item.featureKey === featureKey);
237
+ if (!featureFlag) {
238
+ return (0, ts_res_1.Err)(new kameleoonError_1.KameleoonError(kameleoonError_1.KameleoonException.FeatureFlagConfigurationNotFound, featureKey, visitorCode));
239
+ }
240
+ const targetingData = this.campaignConfiguration.storedTargetingData[visitorCode];
241
+ const variationData = this.variationConfiguration
242
+ .getFeatureFlagVariation({
243
+ visitorCode,
244
+ featureFlag,
245
+ targetingData,
246
+ withAssignment: true,
247
+ })
248
+ .throw();
249
+ if (((_a = variationData.rule) === null || _a === void 0 ? void 0 : _a.type) === campaignConfiguration_1.RuleType.EXPERIMENTATION) {
250
+ const userAgent = utilities_1.Utilities.getUserAgent(visitorCode, this.campaignConfiguration);
251
+ utilities_1.Utilities.trackExperimentationRule({
252
+ visitorCode,
253
+ requester: this.requester,
254
+ featureFlagVariation: variationData,
255
+ campaignConfiguration: this.campaignConfiguration,
256
+ userAgent,
257
+ });
258
+ }
259
+ return (0, ts_res_1.Ok)(variationData);
260
+ }
261
+ }
716
262
  exports.KameleoonClient = KameleoonClient;
717
- var KameleoonException = {
718
- ExperimentConfigurationNotFound: experimentConfigurationNotFound_1.default,
719
- FeatureConfigurationNotFound: featureConfigurationNotFound_1.default,
720
- VariationConfigurationNotFound: variationConfigurationNotFound_1.default,
721
- NotTargeted: notTargeted_1.default,
722
- NotActivated: notActivated_1.default,
723
- CredentialsNotFound: credentialsNotFound_1.default,
724
- };
725
- exports.KameleoonException = KameleoonException;
726
263
  //# sourceMappingURL=kameleoonClient.js.map