@searchspring/snap-controller 0.73.6 → 0.74.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.
@@ -73,6 +73,9 @@ var types_1 = require("../types");
73
73
  var isClickWithinProductLink_1 = require("../utils/isClickWithinProductLink");
74
74
  var defaultConfig = {
75
75
  id: 'recommend',
76
+ beacon: {
77
+ enabled: true,
78
+ },
76
79
  tag: '',
77
80
  batched: true,
78
81
  realtime: false,
@@ -89,28 +92,37 @@ var RecommendationController = /** @class */ (function (_super) {
89
92
  _this.track = {
90
93
  product: {
91
94
  clickThrough: function (e, result) {
92
- var _a, _b, _c, _d;
95
+ var _a, _b, _c, _d, _e, _f;
96
+ if (!result) {
97
+ _this.log.warn('No result provided to track.product.clickThrough');
98
+ return;
99
+ }
93
100
  var responseId = result.responseId;
94
101
  if ((_b = (_a = _this.events[responseId]) === null || _a === void 0 ? void 0 : _a.product[result.id]) === null || _b === void 0 ? void 0 : _b.productClickThrough)
95
102
  return;
96
- var beaconResult = {
97
- type: result.type,
98
- uid: result.id,
99
- parentId: result.id,
100
- sku: (_c = result.mappings.core) === null || _c === void 0 ? void 0 : _c.sku,
101
- };
103
+ var type = (['product', 'banner'].includes(result.type) ? result.type : 'product');
104
+ var beaconResult = __assign({ type: type, uid: result.id ? '' + result.id : '' }, (type === 'product'
105
+ ? {
106
+ parentId: result.id ? '' + result.id : '',
107
+ sku: ((_c = result.mappings.core) === null || _c === void 0 ? void 0 : _c.sku) ? '' + ((_d = result.mappings.core) === null || _d === void 0 ? void 0 : _d.sku) : undefined,
108
+ }
109
+ : {}));
102
110
  var data = {
103
111
  tag: _this.store.profile.tag,
104
112
  responseId: responseId,
105
113
  results: [beaconResult],
106
114
  };
107
115
  _this.eventManager.fire('track.product.clickThrough', { controller: _this, event: e, product: result, trackEvent: data });
108
- _this.tracker.events.recommendations.clickThrough({ data: data, siteId: (_d = _this.config.globals) === null || _d === void 0 ? void 0 : _d.siteId });
116
+ ((_e = _this.config.beacon) === null || _e === void 0 ? void 0 : _e.enabled) && _this.tracker.events.recommendations.clickThrough({ data: data, siteId: (_f = _this.config.globals) === null || _f === void 0 ? void 0 : _f.siteId });
109
117
  _this.events[responseId].product[result.id] = _this.events[responseId].product[result.id] || {};
110
118
  _this.events[responseId].product[result.id].productClickThrough = true;
111
119
  },
112
120
  click: function (e, result) {
113
121
  var _a, _b, _c, _d, _e;
122
+ if (!result) {
123
+ _this.log.warn('No result provided to track.product.click');
124
+ return;
125
+ }
114
126
  var responseId = result.responseId;
115
127
  if (result.type === 'banner') {
116
128
  if ((_b = (_a = _this.events[responseId]) === null || _a === void 0 ? void 0 : _a.product[result.id]) === null || _b === void 0 ? void 0 : _b.inlineBannerClickThrough) {
@@ -136,17 +148,22 @@ var RecommendationController = /** @class */ (function (_super) {
136
148
  }
137
149
  },
138
150
  impression: function (result) {
139
- var _a, _b, _c, _d;
151
+ var _a, _b, _c, _d, _e, _f;
152
+ if (!result) {
153
+ _this.log.warn('No result provided to track.product.impression');
154
+ return;
155
+ }
140
156
  var responseId = result.responseId;
141
157
  if ((_b = (_a = _this.events[responseId]) === null || _a === void 0 ? void 0 : _a.product[result.id]) === null || _b === void 0 ? void 0 : _b.impression) {
142
158
  return;
143
159
  }
144
- var item = {
145
- type: result.type,
146
- uid: result.id,
147
- parentId: result.id,
148
- sku: (_c = result.mappings.core) === null || _c === void 0 ? void 0 : _c.sku,
149
- };
160
+ var type = (['product', 'banner'].includes(result.type) ? result.type : 'product');
161
+ var item = __assign({ type: type, uid: result.id ? '' + result.id : '' }, (type === 'product'
162
+ ? {
163
+ parentId: result.id ? '' + result.id : '',
164
+ sku: ((_c = result.mappings.core) === null || _c === void 0 ? void 0 : _c.sku) ? '' + ((_d = result.mappings.core) === null || _d === void 0 ? void 0 : _d.sku) : undefined,
165
+ }
166
+ : {}));
150
167
  var data = {
151
168
  tag: _this.store.profile.tag,
152
169
  responseId: responseId,
@@ -154,12 +171,16 @@ var RecommendationController = /** @class */ (function (_super) {
154
171
  banners: [],
155
172
  };
156
173
  _this.eventManager.fire('track.product.impression', { controller: _this, product: result, trackEvent: data });
157
- _this.tracker.events.recommendations.impression({ data: data, siteId: (_d = _this.config.globals) === null || _d === void 0 ? void 0 : _d.siteId });
174
+ ((_e = _this.config.beacon) === null || _e === void 0 ? void 0 : _e.enabled) && _this.tracker.events.recommendations.impression({ data: data, siteId: (_f = _this.config.globals) === null || _f === void 0 ? void 0 : _f.siteId });
158
175
  _this.events[responseId].product[result.id] = _this.events[responseId].product[result.id] || {};
159
176
  _this.events[responseId].product[result.id].impression = true;
160
177
  },
161
178
  addToCart: function (result) {
162
- var _a, _b, _c;
179
+ var _a, _b, _c, _d;
180
+ if (!result) {
181
+ _this.log.warn('No result provided to track.product.addToCart');
182
+ return;
183
+ }
163
184
  var responseId = result.responseId;
164
185
  var product = {
165
186
  parentId: result.id,
@@ -174,36 +195,37 @@ var RecommendationController = /** @class */ (function (_super) {
174
195
  results: [product],
175
196
  };
176
197
  _this.eventManager.fire('track.product.addToCart', { controller: _this, product: result, trackEvent: data });
177
- _this.tracker.events.recommendations.addToCart({ data: data, siteId: (_c = _this.config.globals) === null || _c === void 0 ? void 0 : _c.siteId });
198
+ ((_c = _this.config.beacon) === null || _c === void 0 ? void 0 : _c.enabled) && _this.tracker.events.recommendations.addToCart({ data: data, siteId: (_d = _this.config.globals) === null || _d === void 0 ? void 0 : _d.siteId });
178
199
  },
179
200
  },
180
201
  };
181
202
  _this.search = function () { return __awaiter(_this, void 0, void 0, function () {
182
- var params, err_1, searchProfile, response, responseId, afterSearchProfile, err_2, afterStoreProfile, err_3, err_4;
183
- return __generator(this, function (_a) {
184
- switch (_a.label) {
203
+ var params, err_1, searchProfile, response, responseId, afterSearchProfile, err_2, data, afterStoreProfile, err_3, err_4;
204
+ var _a, _b;
205
+ return __generator(this, function (_c) {
206
+ switch (_c.label) {
185
207
  case 0:
186
- _a.trys.push([0, 16, 17, 18]);
208
+ _c.trys.push([0, 16, 17, 18]);
187
209
  if (!!this.initialized) return [3 /*break*/, 2];
188
210
  return [4 /*yield*/, this.init()];
189
211
  case 1:
190
- _a.sent();
191
- _a.label = 2;
212
+ _c.sent();
213
+ _c.label = 2;
192
214
  case 2:
193
215
  params = this.params;
194
216
  this.store.loading = true;
195
- _a.label = 3;
217
+ _c.label = 3;
196
218
  case 3:
197
- _a.trys.push([3, 5, , 6]);
219
+ _c.trys.push([3, 5, , 6]);
198
220
  return [4 /*yield*/, this.eventManager.fire('beforeSearch', {
199
221
  controller: this,
200
222
  request: params,
201
223
  })];
202
224
  case 4:
203
- _a.sent();
225
+ _c.sent();
204
226
  return [3 /*break*/, 6];
205
227
  case 5:
206
- err_1 = _a.sent();
228
+ err_1 = _c.sent();
207
229
  if ((err_1 === null || err_1 === void 0 ? void 0 : err_1.message) == 'cancelled') {
208
230
  this.log.warn("'beforeSearch' middleware cancelled");
209
231
  return [2 /*return*/];
@@ -217,25 +239,25 @@ var RecommendationController = /** @class */ (function (_super) {
217
239
  searchProfile = this.profiler.create({ type: 'event', name: 'search', context: params }).start();
218
240
  return [4 /*yield*/, this.client.recommend(params)];
219
241
  case 7:
220
- response = _a.sent();
242
+ response = _c.sent();
221
243
  searchProfile.stop();
222
244
  this.log.profile(searchProfile);
223
245
  responseId = response.responseId;
224
246
  this.events[responseId] = this.events[responseId] || { product: {} };
225
247
  afterSearchProfile = this.profiler.create({ type: 'event', name: 'afterSearch', context: params }).start();
226
- _a.label = 8;
248
+ _c.label = 8;
227
249
  case 8:
228
- _a.trys.push([8, 10, , 11]);
250
+ _c.trys.push([8, 10, , 11]);
229
251
  return [4 /*yield*/, this.eventManager.fire('afterSearch', {
230
252
  controller: this,
231
253
  request: params,
232
254
  response: response,
233
255
  })];
234
256
  case 9:
235
- _a.sent();
257
+ _c.sent();
236
258
  return [3 /*break*/, 11];
237
259
  case 10:
238
- err_2 = _a.sent();
260
+ err_2 = _c.sent();
239
261
  if ((err_2 === null || err_2 === void 0 ? void 0 : err_2.message) == 'cancelled') {
240
262
  this.log.warn("'afterSearch' middleware cancelled");
241
263
  afterSearchProfile.stop();
@@ -251,20 +273,22 @@ var RecommendationController = /** @class */ (function (_super) {
251
273
  this.log.profile(afterSearchProfile);
252
274
  // update the store
253
275
  this.store.update(response);
276
+ data = { responseId: responseId, tag: this.store.profile.tag };
277
+ ((_a = this.config.beacon) === null || _a === void 0 ? void 0 : _a.enabled) && this.tracker.events.recommendations.render({ data: data, siteId: (_b = this.config.globals) === null || _b === void 0 ? void 0 : _b.siteId });
254
278
  afterStoreProfile = this.profiler.create({ type: 'event', name: 'afterStore', context: params }).start();
255
- _a.label = 12;
279
+ _c.label = 12;
256
280
  case 12:
257
- _a.trys.push([12, 14, , 15]);
281
+ _c.trys.push([12, 14, , 15]);
258
282
  return [4 /*yield*/, this.eventManager.fire('afterStore', {
259
283
  controller: this,
260
284
  request: params,
261
285
  response: response,
262
286
  })];
263
287
  case 13:
264
- _a.sent();
288
+ _c.sent();
265
289
  return [3 /*break*/, 15];
266
290
  case 14:
267
- err_3 = _a.sent();
291
+ err_3 = _c.sent();
268
292
  if ((err_3 === null || err_3 === void 0 ? void 0 : err_3.message) == 'cancelled') {
269
293
  this.log.warn("'afterStore' middleware cancelled");
270
294
  afterStoreProfile.stop();
@@ -280,7 +304,7 @@ var RecommendationController = /** @class */ (function (_super) {
280
304
  this.log.profile(afterStoreProfile);
281
305
  return [3 /*break*/, 18];
282
306
  case 16:
283
- err_4 = _a.sent();
307
+ err_4 = _c.sent();
284
308
  if (err_4) {
285
309
  if (err_4.err && err_4.fetchDetails) {
286
310
  switch (err_4.fetchDetails.status) {
@@ -332,7 +356,11 @@ var RecommendationController = /** @class */ (function (_super) {
332
356
  var products;
333
357
  var _this = this;
334
358
  return __generator(this, function (_a) {
335
- products = typeof _products.slice == 'function' ? _products.slice() : [_products];
359
+ products = typeof (_products === null || _products === void 0 ? void 0 : _products.slice) == 'function' ? _products.slice() : [_products];
360
+ if (!_products || products.length === 0) {
361
+ this.log.warn('No products provided to recommendation controller.addToCart');
362
+ return [2 /*return*/];
363
+ }
336
364
  products.forEach(function (product) {
337
365
  _this.track.product.addToCart(product);
338
366
  });
@@ -357,24 +385,6 @@ var RecommendationController = /** @class */ (function (_super) {
357
385
  // deep merge config with defaults
358
386
  _this.config = (0, deepmerge_1.default)(defaultConfig, _this.config);
359
387
  _this.store.setConfig(_this.config);
360
- _this.eventManager.on('afterStore', function (search, next) { return __awaiter(_this, void 0, void 0, function () {
361
- var controller, responseId, data;
362
- var _a;
363
- return __generator(this, function (_b) {
364
- switch (_b.label) {
365
- case 0: return [4 /*yield*/, next()];
366
- case 1:
367
- _b.sent();
368
- controller = search.controller;
369
- responseId = search.response.responseId;
370
- if (controller.store.loaded && !controller.store.error) {
371
- data = { responseId: responseId, tag: controller.store.profile.tag };
372
- this.tracker.events.recommendations.render({ data: data, siteId: (_a = this.config.globals) === null || _a === void 0 ? void 0 : _a.siteId });
373
- }
374
- return [2 /*return*/];
375
- }
376
- });
377
- }); });
378
388
  // add 'afterStore' middleware
379
389
  // this.eventManager.on('afterStore', async (recommend: AfterStoreObj, next: Next): Promise<void | boolean> => {
380
390
  // await next();
@@ -1 +1 @@
1
- {"version":3,"file":"SearchController.d.ts","sourceRoot":"","sources":["../../../src/Search/SearchController.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AACpE,OAAO,EAAE,YAAY,EAAa,0BAA0B,EAAE,MAAM,+BAA+B,CAAC;AAEpG,OAAO,EAAE,eAAe,EAAuB,MAAM,UAAU,CAAC;AAEhE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,EAAc,MAAM,+BAA+B,CAAC;AAC9F,OAAO,KAAK,EACX,sBAAsB,EAGtB,kBAAkB,EAClB,gBAAgB,EAIhB,MAAM,UAAU,CAAC;AAElB,OAAO,EACN,KAAK,kBAAkB,EAYvB,MAAM,2BAA2B,CAAC;AAsCnC,KAAK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