algolia-experiences 1.4.1 → 1.4.3

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.
@@ -9960,6 +9960,22 @@
9960
9960
  return getCookie(ANONYMOUS_TOKEN_COOKIE_KEY);
9961
9961
  }
9962
9962
 
9963
+ /**
9964
+ * Create UUID according to
9965
+ * https://www.ietf.org/rfc/rfc4122.txt.
9966
+ *
9967
+ * @returns Generated UUID.
9968
+ */
9969
+ function createUUID() {
9970
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
9971
+ /* eslint-disable no-bitwise */
9972
+ var r = Math.random() * 16 | 0;
9973
+ var v = c === 'x' ? r : r & 0x3 | 0x8;
9974
+ /* eslint-enable */
9975
+ return v.toString(16);
9976
+ });
9977
+ }
9978
+
9963
9979
  function _typeof$d(obj) {
9964
9980
  "@babel/helpers - typeof";
9965
9981
 
@@ -10090,6 +10106,7 @@
10090
10106
  $$internal = _props$$$internal === void 0 ? false : _props$$$internal,
10091
10107
  _props$$$automatic = props.$$automatic,
10092
10108
  $$automatic = _props$$$automatic === void 0 ? false : _props$$$automatic;
10109
+ var currentTokenType;
10093
10110
  var potentialInsightsClient = _insightsClient;
10094
10111
  if (!_insightsClient && _insightsClient !== null) {
10095
10112
  safelyRunOnBrowser(function (_ref) {
@@ -10133,6 +10150,7 @@
10133
10150
  _getAppIdAndApiKey2 = _slicedToArray$4(_getAppIdAndApiKey, 2),
10134
10151
  appId = _getAppIdAndApiKey2[0],
10135
10152
  apiKey = _getAppIdAndApiKey2[1];
10153
+ var queuedInitParams = undefined;
10136
10154
  var queuedUserToken = undefined;
10137
10155
  var queuedAuthenticatedUserToken = undefined;
10138
10156
  var userTokenBeforeInit = undefined;
@@ -10149,7 +10167,7 @@
10149
10167
  // At this point, even though `search-insights` is not loaded yet,
10150
10168
  // we still want to read the token from the queue.
10151
10169
  // Otherwise, the first search call will be fired without the token.
10152
- var _map = ['setUserToken', 'setAuthenticatedUserToken'].map(function (key) {
10170
+ var _map = ['setUserToken', 'setAuthenticatedUserToken', 'init'].map(function (key) {
10153
10171
  var _ref3 = find$1(queue.slice().reverse(), function (_ref5) {
10154
10172
  var _ref6 = _slicedToArray$4(_ref5, 1),
10155
10173
  method = _ref6[0];
@@ -10159,9 +10177,10 @@
10159
10177
  value = _ref4[1];
10160
10178
  return value;
10161
10179
  });
10162
- var _map2 = _slicedToArray$4(_map, 2);
10180
+ var _map2 = _slicedToArray$4(_map, 3);
10163
10181
  queuedUserToken = _map2[0];
10164
10182
  queuedAuthenticatedUserToken = _map2[1];
10183
+ queuedInitParams = _map2[2];
10165
10184
  }
10166
10185
 
10167
10186
  // If user called `aa('setUserToken')` or `aa('setAuthenticatedUserToken')`
@@ -10212,6 +10231,23 @@
10212
10231
  started: function started() {
10213
10232
  insightsClient('addAlgoliaAgent', 'insights-middleware');
10214
10233
  helper = instantSearchInstance.mainHelper;
10234
+ var queueAtStart = insightsClient.queue;
10235
+ if (Array.isArray(queueAtStart)) {
10236
+ var _map3 = ['setUserToken', 'setAuthenticatedUserToken', 'init'].map(function (key) {
10237
+ var _ref7 = find$1(queueAtStart.slice().reverse(), function (_ref9) {
10238
+ var _ref10 = _slicedToArray$4(_ref9, 1),
10239
+ method = _ref10[0];
10240
+ return method === key;
10241
+ }) || [],
10242
+ _ref8 = _slicedToArray$4(_ref7, 2),
10243
+ value = _ref8[1];
10244
+ return value;
10245
+ });
10246
+ var _map4 = _slicedToArray$4(_map3, 3);
10247
+ queuedUserToken = _map4[0];
10248
+ queuedAuthenticatedUserToken = _map4[1];
10249
+ queuedInitParams = _map4[2];
10250
+ }
10215
10251
  initialParameters = {
10216
10252
  userToken: helper.state.userToken,
10217
10253
  clickAnalytics: helper.state.clickAnalytics
@@ -10227,8 +10263,9 @@
10227
10263
  if (!$$internal) {
10228
10264
  instantSearchInstance.scheduleSearch();
10229
10265
  }
10230
- var setUserTokenToSearch = function setUserTokenToSearch(userToken) {
10231
- var immediate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
10266
+ var setUserTokenToSearch = function setUserTokenToSearch(userToken, tokenType) {
10267
+ var immediate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
10268
+ var unsetAuthenticatedUserToken = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
10232
10269
  var normalizedUserToken = normalizeUserToken(userToken);
10233
10270
  if (!normalizedUserToken) {
10234
10271
  return;
@@ -10241,6 +10278,13 @@
10241
10278
  if (existingToken && existingToken !== userToken) {
10242
10279
  instantSearchInstance.scheduleSearch();
10243
10280
  }
10281
+ currentTokenType = tokenType;
10282
+ }
10283
+
10284
+ // the authenticated user token cannot be overridden by a user or anonymous token
10285
+ // for instant search query requests
10286
+ if (currentTokenType && currentTokenType === 'authenticated' && tokenType === 'default' && !unsetAuthenticatedUserToken) {
10287
+ return;
10244
10288
  }
10245
10289
 
10246
10290
  // Delay the token application to the next render cycle
@@ -10250,14 +10294,8 @@
10250
10294
  applyToken();
10251
10295
  }
10252
10296
  };
10253
- var anonymousUserToken = getInsightsAnonymousUserTokenInternal();
10254
- if (anonymousUserToken) {
10255
- // When `aa('init', { ... })` is called, it creates an anonymous user token in cookie.
10256
- // We can set it as userToken.
10257
- setUserTokenToSearch(anonymousUserToken, true);
10258
- }
10259
10297
  function setUserToken(token, userToken, authenticatedUserToken) {
10260
- setUserTokenToSearch(token, true);
10298
+ setUserTokenToSearch(token, authenticatedUserToken ? 'authenticated' : 'default', true);
10261
10299
  if (userToken) {
10262
10300
  insightsClient('setUserToken', userToken);
10263
10301
  }
@@ -10265,19 +10303,66 @@
10265
10303
  insightsClient('setAuthenticatedUserToken', authenticatedUserToken);
10266
10304
  }
10267
10305
  }
10306
+ var anonymousUserToken = undefined;
10307
+ var anonymousTokenFromInsights = getInsightsAnonymousUserTokenInternal();
10308
+ if (anonymousTokenFromInsights) {
10309
+ // When `aa('init', { ... })` is called, it creates an anonymous user token in cookie.
10310
+ // We can set it as userToken on instantsearch and insights. If it's not set as an insights
10311
+ // userToken before a sendEvent, insights automatically generates a new anonymous token,
10312
+ // causing a state change and an unnecessary query on instantsearch.
10313
+ anonymousUserToken = anonymousTokenFromInsights;
10314
+ } else {
10315
+ var token = "anonymous-".concat(createUUID());
10316
+ anonymousUserToken = token;
10317
+ }
10318
+ var authenticatedUserTokenFromInit;
10319
+ var userTokenFromInit;
10320
+
10321
+ // With SSR, the token could be be set on the state. We make sure
10322
+ // that insights is in sync with that token since, there is no
10323
+ // insights lib on the server.
10324
+ var tokenFromSearchParameters = initialParameters.userToken;
10325
+
10326
+ // When the first query is sent, the token is possibly not yet be set by
10327
+ // the insights onChange callbacks (if insights isn't yet loaded).
10328
+ // It is explicitly being set here so that the first query has the
10329
+ // initial tokens set and ensure a second query isn't automatically
10330
+ // made when the onChange callback actually changes the state.
10331
+ if (insightsInitParams) {
10332
+ if (insightsInitParams.authenticatedUserToken) {
10333
+ authenticatedUserTokenFromInit = insightsInitParams.authenticatedUserToken;
10334
+ } else if (insightsInitParams.userToken) {
10335
+ userTokenFromInit = insightsInitParams.userToken;
10336
+ }
10337
+ }
10268
10338
 
10269
10339
  // We consider the `userToken` or `authenticatedUserToken` before an
10270
- // `init` call of higher importance than one from the queue.
10340
+ // `init` call of higher importance than one from the queue and ones set
10341
+ // from the init props to be higher than that.
10342
+ var tokenFromInit = authenticatedUserTokenFromInit || userTokenFromInit;
10271
10343
  var tokenBeforeInit = authenticatedUserTokenBeforeInit || userTokenBeforeInit;
10272
- var queuedToken = queuedAuthenticatedUserToken || queuedUserToken;
10273
- if (tokenBeforeInit) {
10344
+ var tokenFromQueue = queuedAuthenticatedUserToken || queuedUserToken;
10345
+ if (tokenFromInit) {
10346
+ setUserToken(tokenFromInit, userTokenFromInit, authenticatedUserTokenFromInit);
10347
+ } else if (tokenFromSearchParameters) {
10348
+ setUserToken(tokenFromSearchParameters, tokenFromSearchParameters, undefined);
10349
+ } else if (tokenBeforeInit) {
10274
10350
  setUserToken(tokenBeforeInit, userTokenBeforeInit, authenticatedUserTokenBeforeInit);
10275
- } else if (queuedToken) {
10276
- setUserToken(queuedToken, queuedUserToken, queuedAuthenticatedUserToken);
10351
+ } else if (tokenFromQueue) {
10352
+ setUserToken(tokenFromQueue, queuedUserToken, queuedAuthenticatedUserToken);
10353
+ } else if (anonymousUserToken) {
10354
+ var _queuedInitParams;
10355
+ setUserToken(anonymousUserToken, anonymousUserToken, undefined);
10356
+ if (insightsInitParams !== null && insightsInitParams !== void 0 && insightsInitParams.useCookie || (_queuedInitParams = queuedInitParams) !== null && _queuedInitParams !== void 0 && _queuedInitParams.useCookie) {
10357
+ var _queuedInitParams2;
10358
+ saveTokenAsCookie(anonymousUserToken, (insightsInitParams === null || insightsInitParams === void 0 ? void 0 : insightsInitParams.cookieDuration) || ((_queuedInitParams2 = queuedInitParams) === null || _queuedInitParams2 === void 0 ? void 0 : _queuedInitParams2.cookieDuration));
10359
+ }
10277
10360
  }
10278
10361
 
10279
10362
  // This updates userToken which is set explicitly by `aa('setUserToken', userToken)`
10280
- insightsClient('onUserTokenChange', setUserTokenToSearch, {
10363
+ insightsClient('onUserTokenChange', function (token) {
10364
+ return setUserTokenToSearch(token, 'default', true);
10365
+ }, {
10281
10366
  immediate: true
10282
10367
  });
10283
10368
 
@@ -10286,10 +10371,10 @@
10286
10371
  // If we're unsetting the `authenticatedUserToken`, we revert to the `userToken`
10287
10372
  if (!authenticatedUserToken) {
10288
10373
  insightsClient('getUserToken', null, function (_, userToken) {
10289
- setUserTokenToSearch(userToken);
10374
+ setUserTokenToSearch(userToken, 'default', true, true);
10290
10375
  });
10291
10376
  }
10292
- setUserTokenToSearch(authenticatedUserToken);
10377
+ setUserTokenToSearch(authenticatedUserToken, 'authenticated', true);
10293
10378
  }, {
10294
10379
  immediate: true
10295
10380
  });
@@ -10307,10 +10392,33 @@
10307
10392
  return insightsClient(method, payload, extraParams);
10308
10393
  };
10309
10394
  }
10395
+ var viewedObjectIDs = new Set();
10396
+ var lastQueryId;
10397
+ instantSearchInstance.mainHelper.derivedHelpers[0].on('result', function (_ref11) {
10398
+ var results = _ref11.results;
10399
+ if (!results.queryID || results.queryID !== lastQueryId) {
10400
+ lastQueryId = results.queryID;
10401
+ viewedObjectIDs.clear();
10402
+ }
10403
+ });
10310
10404
  instantSearchInstance.sendEventToInsights = function (event) {
10311
10405
  if (onEvent) {
10312
10406
  onEvent(event, insightsClientWithLocalCredentials);
10313
10407
  } else if (event.insightsMethod) {
10408
+ if (event.insightsMethod === 'viewedObjectIDs') {
10409
+ var _payload = event.payload;
10410
+ var difference = _payload.objectIDs.filter(function (objectID) {
10411
+ return !viewedObjectIDs.has(objectID);
10412
+ });
10413
+ if (difference.length === 0) {
10414
+ return;
10415
+ }
10416
+ difference.forEach(function (objectID) {
10417
+ return viewedObjectIDs.add(objectID);
10418
+ });
10419
+ _payload.objectIDs = difference;
10420
+ }
10421
+
10314
10422
  // Source is used to differentiate events sent by instantsearch from those sent manually.
10315
10423
  event.payload.algoliaSource = ['instantsearch'];
10316
10424
  if ($$automatic) {
@@ -10335,6 +10443,13 @@
10335
10443
  };
10336
10444
  };
10337
10445
  }
10446
+ function saveTokenAsCookie(token, cookieDuration) {
10447
+ var MONTH = 30 * 24 * 60 * 60 * 1000;
10448
+ var d = new Date();
10449
+ d.setTime(d.getTime() + (cookieDuration || MONTH * 6));
10450
+ var expires = "expires=".concat(d.toUTCString());
10451
+ document.cookie = "_ALGOLIA=".concat(token, ";").concat(expires, ";path=/");
10452
+ }
10338
10453
 
10339
10454
  /**
10340
10455
  * Determines if a given insights `client` supports the optional call to `init`
@@ -12706,7 +12821,7 @@
12706
12821
  };
12707
12822
  }
12708
12823
 
12709
- var version$1 = '4.75.2';
12824
+ var version$1 = '4.75.4';
12710
12825
 
12711
12826
  function _typeof$j(obj) {
12712
12827
  "@babel/helpers - typeof";
@@ -13450,7 +13565,7 @@
13450
13565
  return experience;
13451
13566
  });
13452
13567
  }
13453
- var API_BASE = 'https://experiences-main.platform.algolia.net/1';
13568
+ var API_BASE = 'https://experiences-beta.algolia.com/1';
13454
13569
  function getExperience(_ref2) {
13455
13570
  var id = _ref2.id,
13456
13571
  appId = _ref2.appId,
@@ -13458,7 +13573,7 @@
13458
13573
  return buildRequest({
13459
13574
  appId: appId,
13460
13575
  apiKey: apiKey,
13461
- endpoint: "collections/".concat(id)
13576
+ endpoint: "experiences/".concat(id)
13462
13577
  });
13463
13578
  }
13464
13579
  function buildRequest(_ref5) {