@kameleoon/javascript-sdk 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/dist/configurations/configuration.d.ts +15 -0
- package/dist/configurations/configuration.js +31 -0
- package/dist/configurations/configuration.js.map +1 -0
- package/dist/configurations/experiment.d.ts +13 -0
- package/dist/configurations/experiment.js +54 -0
- package/dist/configurations/experiment.js.map +1 -0
- package/dist/configurations/featureFlag.d.ts +23 -0
- package/dist/configurations/featureFlag.js +105 -0
- package/dist/configurations/featureFlag.js.map +1 -0
- package/dist/configurations/variation.d.ts +15 -0
- package/dist/configurations/variation.js +14 -0
- package/dist/configurations/variation.js.map +1 -0
- package/dist/constants.d.ts +17 -0
- package/dist/constants.js +23 -0
- package/dist/constants.js.map +1 -0
- package/dist/data.d.ts +67 -0
- package/dist/data.js +127 -0
- package/dist/data.js.map +1 -0
- package/dist/exceptions/credentialsNotFound.d.ts +6 -0
- package/dist/exceptions/credentialsNotFound.js +29 -0
- package/dist/exceptions/credentialsNotFound.js.map +1 -0
- package/dist/exceptions/experimentConfigurationNotFound.d.ts +6 -0
- package/dist/exceptions/experimentConfigurationNotFound.js +29 -0
- package/dist/exceptions/experimentConfigurationNotFound.js.map +1 -0
- package/dist/exceptions/featureConfigurationNotFound.d.ts +6 -0
- package/dist/exceptions/featureConfigurationNotFound.js +29 -0
- package/dist/exceptions/featureConfigurationNotFound.js.map +1 -0
- package/dist/exceptions/notActivated.d.ts +6 -0
- package/dist/exceptions/notActivated.js +29 -0
- package/dist/exceptions/notActivated.js.map +1 -0
- package/dist/exceptions/notTargeted.d.ts +6 -0
- package/dist/exceptions/notTargeted.js +29 -0
- package/dist/exceptions/notTargeted.js.map +1 -0
- package/dist/exceptions/siteCodeDisabled.d.ts +5 -0
- package/dist/exceptions/siteCodeDisabled.js +30 -0
- package/dist/exceptions/siteCodeDisabled.js.map +1 -0
- package/dist/exceptions/variationConfigurationNotFound.d.ts +6 -0
- package/dist/exceptions/variationConfigurationNotFound.js +29 -0
- package/dist/exceptions/variationConfigurationNotFound.js.map +1 -0
- package/dist/exceptions/visitorCodeNotValid.d.ts +5 -0
- package/dist/exceptions/visitorCodeNotValid.js +30 -0
- package/dist/exceptions/visitorCodeNotValid.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/interfaces.d.ts +81 -0
- package/dist/interfaces/interfaces.js +3 -0
- package/dist/interfaces/interfaces.js.map +1 -0
- package/dist/kameleoonClient.d.ts +103 -0
- package/dist/kameleoonClient.js +652 -0
- package/dist/kameleoonClient.js.map +1 -0
- package/dist/kameleoonUtils.d.ts +11 -0
- package/dist/kameleoonUtils.js +107 -0
- package/dist/kameleoonUtils.js.map +1 -0
- package/dist/nonce.d.ts +1 -0
- package/dist/nonce.js +16 -0
- package/dist/nonce.js.map +1 -0
- package/dist/realTimeConfigurationService.d.ts +12 -0
- package/dist/realTimeConfigurationService.js +24 -0
- package/dist/realTimeConfigurationService.js.map +1 -0
- package/dist/storage/types.d.ts +13 -0
- package/dist/storage/types.js +3 -0
- package/dist/storage/types.js.map +1 -0
- package/dist/storage/variationStorage.d.ts +11 -0
- package/dist/storage/variationStorage.js +87 -0
- package/dist/storage/variationStorage.js.map +1 -0
- package/dist/storage/visitorVariation.d.ts +9 -0
- package/dist/storage/visitorVariation.js +22 -0
- package/dist/storage/visitorVariation.js.map +1 -0
- package/dist/targeting/conditions/customDatum.d.ts +12 -0
- package/dist/targeting/conditions/customDatum.js +129 -0
- package/dist/targeting/conditions/customDatum.js.map +1 -0
- package/dist/targeting/conditions/exclusiveExperiment.d.ts +9 -0
- package/dist/targeting/conditions/exclusiveExperiment.js +45 -0
- package/dist/targeting/conditions/exclusiveExperiment.js.map +1 -0
- package/dist/targeting/conditions/targetExperiment.d.ts +14 -0
- package/dist/targeting/conditions/targetExperiment.js +57 -0
- package/dist/targeting/conditions/targetExperiment.js.map +1 -0
- package/dist/targeting/conditions/types.d.ts +12 -0
- package/dist/targeting/conditions/types.js +3 -0
- package/dist/targeting/conditions/types.js.map +1 -0
- package/dist/targeting/targetingCondition.d.ts +7 -0
- package/dist/targeting/targetingCondition.js +9 -0
- package/dist/targeting/targetingCondition.js.map +1 -0
- package/dist/targeting/targetingConditionsFactory.d.ts +7 -0
- package/dist/targeting/targetingConditionsFactory.js +30 -0
- package/dist/targeting/targetingConditionsFactory.js.map +1 -0
- package/dist/targeting/targetingData.d.ts +7 -0
- package/dist/targeting/targetingData.js +11 -0
- package/dist/targeting/targetingData.js.map +1 -0
- package/dist/targeting/targetingEngine.d.ts +6 -0
- package/dist/targeting/targetingEngine.js +130 -0
- package/dist/targeting/targetingEngine.js.map +1 -0
- package/dist/targeting/targetingSegment.d.ts +5 -0
- package/dist/targeting/targetingSegment.js +9 -0
- package/dist/targeting/targetingSegment.js.map +1 -0
- package/dist/targeting/targetingTree.d.ts +9 -0
- package/dist/targeting/targetingTree.js +13 -0
- package/dist/targeting/targetingTree.js.map +1 -0
- package/dist/targeting/targetingTreeBuilder.d.ts +8 -0
- package/dist/targeting/targetingTreeBuilder.js +127 -0
- package/dist/targeting/targetingTreeBuilder.js.map +1 -0
- package/dist/types.d.ts +5 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +36 -0
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
35
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
36
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
37
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
38
|
+
function step(op) {
|
|
39
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
40
|
+
while (_) try {
|
|
41
|
+
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;
|
|
42
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
43
|
+
switch (op[0]) {
|
|
44
|
+
case 0: case 1: t = op; break;
|
|
45
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
46
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
47
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
48
|
+
default:
|
|
49
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
50
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
51
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
52
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
53
|
+
if (t[2]) _.ops.pop();
|
|
54
|
+
_.trys.pop(); continue;
|
|
55
|
+
}
|
|
56
|
+
op = body.call(thisArg, _);
|
|
57
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
58
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
62
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
63
|
+
};
|
|
64
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
65
|
+
exports.KameleoonException = exports.KameleoonData = exports.KameleoonClient = void 0;
|
|
66
|
+
var configuration_1 = __importDefault(require("./configurations/configuration"));
|
|
67
|
+
var data_1 = __importDefault(require("./data"));
|
|
68
|
+
exports.KameleoonData = data_1.default;
|
|
69
|
+
var kameleoonUtils_1 = __importStar(require("./kameleoonUtils"));
|
|
70
|
+
var targetingData_1 = __importDefault(require("./targeting/targetingData"));
|
|
71
|
+
var experimentConfigurationNotFound_1 = __importDefault(require("./exceptions/experimentConfigurationNotFound"));
|
|
72
|
+
var featureConfigurationNotFound_1 = __importDefault(require("./exceptions/featureConfigurationNotFound"));
|
|
73
|
+
var notActivated_1 = __importDefault(require("./exceptions/notActivated"));
|
|
74
|
+
var notTargeted_1 = __importDefault(require("./exceptions/notTargeted"));
|
|
75
|
+
var variationConfigurationNotFound_1 = __importDefault(require("./exceptions/variationConfigurationNotFound"));
|
|
76
|
+
var credentialsNotFound_1 = __importDefault(require("./exceptions/credentialsNotFound"));
|
|
77
|
+
var visitorCodeNotValid_1 = require("./exceptions/visitorCodeNotValid");
|
|
78
|
+
var siteCodeDisabled_1 = require("./exceptions/siteCodeDisabled");
|
|
79
|
+
var nonce_1 = require("./nonce");
|
|
80
|
+
var realTimeConfigurationService_1 = require("./realTimeConfigurationService");
|
|
81
|
+
var variationStorage_1 = require("./storage/variationStorage");
|
|
82
|
+
var constants_1 = require("./constants");
|
|
83
|
+
var targetingEngine_1 = require("./targeting/targetingEngine");
|
|
84
|
+
var KameleoonClient = /** @class */ (function () {
|
|
85
|
+
function KameleoonClient(siteCode, configuration) {
|
|
86
|
+
var _this = this;
|
|
87
|
+
this.ready = false;
|
|
88
|
+
this.readyCallbacks = [];
|
|
89
|
+
this.minutesBetweenConfigurationUpdate = 30;
|
|
90
|
+
this.targetingData = {};
|
|
91
|
+
this.unsentData = {};
|
|
92
|
+
this.visitorDataMaximumSize = 1;
|
|
93
|
+
this.environment = null;
|
|
94
|
+
this.realTimeConfigurationService = null;
|
|
95
|
+
this.handlerUpdateConfiguration = null;
|
|
96
|
+
this.variationStorage = new variationStorage_1.VariationStorage();
|
|
97
|
+
this.updateCampaignConfiguration = function (timeStamp) {
|
|
98
|
+
var _a;
|
|
99
|
+
var data = localStorage.kameleoonJavaScriptClientData;
|
|
100
|
+
if (data) {
|
|
101
|
+
try {
|
|
102
|
+
data = JSON.parse(data);
|
|
103
|
+
}
|
|
104
|
+
catch (e) {
|
|
105
|
+
console.log(e);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (!data) {
|
|
109
|
+
data = {
|
|
110
|
+
kameleoonTargetingData: {},
|
|
111
|
+
kameleoonConfiguration: {},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
var savedTargetingData = data.kameleoonTargetingData;
|
|
115
|
+
if (savedTargetingData) {
|
|
116
|
+
try {
|
|
117
|
+
_this.targetingData = savedTargetingData;
|
|
118
|
+
_this.emptyTargetingData();
|
|
119
|
+
}
|
|
120
|
+
catch (e) {
|
|
121
|
+
_this.targetingData = {};
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
var savedConfiguration = data.kameleoonConfiguration;
|
|
125
|
+
var needUpdate = false;
|
|
126
|
+
if (!savedConfiguration || timeStamp != null) {
|
|
127
|
+
// timeStamp != null means update by SSE event and requires force update
|
|
128
|
+
needUpdate = true;
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
try {
|
|
132
|
+
var parsedConfiguration = new configuration_1.default(_this.siteCode, {
|
|
133
|
+
configuration: savedConfiguration.configuration,
|
|
134
|
+
experiments: savedConfiguration.experiments,
|
|
135
|
+
featureFlags: savedConfiguration.featureFlags,
|
|
136
|
+
updateTime: savedConfiguration.updateTime,
|
|
137
|
+
});
|
|
138
|
+
if (parsedConfiguration.updateTime <
|
|
139
|
+
new Date().getTime() -
|
|
140
|
+
_this.minutesBetweenConfigurationUpdate * 1000 * 60 || //refresh by polling interval
|
|
141
|
+
parsedConfiguration.settings.realTimeUpdate) {
|
|
142
|
+
//streaming initial refresh, need to update to recent version of configuration
|
|
143
|
+
needUpdate = true;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
_this.configurations = parsedConfiguration;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
catch (e) {
|
|
150
|
+
needUpdate = true;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (needUpdate) {
|
|
154
|
+
var siteCodeStringUrl = "mobile?siteCode=".concat(_this.siteCode);
|
|
155
|
+
var environmentStringUrl = ((_a = _this.environment) === null || _a === void 0 ? void 0 : _a.length)
|
|
156
|
+
? "&environment=".concat(_this.environment)
|
|
157
|
+
: '';
|
|
158
|
+
var timeStampStringUrl = timeStamp ? "&ts=".concat(timeStamp) : '';
|
|
159
|
+
var configurationUrl = constants_1.CONFIGURATION_URL +
|
|
160
|
+
siteCodeStringUrl +
|
|
161
|
+
environmentStringUrl +
|
|
162
|
+
timeStampStringUrl;
|
|
163
|
+
kameleoonUtils_1.default.doRequest(configurationUrl, kameleoonUtils_1.HttpMethod.GET, null, function (response) {
|
|
164
|
+
try {
|
|
165
|
+
_this.configurations = new configuration_1.default(_this.siteCode, JSON.parse(response));
|
|
166
|
+
_this.ready = true;
|
|
167
|
+
_this.clearCallbacks(true);
|
|
168
|
+
_this.saveConfiguration();
|
|
169
|
+
_this.fireUpdateConfigurationHandler(Boolean(timeStamp)); //fire handler after configuration was updated with SSE event
|
|
170
|
+
_this.manageConfiguraitonUpdate(_this.configurations.settings.realTimeUpdate);
|
|
171
|
+
}
|
|
172
|
+
catch (e) {
|
|
173
|
+
console.log(e);
|
|
174
|
+
}
|
|
175
|
+
}, false);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
_this.ready = true;
|
|
179
|
+
_this.clearCallbacks(true);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
this.runWhenReady = function (successCallback, errorCallback, timeout) {
|
|
183
|
+
if (_this.ready) {
|
|
184
|
+
successCallback && successCallback();
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
_this.readyCallbacks.push({
|
|
188
|
+
error: errorCallback,
|
|
189
|
+
success: successCallback,
|
|
190
|
+
timeout: setTimeout(function () {
|
|
191
|
+
_this.clearCallbacks(false, errorCallback);
|
|
192
|
+
}, timeout),
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
this.clearCallbacks = function (success, errorCallback) {
|
|
197
|
+
var callbacksToKeep = [];
|
|
198
|
+
_this.readyCallbacks.forEach(function (c) {
|
|
199
|
+
if (success) {
|
|
200
|
+
c.success && c.success();
|
|
201
|
+
clearTimeout(c.timeout);
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
if (!success && errorCallback == c.error) {
|
|
205
|
+
c.error && c.error();
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
callbacksToKeep.push(c);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
_this.readyCallbacks = callbacksToKeep;
|
|
213
|
+
};
|
|
214
|
+
this.trackConversion = function (visitorCode, goalId, revenue) {
|
|
215
|
+
_this.validateVisitorCode(visitorCode);
|
|
216
|
+
_this.addData(visitorCode, new data_1.default.Conversion(goalId, revenue, false));
|
|
217
|
+
_this.flush(visitorCode);
|
|
218
|
+
};
|
|
219
|
+
this.flush = function (visitorCode) {
|
|
220
|
+
if (visitorCode != null) {
|
|
221
|
+
var body = _this.getAndClearUnsentData(visitorCode);
|
|
222
|
+
kameleoonUtils_1.default.doRequest(constants_1.API_SSX_URL + _this.getDataTrackingURL(visitorCode), kameleoonUtils_1.HttpMethod.POST, body, null);
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
_this.getUnsentUsers().forEach(function (vc) { return _this.flush(vc); });
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
this.triggerExperiment = function (visitorCode, experimentId) {
|
|
229
|
+
var _a;
|
|
230
|
+
_this.validateVisitorCode(visitorCode);
|
|
231
|
+
var xpConf = (_a = _this.configurations) === null || _a === void 0 ? void 0 : _a.experiments[experimentId];
|
|
232
|
+
_this.checkSiteEnabled(xpConf);
|
|
233
|
+
if (_this.checkTargeting(visitorCode, experimentId, false)) {
|
|
234
|
+
var variationId = 0;
|
|
235
|
+
var noneVariation = true;
|
|
236
|
+
var savedVariationId = _this.variationStorage.getVariationId(visitorCode, experimentId);
|
|
237
|
+
if (savedVariationId) {
|
|
238
|
+
variationId = savedVariationId;
|
|
239
|
+
noneVariation = false;
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
if (xpConf) {
|
|
243
|
+
var hashDouble = _this.obtainHashDouble(experimentId, visitorCode, xpConf);
|
|
244
|
+
var total = 0.0;
|
|
245
|
+
for (var key in xpConf.variationConfigurations) {
|
|
246
|
+
var variationConfiguration = xpConf.variationConfigurations[key];
|
|
247
|
+
total += variationConfiguration.deviation;
|
|
248
|
+
if (total >= hashDouble) {
|
|
249
|
+
noneVariation = false;
|
|
250
|
+
variationId = parseInt(key);
|
|
251
|
+
_this.variationStorage.updateVariation(visitorCode, experimentId, variationConfiguration);
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
var body = _this.getAndClearUnsentData(visitorCode);
|
|
256
|
+
kameleoonUtils_1.default.doRequest(constants_1.API_SSX_URL +
|
|
257
|
+
_this.getExperimentRegisterURL(visitorCode, experimentId, variationId, noneVariation), kameleoonUtils_1.HttpMethod.POST, body);
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
throw new experimentConfigurationNotFound_1.default('KameleoonException.ExperimentConfigurationNotFound');
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (noneVariation) {
|
|
264
|
+
throw new notActivated_1.default('KameleoonException.NotActivated');
|
|
265
|
+
}
|
|
266
|
+
return variationId;
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
throw new notTargeted_1.default('KameleoonException.NotTargeted');
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
this.activateFeature = function (visitorCode, featureIdOrName) {
|
|
273
|
+
var _a, _b;
|
|
274
|
+
_this.checkFeatureKeyOrId(featureIdOrName);
|
|
275
|
+
_this.validateVisitorCode(visitorCode);
|
|
276
|
+
var featureFlag = null;
|
|
277
|
+
var featureId = -1;
|
|
278
|
+
for (var key in (_a = _this.configurations) === null || _a === void 0 ? void 0 : _a.featureFlags) {
|
|
279
|
+
var ff = (_b = _this.configurations) === null || _b === void 0 ? void 0 : _b.featureFlags[key];
|
|
280
|
+
if (key == featureIdOrName || (ff === null || ff === void 0 ? void 0 : ff.identificationKey) == featureIdOrName) {
|
|
281
|
+
featureId = parseInt(key);
|
|
282
|
+
featureFlag = ff;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
_this.checkSiteEnabled(featureFlag);
|
|
286
|
+
if (_this.checkTargeting(visitorCode, featureId, true)) {
|
|
287
|
+
if (featureFlag && featureId) {
|
|
288
|
+
var result = false;
|
|
289
|
+
if (featureFlag.isScheduleActive()) {
|
|
290
|
+
var hashDouble = _this.obtainHashDouble(featureId, visitorCode, featureFlag);
|
|
291
|
+
var total = 0.0;
|
|
292
|
+
var variationId = 0;
|
|
293
|
+
var noneVariation = true;
|
|
294
|
+
for (var key in featureFlag.variationConfigurations) {
|
|
295
|
+
var variationConfiguration = featureFlag.variationConfigurations[key];
|
|
296
|
+
total += variationConfiguration.deviation;
|
|
297
|
+
if (total >= hashDouble) {
|
|
298
|
+
noneVariation = false;
|
|
299
|
+
variationId = parseInt(key);
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
var body = _this.getAndClearUnsentData(visitorCode);
|
|
304
|
+
kameleoonUtils_1.default.doRequest(constants_1.API_SSX_URL +
|
|
305
|
+
_this.getExperimentRegisterURL(visitorCode, featureId, variationId, noneVariation), kameleoonUtils_1.HttpMethod.POST, body);
|
|
306
|
+
result = variationId != 0;
|
|
307
|
+
}
|
|
308
|
+
return result;
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
throw new featureConfigurationNotFound_1.default('KameleoonException.FeatureConfigurationNotFound');
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
throw new notTargeted_1.default('KameleoonException.NotTargeted');
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
this.generateRandomString = function () {
|
|
319
|
+
var alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
320
|
+
var alphabetLength = alphabet.length;
|
|
321
|
+
var randomVisitorCode = '';
|
|
322
|
+
for (var i = 0; i < 16; ++i) {
|
|
323
|
+
var randomNumber = Math.floor(Math.random() * alphabetLength);
|
|
324
|
+
randomVisitorCode += alphabet[randomNumber];
|
|
325
|
+
}
|
|
326
|
+
return randomVisitorCode;
|
|
327
|
+
};
|
|
328
|
+
this.obtainVisitorCode = function (domain, defaultVisitorCode) {
|
|
329
|
+
var value;
|
|
330
|
+
var name = 'kameleoonVisitorCode';
|
|
331
|
+
var cookies = document.cookie.split(';');
|
|
332
|
+
for (var i = 0, cookiesLength = cookies.length; i < cookiesLength; ++i) {
|
|
333
|
+
var cookie = cookies[i];
|
|
334
|
+
var valuePos = cookie.indexOf('=');
|
|
335
|
+
if (cookie.substring(0, valuePos).trim() == name) {
|
|
336
|
+
value = cookie.substring(valuePos + 1);
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (value) {
|
|
341
|
+
return value;
|
|
342
|
+
}
|
|
343
|
+
value = defaultVisitorCode || _this.generateRandomString();
|
|
344
|
+
document.cookie =
|
|
345
|
+
'kameleoonVisitorCode=' +
|
|
346
|
+
value +
|
|
347
|
+
'; Max-Age=32832000; Path=/; Domain=' +
|
|
348
|
+
domain;
|
|
349
|
+
return value;
|
|
350
|
+
};
|
|
351
|
+
this.obtainHashDouble = function (containerId, visitorCode, xpConf) {
|
|
352
|
+
var joinedRespoolTime = '';
|
|
353
|
+
for (var variationId in xpConf.variationConfigurations) {
|
|
354
|
+
var variation = xpConf.variationConfigurations[variationId];
|
|
355
|
+
if (variation.respoolTime && variation.respoolTime != 0) {
|
|
356
|
+
joinedRespoolTime += variation.respoolTime;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
var identifier = visitorCode + containerId + joinedRespoolTime;
|
|
360
|
+
var buff = kameleoonUtils_1.default.computeStringSha256(identifier);
|
|
361
|
+
return parseInt(buff, 16) / Math.pow(2, 256);
|
|
362
|
+
};
|
|
363
|
+
this.getCommonSSXParameters = function (visitorCode) {
|
|
364
|
+
return ('&nonce=' +
|
|
365
|
+
(0, nonce_1.obtainNonce)() +
|
|
366
|
+
'&siteCode=' +
|
|
367
|
+
_this.siteCode +
|
|
368
|
+
'&visitorCode=' +
|
|
369
|
+
visitorCode);
|
|
370
|
+
};
|
|
371
|
+
this.getDataTrackingURL = function (visitorCode) {
|
|
372
|
+
return 'dataTracking?' + _this.getCommonSSXParameters(visitorCode);
|
|
373
|
+
};
|
|
374
|
+
this.getAPIDataRequestURL = function (key) {
|
|
375
|
+
return "".concat(constants_1.API_DATA_URL, "data?siteCode=").concat(_this.siteCode, "&key=").concat(key);
|
|
376
|
+
};
|
|
377
|
+
this.getEventsRequestURL = function (siteCode) {
|
|
378
|
+
return "".concat(constants_1.EVENTS_URL, "sse?siteCode=").concat(siteCode);
|
|
379
|
+
};
|
|
380
|
+
this.getExperimentRegisterURL = function (visitorCode, experimentId, variationId, noneVariation) {
|
|
381
|
+
var baseURL = 'experimentTracking?' +
|
|
382
|
+
_this.getCommonSSXParameters(visitorCode) +
|
|
383
|
+
'&experimentId=' +
|
|
384
|
+
experimentId;
|
|
385
|
+
if (variationId == null) {
|
|
386
|
+
return baseURL;
|
|
387
|
+
}
|
|
388
|
+
else {
|
|
389
|
+
return (baseURL +
|
|
390
|
+
'&variationId=' +
|
|
391
|
+
variationId +
|
|
392
|
+
(noneVariation ? '&noneVariation=true' : ''));
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
this.getUnsentUsers = function () {
|
|
396
|
+
return Object.getOwnPropertyNames(_this.unsentData);
|
|
397
|
+
};
|
|
398
|
+
this.getUnsentData = function (visitorCode) {
|
|
399
|
+
if (!_this.unsentData[visitorCode]) {
|
|
400
|
+
_this.unsentData[visitorCode] = [];
|
|
401
|
+
}
|
|
402
|
+
return _this.unsentData[visitorCode];
|
|
403
|
+
};
|
|
404
|
+
this.getTargetingData = function (visitorCode) {
|
|
405
|
+
if (_this.targetingData[visitorCode] == null) {
|
|
406
|
+
_this.targetingData[visitorCode] = [];
|
|
407
|
+
}
|
|
408
|
+
return _this.targetingData[visitorCode];
|
|
409
|
+
};
|
|
410
|
+
this.emptyUnsentData = function (visitorCode) {
|
|
411
|
+
delete _this.unsentData[visitorCode];
|
|
412
|
+
};
|
|
413
|
+
this.emptyTargetingData = function () {
|
|
414
|
+
var currentTime = new Date().getTime();
|
|
415
|
+
for (var visitorCode in _this.targetingData) {
|
|
416
|
+
_this.targetingData[visitorCode] = _this.targetingData[visitorCode].reduce(function (arr, data) {
|
|
417
|
+
if (data.lastActivityTime + constants_1.MINUTES_SESSION_DURATION > currentTime) {
|
|
418
|
+
arr.push(data);
|
|
419
|
+
}
|
|
420
|
+
return arr;
|
|
421
|
+
}, []);
|
|
422
|
+
}
|
|
423
|
+
_this.saveConfiguration();
|
|
424
|
+
};
|
|
425
|
+
this.addData = function (visitorCode) {
|
|
426
|
+
var data = [];
|
|
427
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
428
|
+
data[_i - 1] = arguments[_i];
|
|
429
|
+
}
|
|
430
|
+
_this.validateVisitorCode(visitorCode);
|
|
431
|
+
var listData = _this.getUnsentData(visitorCode);
|
|
432
|
+
var targetingDataList = _this.getTargetingData(visitorCode);
|
|
433
|
+
data.forEach(function (d) {
|
|
434
|
+
listData.push(d);
|
|
435
|
+
if (targetingDataList.length < _this.targetingDataNumberEntriesAuthorized) {
|
|
436
|
+
targetingDataList.push(new targetingData_1.default(new Date().getTime(), d));
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
targetingDataList.splice(0, 1);
|
|
440
|
+
targetingDataList.push(new targetingData_1.default(new Date().getTime(), d));
|
|
441
|
+
}
|
|
442
|
+
_this.saveConfiguration();
|
|
443
|
+
});
|
|
444
|
+
};
|
|
445
|
+
// Targeting Engine
|
|
446
|
+
// check targeting
|
|
447
|
+
this.checkTargeting = function (visitorCode, campaignId, isFF) {
|
|
448
|
+
var _a, _b;
|
|
449
|
+
var targeting = true;
|
|
450
|
+
var xpConf = null;
|
|
451
|
+
// performing targeting
|
|
452
|
+
if (isFF) {
|
|
453
|
+
xpConf = (_a = _this.configurations) === null || _a === void 0 ? void 0 : _a.featureFlags[campaignId];
|
|
454
|
+
}
|
|
455
|
+
else {
|
|
456
|
+
xpConf = (_b = _this.configurations) === null || _b === void 0 ? void 0 : _b.experiments[campaignId];
|
|
457
|
+
}
|
|
458
|
+
if (xpConf != null) {
|
|
459
|
+
if (true == xpConf.forceTargeting) {
|
|
460
|
+
// assigning targeting to container
|
|
461
|
+
targeting = true;
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
var targetingTree = void 0;
|
|
465
|
+
if (null != xpConf.targetingSegment) {
|
|
466
|
+
targetingTree = xpConf.targetingSegment.targetingTree;
|
|
467
|
+
}
|
|
468
|
+
// obtaining targeting checking result and assigning targeting to container
|
|
469
|
+
targeting = targetingEngine_1.TargetingEngine.checkTargetingTree(targetingTree, function (type) {
|
|
470
|
+
return _this.getConditionData(type, visitorCode, campaignId);
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
return targeting;
|
|
475
|
+
};
|
|
476
|
+
this.getConditionData = function (type, visitorCode, campaignId) {
|
|
477
|
+
switch (type) {
|
|
478
|
+
case constants_1.ConditionTypes.CUSTOM_DATUM:
|
|
479
|
+
return {
|
|
480
|
+
targetingData: _this.getTargetingData(visitorCode),
|
|
481
|
+
};
|
|
482
|
+
case constants_1.ConditionTypes.EXCLUSIVE_EXPERIMENT:
|
|
483
|
+
return {
|
|
484
|
+
currentExperimentId: campaignId,
|
|
485
|
+
variationStorage: _this.variationStorage.getMapVariationId(visitorCode),
|
|
486
|
+
};
|
|
487
|
+
case constants_1.ConditionTypes.TARGET_EXPERIMENT:
|
|
488
|
+
return {
|
|
489
|
+
variationStorage: _this.variationStorage.getMapVariationId(visitorCode),
|
|
490
|
+
};
|
|
491
|
+
default:
|
|
492
|
+
return null;
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
this.obtainVariationAssociatedData = function (variationId) {
|
|
496
|
+
var _a, _b, _c;
|
|
497
|
+
for (var vid in (_a = _this.configurations) === null || _a === void 0 ? void 0 : _a.experiments) {
|
|
498
|
+
var experiment = (_b = _this.configurations) === null || _b === void 0 ? void 0 : _b.experiments[vid];
|
|
499
|
+
if (experiment === null || experiment === void 0 ? void 0 : experiment.variationConfigurations[variationId]) {
|
|
500
|
+
return ((_c = experiment === null || experiment === void 0 ? void 0 : experiment.variationConfigurations[variationId].customJson) !== null && _c !== void 0 ? _c : {});
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
throw new variationConfigurationNotFound_1.default('KameleoonException.VariationConfigurationNotFound');
|
|
504
|
+
};
|
|
505
|
+
this.obtainFeatureVariable = function (featureIdOrName, variable) {
|
|
506
|
+
var _a, _b, _c, _d;
|
|
507
|
+
_this.checkFeatureKeyOrId(featureIdOrName);
|
|
508
|
+
for (var ffid in (_a = _this.configurations) === null || _a === void 0 ? void 0 : _a.featureFlags) {
|
|
509
|
+
var ff = (_b = _this.configurations) === null || _b === void 0 ? void 0 : _b.featureFlags[ffid];
|
|
510
|
+
if (ffid == featureIdOrName ||
|
|
511
|
+
(ff === null || ff === void 0 ? void 0 : ff.identificationKey) === featureIdOrName) {
|
|
512
|
+
for (var vid in ff === null || ff === void 0 ? void 0 : ff.variationConfigurations) {
|
|
513
|
+
if (vid !== '0') {
|
|
514
|
+
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]);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
throw new featureConfigurationNotFound_1.default('KameleoonExceptinpmon.FeatureConfigurationNotFound');
|
|
520
|
+
};
|
|
521
|
+
this.retrieveDataFromRemoteSource = function (key) { return __awaiter(_this, void 0, void 0, function () {
|
|
522
|
+
var _this = this;
|
|
523
|
+
return __generator(this, function (_a) {
|
|
524
|
+
return [2 /*return*/, new Promise(function (resolve) {
|
|
525
|
+
kameleoonUtils_1.default.doRequest(_this.getAPIDataRequestURL(key), kameleoonUtils_1.HttpMethod.GET, null, function (response) {
|
|
526
|
+
resolve(JSON.parse(response));
|
|
527
|
+
});
|
|
528
|
+
})];
|
|
529
|
+
});
|
|
530
|
+
}); };
|
|
531
|
+
this._parseJsonValue = function (obj) {
|
|
532
|
+
if (obj == null) {
|
|
533
|
+
throw new featureConfigurationNotFound_1.default('KameleoonException.FeatureConfigurationNotFound');
|
|
534
|
+
}
|
|
535
|
+
else {
|
|
536
|
+
switch (obj.type) {
|
|
537
|
+
case 'Boolean':
|
|
538
|
+
return obj.value == 'True';
|
|
539
|
+
case 'Number':
|
|
540
|
+
return parseInt(obj.value);
|
|
541
|
+
case 'JSON':
|
|
542
|
+
return JSON.parse(obj.value);
|
|
543
|
+
default:
|
|
544
|
+
return obj.value;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
this.saveConfiguration = function () {
|
|
549
|
+
var _a, _b;
|
|
550
|
+
if (_this.configurations) {
|
|
551
|
+
var data = {
|
|
552
|
+
kameleoonTargetingData: _this.targetingData,
|
|
553
|
+
kameleoonConfiguration: {
|
|
554
|
+
updateTime: _this.configurations.updateTime,
|
|
555
|
+
experiments: (_a = _this.configurations.configuration) === null || _a === void 0 ? void 0 : _a.experiments,
|
|
556
|
+
featureFlags: (_b = _this.configurations.configuration) === null || _b === void 0 ? void 0 : _b.featureFlags,
|
|
557
|
+
configuration: _this.configurations.settings,
|
|
558
|
+
},
|
|
559
|
+
};
|
|
560
|
+
try {
|
|
561
|
+
localStorage.setItem('kameleoonJavaScriptClientData', JSON.stringify(data));
|
|
562
|
+
}
|
|
563
|
+
catch (e) {
|
|
564
|
+
console.log(e);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
this.validateVisitorCode = function (visitorCode) {
|
|
569
|
+
if (visitorCode == null || visitorCode.length == 0) {
|
|
570
|
+
throw new visitorCodeNotValid_1.VisitorCodeNotValid('Visitor code is empty');
|
|
571
|
+
}
|
|
572
|
+
else if (visitorCode.length > KameleoonClient.VISITOR_CODE_MAX_LENGTH) {
|
|
573
|
+
throw new visitorCodeNotValid_1.VisitorCodeNotValid('Visitor code max length is ' +
|
|
574
|
+
KameleoonClient.VISITOR_CODE_MAX_LENGTH +
|
|
575
|
+
' characters');
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
this.onUpdateConfiguration = function (handler) {
|
|
579
|
+
_this.handlerUpdateConfiguration = handler;
|
|
580
|
+
};
|
|
581
|
+
this.siteCode = siteCode;
|
|
582
|
+
if (configuration) {
|
|
583
|
+
if (configuration.visitor_data_maximum_size) {
|
|
584
|
+
this.visitorDataMaximumSize = configuration.visitor_data_maximum_size;
|
|
585
|
+
}
|
|
586
|
+
if (configuration.actions_configuration_refresh_interval) {
|
|
587
|
+
this.minutesBetweenConfigurationUpdate =
|
|
588
|
+
configuration.actions_configuration_refresh_interval;
|
|
589
|
+
}
|
|
590
|
+
if (configuration.environment) {
|
|
591
|
+
this.environment = configuration.environment;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
this.targetingDataNumberEntriesAuthorized =
|
|
595
|
+
(this.visitorDataMaximumSize * 1000000) /
|
|
596
|
+
constants_1.TARGETING_DATA_MAXIMUM_SIZE_BYTES;
|
|
597
|
+
this.updateCampaignConfiguration();
|
|
598
|
+
setInterval(this.emptyTargetingData, constants_1.DEFAULT_MILLISECONDS_BETWEEN_TARGETING_DATA_UPDATE);
|
|
599
|
+
}
|
|
600
|
+
KameleoonClient.prototype.checkSiteEnabled = function (experimentOrFeatureFlag) {
|
|
601
|
+
if (experimentOrFeatureFlag != null &&
|
|
602
|
+
!experimentOrFeatureFlag.siteEnabled) {
|
|
603
|
+
throw new siteCodeDisabled_1.SiteCodeDisabled(this.siteCode);
|
|
604
|
+
}
|
|
605
|
+
};
|
|
606
|
+
KameleoonClient.prototype.checkFeatureKeyOrId = function (featureIdOrName) {
|
|
607
|
+
if (typeof featureIdOrName === 'string') {
|
|
608
|
+
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');
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
KameleoonClient.prototype.getAndClearUnsentData = function (visitorCode) {
|
|
612
|
+
var body = '';
|
|
613
|
+
this.getUnsentData(visitorCode).forEach(function (element) {
|
|
614
|
+
body += element.obtainFullPostTextLine() + '\n';
|
|
615
|
+
});
|
|
616
|
+
this.emptyUnsentData(visitorCode);
|
|
617
|
+
return body;
|
|
618
|
+
};
|
|
619
|
+
KameleoonClient.prototype.manageConfiguraitonUpdate = function (subsciptionRequired) {
|
|
620
|
+
var _this = this;
|
|
621
|
+
if (subsciptionRequired && !this.realTimeConfigurationService) {
|
|
622
|
+
this.realTimeConfigurationService = new realTimeConfigurationService_1.RealTimeConfigurationService(this.getEventsRequestURL(this.siteCode), function (event) {
|
|
623
|
+
_this.updateCampaignConfiguration(event.ts);
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
else if (!subsciptionRequired) {
|
|
627
|
+
if (this.realTimeConfigurationService) {
|
|
628
|
+
this.realTimeConfigurationService.disconnect();
|
|
629
|
+
this.realTimeConfigurationService = null;
|
|
630
|
+
}
|
|
631
|
+
setTimeout(this.updateCampaignConfiguration, this.minutesBetweenConfigurationUpdate * constants_1.Milliseconds.Minute);
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
KameleoonClient.prototype.fireUpdateConfigurationHandler = function (fire) {
|
|
635
|
+
fire &&
|
|
636
|
+
this.handlerUpdateConfiguration &&
|
|
637
|
+
this.handlerUpdateConfiguration();
|
|
638
|
+
};
|
|
639
|
+
KameleoonClient.VISITOR_CODE_MAX_LENGTH = 255;
|
|
640
|
+
return KameleoonClient;
|
|
641
|
+
}());
|
|
642
|
+
exports.KameleoonClient = KameleoonClient;
|
|
643
|
+
var KameleoonException = {
|
|
644
|
+
ExperimentConfigurationNotFound: experimentConfigurationNotFound_1.default,
|
|
645
|
+
FeatureConfigurationNotFound: featureConfigurationNotFound_1.default,
|
|
646
|
+
VariationConfigurationNotFound: variationConfigurationNotFound_1.default,
|
|
647
|
+
NotTargeted: notTargeted_1.default,
|
|
648
|
+
NotActivated: notActivated_1.default,
|
|
649
|
+
CredentialsNotFound: credentialsNotFound_1.default,
|
|
650
|
+
};
|
|
651
|
+
exports.KameleoonException = KameleoonException;
|
|
652
|
+
//# sourceMappingURL=kameleoonClient.js.map
|