@kameleoon/javascript-sdk-core 1.1.0 → 2.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.
- package/dist/campaignConfiguration/campaignConfiguration.d.ts +2 -1
- package/dist/campaignConfiguration/campaignConfiguration.js +239 -197
- package/dist/campaignConfiguration/campaignConfiguration.js.map +1 -1
- package/dist/campaignConfiguration/constants.js +20 -14
- package/dist/campaignConfiguration/constants.js.map +1 -1
- package/dist/campaignConfiguration/index.js +78 -6
- package/dist/campaignConfiguration/index.js.map +1 -1
- package/dist/campaignConfiguration/types.d.ts +3 -2
- package/dist/campaignConfiguration/types.js +53 -45
- package/dist/campaignConfiguration/types.js.map +1 -1
- package/dist/clientSettings/clientSettings.d.ts +1 -1
- package/dist/clientSettings/clientSettings.js +28 -21
- package/dist/clientSettings/clientSettings.js.map +1 -1
- package/dist/clientSettings/index.js +18 -4
- package/dist/clientSettings/index.js.map +1 -1
- package/dist/clientSettings/types.d.ts +1 -1
- package/dist/clientSettings/types.js +4 -1
- package/dist/clientSettings/types.js.map +1 -1
- package/dist/constants.js +22 -14
- package/dist/constants.js.map +1 -1
- package/dist/eventSource/constants.js +6 -2
- package/dist/eventSource/constants.js.map +1 -1
- package/dist/eventSource/externalEventSource.js +28 -20
- package/dist/eventSource/externalEventSource.js.map +1 -1
- package/dist/eventSource/index.js +49 -6
- package/dist/eventSource/index.js.map +1 -1
- package/dist/eventSource/types.d.ts +1 -1
- package/dist/eventSource/types.js +4 -1
- package/dist/eventSource/types.js.map +1 -1
- package/dist/hasher/constants.js +6 -2
- package/dist/hasher/constants.js.map +1 -1
- package/dist/hasher/hasher.js +35 -24
- package/dist/hasher/hasher.js.map +1 -1
- package/dist/hasher/index.js +11 -4
- package/dist/hasher/index.js.map +1 -1
- package/dist/hasher/types.js +4 -1
- package/dist/hasher/types.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +297 -27
- package/dist/index.js.map +1 -1
- package/dist/kameleoonClient.d.ts +16 -16
- package/dist/kameleoonClient.js +307 -236
- package/dist/kameleoonClient.js.map +1 -1
- package/dist/kameleoonData/browser.js +37 -32
- package/dist/kameleoonData/browser.js.map +1 -1
- package/dist/kameleoonData/constants.js +6 -2
- package/dist/kameleoonData/constants.js.map +1 -1
- package/dist/kameleoonData/conversion.js +47 -40
- package/dist/kameleoonData/conversion.js.map +1 -1
- package/dist/kameleoonData/customData.js +62 -58
- package/dist/kameleoonData/customData.js.map +1 -1
- package/dist/kameleoonData/device.js +37 -32
- package/dist/kameleoonData/device.js.map +1 -1
- package/dist/kameleoonData/index.js +101 -18
- package/dist/kameleoonData/index.js.map +1 -1
- package/dist/kameleoonData/nonce.js +25 -13
- package/dist/kameleoonData/nonce.js.map +1 -1
- package/dist/kameleoonData/pageView.js +52 -44
- package/dist/kameleoonData/pageView.js.map +1 -1
- package/dist/kameleoonData/types.js +41 -26
- package/dist/kameleoonData/types.js.map +1 -1
- package/dist/kameleoonData/userAgent.js +33 -24
- package/dist/kameleoonData/userAgent.js.map +1 -1
- package/dist/kameleoonError/constants.js +25 -21
- package/dist/kameleoonError/constants.js.map +1 -1
- package/dist/kameleoonError/helpers.js +6 -4
- package/dist/kameleoonError/helpers.js.map +1 -1
- package/dist/kameleoonError/index.js +25 -8
- package/dist/kameleoonError/index.js.map +1 -1
- package/dist/kameleoonError/kameleoonError.js +64 -53
- package/dist/kameleoonError/kameleoonError.js.map +1 -1
- package/dist/kameleoonError/types.js +26 -22
- package/dist/kameleoonError/types.js.map +1 -1
- package/dist/kameleoonUtils.js +34 -33
- package/dist/kameleoonUtils.js.map +1 -1
- package/dist/requester/constants.d.ts +6 -7
- package/dist/requester/constants.js +58 -45
- package/dist/requester/constants.js.map +1 -1
- package/dist/requester/index.d.ts +2 -2
- package/dist/requester/index.js +67 -8
- package/dist/requester/index.js.map +1 -1
- package/dist/requester/requester.d.ts +7 -7
- package/dist/requester/requester.js +100 -113
- package/dist/requester/requester.js.map +1 -1
- package/dist/requester/types.d.ts +38 -10
- package/dist/requester/types.js +14 -36
- package/dist/requester/types.js.map +1 -1
- package/dist/storage/constants.js +11 -7
- package/dist/storage/constants.js.map +1 -1
- package/dist/storage/externalStorage.js +33 -26
- package/dist/storage/externalStorage.js.map +1 -1
- package/dist/storage/index.js +55 -6
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/types.js +4 -1
- package/dist/storage/types.js.map +1 -1
- package/dist/targeting/conditions/conditionFactory.js +24 -19
- package/dist/targeting/conditions/conditionFactory.js.map +1 -1
- package/dist/targeting/conditions/customDataCondition.js +99 -88
- package/dist/targeting/conditions/customDataCondition.js.map +1 -1
- package/dist/targeting/conditions/exclusiveExperimentCondition.js +27 -15
- package/dist/targeting/conditions/exclusiveExperimentCondition.js.map +1 -1
- package/dist/targeting/conditions/index.js +45 -10
- package/dist/targeting/conditions/index.js.map +1 -1
- package/dist/targeting/conditions/targetExperimentCondition.js +54 -32
- package/dist/targeting/conditions/targetExperimentCondition.js.map +1 -1
- package/dist/targeting/conditions/types.js +4 -1
- package/dist/targeting/conditions/types.js.map +1 -1
- package/dist/targeting/index.js +79 -13
- package/dist/targeting/index.js.map +1 -1
- package/dist/targeting/targetingNode.js +41 -33
- package/dist/targeting/targetingNode.js.map +1 -1
- package/dist/targeting/targetingTree.js +125 -104
- package/dist/targeting/targetingTree.js.map +1 -1
- package/dist/targeting/types.js +99 -91
- package/dist/targeting/types.js.map +1 -1
- package/dist/types.d.ts +17 -2
- package/dist/types.js +50 -21
- package/dist/types.js.map +1 -1
- package/dist/utilities/constants.js +6 -2
- package/dist/utilities/constants.js.map +1 -1
- package/dist/utilities/index.js +11 -4
- package/dist/utilities/index.js.map +1 -1
- package/dist/utilities/types.d.ts +1 -1
- package/dist/utilities/types.js +4 -1
- package/dist/utilities/types.js.map +1 -1
- package/dist/utilities/utilities.d.ts +2 -2
- package/dist/utilities/utilities.js +117 -105
- package/dist/utilities/utilities.js.map +1 -1
- package/dist/variationConfiguration/index.js +24 -4
- package/dist/variationConfiguration/index.js.map +1 -1
- package/dist/variationConfiguration/types.js +4 -1
- package/dist/variationConfiguration/types.js.map +1 -1
- package/dist/variationConfiguration/variation.js +35 -28
- package/dist/variationConfiguration/variation.js.map +1 -1
- package/dist/variationConfiguration/variationConfiguration.d.ts +5 -5
- package/dist/variationConfiguration/variationConfiguration.js +270 -228
- package/dist/variationConfiguration/variationConfiguration.js.map +1 -1
- package/package.json +7 -5
package/dist/kameleoonClient.js
CHANGED
|
@@ -1,263 +1,334 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
12
6
|
exports.KameleoonClient = void 0;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
7
|
+
var _tsRes = require("ts-res");
|
|
8
|
+
var _campaignConfiguration = require("./campaignConfiguration");
|
|
9
|
+
var _clientSettings = require("./clientSettings");
|
|
10
|
+
var _constants = require("./constants");
|
|
11
|
+
var _kameleoonData = require("./kameleoonData");
|
|
12
|
+
var _kameleoonError = require("./kameleoonError");
|
|
13
|
+
var _requester = require("./requester");
|
|
14
|
+
var _storage = require("./storage");
|
|
15
|
+
var _externalStorage = require("./storage/externalStorage");
|
|
16
|
+
var _utilities = require("./utilities");
|
|
17
|
+
var _variationConfiguration = require("./variationConfiguration");
|
|
18
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
19
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
20
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
21
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
22
|
+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
24
23
|
/**
|
|
25
24
|
* @class
|
|
26
25
|
* KameleoonClient - a class for creating kameleoon client instance
|
|
27
26
|
* for communicating with Kameleoon JavaScript SDK Core, when new instance is created
|
|
28
|
-
|
|
27
|
+
*/
|
|
29
28
|
class KameleoonClient {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
29
|
+
/**
|
|
30
|
+
* @param {SDKCoreParameters} sdkCoreParameters - parameters for initializing sdk core
|
|
31
|
+
*/
|
|
32
|
+
constructor({
|
|
33
|
+
siteCode,
|
|
34
|
+
configuration,
|
|
35
|
+
internalConfiguration
|
|
36
|
+
}) {
|
|
37
|
+
_defineProperty(this, "internalConfiguration", void 0);
|
|
38
|
+
_defineProperty(this, "variationConfiguration", void 0);
|
|
39
|
+
_defineProperty(this, "campaignConfiguration", void 0);
|
|
40
|
+
_defineProperty(this, "settings", void 0);
|
|
41
|
+
_defineProperty(this, "requester", void 0);
|
|
42
|
+
const {
|
|
43
|
+
settings: clientSettings
|
|
44
|
+
} = new _clientSettings.ClientSettings(siteCode, configuration);
|
|
45
|
+
const {
|
|
46
|
+
externalPackageInfo,
|
|
47
|
+
externalRequestDispatcher
|
|
48
|
+
} = internalConfiguration;
|
|
49
|
+
const requester = new _requester.Requester({
|
|
50
|
+
siteCode,
|
|
51
|
+
environment: clientSettings.environment,
|
|
52
|
+
packageInfo: externalPackageInfo,
|
|
53
|
+
requestDispatcher: externalRequestDispatcher
|
|
54
|
+
});
|
|
55
|
+
this.requester = requester;
|
|
56
|
+
this.settings = clientSettings;
|
|
57
|
+
this.internalConfiguration = internalConfiguration;
|
|
58
|
+
}
|
|
59
|
+
async initialize() {
|
|
60
|
+
const {
|
|
61
|
+
externalStorage,
|
|
62
|
+
externalEventSource,
|
|
63
|
+
externalClientConfiguration
|
|
64
|
+
} = this.internalConfiguration;
|
|
65
|
+
const clientDataStorage = new _externalStorage.ExternalStorage(externalStorage.initialize(_storage.KameleoonStorageKey.ClientData));
|
|
66
|
+
const variationDataStorage = new _externalStorage.ExternalStorage(externalStorage.initialize(_storage.KameleoonStorageKey.VariationData));
|
|
67
|
+
const campaignConfiguration = new _campaignConfiguration.CampaignConfiguration({
|
|
68
|
+
settings: this.settings,
|
|
69
|
+
storage: clientDataStorage,
|
|
70
|
+
requester: this.requester,
|
|
71
|
+
externalClientConfiguration
|
|
72
|
+
});
|
|
73
|
+
const result = await campaignConfiguration.initialize(externalEventSource);
|
|
74
|
+
result.throw();
|
|
75
|
+
const variationConfiguration = new _variationConfiguration.VariationConfiguration(campaignConfiguration.experiments, variationDataStorage);
|
|
76
|
+
this.variationConfiguration = variationConfiguration;
|
|
77
|
+
this.campaignConfiguration = campaignConfiguration;
|
|
78
|
+
return result.ok;
|
|
79
|
+
}
|
|
80
|
+
addData(visitorCode, ...data) {
|
|
81
|
+
_utilities.Utilities.validateVisitorCode(visitorCode).throw();
|
|
82
|
+
if (!this.campaignConfiguration) {
|
|
83
|
+
throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);
|
|
39
84
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
settings: this.settings,
|
|
47
|
-
storage: clientDataStorage,
|
|
48
|
-
requester: this.requester,
|
|
49
|
-
});
|
|
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
|
-
});
|
|
85
|
+
this.campaignConfiguration.addTargetingData(visitorCode, ...data).throw();
|
|
86
|
+
}
|
|
87
|
+
triggerExperiment(visitorCode, experimentId) {
|
|
88
|
+
_utilities.Utilities.validateVisitorCode(visitorCode).throw();
|
|
89
|
+
if (!this.campaignConfiguration || !this.variationConfiguration) {
|
|
90
|
+
throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);
|
|
57
91
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
throw new kameleoonError_1.KameleoonError(kameleoonError_1.KameleoonException.Initialization);
|
|
62
|
-
}
|
|
63
|
-
this.campaignConfiguration.addTargetingData(visitorCode, ...data).throw();
|
|
92
|
+
const experiment = this.campaignConfiguration.experiments.find(item => item.id === String(experimentId));
|
|
93
|
+
if (!experiment) {
|
|
94
|
+
throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.ExperimentConfigurationNotFound, experimentId, visitorCode);
|
|
64
95
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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,
|
|
90
|
-
};
|
|
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;
|
|
96
|
+
const targetingData = this.campaignConfiguration.storedTargetingData[visitorCode];
|
|
97
|
+
const targetingResult = _utilities.Utilities.checkTargeting({
|
|
98
|
+
visitorCode,
|
|
99
|
+
experimentId,
|
|
100
|
+
targetingData,
|
|
101
|
+
segment: experiment.segment,
|
|
102
|
+
variationConfiguration: this.variationConfiguration
|
|
103
|
+
}).throw();
|
|
104
|
+
if (!targetingResult) {
|
|
105
|
+
throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.NotTargeted, visitorCode);
|
|
103
106
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
+
const dataUrl = _utilities.Utilities.getDataUrl(visitorCode, this.campaignConfiguration);
|
|
108
|
+
const trackingParameters = {
|
|
109
|
+
body: dataUrl,
|
|
110
|
+
visitorCode,
|
|
111
|
+
experimentId
|
|
112
|
+
};
|
|
113
|
+
const userAgent = _utilities.Utilities.getUserAgent(visitorCode, this.campaignConfiguration);
|
|
114
|
+
const variationId = this.variationConfiguration.getAssociatedVariationId(visitorCode, experimentId).else(error => {
|
|
115
|
+
if (error.type === _kameleoonError.KameleoonException.NotAllocated) {
|
|
116
|
+
this.requester.trackExperiment(_objectSpread({
|
|
117
|
+
variationId: _constants.UNALLOCATED_VARIATION_ID,
|
|
118
|
+
isUnallocated: true,
|
|
119
|
+
userAgent
|
|
120
|
+
}, trackingParameters));
|
|
121
|
+
return _constants.UNALLOCATED_VARIATION_ID;
|
|
122
|
+
}
|
|
123
|
+
throw error;
|
|
124
|
+
});
|
|
125
|
+
this.requester.trackExperiment(_objectSpread({
|
|
126
|
+
variationId,
|
|
127
|
+
isUnallocated: false,
|
|
128
|
+
userAgent
|
|
129
|
+
}, trackingParameters));
|
|
130
|
+
return variationId;
|
|
131
|
+
}
|
|
132
|
+
trackConversion({
|
|
133
|
+
visitorCode,
|
|
134
|
+
goalId,
|
|
135
|
+
revenue
|
|
136
|
+
}) {
|
|
137
|
+
this.addData(visitorCode, new _kameleoonData.Conversion({
|
|
138
|
+
goalId,
|
|
139
|
+
revenue
|
|
140
|
+
}));
|
|
141
|
+
this.flushData(visitorCode);
|
|
142
|
+
}
|
|
143
|
+
flushData(visitorCode) {
|
|
144
|
+
if (!this.campaignConfiguration) {
|
|
145
|
+
throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);
|
|
107
146
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
147
|
+
const dataUrl = _utilities.Utilities.getDataUrl(visitorCode, this.campaignConfiguration);
|
|
148
|
+
const userAgent = _utilities.Utilities.getUserAgent(visitorCode, this.campaignConfiguration);
|
|
149
|
+
this.requester.trackData({
|
|
150
|
+
visitorCode,
|
|
151
|
+
body: dataUrl,
|
|
152
|
+
userAgent
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
getExperiments() {
|
|
156
|
+
if (!this.campaignConfiguration) {
|
|
157
|
+
throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);
|
|
115
158
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
159
|
+
return this.campaignConfiguration.experiments.map(({
|
|
160
|
+
id,
|
|
161
|
+
name
|
|
162
|
+
}) => ({
|
|
163
|
+
id: Number(id),
|
|
164
|
+
name
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
getVisitorExperiments(visitorCode, isActive = true) {
|
|
168
|
+
if (!this.variationConfiguration || !this.campaignConfiguration) {
|
|
169
|
+
throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);
|
|
124
170
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
171
|
+
_utilities.Utilities.validateVisitorCode(visitorCode).throw();
|
|
172
|
+
const experiments = this.campaignConfiguration.experiments;
|
|
173
|
+
const result = [];
|
|
174
|
+
const targetingData = this.campaignConfiguration.storedTargetingData[visitorCode];
|
|
175
|
+
for (const experiment of experiments) {
|
|
176
|
+
const {
|
|
177
|
+
id,
|
|
178
|
+
name,
|
|
179
|
+
segment
|
|
180
|
+
} = experiment;
|
|
181
|
+
const targetingResult = _utilities.Utilities.checkTargeting({
|
|
182
|
+
targetingData,
|
|
183
|
+
visitorCode,
|
|
184
|
+
segment,
|
|
185
|
+
variationConfiguration: this.variationConfiguration,
|
|
186
|
+
experimentId: Number(id)
|
|
187
|
+
}).throw();
|
|
188
|
+
if (!targetingResult) {
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// -- If `isActive=false` then return every experiment
|
|
193
|
+
if (!isActive) {
|
|
194
|
+
result.push({
|
|
195
|
+
id: Number(id),
|
|
196
|
+
name
|
|
197
|
+
});
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// -- Else first check if there are saved variations for the experiment
|
|
202
|
+
// if there is at least one than it's active
|
|
203
|
+
const experimentVariations = this.variationConfiguration.getAssignedVariations(visitorCode);
|
|
204
|
+
if (experimentVariations.ok) {
|
|
205
|
+
const experimentVariation = experimentVariations.data[experiment.id];
|
|
206
|
+
if (experimentVariation) {
|
|
207
|
+
result.push({
|
|
208
|
+
id: Number(id),
|
|
209
|
+
name
|
|
210
|
+
});
|
|
211
|
+
continue;
|
|
166
212
|
}
|
|
167
|
-
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// -- Second check if the experiment will have an allocated variation upon triggering
|
|
216
|
+
// if yes than it's active
|
|
217
|
+
const variationResult = this.variationConfiguration.getExperimentVariation(visitorCode, experiment);
|
|
218
|
+
if (variationResult.ok) {
|
|
219
|
+
result.push({
|
|
220
|
+
id: Number(id),
|
|
221
|
+
name
|
|
222
|
+
});
|
|
223
|
+
}
|
|
168
224
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
225
|
+
return result;
|
|
226
|
+
}
|
|
227
|
+
getFeatureFlags() {
|
|
228
|
+
if (!this.campaignConfiguration) {
|
|
229
|
+
throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);
|
|
174
230
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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;
|
|
231
|
+
return this.campaignConfiguration.featureFlags.map(({
|
|
232
|
+
id,
|
|
233
|
+
featureKey
|
|
234
|
+
}) => ({
|
|
235
|
+
id,
|
|
236
|
+
key: featureKey
|
|
237
|
+
}));
|
|
238
|
+
}
|
|
239
|
+
getVisitorFeatureFlags(visitorCode) {
|
|
240
|
+
if (!this.variationConfiguration || !this.campaignConfiguration) {
|
|
241
|
+
throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);
|
|
194
242
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
243
|
+
const featureFlags = this.campaignConfiguration.featureFlags;
|
|
244
|
+
const resultFeatureFlags = [];
|
|
245
|
+
const targetingData = this.campaignConfiguration.storedTargetingData[visitorCode];
|
|
246
|
+
for (const featureFlag of featureFlags) {
|
|
247
|
+
const variationData = this.variationConfiguration.getFeatureFlagVariation({
|
|
248
|
+
visitorCode,
|
|
249
|
+
targetingData,
|
|
250
|
+
featureFlag
|
|
251
|
+
}).throw();
|
|
252
|
+
if (variationData.variationKey !== _constants.OFF_VARIATION_KEY) {
|
|
253
|
+
const {
|
|
254
|
+
featureFlagId,
|
|
255
|
+
featureKey
|
|
256
|
+
} = variationData;
|
|
257
|
+
resultFeatureFlags.push({
|
|
258
|
+
id: featureFlagId,
|
|
259
|
+
key: featureKey
|
|
260
|
+
});
|
|
261
|
+
}
|
|
198
262
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
263
|
+
return resultFeatureFlags;
|
|
264
|
+
}
|
|
265
|
+
getFeatureFlagVariationKey(visitorCode, featureKey) {
|
|
266
|
+
const {
|
|
267
|
+
variationKey
|
|
268
|
+
} = this._getFeatureFlagVariation(visitorCode, featureKey).throw();
|
|
269
|
+
return variationKey;
|
|
270
|
+
}
|
|
271
|
+
getFeatureFlagVariable({
|
|
272
|
+
visitorCode,
|
|
273
|
+
featureKey,
|
|
274
|
+
variableKey
|
|
275
|
+
}) {
|
|
276
|
+
const {
|
|
277
|
+
variationKey
|
|
278
|
+
} = this._getFeatureFlagVariation(visitorCode, featureKey).throw();
|
|
279
|
+
|
|
280
|
+
// --- Note ---
|
|
281
|
+
// `_getFeatureFlagVariation` already checks `campaignConfiguration` initialization
|
|
282
|
+
const featureFlags = this.campaignConfiguration.featureFlags;
|
|
283
|
+
const variable = featureFlags.find(item => item.featureKey === featureKey)?.variations.find(item => item.key === variationKey)?.variables.find(item => item.key === variableKey);
|
|
284
|
+
if (!variable) {
|
|
285
|
+
throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagConfigurationNotFound, featureKey, visitorCode);
|
|
213
286
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
287
|
+
const featureVariable = _utilities.Utilities.parseFeatureVariable(variable).throw();
|
|
288
|
+
return featureVariable;
|
|
289
|
+
}
|
|
290
|
+
isFeatureFlagActive(visitorCode, featureKey) {
|
|
291
|
+
const variationKey = this.getFeatureFlagVariationKey(visitorCode, featureKey);
|
|
292
|
+
return variationKey !== _constants.OFF_VARIATION_KEY;
|
|
293
|
+
}
|
|
294
|
+
async getRemoteData(key) {
|
|
295
|
+
const remoteData = await this.requester.getRemoteData(key);
|
|
296
|
+
return remoteData.throw();
|
|
297
|
+
}
|
|
298
|
+
onConfigurationUpdate(callback) {
|
|
299
|
+
if (!this.campaignConfiguration) {
|
|
300
|
+
throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);
|
|
217
301
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
302
|
+
this.campaignConfiguration.onConfigurationUpdate(callback);
|
|
303
|
+
}
|
|
304
|
+
_getFeatureFlagVariation(visitorCode, featureKey) {
|
|
305
|
+
if (!this.variationConfiguration || !this.campaignConfiguration) {
|
|
306
|
+
return (0, _tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization));
|
|
223
307
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
this.campaignConfiguration.onConfigurationUpdate(callback);
|
|
308
|
+
const featureFlags = this.campaignConfiguration.featureFlags;
|
|
309
|
+
const featureFlag = featureFlags.find(item => item.featureKey === featureKey);
|
|
310
|
+
if (!featureFlag) {
|
|
311
|
+
return (0, _tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagConfigurationNotFound, featureKey, visitorCode));
|
|
229
312
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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);
|
|
313
|
+
const targetingData = this.campaignConfiguration.storedTargetingData[visitorCode];
|
|
314
|
+
const variationData = this.variationConfiguration.getFeatureFlagVariation({
|
|
315
|
+
visitorCode,
|
|
316
|
+
featureFlag,
|
|
317
|
+
targetingData,
|
|
318
|
+
withAssignment: true
|
|
319
|
+
}).throw();
|
|
320
|
+
const userAgent = _utilities.Utilities.getUserAgent(visitorCode, this.campaignConfiguration);
|
|
321
|
+
if (variationData.rule) {
|
|
322
|
+
_utilities.Utilities.trackRule({
|
|
323
|
+
visitorCode,
|
|
324
|
+
requester: this.requester,
|
|
325
|
+
featureFlagVariation: variationData,
|
|
326
|
+
campaignConfiguration: this.campaignConfiguration,
|
|
327
|
+
userAgent
|
|
328
|
+
});
|
|
260
329
|
}
|
|
330
|
+
return (0, _tsRes.Ok)(variationData);
|
|
331
|
+
}
|
|
261
332
|
}
|
|
262
333
|
exports.KameleoonClient = KameleoonClient;
|
|
263
334
|
//# sourceMappingURL=kameleoonClient.js.map
|