algolia / search-insights.js

Library for reporting click, conversion and view metrics using the Algolia Insights API
MIT License
53 stars 18 forks source link

fix: save existing anon tokens for multiple init calls #562

Closed shaejaz closed 2 days ago

shaejaz commented 5 days ago

Context

In InstantSearch, in order for user tokens to be set in time for the first query on page load, anonymous user tokens are generated and set as both the InstantSearch token and Insights token.

This flow has one issue though. When useCookies flag needs to be changed later (such as with a cookie consent banner), a second init call to insights with the flag set to true does not save the existing token to a cookie.

A short repro is as follows with the very latest version of InstantSearch where this behaviour was added (PR: https://github.com/algolia/instantsearch/pull/6377):

const searchClient = algoliasearch(
  // ...
);

window.aa('init', {
  appId: 'id',
  apiKey: 'key',
});

const search = instantsearch({
  indexName: 'index',
  searchClient,
  insights: true,
});

search.addWidgets([
  // ...
]);

// This does not save the cookie atm
setTimeout(() => {
  window.aa('init', {
    partial: true,
    useCookie: true,
  });
}, 5000);
codesandbox-ci[bot] commented 5 days ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.