Skip to content

Commit

Permalink
Merge pull request #409 from gitroomhq/feat/posthog
Browse files Browse the repository at this point in the history
Add posthog analytics
  • Loading branch information
nevo-david authored Oct 30, 2024
2 parents d9827f7 + 792fa7f commit 75caac2
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
8 changes: 7 additions & 1 deletion apps/frontend/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import PlausibleProvider from 'next-plausible';
import clsx from 'clsx';
import { VariableContextComponent } from '@gitroom/react/helpers/variable.context';
import { Fragment } from 'react';
import { PHProvider } from '@gitroom/react/helpers/posthog';

const chakra = Chakra_Petch({ weight: '400', subsets: ['latin'] });

Expand Down Expand Up @@ -44,7 +45,12 @@ export default async function AppLayout({ children }: { children: ReactNode }) {
<Plausible
domain={!!process.env.IS_GENERAL ? 'postiz.com' : 'gitroom.com'}
>
<LayoutContext>{children}</LayoutContext>
<PHProvider
key={process.env.NEXT_PUBLIC_POSTHOG_KEY}
host={process.env.NEXT_PUBLIC_POSTHOG_HOST}
>
<LayoutContext>{children}</LayoutContext>
</PHProvider>
</Plausible>
</VariableContextComponent>
</body>
Expand Down
5 changes: 5 additions & 0 deletions libraries/helpers/src/utils/use.fire.events.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { usePlausible } from 'next-plausible';
import { useCallback } from 'react';
import { useVariables } from '@gitroom/react/helpers/variable.context';
import { usePostHog } from 'posthog-js/react';

export const useFireEvents = () => {
const { billingEnabled } = useVariables();
const plausible = usePlausible();
const posthog = usePostHog();

return useCallback((name: string, props?: any) => {
if (!billingEnabled) {
return;
}

posthog.capture(name, props);
plausible(name, { props });
}, []);
};
28 changes: 28 additions & 0 deletions libraries/react-shared-libraries/src/helpers/posthog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use client';

import posthog from 'posthog-js';
import { PostHogProvider } from 'posthog-js/react';
import { FC, ReactNode, useEffect } from 'react';

export const PHProvider: FC<{
children: ReactNode;
key?: string;
host?: string;
}> = ({ children, key, host }) => {
useEffect(() => {
if (!key || !host) {
return;
}

posthog.init(key, {
api_host: host,
person_profiles: 'identified_only',
capture_pageview: false, // Disable automatic pageview capture, as we capture manually
});
}, []);

if (!key || !host) {
return <>{children}</>;
}
return <PostHogProvider client={posthog}>{children}</PostHogProvider>;
};
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"nx": "19.7.2",
"openai": "^4.47.1",
"polotno": "^2.10.5",
"posthog-js": "^1.178.0",
"react": "18.3.1",
"react-colorful": "^5.6.1",
"react-dnd": "^16.0.1",
Expand Down

0 comments on commit 75caac2

Please sign in to comment.