MCPcopy Index your code
hub / github.com/triggerdotdev/trigger.dev / usePostHog

Function usePostHog

apps/webapp/app/hooks/usePostHog.ts:9–79  ·  view source on GitHub ↗
(apiKey?: string, logging = false, debug = false)

Source from the content-addressed store, hash-verified

7import { useJobChanged } from "./useJob";
8
9export 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) => {

Callers 1

AppFunction · 0.90

Calls 8

useOptionalUserFunction · 0.90
useUserChangedFunction · 0.90
useOrganizationChangedFunction · 0.90
useProjectChangedFunction · 0.90
useJobChangedFunction · 0.90
identifyMethod · 0.80
logMethod · 0.65
resetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…