javascriptlit

How to trigger an analytic when component gets re-rendered?


I need to trigger an analytic when a component gets re-rendered because someones searches in the search bar. When someone searches, the component LibraryManager gets re-rendered to address that search value. I need to trigger an analytic when that component gets re-rendered. The best way to trigger that analytic is in the render function but I'm told not to add the trigger to the render function because of side effects. Is there another function I can use to add the analytics trigger in lit?


Solution

  • If calling the analytic instance from the render method is not possible, you can listen to the onChange event of the search bar input. When the change event is fired, you'll make the analytics call in the callback function.

    Just make sure that if you subscribe to the event, to also unsubscribe if necessary.