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

Local storage in react native #541

Closed aleksandar-vasic-ananas closed 2 months ago

aleksandar-vasic-ananas commented 3 months ago

I was using search-insights inside my React Native project, and everything was fine. After I moved to version 2.15.0, I started getting the error "Property 'localStorage' doesn't exist."

VERSION: 2.14.0

export function clickedObjectIDsAfterSearch(
  this: AlgoliaAnalytics,
  ...params: Array<WithAdditionalParams<InsightsSearchClickEvent>>
): ReturnType<AlgoliaAnalytics["sendEvents"]> {
  const { events, additionalParams } =
    extractAdditionalParams<InsightsSearchClickEvent>(params);

  return this.sendEvents(addEventType("click", events), additionalParams);
}

VERSION: 2.15.0

export function clickedObjectIDsAfterSearch(
  this: AlgoliaAnalytics,
  ...params: Array<WithAdditionalParams<InsightsSearchClickEvent>>
): ReturnType<AlgoliaAnalytics["sendEvents"]> {
  const { events, additionalParams } =
    extractAdditionalParams<InsightsSearchClickEvent>(params);

  events.forEach(({ index, queryID, objectIDs }) =>
    objectIDs.forEach((objectID) =>
      storeQueryForObject(index, objectID, queryID)
    )
  );

  return this.sendEvents(addEventType("click", events), additionalParams);
}

I detected the issue inside clickedObjectIDsAfterSearch, since from version 2.15.0, they added the function storeQueryForObject that talks to local storage. Since React Native doesn't use local storage, can you please do something about it?

Maybe you could set it so that if it’s a mobile phone, it uses AsyncStorage or MMKV, or even better, add functionality where we can pass the storage we use.

zaq42 commented 3 months ago

Not just React Native. In a node app, with search-insights 2.15.0:

ReferenceError: localStorage is not defined
    at LocalStorage.get (~/.../.yarn/cache/search-insights-npm-2.15.0-99e33c6255-87b5ca470d.zip/node_modules/search-insights/dist/search-insights-node.cjs.min.js:1:699)