(apiKey?: string, logging = false, debug = false)
| 7 | import { useJobChanged } from "./useJob"; |
| 8 | |
| 9 | export const usePostHog = (apiKey?: string, logging = false, debug = false): void => { |
| 10 | const postHogInitialized = useRef(false); |
| 11 | const location = useLocation(); |
| 12 | const user = useOptionalUser(); |
| 13 | |
| 14 | //start PostHog once |
| 15 | useEffect(() => { |
| 16 | if (apiKey === undefined || apiKey === "") return; |
| 17 | if (postHogInitialized.current === true) return; |
| 18 | if (logging) console.log("Initializing PostHog"); |
| 19 | posthog.init(apiKey, { |
| 20 | api_host: "https://eu.posthog.com", |
| 21 | opt_in_site_apps: true, |
| 22 | debug, |
| 23 | loaded: function (posthog) { |
| 24 | if (logging) console.log("PostHog loaded"); |
| 25 | if (user !== undefined) { |
| 26 | if (logging) console.log("Loaded: Identifying user", user); |
| 27 | posthog.identify(user.id, { email: user.email }); |
| 28 | } |
| 29 | }, |
| 30 | }); |
| 31 | postHogInitialized.current = true; |
| 32 | }, [apiKey, logging, user]); |
| 33 | |
| 34 | useUserChanged((user) => { |
| 35 | if (postHogInitialized.current === false) return; |
| 36 | if (logging) console.log("User changed"); |
| 37 | if (user) { |
| 38 | if (logging) console.log("Identifying user", user); |
| 39 | posthog.identify(user.id, { email: user.email }); |
| 40 | } else { |
| 41 | if (logging) console.log("Resetting user"); |
| 42 | posthog.reset(); |
| 43 | } |
| 44 | }); |
| 45 | |
| 46 | useOrganizationChanged((org) => { |
| 47 | if (postHogInitialized.current === false) return; |
| 48 | if (org) { |
| 49 | if (logging) console.log(`Grouping by organization`, org); |
| 50 | posthog.group("organization", org.id); |
| 51 | } else { |
| 52 | //reset the groups when you go to one of the top-level pages |
| 53 | if (logging) console.log("Resetting groups"); |
| 54 | posthog.resetGroups(); |
| 55 | } |
| 56 | }); |
| 57 | |
| 58 | useProjectChanged((project) => { |
| 59 | if (postHogInitialized.current === false) return; |
| 60 | if (project) { |
| 61 | if (logging) console.log(`Grouping by project`, project); |
| 62 | posthog.group("project", project.id); |
| 63 | } |
| 64 | }); |
| 65 | |
| 66 | useJobChanged((job) => { |
no test coverage detected
searching dependent graphs…