@algolia/autocomplete-plugin-algolia-insights 1.8.2 → 1.9.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.
@@ -1,23 +1,47 @@
1
- /*! @algolia/autocomplete-plugin-algolia-insights 1.8.2 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/autocomplete */
1
+ /*! @algolia/autocomplete-plugin-algolia-insights 1.9.0 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/autocomplete */
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
4
4
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
5
5
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@algolia/autocomplete-plugin-algolia-insights"] = {}));
6
6
  })(this, (function (exports) { 'use strict';
7
7
 
8
+ function _iterableToArrayLimit(arr, i) {
9
+ var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
10
+ if (null != _i) {
11
+ var _s,
12
+ _e,
13
+ _x,
14
+ _r,
15
+ _arr = [],
16
+ _n = !0,
17
+ _d = !1;
18
+ try {
19
+ if (_x = (_i = _i.call(arr)).next, 0 === i) {
20
+ if (Object(_i) !== _i) return;
21
+ _n = !1;
22
+ } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0);
23
+ } catch (err) {
24
+ _d = !0, _e = err;
25
+ } finally {
26
+ try {
27
+ if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return;
28
+ } finally {
29
+ if (_d) throw _e;
30
+ }
31
+ }
32
+ return _arr;
33
+ }
34
+ }
8
35
  function ownKeys(object, enumerableOnly) {
9
36
  var keys = Object.keys(object);
10
-
11
37
  if (Object.getOwnPropertySymbols) {
12
38
  var symbols = Object.getOwnPropertySymbols(object);
13
39
  enumerableOnly && (symbols = symbols.filter(function (sym) {
14
40
  return Object.getOwnPropertyDescriptor(object, sym).enumerable;
15
41
  })), keys.push.apply(keys, symbols);
16
42
  }
17
-
18
43
  return keys;
19
44
  }
20
-
21
45
  function _objectSpread2(target) {
22
46
  for (var i = 1; i < arguments.length; i++) {
23
47
  var source = null != arguments[i] ? arguments[i] : {};
@@ -27,11 +51,10 @@
27
51
  Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
28
52
  });
29
53
  }
30
-
31
54
  return target;
32
55
  }
