From 441a867ea7b79f8f31bf200db425f1d2de30e04a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chy=C5=82a?= Date: Thu, 16 Jan 2025 12:16:36 +0100 Subject: [PATCH] Update ls data and add comment --- .../ProductAnalytics/useAnalytics.ts | 25 ++++++------------- 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/src/components/ProductAnalytics/useAnalytics.ts b/src/components/ProductAnalytics/useAnalytics.ts index 547d906377..b97a7651db 100644 --- a/src/components/ProductAnalytics/useAnalytics.ts +++ b/src/components/ProductAnalytics/useAnalytics.ts @@ -13,24 +13,23 @@ interface Analytics { export function useAnalytics(): Analytics { const posthog = usePostHog(); - const [lastUserProperties, setLastUserProperties] = useLocalStorage( - "analyticsUserProperties", - { - domain: "", - email_domain: "", - }, + const [hasBeenUserSet, setHasBeenUserSet] = useLocalStorage( + "analyticsHasBeenUserSet", + false, ); function initialize(userProperties: UserProperties) { if (!posthog) return; - if (!hasUserPropertiesChanged(userProperties, lastUserProperties)) return; + // initialize function is called on each reload that cause generates new used id by identify function + // to avoid this we need to check if user has been set + if (hasBeenUserSet) return; const id = posthog.get_distinct_id(); posthog.identify(id, userProperties); - setLastUserProperties(userProperties); + setHasBeenUserSet(true); } function trackEvent(event: string, properties?: Record) { @@ -41,13 +40,3 @@ export function useAnalytics(): Analytics { return { trackEvent, initialize }; } - -function hasUserPropertiesChanged( - userProperties: UserProperties, - lastInitializedUserProperties: UserProperties, -) { - return ( - userProperties.domain !== lastInitializedUserProperties.domain || - userProperties.email_domain !== lastInitializedUserProperties.email_domain - ); -}