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