-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: posthog record only when user is logged in (#2470)
- Loading branch information
Showing
2 changed files
with
58 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { PostHog } from 'posthog-js'; | ||
import { useEffect } from 'react'; | ||
import { getUtmParams } from 'tg.fixtures/utmCookie'; | ||
import { useConfig, useUser } from 'tg.globalContext/helpers'; | ||
|
||
const POSTHOG_INSTANCE_WINDOW_PROPERTY = 'posthogInstance'; | ||
|
||
const IGNORED_USER_DOMAINS = ['tolgee.io']; | ||
|
||
async function loadPosthog() { | ||
return (await import('posthog-js')).default; | ||
} | ||
|
||
export function usePosthog() { | ||
const userData = useUser(); | ||
const config = useConfig(); | ||
|
||
useEffect(() => { | ||
if ( | ||
config?.postHogApiKey && | ||
userData?.id !== undefined && | ||
IGNORED_USER_DOMAINS.every( | ||
(domain) => !userData.username.endsWith(domain) | ||
) | ||
) { | ||
let cancelled = false; | ||
const postHogAPIKey = config?.postHogApiKey; | ||
if (postHogAPIKey) { | ||
Promise.resolve( | ||
window[POSTHOG_INSTANCE_WINDOW_PROPERTY] || loadPosthog() | ||
).then((posthog: PostHog) => { | ||
window[POSTHOG_INSTANCE_WINDOW_PROPERTY] = posthog; | ||
if (!cancelled) { | ||
posthog.init(postHogAPIKey, { | ||
api_host: config?.postHogHost || undefined, | ||
disable_session_recording: true, | ||
}); | ||
posthog.identify(userData.id.toString(), { | ||
name: userData.username, | ||
email: userData.username, | ||
...getUtmParams(), | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
return () => { | ||
cancelled = true; | ||
const ph = window[POSTHOG_INSTANCE_WINDOW_PROPERTY] as | ||
| PostHog | ||
| undefined; | ||
ph?.reset(); | ||
}; | ||
} | ||
}, [userData?.id, config?.postHogApiKey]); | ||
} |