@constructor-io/constructorio-client-javascript 2.37.0 → 2.37.2
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/lib/constructorio.js +36 -60
- package/lib/modules/autocomplete.js +40 -55
- package/lib/modules/browse.js +86 -130
- package/lib/modules/quizzes.js +39 -60
- package/lib/modules/recommendations.js +38 -52
- package/lib/modules/search.js +66 -91
- package/lib/modules/tracker.js +430 -656
- package/lib/types/recommendations.d.ts +1 -1
- package/lib/utils/botlist.js +1 -1
- package/lib/utils/event-dispatcher.js +14 -20
- package/lib/utils/events.js +29 -95
- package/lib/utils/helpers.js +7 -29
- package/lib/utils/humanity-check.js +10 -19
- package/lib/utils/request-queue.js +28 -48
- package/lib/utils/store.js +2 -3
- package/lib/utils/store.overflow.js +6 -27
- package/lib/utils/store_old.js +9 -0
- package/package.json +1 -1
package/lib/constructorio.js
CHANGED
|
@@ -1,56 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
6
|
-
|
|
7
5
|
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
8
|
-
|
|
9
6
|
/* eslint-disable camelcase, no-unneeded-ternary, max-len, complexity */
|
|
10
7
|
var ConstructorioID = require('@constructor-io/constructorio-id');
|
|
8
|
+
var fetchPonyfill = require('fetch-ponyfill');
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
// Modules
|
|
15
11
|
var Search = require('./modules/search');
|
|
16
|
-
|
|
17
12
|
var Browse = require('./modules/browse');
|
|
18
|
-
|
|
19
13
|
var Autocomplete = require('./modules/autocomplete');
|
|
20
|
-
|
|
21
14
|
var Recommendations = require('./modules/recommendations');
|
|
22
|
-
|
|
23
15
|
var Tracker = require('./modules/tracker');
|
|
24
|
-
|
|
25
16
|
var EventDispatcher = require('./utils/event-dispatcher');
|
|
26
|
-
|
|
27
17
|
var helpers = require('./utils/helpers');
|
|
28
|
-
|
|
29
18
|
var _require = require('../package.json'),
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
var Quizzes = require('./modules/quizzes'); // Compute package version string
|
|
33
|
-
|
|
19
|
+
packageVersion = _require.version;
|
|
20
|
+
var Quizzes = require('./modules/quizzes');
|
|
34
21
|
|
|
22
|
+
// Compute package version string
|
|
35
23
|
var computePackageVersion = function computePackageVersion() {
|
|
36
24
|
var versionPrefix = 'ciojs-client-';
|
|
37
25
|
var versionModifiers = [];
|
|
38
|
-
|
|
39
26
|
if (!helpers.canUseDOM()) {
|
|
40
27
|
versionModifiers.push('domless');
|
|
41
28
|
}
|
|
42
|
-
|
|
43
29
|
if (typeof process !== 'undefined' && typeof process.env !== 'undefined' && process.env.BUNDLED) {
|
|
44
30
|
versionModifiers.push('bundled');
|
|
45
31
|
}
|
|
46
|
-
|
|
47
32
|
return "".concat(versionPrefix).concat(versionModifiers.join('-')).concat(versionModifiers.length ? '-' : '').concat(packageVersion);
|
|
48
33
|
};
|
|
34
|
+
|
|
49
35
|
/**
|
|
50
36
|
* Class to instantiate the ConstructorIO client.
|
|
51
37
|
*/
|
|
52
|
-
|
|
53
|
-
|
|
54
38
|
var ConstructorIO = /*#__PURE__*/function () {
|
|
55
39
|
/**
|
|
56
40
|
* @param {object} parameters - Parameters for client instantiation
|
|
@@ -84,48 +68,45 @@ var ConstructorIO = /*#__PURE__*/function () {
|
|
|
84
68
|
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
85
69
|
(0, _classCallCheck2["default"])(this, ConstructorIO);
|
|
86
70
|
var apiKey = options.apiKey,
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
71
|
+
versionFromOptions = options.version,
|
|
72
|
+
serviceUrl = options.serviceUrl,
|
|
73
|
+
quizzesServiceUrl = options.quizzesServiceUrl,
|
|
74
|
+
segments = options.segments,
|
|
75
|
+
testCells = options.testCells,
|
|
76
|
+
clientId = options.clientId,
|
|
77
|
+
sessionId = options.sessionId,
|
|
78
|
+
userId = options.userId,
|
|
79
|
+
fetch = options.fetch,
|
|
80
|
+
trackingSendDelay = options.trackingSendDelay,
|
|
81
|
+
sendReferrerWithTrackingEvents = options.sendReferrerWithTrackingEvents,
|
|
82
|
+
sendTrackingEvents = options.sendTrackingEvents,
|
|
83
|
+
eventDispatcher = options.eventDispatcher,
|
|
84
|
+
idOptions = options.idOptions,
|
|
85
|
+
beaconMode = options.beaconMode,
|
|
86
|
+
networkParameters = options.networkParameters;
|
|
104
87
|
if (!apiKey || typeof apiKey !== 'string') {
|
|
105
88
|
throw new Error('API key is a required parameter of type string');
|
|
106
89
|
}
|
|
107
|
-
|
|
108
90
|
var session_id;
|
|
109
91
|
var client_id;
|
|
110
|
-
var versionFromGlobal = typeof global !== 'undefined' && global.CLIENT_VERSION;
|
|
92
|
+
var versionFromGlobal = typeof global !== 'undefined' && global.CLIENT_VERSION;
|
|
111
93
|
|
|
94
|
+
// Initialize ID session if DOM context is available
|
|
112
95
|
if (helpers.canUseDOM()) {
|
|
113
96
|
var _ConstructorioID = new ConstructorioID(idOptions || {});
|
|
114
|
-
|
|
115
97
|
session_id = _ConstructorioID.session_id;
|
|
116
98
|
client_id = _ConstructorioID.client_id;
|
|
117
99
|
} else {
|
|
118
100
|
// Validate session ID is provided
|
|
119
101
|
if (!sessionId || typeof sessionId !== 'number') {
|
|
120
102
|
throw new Error('sessionId is a required user parameter of type number');
|
|
121
|
-
}
|
|
122
|
-
|
|
103
|
+
}
|
|
123
104
|
|
|
105
|
+
// Validate client ID is provided
|
|
124
106
|
if (!clientId || typeof clientId !== 'string') {
|
|
125
107
|
throw new Error('clientId is a required user parameter of type string');
|
|
126
108
|
}
|
|
127
109
|
}
|
|
128
|
-
|
|
129
110
|
this.options = {
|
|
130
111
|
apiKey: apiKey,
|
|
131
112
|
version: versionFromOptions || versionFromGlobal || computePackageVersion(),
|
|
@@ -146,17 +127,20 @@ var ConstructorIO = /*#__PURE__*/function () {
|
|
|
146
127
|
beaconMode: beaconMode === false ? false : true,
|
|
147
128
|
// Defaults to 'true',
|
|
148
129
|
networkParameters: networkParameters || {}
|
|
149
|
-
};
|
|
130
|
+
};
|
|
150
131
|
|
|
132
|
+
// Expose global modules
|
|
151
133
|
this.search = new Search(this.options);
|
|
152
134
|
this.browse = new Browse(this.options);
|
|
153
135
|
this.autocomplete = new Autocomplete(this.options);
|
|
154
136
|
this.recommendations = new Recommendations(this.options);
|
|
155
137
|
this.tracker = new Tracker(this.options);
|
|
156
|
-
this.quizzes = new Quizzes(this.options);
|
|
138
|
+
this.quizzes = new Quizzes(this.options);
|
|
157
139
|
|
|
140
|
+
// Dispatch initialization event
|
|
158
141
|
new EventDispatcher(options.eventDispatcher).queue('instantiated', this.options);
|
|
159
142
|
}
|
|
143
|
+
|
|
160
144
|
/**
|
|
161
145
|
* Sets the client options
|
|
162
146
|
*
|
|
@@ -165,29 +149,23 @@ var ConstructorIO = /*#__PURE__*/function () {
|
|
|
165
149
|
* @param {object} [testCells] - User test cells
|
|
166
150
|
* @param {string} [userId] - User ID
|
|
167
151
|
*/
|
|
168
|
-
|
|
169
|
-
|
|
170
152
|
(0, _createClass2["default"])(ConstructorIO, [{
|
|
171
153
|
key: "setClientOptions",
|
|
172
154
|
value: function setClientOptions(options) {
|
|
173
155
|
if (Object.keys(options).length) {
|
|
174
156
|
var apiKey = options.apiKey,
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
157
|
+
segments = options.segments,
|
|
158
|
+
testCells = options.testCells,
|
|
159
|
+
userId = options.userId;
|
|
179
160
|
if (apiKey) {
|
|
180
161
|
this.options.apiKey = apiKey;
|
|
181
162
|
}
|
|
182
|
-
|
|
183
163
|
if (segments) {
|
|
184
164
|
this.options.segments = segments;
|
|
185
165
|
}
|
|
186
|
-
|
|
187
166
|
if (testCells) {
|
|
188
167
|
this.options.testCells = testCells;
|
|
189
168
|
}
|
|
190
|
-
|
|
191
169
|
if (userId) {
|
|
192
170
|
this.options.userId = userId;
|
|
193
171
|
}
|
|
@@ -196,12 +174,10 @@ var ConstructorIO = /*#__PURE__*/function () {
|
|
|
196
174
|
}]);
|
|
197
175
|
return ConstructorIO;
|
|
198
176
|
}(); // Exposed for testing
|
|
177
|
+
ConstructorIO.Tracker = Tracker;
|
|
199
178
|
|
|
200
|
-
|
|
201
|
-
ConstructorIO.Tracker = Tracker; // Expose on window object if available
|
|
202
|
-
|
|
179
|
+
// Expose on window object if available
|
|
203
180
|
if (helpers.canUseDOM()) {
|
|
204
181
|
window.ConstructorioClient = ConstructorIO;
|
|
205
182
|
}
|
|
206
|
-
|
|
207
183
|
module.exports = ConstructorIO;
|
|
@@ -1,84 +1,81 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
6
|
-
|
|
7
5
|
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
8
|
-
|
|
9
6
|
/* eslint-disable object-curly-newline, no-underscore-dangle */
|
|
10
7
|
var EventDispatcher = require('../utils/event-dispatcher');
|
|
11
|
-
|
|
12
8
|
var _require = require('../utils/helpers'),
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
9
|
+
throwHttpErrorFromResponse = _require.throwHttpErrorFromResponse,
|
|
10
|
+
cleanParams = _require.cleanParams,
|
|
11
|
+
applyNetworkTimeout = _require.applyNetworkTimeout,
|
|
12
|
+
trimNonBreakingSpaces = _require.trimNonBreakingSpaces,
|
|
13
|
+
encodeURIComponentRFC3986 = _require.encodeURIComponentRFC3986,
|
|
14
|
+
stringify = _require.stringify;
|
|
15
|
+
|
|
16
|
+
// Create URL from supplied query (term) and parameters
|
|
21
17
|
function createAutocompleteUrl(query, parameters, options) {
|
|
22
18
|
var apiKey = options.apiKey,
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
19
|
+
version = options.version,
|
|
20
|
+
serviceUrl = options.serviceUrl,
|
|
21
|
+
sessionId = options.sessionId,
|
|
22
|
+
clientId = options.clientId,
|
|
23
|
+
userId = options.userId,
|
|
24
|
+
segments = options.segments,
|
|
25
|
+
testCells = options.testCells;
|
|
30
26
|
var queryParams = {
|
|
31
27
|
c: version
|
|
32
28
|
};
|
|
33
29
|
queryParams.key = apiKey;
|
|
34
30
|
queryParams.i = clientId;
|
|
35
|
-
queryParams.s = sessionId;
|
|
31
|
+
queryParams.s = sessionId;
|
|
36
32
|
|
|
33
|
+
// Validate query (term) is provided
|
|
37
34
|
if (!query || typeof query !== 'string') {
|
|
38
35
|
throw new Error('query is a required parameter of type string');
|
|
39
|
-
}
|
|
40
|
-
|
|
36
|
+
}
|
|
41
37
|
|
|
38
|
+
// Pull test cells from options
|
|
42
39
|
if (testCells) {
|
|
43
40
|
Object.keys(testCells).forEach(function (testCellKey) {
|
|
44
41
|
queryParams["ef-".concat(testCellKey)] = testCells[testCellKey];
|
|
45
42
|
});
|
|
46
|
-
}
|
|
47
|
-
|
|
43
|
+
}
|
|
48
44
|
|
|
45
|
+
// Pull user segments from options
|
|
49
46
|
if (segments && segments.length) {
|
|
50
47
|
queryParams.us = segments;
|
|
51
|
-
}
|
|
52
|
-
|
|
48
|
+
}
|
|
53
49
|
|
|
50
|
+
// Pull user id from options and ensure string
|
|
54
51
|
if (userId) {
|
|
55
52
|
queryParams.ui = String(userId);
|
|
56
53
|
}
|
|
57
|
-
|
|
58
54
|
if (parameters) {
|
|
59
55
|
var numResults = parameters.numResults,
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
56
|
+
resultsPerSection = parameters.resultsPerSection,
|
|
57
|
+
filters = parameters.filters,
|
|
58
|
+
hiddenFields = parameters.hiddenFields,
|
|
59
|
+
variationsMap = parameters.variationsMap;
|
|
64
60
|
|
|
61
|
+
// Pull results number from parameters
|
|
65
62
|
if (numResults) {
|
|
66
63
|
queryParams.num_results = numResults;
|
|
67
|
-
}
|
|
68
|
-
|
|
64
|
+
}
|
|
69
65
|
|
|
66
|
+
// Pull results number per section from parameters
|
|
70
67
|
if (resultsPerSection) {
|
|
71
68
|
Object.keys(resultsPerSection).forEach(function (section) {
|
|
72
69
|
queryParams["num_results_".concat(section)] = resultsPerSection[section];
|
|
73
70
|
});
|
|
74
|
-
}
|
|
75
|
-
|
|
71
|
+
}
|
|
76
72
|
|
|
73
|
+
// Pull filters from parameters
|
|
77
74
|
if (filters) {
|
|
78
75
|
queryParams.filters = filters;
|
|
79
|
-
}
|
|
80
|
-
|
|
76
|
+
}
|
|
81
77
|
|
|
78
|
+
// Pull hidden fields from parameters
|
|
82
79
|
if (hiddenFields) {
|
|
83
80
|
if (queryParams.fmt_options) {
|
|
84
81
|
queryParams.fmt_options.hidden_fields = hiddenFields;
|
|
@@ -87,14 +84,13 @@ function createAutocompleteUrl(query, parameters, options) {
|
|
|
87
84
|
hidden_fields: hiddenFields
|
|
88
85
|
};
|
|
89
86
|
}
|
|
90
|
-
}
|
|
91
|
-
|
|
87
|
+
}
|
|
92
88
|
|
|
89
|
+
// Pull variations map from parameters
|
|
93
90
|
if (variationsMap) {
|
|
94
91
|
queryParams.variations_map = JSON.stringify(variationsMap);
|
|
95
92
|
}
|
|
96
93
|
}
|
|
97
|
-
|
|
98
94
|
queryParams._dt = Date.now();
|
|
99
95
|
queryParams = cleanParams(queryParams);
|
|
100
96
|
var queryString = stringify(queryParams);
|
|
@@ -102,6 +98,7 @@ function createAutocompleteUrl(query, parameters, options) {
|
|
|
102
98
|
|
|
103
99
|
return "".concat(serviceUrl, "/autocomplete/").concat(encodeURIComponentRFC3986(trimNonBreakingSpaces(cleanedQuery)), "?").concat(queryString);
|
|
104
100
|
}
|
|
101
|
+
|
|
105
102
|
/**
|
|
106
103
|
* Interface to autocomplete related API calls.
|
|
107
104
|
*
|
|
@@ -109,14 +106,13 @@ function createAutocompleteUrl(query, parameters, options) {
|
|
|
109
106
|
* @inner
|
|
110
107
|
* @returns {object}
|
|
111
108
|
*/
|
|
112
|
-
|
|
113
|
-
|
|
114
109
|
var Autocomplete = /*#__PURE__*/function () {
|
|
115
110
|
function Autocomplete(options) {
|
|
116
111
|
(0, _classCallCheck2["default"])(this, Autocomplete);
|
|
117
112
|
this.options = options || {};
|
|
118
113
|
this.eventDispatcher = new EventDispatcher(options.eventDispatcher);
|
|
119
114
|
}
|
|
115
|
+
|
|
120
116
|
/**
|
|
121
117
|
* Retrieve autocomplete results from API
|
|
122
118
|
*
|
|
@@ -144,38 +140,32 @@ var Autocomplete = /*#__PURE__*/function () {
|
|
|
144
140
|
* },
|
|
145
141
|
* });
|
|
146
142
|
*/
|
|
147
|
-
|
|
148
|
-
|
|
149
143
|
(0, _createClass2["default"])(Autocomplete, [{
|
|
150
144
|
key: "getAutocompleteResults",
|
|
151
145
|
value: function getAutocompleteResults(query, parameters) {
|
|
152
146
|
var _this = this;
|
|
153
|
-
|
|
154
147
|
var networkParameters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
155
148
|
var requestUrl;
|
|
156
149
|
var fetch = this.options.fetch;
|
|
157
150
|
var signal;
|
|
158
|
-
|
|
159
151
|
if (typeof AbortController === 'function') {
|
|
160
152
|
var controller = new AbortController();
|
|
161
|
-
signal = controller && controller.signal;
|
|
153
|
+
signal = controller && controller.signal;
|
|
162
154
|
|
|
155
|
+
// Handle network timeout if specified
|
|
163
156
|
applyNetworkTimeout(this.options, networkParameters, controller);
|
|
164
157
|
}
|
|
165
|
-
|
|
166
158
|
try {
|
|
167
159
|
requestUrl = createAutocompleteUrl(query, parameters, this.options);
|
|
168
160
|
} catch (e) {
|
|
169
161
|
return Promise.reject(e);
|
|
170
162
|
}
|
|
171
|
-
|
|
172
163
|
return fetch(requestUrl, {
|
|
173
164
|
signal: signal
|
|
174
165
|
}).then(function (response) {
|
|
175
166
|
if (response.ok) {
|
|
176
167
|
return response.json();
|
|
177
168
|
}
|
|
178
|
-
|
|
179
169
|
return throwHttpErrorFromResponse(new Error(), response);
|
|
180
170
|
}).then(function (json) {
|
|
181
171
|
if (json.sections) {
|
|
@@ -183,7 +173,6 @@ var Autocomplete = /*#__PURE__*/function () {
|
|
|
183
173
|
var sectionKeys = Object.keys(json.sections);
|
|
184
174
|
sectionKeys.forEach(function (section) {
|
|
185
175
|
var sectionItems = json.sections[section];
|
|
186
|
-
|
|
187
176
|
if (sectionItems.length) {
|
|
188
177
|
// Append `result_id` to each section item
|
|
189
178
|
sectionItems.forEach(function (item) {
|
|
@@ -193,17 +182,13 @@ var Autocomplete = /*#__PURE__*/function () {
|
|
|
193
182
|
}
|
|
194
183
|
});
|
|
195
184
|
}
|
|
196
|
-
|
|
197
185
|
_this.eventDispatcher.queue('autocomplete.getAutocompleteResults.completed', json);
|
|
198
|
-
|
|
199
186
|
return json;
|
|
200
187
|
}
|
|
201
|
-
|
|
202
188
|
throw new Error('getAutocompleteResults response data is malformed');
|
|
203
189
|
});
|
|
204
190
|
}
|
|
205
191
|
}]);
|
|
206
192
|
return Autocomplete;
|
|
207
193
|
}();
|
|
208
|
-
|
|
209
194
|
module.exports = Autocomplete;
|