instantsearch icon indicating copy to clipboard operation
instantsearch copied to clipboard

Insight track function called multiple times

Open FaroukMekk opened this issue 2 years ago • 10 comments

🐛 Current behavior

Insights track called multiple times

Insights track function is called as many times as the useHits hook or Hit component are used in the InstantSearch component.

🔍 Steps to reproduce

Go to the live reproduction See the multiple logs in the console Remove one component using the useHits hook See the number of logs decreasing

Live reproduction

https://codesandbox.io/s/relaxed-james-x92y0k?file=/src/App.tsx

💭 Expected behavior

the 'view Hits' event is supposed to be called juste one time, regardless of how many times you used the useHits hook.

Package version

algoliasearch 4.14.3, instantsearch.js 4.43.1, react-instantsearch-hooks-web 6.38.1, search-insights 2.2.3-hooks-web

Operating system

No response

Browser

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

FaroukMekk avatar Jan 23 '23 15:01 FaroukMekk

This is a good point. One solution I'm seeing is memoizing sendEvent somewhat, maybe only for the view case, as you could have clicks that happen multiple times on the same element, but viewing it multiple times still counts as viewing it once.

Haroenv avatar Jan 23 '23 15:01 Haroenv

I don't understand your solution. On my side can I do anything to avoid this behavior beside not using the hook more than one time ? Can you edit my codesandbox maybe ?

FaroukMekk avatar Jan 24 '23 15:01 FaroukMekk

Sorry, the solution would be in the source code, I'm not sure what an user land solution could be for this issue.

As a temporary workaround, I guess you could divide the stats by the number of hits components you have

Haroenv avatar Jan 24 '23 15:01 Haroenv

In addition to the statistics analysis deffect, it provide a big dump of performance, maybe to link with this ticket : https://github.com/algolia/instantsearch/issues/5237

For example on our side, we were using useHits in every HitComponent and we are displaying 100 hits by page. That was producing nearly 500 to 1000 HTTP requests only to send viewed events when page display. 😆

Does this has a solution? we have a similar problem but is affecting our limits in Algolia because are receiving 6 times the same view event

mordonez-me avatar Jun 27 '24 19:06 mordonez-me