Skip to content

Commit

Permalink
Update ls data and add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
poulch committed Jan 16, 2025
1 parent 364c924 commit 441a867
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions src/components/ProductAnalytics/useAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@ interface Analytics {

export function useAnalytics(): Analytics {
const posthog = usePostHog();
const [lastUserProperties, setLastUserProperties] = useLocalStorage<UserProperties>(
"analyticsUserProperties",
{
domain: "",
email_domain: "",
},
const [hasBeenUserSet, setHasBeenUserSet] = useLocalStorage<boolean>(
"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<string, any>) {
Expand All @@ -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
);
}

0 comments on commit 441a867

Please sign in to comment.