Skip to content

Commit 95418cd

Browse files
authoredAug 5, 2024··
feat(analytics): add posthog (#2610)
* feat(analytics): add posthog * feat(analytics): add posthog * chore(sync): fix cross env keys for local

File tree

12 files changed

+118
-5
lines changed

12 files changed

+118
-5
lines changed
 

‎apps/backoffice-v2/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,17 @@
7575
"@radix-ui/react-slot": "^1.0.1",
7676
"@radix-ui/react-switch": "^1.0.3",
7777
"@radix-ui/react-tabs": "^1.0.4",
78-
"@react-pdf/renderer": "^3.1.14",
7978
"@radix-ui/react-tooltip": "^1.0.7",
79+
"@react-pdf/renderer": "^3.1.14",
8080
"@rjsf/utils": "^5.9.0",
8181
"@tanstack/react-query": "^4.19.1",
8282
"@tanstack/react-table": "^8.9.2",
8383
"ballerine-daisyui": "^2.49.6",
8484
"broadcast-channel": "^7.0.0",
8585
"class-variance-authority": "^0.6.0",
8686
"clsx": "^1.2.1",
87-
"dayjs": "^1.11.6",
8887
"date-fns": "^3.0.6",
88+
"dayjs": "^1.11.6",
8989
"eslint-plugin-tailwindcss": "^3.8.0",
9090
"face-api.js": "^0.22.2",
9191
"framer-motion": "^8.3.4",
@@ -97,6 +97,7 @@
9797
"lucide-react": "^0.239.0",
9898
"match-sorter": "^6.3.1",
9999
"msw": "^1.0.0",
100+
"posthog-js": "^1.154.2",
100101
"qs": "^6.11.2",
101102
"react": "^18.2.0",
102103
"react-day-picker": "^8.10.1",

‎apps/backoffice-v2/src/common/env/schema.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { z } from 'zod';
22

33
export const EnvSchema = z.object({
44
MODE: z.enum(['development', 'production', 'test']),
5+
VITE_ENVIRONMENT_NAME: z.enum(['development', 'production', 'sandbox', 'local']),
56
VITE_API_URL: z.string().url().default('https://api-dev.ballerine.io/v2'),
67
VITE_API_KEY: z.string(),
78
VITE_AUTH_ENABLED: z.preprocess(
@@ -27,4 +28,6 @@ export const EnvSchema = z.object({
2728
value => (typeof value === 'string' ? JSON.parse(value) : value),
2829
z.boolean().default(true),
2930
),
31+
VITE_REACT_APP_PUBLIC_POSTHOG_KEY: z.string().optional(),
32+
VITE_REACT_APP_PUBLIC_POSTHOG_HOST: z.string().optional(),
3033
});

‎apps/backoffice-v2/src/domains/auth/fetchers.ts

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { z } from 'zod';
44
import { handleZodError } from '../../common/utils/handle-zod-error/handle-zod-error';
55
import { Method } from '../../common/enums';
66
import { AuthenticatedUserSchema } from './validation-schemas';
7+
import posthog from 'posthog-js';
78

89
export const fetchSignOut = async ({ callbackUrl }: ISignInProps) => {
910
const [session, error] = await apiClient({
@@ -15,6 +16,8 @@ export const fetchSignOut = async ({ callbackUrl }: ISignInProps) => {
1516
},
1617
});
1718

19+
posthog.reset();
20+
1821
return handleZodError(error, session);
1922
};
2023

@@ -52,5 +55,10 @@ export const fetchAuthenticatedUser = async () => {
5255
}),
5356
});
5457

58+
posthog.identify(session?.user?.id, {
59+
email: session?.user?.email,
60+
name: session?.user?.fullName,
61+
});
62+
5563
return handleZodError(error, session);
5664
};

‎apps/backoffice-v2/src/main.tsx

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ import './i18n';
1515
import './index.css';
1616
import dayjs from 'dayjs';
1717
import advancedFormat from 'dayjs/plugin/advancedFormat';
18+
import posthog from 'posthog-js';
19+
20+
if (
21+
!window.location.host.includes('127.0.0.1') &&
22+
!window.location.host.includes('localhost') &&
23+
env.VITE_REACT_APP_PUBLIC_POSTHOG_KEY &&
24+
env.VITE_REACT_APP_PUBLIC_POSTHOG_HOST
25+
) {
26+
posthog.init(env.VITE_REACT_APP_PUBLIC_POSTHOG_KEY!, {
27+
api_host: env.VITE_REACT_APP_PUBLIC_POSTHOG_HOST!,
28+
person_profiles: 'identified_only',
29+
loaded: ph => {
30+
ph.register_for_session({ environment: env.VITE_ENVIRONMENT_NAME });
31+
},
32+
});
33+
}
1834

1935
dayjs.extend(advancedFormat);
2036