33
-
34
56
  function _defineProperty(obj, key, value) {
57
+ key = _toPropertyKey(key);
35
58
  if (key in obj) {
36
59
  Object.defineProperty(obj, key, {
37
60
  value: value,
@@ -42,22 +65,50 @@
42
65
  } else {
43
66
  obj[key] = value;
44
67
  }
45
-
46
68
  return obj;
47
69
  }
48
-
70
+ function _objectWithoutPropertiesLoose(source, excluded) {
71
+ if (source == null) return {};
72
+ var target = {};
73
+ var sourceKeys = Object.keys(source);
74
+ var key, i;
75
+ for (i = 0; i < sourceKeys.length; i++) {
76
+ key = sourceKeys[i];
77
+ if (excluded.indexOf(key) >= 0) continue;
78
+ target[key] = source[key];
79
+ }
80
+ return target;
81
+ }
82
+ function _objectWithoutProperties(source, excluded) {
83
+ if (source == null) return {};
84
+ var target = _objectWithoutPropertiesLoose(source, excluded);
85
+ var key, i;
86
+ if (Object.getOwnPropertySymbols) {
87
+ var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
88
+ for (i = 0; i < sourceSymbolKeys.length; i++) {
89
+ key = sourceSymbolKeys[i];
90
+ if (excluded.indexOf(key) >= 0) continue;
91
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
92
+ target[key] = source[key];
93
+ }
94
+ }
95
+ return target;
96
+ }
97
+ function _slicedToArray(arr, i) {
98
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
99
+ }
49
100
  function _toConsumableArray(arr) {
50
101
  return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
51
102
  }
52
-
53
103
  function _arrayWithoutHoles(arr) {
54
104
  if (Array.isArray(arr)) return _arrayLikeToArray(arr);
55
105
  }
56
-
106
+ function _arrayWithHoles(arr) {
107
+ if (Array.isArray(arr)) return arr;
108
+ }
57
109
  function _iterableToArray(iter) {
58
110
  if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
59
111
  }
60
-
61
112
  function _unsupportedIterableToArray(o, minLen) {
62
113
  if (!o) return;
63
114
  if (typeof o === "string") return _arrayLikeToArray(o, minLen);
@@ -66,18 +117,31 @@
66
117
  if (n === "Map" || n === "Set") return Array.from(o);
67
118
  if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
68
119
  }
69
-
70
120
  function _arrayLikeToArray(arr, len) {
71
121
  if (len == null || len > arr.length) len = arr.length;
72
-
73
122
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
74
-
75
123
  return arr2;
76
124
  }
77
-
78
125
  function _nonIterableSpread() {
79
126
  throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
80
127
  }
128
+ function _nonIterableRest() {
129
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
130
+ }
131
+ function _toPrimitive(input, hint) {
132
+ if (typeof input !== "object" || input === null) return input;
133
+ var prim = input[Symbol.toPrimitive];
134
+ if (prim !== undefined) {
135
+ var res = prim.call(input, hint || "default");
136
+ if (typeof res !== "object") return res;
137
+ throw new TypeError("@@toPrimitive must return a primitive value.");
138
+ }
139
+ return (hint === "string" ? String : Number)(input);
140
+ }
141
+ function _toPropertyKey(arg) {
142
+ var key = _toPrimitive(arg, "string");
143
+ return typeof key === "symbol" ? key : String(key);
144
+ }
81
145
 
82
146
  function createRef(initialValue) {
83
147
  return {
@@ -91,11 +155,9 @@
91
155
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
92
156
  args[_key] = arguments[_key];
93
157
  }
94
-
95
158
  if (timerId) {
96
159
  clearTimeout(timerId);
97
160
  }
98
-
99
161
  timerId = setTimeout(function () {
100
162
  return fn.apply(void 0, args);
101
163
  }, time);
@@ -105,64 +167,116 @@
105
167
  function isPrimitive(obj) {
106
168
  return obj !== Object(obj);
107
169
  }
108
-
109
170
  function isEqual(first, second) {
110
171
  if (first === second) {
111
172
  return true;
112
173
  }
113
-
114
174
  if (isPrimitive(first) || isPrimitive(second) || typeof first === 'function' || typeof second === 'function') {
115
175
  return first === second;
116
176
  }
117
-
118
177
  if (Object.keys(first).length !== Object.keys(second).length) {
119
178
  return false;
120
179
  }
121
-
122
180
  for (var _i = 0, _Object$keys = Object.keys(first); _i < _Object$keys.length; _i++) {
123
181
  var key = _Object$keys[_i];
124
-
125
182
  if (!(key in second)) {
126
183
  return false;
127
184
  }
128
-
129
185
  if (!isEqual(first[key], second[key])) {
130
186
  return false;
131
187
  }
132
188
  }
133
-
134
189
  return true;
135
190
  }
136
191
 
137
192
  var noop = function noop() {};
138
193
 
194
+ /**
195
+ * Safely runs code meant for browser environments only.
196
+ */
197
+ function safelyRunOnBrowser(callback) {
198
+ if (typeof window !== 'undefined') {
199
+ return callback({
200
+ window: window
201
+ });
202
+ }
203
+ return undefined;
204
+ }
205
+
139
206
  function createClickedEvent(_ref) {
140
207
  var item = _ref.item,
141
- items = _ref.items;
208
+ items = _ref.items;
142
209
  return {
143
210
  index: item.__autocomplete_indexName,
144
- objectIDs: [item.objectID],
211
+ items: [item],
145
212
  positions: [1 + items.findIndex(function (x) {
146
213
  return x.objectID === item.objectID;
147
214
  })],
148
- queryID: item.__autocomplete_queryID
215
+ queryID: item.__autocomplete_queryID,
216
+ algoliaSource: ['autocomplete']
149
217
  };
150
218
  }
151
219
 
220
+ /**
221
+ * Determines if a given insights `client` supports the optional call to `init`
222
+ * and the ability to set credentials via extra parameters when sending events.
223
+ */
224
+ function isModernInsightsClient(client) {
225
+ var _split$map = (client.version || '').split('.').map(Number),
226
+ _split$map2 = _slicedToArray(_split$map, 2),
227
+ major = _split$map2[0],
228
+ minor = _split$map2[1];
229
+
230
+ /* eslint-disable @typescript-eslint/camelcase */
231
+ var v3 = major >= 3;
232
+ var v2_4 = major === 2 && minor >= 4;
233
+ var v1_10 = major === 1 && minor >= 10;
234
+ return v3 || v2_4 || v1_10;
235
+ /* eslint-enable @typescript-eslint/camelcase */
236
+ }
237
+
238
+ var _excluded = ["items"],
239
+ _excluded2 = ["items"];
152
240
  function chunk(item) {
153
241
  var chunkSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 20;
154
242
  var chunks = [];
155
-
156
243
  for (var i = 0; i < item.objectIDs.length; i += chunkSize) {
157
244
  chunks.push(_objectSpread2(_objectSpread2({}, item), {}, {
158
245
  objectIDs: item.objectIDs.slice(i, i + chunkSize)
159
246
  }));
160
247
  }
161
-
162
248
  return chunks;
163
249
  }
164
-
250
+ function mapToInsightsParamsApi(params) {
251
+ return params.map(function (_ref) {
252
+ var items = _ref.items,
253
+ param = _objectWithoutProperties(_ref, _excluded);
254
+ return _objectSpread2(_objectSpread2({}, param), {}, {
255
+ objectIDs: (items === null || items === void 0 ? void 0 : items.map(function (_ref2) {
256
+ var objectID = _ref2.objectID;
257
+ return objectID;
258
+ })) || param.objectIDs
259
+ });
260
+ });
261
+ }
165
262
  function createSearchInsightsApi(searchInsights) {
263
+ var canSendHeaders = isModernInsightsClient(searchInsights);
264
+ function sendToInsights(method, payloads, items) {
265
+ if (canSendHeaders && typeof items !== 'undefined') {
266
+ var _items$0$__autocomple = items[0].__autocomplete_algoliaCredentials,
267
+ appId = _items$0$__autocomple.appId,
268
+ apiKey = _items$0$__autocomple.apiKey;
269
+ var headers = {
270
+ 'X-Algolia-Application-Id': appId,
271
+ 'X-Algolia-API-Key': apiKey
272
+ };
273
+ searchInsights.apply(void 0, [method].concat(_toConsumableArray(payloads), [{
274
+ headers: headers
275
+ }]));
276
+ } else {
277
+ searchInsights.apply(void 0, [method].concat(_toConsumableArray(payloads)));
278
+ }
279
+ }
166
280
  return {
167
281
  /**
168
282
  * Initializes Insights with Algolia credentials.
@@ -173,14 +287,12 @@
173
287
  apiKey: apiKey
174
288
  });
175
289
  },
176
-
177
290
  /**
178
291
  * Sets the user token to attach to events.
179
292
  */
180
293
  setUserToken: function setUserToken(userToken) {
181
294
  searchInsights('setUserToken', userToken);
182
295
  },
183
-
184
296
  /**
185
297
  * Sends click events to capture a query and its clicked items and positions.
186
298
  *
@@ -190,12 +302,10 @@
190
302
  for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
191
303
  params[_key] = arguments[_key];
192
304
  }
193
-
194
305
  if (params.length > 0) {
195
- searchInsights.apply(void 0, ['clickedObjectIDsAfterSearch'].concat(params));
306
+ sendToInsights('clickedObjectIDsAfterSearch', mapToInsightsParamsApi(params), params[0].items);
196
307
  }
197
308
  },
198
-
199
309
  /**
200
310
  * Sends click events to capture clicked items.
201
311
  *
@@ -205,12 +315,10 @@
205
315
  for (var _len2 = arguments.length, params = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
206
316
  params[_key2] = arguments[_key2];
207
317
  }
208
-
209
318
  if (params.length > 0) {
210
- searchInsights.apply(void 0, ['clickedObjectIDs'].concat(params));
319
+ sendToInsights('clickedObjectIDs', mapToInsightsParamsApi(params), params[0].items);
211
320
  }
212
321
  },
213
-
214
322
  /**
215
323
  * Sends click events to capture the filters a user clicks on.
216
324
  *
@@ -220,12 +328,10 @@
220
328
  for (var _len3 = arguments.length, params = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
221
329
  params[_key3] = arguments[_key3];
222
330
  }
223
-
224
331
  if (params.length > 0) {
225
332
  searchInsights.apply(void 0, ['clickedFilters'].concat(params));
226
333
  }
227
334
  },
228
-
229
335
  /**
230
336
  * Sends conversion events to capture a query and its clicked items.
231
337
  *
@@ -235,12 +341,10 @@
235
341
  for (var _len4 = arguments.length, params = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
236
342
  params[_key4] = arguments[_key4];
237
343
  }
238
-
239
344
  if (params.length > 0) {
240
- searchInsights.apply(void 0, ['convertedObjectIDsAfterSearch'].concat(params));
345
+ sendToInsights('convertedObjectIDsAfterSearch', mapToInsightsParamsApi(params), params[0].items);
241
346
  }
242
347
  },
243
-
244
348
  /**
245
349
  * Sends conversion events to capture clicked items.
246
350
  *
@@ -250,12 +354,10 @@
250
354
  for (var _len5 = arguments.length, params = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
251
355
  params[_key5] = arguments[_key5];
252
356
  }
253
-
254
357
  if (params.length > 0) {
255
- searchInsights.apply(void 0, ['convertedObjectIDs'].concat(params));
358
+ sendToInsights('convertedObjectIDs', mapToInsightsParamsApi(params), params[0].items);
256
359
  }
257
360
  },
258
-
259
361
  /**
260
362
  * Sends conversion events to capture the filters a user uses when converting.
261
363
  *
@@ -265,12 +367,10 @@
265
367
  for (var _len6 = arguments.length, params = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
266
368
  params[_key6] = arguments[_key6];
267
369
  }
268
-
269
370
  if (params.length > 0) {
270
371
  searchInsights.apply(void 0, ['convertedFilters'].concat(params));
271
372
  }
272
373
  },
273
-
274
374
  /**
275
375
  * Sends view events to capture clicked items.
276
376
  *
@@ -280,16 +380,28 @@
280
380
  for (var _len7 = arguments.length, params = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
281
381
  params[_key7] = arguments[_key7];
282
382
  }
283
-
284
383
  if (params.length > 0) {
285
- params.reduce(function (acc, param) {
286
- return [].concat(_toConsumableArray(acc), _toConsumableArray(chunk(param)));
287
- }, []).forEach(function (param) {
288
- return searchInsights('viewedObjectIDs', param);
384
+ params.reduce(function (acc, _ref3) {
385
+ var items = _ref3.items,
386
+ param = _objectWithoutProperties(_ref3, _excluded2);
387
+ return [].concat(_toConsumableArray(acc), _toConsumableArray(chunk(_objectSpread2(_objectSpread2({}, param), {}, {
388
+ objectIDs: (items === null || items === void 0 ? void 0 : items.map(function (_ref4) {
389
+ var objectID = _ref4.objectID;
390
+ return objectID;
391
+ })) || param.objectIDs
392
+ })).map(function (payload) {
393
+ return {
394
+ items: items,
395
+ payload: payload
396
+ };
397
+ })));
398
+ }, []).forEach(function (_ref5) {
399
+ var items = _ref5.items,
400
+ payload = _ref5.payload;
401
+ return sendToInsights('viewedObjectIDs', [payload], items);
289
402
  });
290
403
  }
291
404
  },
292
-
293
405
  /**
294
406
  * Sends view events to capture the filters a user uses when viewing.
295
407
  *
@@ -299,7 +411,6 @@
299
411
  for (var _len8 = arguments.length, params = new Array(_len8), _key8 = 0; _key8 < _len8; _key8++) {
300
412
  params[_key8] = arguments[_key8];
301
413
  }
302
-
303
414
  if (params.length > 0) {
304
415
  searchInsights.apply(void 0, ['viewedFilters'].concat(params));
305
416
  }
@@ -309,17 +420,17 @@
309
420
 
310
421
  function createViewedEvents(_ref) {
311
422
  var items = _ref.items;
312
- var objectIDsByIndexName = items.reduce(function (acc, current) {
423
+ var itemsByIndexName = items.reduce(function (acc, current) {
313
424
  var _acc$current$__autoco;
314
-
315
- acc[current.__autocomplete_indexName] = ((_acc$current$__autoco = acc[current.__autocomplete_indexName]) !== null && _acc$current$__autoco !== void 0 ? _acc$current$__autoco : []).concat(current.objectID);
425
+ acc[current.__autocomplete_indexName] = ((_acc$current$__autoco = acc[current.__autocomplete_indexName]) !== null && _acc$current$__autoco !== void 0 ? _acc$current$__autoco : []).concat(current);
316
426
  return acc;
317
427
  }, {});
318
- return Object.keys(objectIDsByIndexName).map(function (indexName) {
319
- var objectIDs = objectIDsByIndexName[indexName];
428
+ return Object.keys(itemsByIndexName).map(function (indexName) {
429
+ var items = itemsByIndexName[indexName];
320
430
  return {
321
431
  index: indexName,
322
- objectIDs: objectIDs
432
+ items: items,
433
+ algoliaSource: ['autocomplete']
323
434
  };
324
435
  });
325
436
  }
@@ -329,11 +440,13 @@
329
440
  }
330
441
 
331
442
  var VIEW_EVENT_DELAY = 400;
443
+ var ALGOLIA_INSIGHTS_VERSION = '2.4.0';
444
+ var ALGOLIA_INSIGHTS_SRC = "https://cdn.jsdelivr.net/npm/search-insights@".concat(ALGOLIA_INSIGHTS_VERSION, "/dist/search-insights.min.js");
332
445
  var sendViewedObjectIDs = debounce(function (_ref) {
333
446
  var onItemsChange = _ref.onItemsChange,
334
- items = _ref.items,
335
- insights = _ref.insights,
336
- state = _ref.state;
447
+ items = _ref.items,
448
+ insights = _ref.insights,
449
+ state = _ref.state;
337
450
  onItemsChange({
338
451
  insights: insights,
339
452
  insightsEvents: createViewedEvents({
@@ -348,31 +461,53 @@
348
461
  }, VIEW_EVENT_DELAY);
349
462
  function createAlgoliaInsightsPlugin(options) {
350
463
  var _getOptions = getOptions(options),
351
- insightsClient = _getOptions.insightsClient,
352
- onItemsChange = _getOptions.onItemsChange,
353
- onSelectEvent = _getOptions.onSelect,
354
- onActiveEvent = _getOptions.onActive;
355
-
464
+ providedInsightsClient = _getOptions.insightsClient,
465
+ onItemsChange = _getOptions.onItemsChange,
466
+ onSelectEvent = _getOptions.onSelect,
467
+ onActiveEvent = _getOptions.onActive;
468
+ var insightsClient = providedInsightsClient;
469
+ if (!providedInsightsClient) {
470
+ safelyRunOnBrowser(function (_ref2) {
471
+ var window = _ref2.window;
472
+ var pointer = window.AlgoliaAnalyticsObject || 'aa';
473
+ if (typeof pointer === 'string') {
474
+ insightsClient = window[pointer];
475
+ }
476
+ if (!insightsClient) {
477
+ window.AlgoliaAnalyticsObject = pointer;
478
+ if (!window[pointer]) {
479
+ window[pointer] = function () {
480
+ if (!window[pointer].queue) {
481
+ window[pointer].queue = [];
482
+ }
483
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
484
+ args[_key] = arguments[_key];
485
+ }
486
+ window[pointer].queue.push(args);
487
+ };
488
+ }
489
+ window[pointer].version = ALGOLIA_INSIGHTS_VERSION;
490
+ insightsClient = window[pointer];
491
+ loadInsights(window);
492
+ }
493
+ });
494
+ }
356
495
  var insights = createSearchInsightsApi(insightsClient);
357
496
  var previousItems = createRef([]);
358
- var debouncedOnStateChange = debounce(function (_ref2) {
359
- var state = _ref2.state;
360
-
497
+ var debouncedOnStateChange = debounce(function (_ref3) {
498
+ var state = _ref3.state;
361
499
  if (!state.isOpen) {
362
500
  return;
363
501
  }
364
-
365
502
  var items = state.collections.reduce(function (acc, current) {
366
503
  return [].concat(_toConsumableArray(acc), _toConsumableArray(current.items));
367
504
  }, []).filter(isAlgoliaInsightsHit);
368
-
369
505
  if (!isEqual(previousItems.current.map(function (x) {
370
506
  return x.objectID;
371
507
  }), items.map(function (x) {
372
508
  return x.objectID;
373
509
  }))) {
374
510
  previousItems.current = items;
375
-
376
511
  if (items.length > 0) {
377
512
  sendViewedObjectIDs({
378
513
  onItemsChange: onItemsChange,
@@ -385,10 +520,10 @@
385
520
  }, 0);
386
521
  return {
387
522
  name: 'aa.algoliaInsightsPlugin',
388
- subscribe: function subscribe(_ref3) {
389
- var setContext = _ref3.setContext,
390
- onSelect = _ref3.onSelect,
391
- onActive = _ref3.onActive;
523
+ subscribe: function subscribe(_ref4) {
524
+ var setContext = _ref4.setContext,
525
+ onSelect = _ref4.onSelect,
526
+ onActive = _ref4.onActive;
392
527
  insightsClient('addAlgoliaAgent', 'insights-plugin');
393
528
  setContext({
394
529
  algoliaInsightsPlugin: {
@@ -398,15 +533,13 @@
398
533
  insights: insights
399
534
  }
400
535
  });
401
- onSelect(function (_ref4) {
402
- var item = _ref4.item,
403
- state = _ref4.state,
404
- event = _ref4.event;
405
-
536
+ onSelect(function (_ref5) {
537
+ var item = _ref5.item,
538
+ state = _ref5.state,
539
+ event = _ref5.event;
406
540
  if (!isAlgoliaInsightsHit(item)) {
407
541
  return;
408
542
  }
409
-
410
543
  onSelectEvent({
411
544
  state: state,
412
545
  event: event,
@@ -420,15 +553,13 @@
420
553
  }))]
421
554
  });
422
555
  });
423
- onActive(function (_ref5) {
424
- var item = _ref5.item,
425
- state = _ref5.state,
426
- event = _ref5.event;
427
-
556
+ onActive(function (_ref6) {
557
+ var item = _ref6.item,
558
+ state = _ref6.state,
559
+ event = _ref6.event;
428
560
  if (!isAlgoliaInsightsHit(item)) {
429
561
  return;
430
562
  }
431
-
432
563
  onActiveEvent({
433
564
  state: state,
434
565
  event: event,
@@ -443,8 +574,8 @@
443
574
  });
444
575
  });
445
576
  },
446
- onStateChange: function onStateChange(_ref6) {
447
- var state = _ref6.state;
577
+ onStateChange: function onStateChange(_ref7) {
578
+ var state = _ref7.state;
448
579
  debouncedOnStateChange({
449
580
  state: state
450
581
  });
@@ -452,22 +583,45 @@
452
583
  __autocomplete_pluginOptions: options
453
584
  };
454
585
  }
455
-
456
586
  function getOptions(options) {
457
587
  return _objectSpread2({
458
- onItemsChange: function onItemsChange(_ref7) {
459
- var insights = _ref7.insights,
460
- insightsEvents = _ref7.insightsEvents;
461
- insights.viewedObjectIDs.apply(insights, _toConsumableArray(insightsEvents));
462
- },
463
- onSelect: function onSelect(_ref8) {
588
+ onItemsChange: function onItemsChange(_ref8) {
464
589
  var insights = _ref8.insights,
465
- insightsEvents = _ref8.insightsEvents;
466
- insights.clickedObjectIDsAfterSearch.apply(insights, _toConsumableArray(insightsEvents));
590
+ insightsEvents = _ref8.insightsEvents;
591
+ insights.viewedObjectIDs.apply(insights, _toConsumableArray(insightsEvents.map(function (event) {
592
+ return _objectSpread2(_objectSpread2({}, event), {}, {
593
+ algoliaSource: [].concat(_toConsumableArray(event.algoliaSource || []), ['autocomplete-internal'])
594
+ });
595
+ })));
596
+ },
597
+ onSelect: function onSelect(_ref9) {
598
+ var insights = _ref9.insights,
599
+ insightsEvents = _ref9.insightsEvents;
600
+ insights.clickedObjectIDsAfterSearch.apply(insights, _toConsumableArray(insightsEvents.map(function (event) {
601
+ return _objectSpread2(_objectSpread2({}, event), {}, {
602
+ algoliaSource: [].concat(_toConsumableArray(event.algoliaSource || []), ['autocomplete-internal'])
603
+ });
604
+ })));
467
605
  },
468
606
  onActive: noop
469
607
  }, options);
470
608
  }
609
+ function loadInsights(environment) {
610
+ var errorMessage = "[Autocomplete]: Could not load search-insights.js. Please load it manually following https://alg.li/insights-autocomplete";
611
+ try {
612
+ var script = environment.document.createElement('script');
613
+ script.async = true;
614
+ script.src = ALGOLIA_INSIGHTS_SRC;
615
+ script.onerror = function () {
616
+ // eslint-disable-next-line no-console
617
+ console.error(errorMessage);
618
+ };
619
+ document.body.appendChild(script);
620
+ } catch (cause) {
621
+ // eslint-disable-next-line no-console
622
+ console.error(errorMessage);
623
+ }
624
+ }
471
625
 
472
626
  exports.createAlgoliaInsightsPlugin = createAlgoliaInsightsPlugin;
473
627