Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: track posthog events #3063

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import { QueryClientProvider } from '@tanstack/react-query';
import { FunctionComponent, PropsWithChildren } from 'react';
import { TooltipProvider } from '@ballerine/ui';
import { PostHogProvider } from 'posthog-js/react';

import { queryClient } from '@/lib/react-query/query-client';
import { AuthProvider } from '@/domains/auth/context/AuthProvider/AuthProvider';
import { TooltipProvider } from '@ballerine/ui';
import { env } from '@/common/env/env';

export const Providers: FunctionComponent<PropsWithChildren> = ({ children }) => {
return (
<QueryClientProvider client={queryClient}>
<AuthProvider>
<TooltipProvider delayDuration={100}>{children}</TooltipProvider>
</AuthProvider>
</QueryClientProvider>
<PostHogProvider
apiKey={env.VITE_POSTHOG_KEY}
options={{
api_host: env.VITE_POSTHOG_HOST,
person_profiles: 'identified_only',
loaded: ph => {
ph.register_for_session({ environment: env.VITE_ENVIRONMENT_NAME });
},
}}
>
<QueryClientProvider client={queryClient}>
<AuthProvider>
<TooltipProvider delayDuration={100}>{children}</TooltipProvider>
</AuthProvider>
</QueryClientProvider>
</PostHogProvider>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ export const initializeMonitoring = () => {
return;
}

if (env.VITE_POSTHOG_KEY && env.VITE_POSTHOG_HOST) {
posthog.init(env.VITE_POSTHOG_KEY, {
api_host: env.VITE_POSTHOG_HOST,
person_profiles: 'identified_only',
loaded: ph => {
ph.register_for_session({ environment: env.VITE_ENVIRONMENT_NAME });
},
});
}

if (env.VITE_SENTRY_DSN) {
Sentry.init({
dsn: env.VITE_SENTRY_DSN,
Expand Down
2 changes: 1 addition & 1 deletion apps/backoffice-v2/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import dayjs from 'dayjs';
import advancedFormat from 'dayjs/plugin/advancedFormat';
import { initializeMonitoring } from '@/initialize-monitoring/initialize-monitoring';
import { initializeSessionRecording } from '@/initialize-session-recording/initialize-session-recording';
initializeMonitoring();

initializeMonitoring();
initializeSessionRecording();

dayjs.extend(advancedFormat);
Expand Down
2 changes: 2 additions & 0 deletions apps/backoffice-v2/src/pages/Root/Root.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FullScreenLoader } from '@/common/components/molecules/FullScreenLoader
import Chatbot from '@/domains/chat/chatbot-opengpt';
import { env } from '@/common/env/env';
import { Outlet } from 'react-router-dom';
import { PostHogPageView } from './components/PostHogRootEvents';

const ReactQueryDevtools = lazy(() =>
process.env.NODE_ENV !== 'production'
Expand Down Expand Up @@ -44,6 +45,7 @@ export const Root: FunctionComponent = () => {
return (
<Providers>
<Outlet />
<PostHogPageView />
<ChatbotLayout />
{/*<Suspense>*/}
{/* <ReactQueryDevtools />*/}
Expand Down
19 changes: 19 additions & 0 deletions apps/backoffice-v2/src/pages/Root/components/PostHogRootEvents.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// https://posthog.com/tutorials/single-page-app-pageviews#tracking-pageviews-in-react-router
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import { usePostHog } from 'posthog-js/react';

export const PostHogPageView = () => {
const location = useLocation();
const posthog = usePostHog();

useEffect(() => {
if (posthog) {
posthog.capture('$pageview', {
$current_url: window.location.href,
});
}
}, [posthog, location]);

return null;
};