‎apps/kyb-app/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"test:dev": "vitest"
1515
},
1616
"dependencies": {
17-
"@ballerine/common": "^0.9.19",
1817
"@ballerine/blocks": "0.2.11",
18+
"@ballerine/common": "^0.9.19",
1919
"@ballerine/ui": "0.5.12",
2020
"@ballerine/workflow-browser-sdk": "0.6.30",
2121
"@lukemorales/query-key-factory": "^1.0.3",
@@ -46,6 +46,7 @@
4646
"lodash": "^4.17.21",
4747
"lucide-react": "^0.144.0",
4848
"p-queue": "^7.4.1",
49+
"posthog-js": "^1.154.2",
4950
"qs": "^6.11.2",
5051
"react": "^18.2.0",
5152
"react-dom": "^18.2.0",

‎apps/kyb-app/src/domains/collection-flow/collection-flow.api.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@ import {
88
UISchema,
99
} from '@/domains/collection-flow/types';
1010
import { CollectionFlowContext } from '@/domains/collection-flow/types/flow-context.types';
11+
import posthog from 'posthog-js';
1112

1213
export const fetchUser = async (): Promise<TUser> => {
13-
return await request.get('collection-flow/user').json<TUser>();
14+
const user = await request.get('collection-flow/user').json<TUser>();
15+
if (user) {
16+
try {
17+
posthog.identify(user.id, {
18+
email: user.email,
19+
});
20+
} catch (error) {
21+
console.error('Error identifying user in PostHog:', error);
22+
}
23+
}
24+
return user;
1425
};
1526

1627
export const getFlowSession = fetchUser;

‎apps/kyb-app/src/main.tsx

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ import { Head } from './Head';
1515
import './i18next';
1616
import './index.css';
1717
import { sentyRouterInstrumentation } from './router';
18+
import posthog from 'posthog-js';
19+
20+
if (
21+
!window.location.host.includes('127.0.0.1') &&
22+
!window.location.host.includes('localhost') &&
23+
import.meta.env.VITE_REACT_APP_PUBLIC_POSTHOG_KEY &&
24+
import.meta.env.VITE_REACT_APP_PUBLIC_POSTHOG_HOST
25+
) {
26+
posthog.init(import.meta.env.VITE_REACT_APP_PUBLIC_POSTHOG_KEY!, {
27+
api_host: import.meta.env.VITE_REACT_APP_PUBLIC_POSTHOG_HOST!,
28+
person_profiles: 'identified_only',
29+
loaded: ph => {
30+
ph.register_for_session({ environment: import.meta.env.VITE_ENVIRONMENT_NAME });
31+
},
32+
});
33+
}
1834

1935
const getApiOrigin = () => {
2036
const url = new URL(import.meta.env.VITE_API_URL);

‎apps/workflows-dashboard/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"jsoneditor": "^10.1.0",
4343
"lodash": "^4.17.21",
4444
"lucide-react": "^0.144.0",
45+
"posthog-js": "^1.154.2",
4546
"react": "^18.2.0",
4647
"react-custom-scrollbars": "^4.2.1",
4748
"react-dom": "^18.2.0",
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
import { GetSessionResponse } from '@/domains/auth/api/session/session.types';
22
import { IUser } from '@/domains/auth/common/types';
33
import { request } from '@/lib/request';
4+
import posthog from 'posthog-js';
45

56
export async function fetchSession(): Promise<IUser | null> {
67
const result = await request.get<GetSessionResponse>('internal/auth/session');
8+
if (result.data.user) {
9+
try {
10+
posthog.identify(result.data.user.id, {
11+
email: result.data.user.email,
12+
name: result.data.user.firstName + ' ' + result.data.user.lastName,
13+
});
14+
} catch (error) {
15+
console.error('Error identifying user in PostHog:', error);
16+
}
17+
}
718

819
return result.data.user ? result.data.user : null;
920
}

‎apps/workflows-dashboard/src/main.tsx

+16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ import * as React from 'react';
33
import * as ReactDOM from 'react-dom/client';
44
import { RouterProvider } from 'react-router-dom';
55
import './index.css';
6+
import posthog from 'posthog-js';
7+
8+
if (
9+
// !window.location.host.includes('127.0.0.1') &&
10+
// !window.location.host.includes('localhost') &&
11+
import.meta.env.VITE_REACT_APP_PUBLIC_POSTHOG_KEY &&
12+
import.meta.env.VITE_REACT_APP_PUBLIC_POSTHOG_HOST
13+
) {
14+
posthog.init(import.meta.env.VITE_REACT_APP_PUBLIC_POSTHOG_KEY!, {
15+
api_host: import.meta.env.VITE_REACT_APP_PUBLIC_POSTHOG_HOST!,
16+
person_profiles: 'identified_only',
17+
loaded: ph => {
18+
ph.register_for_session({ environment: import.meta.env.VITE_ENVIRONMENT_NAME });
19+
},
20+
});
21+
}
622

723
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
824
<React.StrictMode>

‎pnpm-lock.yaml

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.