instantsearch.js 4.75.2 → 4.75.4

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.
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createUUID = createUUID;
7
+ /**
8
+ * Create UUID according to
9
+ * https://www.ietf.org/rfc/rfc4122.txt.
10
+ *
11
+ * @returns Generated UUID.
12
+ */
13
+ function createUUID() {
14
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
15
+ /* eslint-disable no-bitwise */
16
+ var r = Math.random() * 16 | 0;
17
+ var v = c === 'x' ? r : r & 0x3 | 0x8;
18
+ /* eslint-enable */
19
+ return v.toString(16);
20
+ });
21
+ }
@@ -4,5 +4,5 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _default = '4.75.2';
7
+ var _default = '4.75.4';
8
8
  exports.default = _default;
@@ -7,6 +7,7 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports.createInsightsMiddleware = createInsightsMiddleware;
8
8
  var _helpers = require("../helpers");
9
9
  var _utils = require("../lib/utils");
10
+ var _uuid = require("../lib/utils/uuid");
10
11
  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; }
11
12
  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; }
12
13
  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; }
@@ -33,6 +34,7 @@ function createInsightsMiddleware() {
33
34
  $$internal = _props$$$internal === void 0 ? false : _props$$$internal,
34
35
  _props$$$automatic = props.$$automatic,
35
36
  $$automatic = _props$$$automatic === void 0 ? false : _props$$$automatic;
37
+ var currentTokenType;
36
38
  var potentialInsightsClient = _insightsClient;
37
39
  if (!_insightsClient && _insightsClient !== null) {
38
40
  (0, _utils.safelyRunOnBrowser)(function (_ref) {
@@ -79,6 +81,7 @@ function createInsightsMiddleware() {
79
81
 
80
82
  // search-insights.js also throws an error so dev-only clarification is sufficient
81
83
  process.env.NODE_ENV === 'development' ? (0, _utils.warning)(Boolean(appId && apiKey), 'could not extract Algolia credentials from searchClient in insights middleware.') : void 0;
84
+ var queuedInitParams = undefined;
82
85
  var queuedUserToken = undefined;
83
86
  var queuedAuthenticatedUserToken = undefined;
84
87
  var userTokenBeforeInit = undefined;
@@ -95,7 +98,7 @@ function createInsightsMiddleware() {
95
98
  // At this point, even though `search-insights` is not loaded yet,
96
99
  // we still want to read the token from the queue.
97
100
  // Otherwise, the first search call will be fired without the token.
98
- var _map = ['setUserToken', 'setAuthenticatedUserToken'].map(function (key) {
101
+ var _map = ['setUserToken', 'setAuthenticatedUserToken', 'init'].map(function (key) {
99
102
  var _ref3 = (0, _utils.find)(queue.slice().reverse(), function (_ref5) {
100
103
  var _ref6 = _slicedToArray(_ref5, 1),
101
104
  method = _ref6[0];
@@ -105,9 +108,10 @@ function createInsightsMiddleware() {
105
108
  value = _ref4[1];
106
109
  return value;
107
110
  });
108
- var _map2 = _slicedToArray(_map, 2);
111
+ var _map2 = _slicedToArray(_map, 3);
109
112
  queuedUserToken = _map2[0];
110
113
  queuedAuthenticatedUserToken = _map2[1];
114
+ queuedInitParams = _map2[2];
111
115
  }
112
116
 
113
117
  // If user called `aa('setUserToken')` or `aa('setAuthenticatedUserToken')`
@@ -158,6 +162,23 @@ function createInsightsMiddleware() {
158
162
  started: function started() {
159
163
  insightsClient('addAlgoliaAgent', 'insights-middleware');
160
164
  helper = instantSearchInstance.mainHelper;
165
+ var queueAtStart = insightsClient.queue;
166
+ if (Array.isArray(queueAtStart)) {
167
+ var _map3 = ['setUserToken', 'setAuthenticatedUserToken', 'init'].map(function (key) {
168
+ var _ref7 = (0, _utils.find)(queueAtStart.slice().reverse(), function (_ref9) {
169
+ var _ref10 = _slicedToArray(_ref9, 1),
170
+ method = _ref10[0];
171
+ return method === key;
172
+ }) || [],
173
+ _ref8 = _slicedToArray(_ref7, 2),
174
+ value = _ref8[1];
175
+ return value;
176
+ });
177
+ var _map4 = _slicedToArray(_map3, 3);
178
+ queuedUserToken = _map4[0];
179
+ queuedAuthenticatedUserToken = _map4[1];
180
+ queuedInitParams = _map4[2];
181
+ }
161
182
  initialParameters = {
162
183
  userToken: helper.state.userToken,
163
184
  clickAnalytics: helper.state.clickAnalytics
@@ -173,8 +194,9 @@ function createInsightsMiddleware() {
173
194
  if (!$$internal) {
174
195
  instantSearchInstance.scheduleSearch();
175
196
  }
176
- var setUserTokenToSearch = function setUserTokenToSearch(userToken) {
177
- var immediate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
197
+ var setUserTokenToSearch = function setUserTokenToSearch(userToken, tokenType) {
198
+ var immediate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
199
+ var unsetAuthenticatedUserToken = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
178
200
  var normalizedUserToken = normalizeUserToken(userToken);
179
201
  if (!normalizedUserToken) {
180
202
  return;
@@ -187,6 +209,13 @@ function createInsightsMiddleware() {
187
209
  if (existingToken && existingToken !== userToken) {
188
210
  instantSearchInstance.scheduleSearch();
189
211
  }
212
+ currentTokenType = tokenType;
213
+ }
214
+
215
+ // the authenticated user token cannot be overridden by a user or anonymous token
216
+ // for instant search query requests
217
+ if (currentTokenType && currentTokenType === 'authenticated' && tokenType === 'default' && !unsetAuthenticatedUserToken) {
218
+ return;
190
219
  }
191
220
 
192
221
  // Delay the token application to the next render cycle
@@ -196,14 +225,8 @@ function createInsightsMiddleware() {
196
225
  applyToken();
197
226
  }
198
227
  };
199
- var anonymousUserToken = (0, _helpers.getInsightsAnonymousUserTokenInternal)();
200
- if (anonymousUserToken) {
201
- // When `aa('init', { ... })` is called, it creates an anonymous user token in cookie.
202
- // We can set it as userToken.
203
- setUserTokenToSearch(anonymousUserToken, true);
204
- }
205
228
  function setUserToken(token, userToken, authenticatedUserToken) {
206
- setUserTokenToSearch(token, true);
229
+ setUserTokenToSearch(token, authenticatedUserToken ? 'authenticated' : 'default', true);
207
230
  if (userToken) {
208
231
  insightsClient('setUserToken', userToken);
209
232
  }
@@ -211,19 +234,66 @@ function createInsightsMiddleware() {
211
234
  insightsClient('setAuthenticatedUserToken', authenticatedUserToken);
212
235
  }
213
236
  }
237
+ var anonymousUserToken = undefined;
238
+ var anonymousTokenFromInsights = (0, _helpers.getInsightsAnonymousUserTokenInternal)();
239
+ if (anonymousTokenFromInsights) {
240
+ // When `aa('init', { ... })` is called, it creates an anonymous user token in cookie.
241
+ // We can set it as userToken on instantsearch and insights. If it's not set as an insights
242
+ // userToken before a sendEvent, insights automatically generates a new anonymous token,
243
+ // causing a state change and an unnecessary query on instantsearch.
244
+ anonymousUserToken = anonymousTokenFromInsights;
245
+ } else {
246
+ var token = "anonymous-".concat((0, _uuid.createUUID)());
247
+ anonymousUserToken = token;
248
+ }
249
+ var authenticatedUserTokenFromInit;
250
+ var userTokenFromInit;
251
+
252
+ // With SSR, the token could be be set on the state. We make sure
253
+ // that insights is in sync with that token since, there is no
254
+ // insights lib on the server.
255
+ var tokenFromSearchParameters = initialParameters.userToken;
256
+
257
+ // When the first query is sent, the token is possibly not yet be set by
258
+ // the insights onChange callbacks (if insights isn't yet loaded).
259
+ // It is explicitly being set here so that the first query has the
260
+ // initial tokens set and ensure a second query isn't automatically
261
+ // made when the onChange callback actually changes the state.
262
+ if (insightsInitParams) {
263
+ if (insightsInitParams.authenticatedUserToken) {
264
+ authenticatedUserTokenFromInit = insightsInitParams.authenticatedUserToken;
265
+ } else if (insightsInitParams.userToken) {
266
+ userTokenFromInit = insightsInitParams.userToken;
267
+ }
268
+ }
214
269
 
215
270
  // We consider the `userToken` or `authenticatedUserToken` before an
216
- // `init` call of higher importance than one from the queue.
271
+ // `init` call of higher importance than one from the queue and ones set
272
+ // from the init props to be higher than that.
273
+ var tokenFromInit = authenticatedUserTokenFromInit || userTokenFromInit;
217
274
  var tokenBeforeInit = authenticatedUserTokenBeforeInit || userTokenBeforeInit;
218
- var queuedToken = queuedAuthenticatedUserToken || queuedUserToken;
219
- if (tokenBeforeInit) {
275
+ var tokenFromQueue = queuedAuthenticatedUserToken || queuedUserToken;
276
+ if (tokenFromInit) {
277
+ setUserToken(tokenFromInit, userTokenFromInit, authenticatedUserTokenFromInit);
278
+ } else if (tokenFromSearchParameters) {
279
+ setUserToken(tokenFromSearchParameters, tokenFromSearchParameters, undefined);
280
+ } else if (tokenBeforeInit) {
220
281
  setUserToken(tokenBeforeInit, userTokenBeforeInit, authenticatedUserTokenBeforeInit);
221
- } else if (queuedToken) {
222
- setUserToken(queuedToken, queuedUserToken, queuedAuthenticatedUserToken);
282
+ } else if (tokenFromQueue) {
283
+ setUserToken(tokenFromQueue, queuedUserToken, queuedAuthenticatedUserToken);
284
+ } else if (anonymousUserToken) {
285
+ var _queuedInitParams;
286
+ setUserToken(anonymousUserToken, anonymousUserToken, undefined);
287
+ if (insightsInitParams !== null && insightsInitParams !== void 0 && insightsInitParams.useCookie || (_queuedInitParams = queuedInitParams) !== null && _queuedInitParams !== void 0 && _queuedInitParams.useCookie) {
288
+ var _queuedInitParams2;
289
+ saveTokenAsCookie(anonymousUserToken, (insightsInitParams === null || insightsInitParams === void 0 ? void 0 : insightsInitParams.cookieDuration) || ((_queuedInitParams2 = queuedInitParams) === null || _queuedInitParams2 === void 0 ? void 0 : _queuedInitParams2.cookieDuration));
290
+ }
223
291
  }
224
292
 
225
293
  // This updates userToken which is set explicitly by `aa('setUserToken', userToken)`
226
- insightsClient('onUserTokenChange', setUserTokenToSearch, {
294
+ insightsClient('onUserTokenChange', function (token) {
295
+ return setUserTokenToSearch(token, 'default', true);
296
+ }, {
227
297
  immediate: true
228
298
  });
229
299
 
@@ -232,10 +302,10 @@ function createInsightsMiddleware() {
232
302
  // If we're unsetting the `authenticatedUserToken`, we revert to the `userToken`
233
303
  if (!authenticatedUserToken) {
234
304
  insightsClient('getUserToken', null, function (_, userToken) {
235
- setUserTokenToSearch(userToken);
305
+ setUserTokenToSearch(userToken, 'default', true, true);
236
306
  });
237
307
  }
238
- setUserTokenToSearch(authenticatedUserToken);
308
+ setUserTokenToSearch(authenticatedUserToken, 'authenticated', true);
239
309
  }, {
240
310
  immediate: true
241
311
  });
@@ -253,10 +323,33 @@ function createInsightsMiddleware() {
253
323
  return insightsClient(method, payload, extraParams);
254
324
  };
255
325
  }
326
+ var viewedObjectIDs = new Set();
327
+ var lastQueryId;
328
+ instantSearchInstance.mainHelper.derivedHelpers[0].on('result', function (_ref11) {
329
+ var results = _ref11.results;
330
+ if (!results.queryID || results.queryID !== lastQueryId) {
331
+ lastQueryId = results.queryID;
332
+ viewedObjectIDs.clear();
333
+ }
334
+ });
256
335
  instantSearchInstance.sendEventToInsights = function (event) {
257
336
  if (onEvent) {
258
337
  onEvent(event, insightsClientWithLocalCredentials);
259
338
  } else if (event.insightsMethod) {
339
+ if (event.insightsMethod === 'viewedObjectIDs') {
340
+ var _payload = event.payload;
341
+ var difference = _payload.objectIDs.filter(function (objectID) {
342
+ return !viewedObjectIDs.has(objectID);
343
+ });
344
+ if (difference.length === 0) {
345
+ return;
346
+ }
347
+ difference.forEach(function (objectID) {
348
+ return viewedObjectIDs.add(objectID);
349
+ });
350
+ _payload.objectIDs = difference;
351
+ }
352
+
260
353
  // Source is used to differentiate events sent by instantsearch from those sent manually.
261
354
  event.payload.algoliaSource = ['instantsearch'];
262
355
  if ($$automatic) {
@@ -284,6 +377,13 @@ function createInsightsMiddleware() {
284
377
  };
285
378
  };
286
379
  }
380
+ function saveTokenAsCookie(token, cookieDuration) {
381
+ var MONTH = 30 * 24 * 60 * 60 * 1000;
382
+ var d = new Date();
383
+ d.setTime(d.getTime() + (cookieDuration || MONTH * 6));
384
+ var expires = "expires=".concat(d.toUTCString());
385
+ document.cookie = "_ALGOLIA=".concat(token, ";").concat(expires, ";path=/");
386
+ }
287
387
 
288
388
  /**
289
389
  * Determines if a given insights `client` supports the optional call to `init`
@@ -1,4 +1,4 @@
1
- /*! InstantSearch.js 4.75.2 | © Algolia, Inc. and contributors; MIT License | https://github.com/algolia/instantsearch */
1
+ /*! InstantSearch.js 4.75.4 | © Algolia, Inc. and contributors; MIT License | https://github.com/algolia/instantsearch */
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
4
4
  typeof define === 'function' && define.amd ? define(factory) :
@@ -13727,6 +13727,22 @@
13727
13727
  };
13728
13728
  }
13729
13729
 
13730
+ /**
13731
+ * Create UUID according to
13732
+ * https://www.ietf.org/rfc/rfc4122.txt.
13733
+ *
13734
+ * @returns Generated UUID.
13735
+ */
13736
+ function createUUID() {
13737
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
13738
+ /* eslint-disable no-bitwise */
13739
+ var r = Math.random() * 16 | 0;
13740
+ var v = c === 'x' ? r : r & 0x3 | 0x8;
13741
+ /* eslint-enable */
13742
+ return v.toString(16);
13743
+ });
13744
+ }
13745
+
13730
13746
  var ALGOLIA_INSIGHTS_VERSION = '2.15.0';
13731
13747
  var ALGOLIA_INSIGHTS_SRC = "https://cdn.jsdelivr.net/npm/search-insights@".concat(ALGOLIA_INSIGHTS_VERSION, "/dist/search-insights.min.js");
13732
13748
  function createInsightsMiddleware() {
@@ -13738,6 +13754,7 @@
13738
13754
  $$internal = _props$$$internal === void 0 ? false : _props$$$internal,
13739
13755
  _props$$$automatic = props.$$automatic,
13740
13756
  $$automatic = _props$$$automatic === void 0 ? false : _props$$$automatic;
13757
+ var currentTokenType;
13741
13758
  var potentialInsightsClient = _insightsClient;
13742
13759
  if (!_insightsClient && _insightsClient !== null) {
13743
13760
  safelyRunOnBrowser(function (_ref) {
@@ -13784,6 +13801,7 @@
13784
13801
 
13785
13802
  // search-insights.js also throws an error so dev-only clarification is sufficient
13786
13803
  _warning(Boolean(appId && apiKey), 'could not extract Algolia credentials from searchClient in insights middleware.') ;
13804
+ var queuedInitParams = undefined;
13787
13805
  var queuedUserToken = undefined;
13788
13806
  var queuedAuthenticatedUserToken = undefined;
13789
13807
  var userTokenBeforeInit = undefined;
@@ -13800,7 +13818,7 @@
13800
13818
  // At this point, even though `search-insights` is not loaded yet,
13801
13819
  // we still want to read the token from the queue.
13802
13820
  // Otherwise, the first search call will be fired without the token.
13803
- var _map = ['setUserToken', 'setAuthenticatedUserToken'].map(function (key) {
13821
+ var _map = ['setUserToken', 'setAuthenticatedUserToken', 'init'].map(function (key) {
13804
13822
  var _ref3 = find(queue.slice().reverse(), function (_ref5) {
13805
13823
  var _ref6 = _slicedToArray(_ref5, 1),
13806
13824
  method = _ref6[0];
@@ -13810,9 +13828,10 @@
13810
13828
  value = _ref4[1];
13811
13829
  return value;
13812
13830
  });
13813
- var _map2 = _slicedToArray(_map, 2);
13831
+ var _map2 = _slicedToArray(_map, 3);
13814
13832
  queuedUserToken = _map2[0];
13815
13833
  queuedAuthenticatedUserToken = _map2[1];
13834
+ queuedInitParams = _map2[2];
13816
13835
  }
13817
13836
 
13818
13837
  // If user called `aa('setUserToken')` or `aa('setAuthenticatedUserToken')`
@@ -13863,6 +13882,23 @@
13863
13882
  started: function started() {
13864
13883
  insightsClient('addAlgoliaAgent', 'insights-middleware');
13865
13884
  helper = instantSearchInstance.mainHelper;
13885
+ var queueAtStart = insightsClient.queue;
13886
+ if (Array.isArray(queueAtStart)) {
13887
+ var _map3 = ['setUserToken', 'setAuthenticatedUserToken', 'init'].map(function (key) {
13888
+ var _ref7 = find(queueAtStart.slice().reverse(), function (_ref9) {
13889
+ var _ref10 = _slicedToArray(_ref9, 1),
13890
+ method = _ref10[0];
13891
+ return method === key;
13892
+ }) || [],
13893
+ _ref8 = _slicedToArray(_ref7, 2),
13894
+ value = _ref8[1];
13895
+ return value;
13896
+ });
13897
+ var _map4 = _slicedToArray(_map3, 3);
13898
+ queuedUserToken = _map4[0];
13899
+ queuedAuthenticatedUserToken = _map4[1];
13900
+ queuedInitParams = _map4[2];
13901
+ }
13866
13902
  initialParameters = {
13867
13903
  userToken: helper.state.userToken,
13868
13904
  clickAnalytics: helper.state.clickAnalytics
@@ -13878,8 +13914,9 @@
13878
13914
  if (!$$internal) {
13879
13915
  instantSearchInstance.scheduleSearch();
13880
13916
  }
13881
- var setUserTokenToSearch = function setUserTokenToSearch(userToken) {
13882
- var immediate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
13917
+ var setUserTokenToSearch = function setUserTokenToSearch(userToken, tokenType) {
13918
+ var immediate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
13919
+ var unsetAuthenticatedUserToken = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
13883
13920
  var normalizedUserToken = normalizeUserToken(userToken);
13884
13921
  if (!normalizedUserToken) {
13885
13922
  return;
@@ -13892,6 +13929,13 @@
13892
13929
  if (existingToken && existingToken !== userToken) {
13893
13930
  instantSearchInstance.scheduleSearch();
13894
13931
  }
13932
+ currentTokenType = tokenType;
13933
+ }
13934
+
13935
+ // the authenticated user token cannot be overridden by a user or anonymous token
13936
+ // for instant search query requests
13937
+ if (currentTokenType && currentTokenType === 'authenticated' && tokenType === 'default' && !unsetAuthenticatedUserToken) {
13938
+ return;
13895
13939
  }
13896
13940
 
13897
13941
  // Delay the token application to the next render cycle
@@ -13901,14 +13945,8 @@
13901
13945
  applyToken();
13902
13946
  }
13903
13947
  };
13904
- var anonymousUserToken = getInsightsAnonymousUserTokenInternal();
13905
- if (anonymousUserToken) {
13906
- // When `aa('init', { ... })` is called, it creates an anonymous user token in cookie.
13907
- // We can set it as userToken.
13908
- setUserTokenToSearch(anonymousUserToken, true);
13909
- }
13910
13948
  function setUserToken(token, userToken, authenticatedUserToken) {
13911
- setUserTokenToSearch(token, true);
13949
+ setUserTokenToSearch(token, authenticatedUserToken ? 'authenticated' : 'default', true);
13912
13950
  if (userToken) {
13913
13951
  insightsClient('setUserToken', userToken);
13914
13952
  }
@@ -13916,19 +13954,66 @@
13916
13954
  insightsClient('setAuthenticatedUserToken', authenticatedUserToken);
13917
13955
  }
13918
13956
  }
13957
+ var anonymousUserToken = undefined;
13958
+ var anonymousTokenFromInsights = getInsightsAnonymousUserTokenInternal();
13959
+ if (anonymousTokenFromInsights) {
13960
+ // When `aa('init', { ... })` is called, it creates an anonymous user token in cookie.
13961
+ // We can set it as userToken on instantsearch and insights. If it's not set as an insights
13962
+ // userToken before a sendEvent, insights automatically generates a new anonymous token,
13963
+ // causing a state change and an unnecessary query on instantsearch.
13964
+ anonymousUserToken = anonymousTokenFromInsights;
13965
+ } else {
13966
+ var token = "anonymous-".concat(createUUID());
13967
+ anonymousUserToken = token;
13968
+ }
13969
+ var authenticatedUserTokenFromInit;
13970
+ var userTokenFromInit;
13971
+
13972
+ // With SSR, the token could be be set on the state. We make sure
13973
+ // that insights is in sync with that token since, there is no
13974
+ // insights lib on the server.
13975
+ var tokenFromSearchParameters = initialParameters.userToken;
13976
+
13977
+ // When the first query is sent, the token is possibly not yet be set by
13978
+ // the insights onChange callbacks (if insights isn't yet loaded).
13979
+ // It is explicitly being set here so that the first query has the
13980
+ // initial tokens set and ensure a second query isn't automatically
13981
+ // made when the onChange callback actually changes the state.
13982
+ if (insightsInitParams) {
13983
+ if (insightsInitParams.authenticatedUserToken) {
13984
+ authenticatedUserTokenFromInit = insightsInitParams.authenticatedUserToken;
13985
+ } else if (insightsInitParams.userToken) {
13986
+ userTokenFromInit = insightsInitParams.userToken;
13987
+ }
13988
+ }
13919
13989
 
13920
13990
  // We consider the `userToken` or `authenticatedUserToken` before an
13921
- // `init` call of higher importance than one from the queue.
13991
+ // `init` call of higher importance than one from the queue and ones set
13992
+ // from the init props to be higher than that.
13993
+ var tokenFromInit = authenticatedUserTokenFromInit || userTokenFromInit;
13922
13994
  var tokenBeforeInit = authenticatedUserTokenBeforeInit || userTokenBeforeInit;
13923
- var queuedToken = queuedAuthenticatedUserToken || queuedUserToken;
13924
- if (tokenBeforeInit) {
13995
+ var tokenFromQueue = queuedAuthenticatedUserToken || queuedUserToken;
13996
+ if (tokenFromInit) {
13997
+ setUserToken(tokenFromInit, userTokenFromInit, authenticatedUserTokenFromInit);
13998
+ } else if (tokenFromSearchParameters) {
13999
+ setUserToken(tokenFromSearchParameters, tokenFromSearchParameters, undefined);
14000
+ } else if (tokenBeforeInit) {
13925
14001
  setUserToken(tokenBeforeInit, userTokenBeforeInit, authenticatedUserTokenBeforeInit);
13926
- } else if (queuedToken) {
13927
- setUserToken(queuedToken, queuedUserToken, queuedAuthenticatedUserToken);
14002
+ } else if (tokenFromQueue) {
14003
+ setUserToken(tokenFromQueue, queuedUserToken, queuedAuthenticatedUserToken);
14004
+ } else if (anonymousUserToken) {
14005
+ var _queuedInitParams;
14006
+ setUserToken(anonymousUserToken, anonymousUserToken, undefined);
14007
+ if (insightsInitParams !== null && insightsInitParams !== void 0 && insightsInitParams.useCookie || (_queuedInitParams = queuedInitParams) !== null && _queuedInitParams !== void 0 && _queuedInitParams.useCookie) {
14008
+ var _queuedInitParams2;
14009
+ saveTokenAsCookie(anonymousUserToken, (insightsInitParams === null || insightsInitParams === void 0 ? void 0 : insightsInitParams.cookieDuration) || ((_queuedInitParams2 = queuedInitParams) === null || _queuedInitParams2 === void 0 ? void 0 : _queuedInitParams2.cookieDuration));
14010
+ }
13928
14011
  }
13929
14012
 
13930
14013
  // This updates userToken which is set explicitly by `aa('setUserToken', userToken)`
13931
- insightsClient('onUserTokenChange', setUserTokenToSearch, {
14014
+ insightsClient('onUserTokenChange', function (token) {
14015
+ return setUserTokenToSearch(token, 'default', true);
14016
+ }, {
13932
14017
  immediate: true
13933
14018
  });
13934
14019
 
@@ -13937,10 +14022,10 @@
13937
14022
  // If we're unsetting the `authenticatedUserToken`, we revert to the `userToken`
13938
14023
  if (!authenticatedUserToken) {
13939
14024
  insightsClient('getUserToken', null, function (_, userToken) {
13940
- setUserTokenToSearch(userToken);
14025
+ setUserTokenToSearch(userToken, 'default', true, true);
13941
14026
  });
13942
14027
  }
13943
- setUserTokenToSearch(authenticatedUserToken);
14028
+ setUserTokenToSearch(authenticatedUserToken, 'authenticated', true);
13944
14029
  }, {
13945
14030
  immediate: true
13946
14031
  });
@@ -13958,10 +14043,33 @@
13958
14043
  return insightsClient(method, payload, extraParams);
13959
14044
  };
13960
14045
  }
14046
+ var viewedObjectIDs = new Set();
14047
+ var lastQueryId;
14048
+ instantSearchInstance.mainHelper.derivedHelpers[0].on('result', function (_ref11) {
14049
+ var results = _ref11.results;
14050
+ if (!results.queryID || results.queryID !== lastQueryId) {
14051
+ lastQueryId = results.queryID;
14052
+ viewedObjectIDs.clear();
14053
+ }
14054
+ });
13961
14055
  instantSearchInstance.sendEventToInsights = function (event) {
13962
14056
  if (onEvent) {
13963
14057
  onEvent(event, insightsClientWithLocalCredentials);
13964
14058
  } else if (event.insightsMethod) {
14059
+ if (event.insightsMethod === 'viewedObjectIDs') {
14060
+ var _payload = event.payload;
14061
+ var difference = _payload.objectIDs.filter(function (objectID) {
14062
+ return !viewedObjectIDs.has(objectID);
14063
+ });
14064
+ if (difference.length === 0) {
14065
+ return;
14066
+ }
14067
+ difference.forEach(function (objectID) {
14068
+ return viewedObjectIDs.add(objectID);
14069
+ });
14070
+ _payload.objectIDs = difference;
14071
+ }
14072
+
13965
14073
  // Source is used to differentiate events sent by instantsearch from those sent manually.
13966
14074
  event.payload.algoliaSource = ['instantsearch'];
13967
14075
  if ($$automatic) {
@@ -13989,6 +14097,13 @@
13989
14097
  };
13990
14098
  };
13991
14099
  }
14100
+ function saveTokenAsCookie(token, cookieDuration) {
14101
+ var MONTH = 30 * 24 * 60 * 60 * 1000;
14102
+ var d = new Date();
14103
+ d.setTime(d.getTime() + (cookieDuration || MONTH * 6));
14104
+ var expires = "expires=".concat(d.toUTCString());
14105
+ document.cookie = "_ALGOLIA=".concat(token, ";").concat(expires, ";path=/");
14106
+ }
13992
14107
 
13993
14108
  /**
13994
14109
  * Determines if a given insights `client` supports the optional call to `init`
@@ -16017,7 +16132,7 @@
16017
16132
  };
16018
16133
  }
16019
16134
 
16020
- var version$1 = '4.75.2';
16135
+ var version$1 = '4.75.4';
16021
16136
 
16022
16137
  var withUsage$v = createDocumentationMessageGenerator({
16023
16138
  name: 'instantsearch'