Skip to content

Commit e85d198

Browse files
jnsdlsMananTank
authored andcommitted
posthog migration part 2
1 parent 2d67599 commit e85d198

File tree

10 files changed

+84
-2
lines changed

10 files changed

+84
-2
lines changed

apps/dashboard/.eslintrc.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ module.exports = {
110110
message:
111111
'This is likely a mistake. If you really want to import this - postfix the imported name with Icon. Example - "LinkIcon"',
112112
},
113+
{
114+
name: "posthog-js",
115+
message:
116+
'Import "posthog-js" directly only within the analytics helpers ("src/@/analytics/*"). Use the exported helpers from "@/analytics/track" elsewhere.',
117+
},
113118
],
114119
},
115120
],
@@ -139,6 +144,13 @@ module.exports = {
139144
"no-restricted-imports": ["off"],
140145
},
141146
},
147+
// allow direct PostHog imports inside analytics helpers
148+
{
149+
files: "src/@/analytics/**/*",
150+
rules: {
151+
"no-restricted-imports": ["off"],
152+
},
153+
},
142154
// enable rule specifically for TypeScript files
143155
{
144156
files: ["*.ts", "*.tsx"],
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use client";
2+
3+
import posthog from "posthog-js";
4+
import { useEffect } from "react";
5+
6+
export function useIdentifyAccount(opts?: {
7+
accountId: string;
8+
email?: string;
9+
}) {
10+
// eslint-disable-next-line no-restricted-syntax
11+
useEffect(() => {
12+
// if no accountId, don't identify
13+
if (!opts?.accountId) {
14+
return;
15+
}
16+
17+
// if email is provided, add it to the identify
18+
posthog.identify(opts.accountId, opts.email ? { email: opts.email } : {});
19+
}, [opts?.accountId, opts?.email]);
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use client";
2+
3+
import posthog from "posthog-js";
4+
import { useEffect } from "react";
5+
6+
export function useIdentifyTeam(opts?: {
7+
teamId: string;
8+
}) {
9+
// eslint-disable-next-line no-restricted-syntax
10+
useEffect(() => {
11+
// if no teamId, don't identify
12+
if (!opts?.teamId) {
13+
return;
14+
}
15+
16+
// identify the team
17+
posthog.group("team", opts.teamId);
18+
}, [opts?.teamId]);
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use client";
2+
3+
import posthog from "posthog-js";
4+
5+
export function resetAnalytics() {
6+
posthog.reset();
7+
}

apps/dashboard/src/@3rdweb-sdk/react/components/connect-wallet/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use client";
22

3+
import { resetAnalytics } from "@/analytics/reset";
34
import { Button } from "@/components/ui/button";
45
import { useStore } from "@/lib/reactive";
56
import { getSDKTheme } from "app/(app)/components/sdk-component-theme";
@@ -156,6 +157,7 @@ export const CustomConnectWallet = (props: {
156157
onDisconnect={async () => {
157158
try {
158159
await doLogout();
160+
resetAnalytics();
159161
} catch (err) {
160162
console.error("Failed to log out", err);
161163
}

apps/dashboard/src/app/(app)/account/components/AccountHeader.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use client";
22

33
import { createTeam } from "@/actions/createTeam";
4+
import { resetAnalytics } from "@/analytics/reset";
45
import type { Project } from "@/api/projects";
56
import type { Team } from "@/api/team";
67
import { useDashboardRouter } from "@/lib/DashboardRouter";
@@ -35,6 +36,7 @@ export function AccountHeader(props: {
3536
const logout = useCallback(async () => {
3637
try {
3738
await doLogout();
39+
resetAnalytics();
3840
if (wallet) {
3941
disconnect(wallet);
4042
}

apps/dashboard/src/app/(app)/account/settings/AccountSettingsPage.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { confirmEmailWithOTP } from "@/actions/confirmEmail";
33
import { apiServerProxy } from "@/actions/proxies";
44
import { updateAccount } from "@/actions/updateAccount";
5+
import { resetAnalytics } from "@/analytics/reset";
56
import { useDashboardRouter } from "@/lib/DashboardRouter";
67
import type { Account } from "@3rdweb-sdk/react/hooks/useApi";
78
import type { ThirdwebClient } from "thirdweb";
@@ -46,6 +47,7 @@ export function AccountSettingsPage(props: {
4647
}}
4748
onAccountDeleted={async () => {
4849
await doLogout();
50+
resetAnalytics();
4951
if (activeWallet) {
5052
disconnect(activeWallet);
5153
}

apps/dashboard/src/app/(app)/login/LoginPage.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use client";
22

33
import { getRawAccountAction } from "@/actions/getAccount";
4+
import { resetAnalytics } from "@/analytics/reset";
45
import { ClientOnly } from "@/components/blocks/client-only";
56
import { GenericLoadingPage } from "@/components/blocks/skeletons/GenericLoadingPage";
67
import { ToggleThemeButton } from "@/components/color-mode-toggle";
@@ -313,7 +314,10 @@ function CustomConnectEmbed(props: {
313314
throw e;
314315
}
315316
},
316-
doLogout,
317+
doLogout: async () => {
318+
await doLogout();
319+
resetAnalytics();
320+
},
317321
isLoggedIn: async (x) => {
318322
const isLoggedInResult = await isLoggedIn(x);
319323
if (isLoggedInResult) {

apps/dashboard/src/app/(app)/team/components/TeamHeader/team-header-logged-in.client.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"use client";
22

33
import { createTeam } from "@/actions/createTeam";
4+
import { useIdentifyAccount } from "@/analytics/hooks/identify-account";
5+
import { useIdentifyTeam } from "@/analytics/hooks/identify-team";
6+
import { resetAnalytics } from "@/analytics/reset";
47
import type { Project } from "@/api/projects";
58
import type { Team } from "@/api/team";
69
import { useDashboardRouter } from "@/lib/DashboardRouter";
@@ -12,7 +15,6 @@ import { toast } from "sonner";
1215
import type { ThirdwebClient } from "thirdweb";
1316
import { useActiveWallet, useDisconnect } from "thirdweb/react";
1417
import { doLogout } from "../../../login/auth-actions";
15-
1618
import {
1719
type TeamHeaderCompProps,
1820
TeamHeaderDesktopUI,
@@ -27,6 +29,16 @@ export function TeamHeaderLoggedIn(props: {
2729
accountAddress: string;
2830
client: ThirdwebClient;
2931
}) {
32+
// identify the account
33+
useIdentifyAccount({
34+
accountId: props.account.id,
35+
email: props.account.email,
36+
});
37+
38+
// identify the team
39+
useIdentifyTeam({
40+
teamId: props.currentTeam.id,
41+
});
3042
const [createProjectDialogState, setCreateProjectDialogState] = useState<
3143
{ team: Team; isOpen: true } | { isOpen: false }
3244
>({ isOpen: false });
@@ -38,6 +50,7 @@ export function TeamHeaderLoggedIn(props: {
3850
// log out the user
3951
try {
4052
await doLogout();
53+
resetAnalytics();
4154
if (activeWallet) {
4255
disconnect(activeWallet);
4356
}

apps/dashboard/src/instrumentation-client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// eslint-disable-next-line no-restricted-imports
12
import posthog from "posthog-js";
23

34
const NEXT_PUBLIC_POSTHOG_KEY = process.env.NEXT_PUBLIC_POSTHOG_KEY;

0 commit comments

Comments
 (0)