Skip to content

posthog migration: part 2 #7364

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

Merged
merged 1 commit into from
Jun 19, 2025
Merged
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
12 changes: 12 additions & 0 deletions apps/dashboard/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ module.exports = {
message:
'This is likely a mistake. If you really want to import this - postfix the imported name with Icon. Example - "LinkIcon"',
},
{
name: "posthog-js",
message:
'Import "posthog-js" directly only within the analytics helpers ("src/@/analytics/*"). Use the exported helpers from "@/analytics/track" elsewhere.',
},
],
},
],
Expand Down Expand Up @@ -139,6 +144,13 @@ module.exports = {
"no-restricted-imports": ["off"],
},
},
// allow direct PostHog imports inside analytics helpers
{
files: "src/@/analytics/**/*",
rules: {
"no-restricted-imports": ["off"],
},
},
// enable rule specifically for TypeScript files
{
files: ["*.ts", "*.tsx"],
Expand Down
20 changes: 20 additions & 0 deletions apps/dashboard/src/@/analytics/hooks/identify-account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use client";

import posthog from "posthog-js";
import { useEffect } from "react";

export function useIdentifyAccount(opts?: {
accountId: string;
email?: string;
}) {
// eslint-disable-next-line no-restricted-syntax
useEffect(() => {
// if no accountId, don't identify
if (!opts?.accountId) {
return;
}

// if email is provided, add it to the identify
posthog.identify(opts.accountId, opts.email ? { email: opts.email } : {});
}, [opts?.accountId, opts?.email]);
}
19 changes: 19 additions & 0 deletions apps/dashboard/src/@/analytics/hooks/identify-team.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use client";

import posthog from "posthog-js";
import { useEffect } from "react";

export function useIdentifyTeam(opts?: {
teamId: string;
}) {
// eslint-disable-next-line no-restricted-syntax
useEffect(() => {
// if no teamId, don't identify
if (!opts?.teamId) {
return;
}

// identify the team
posthog.group("team", opts.teamId);
}, [opts?.teamId]);
}
7 changes: 7 additions & 0 deletions apps/dashboard/src/@/analytics/reset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use client";

import posthog from "posthog-js";

export function resetAnalytics() {
posthog.reset();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";

import { resetAnalytics } from "@/analytics/reset";
import { Button } from "@/components/ui/button";
import { useStore } from "@/lib/reactive";
import { getSDKTheme } from "app/(app)/components/sdk-component-theme";
Expand Down Expand Up @@ -156,6 +157,7 @@ export const CustomConnectWallet = (props: {
onDisconnect={async () => {
try {
await doLogout();
resetAnalytics();
} catch (err) {
console.error("Failed to log out", err);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import { createTeam } from "@/actions/createTeam";
import { resetAnalytics } from "@/analytics/reset";
import type { Project } from "@/api/projects";
import type { Team } from "@/api/team";
import { useDashboardRouter } from "@/lib/DashboardRouter";
Expand Down Expand Up @@ -35,6 +36,7 @@ export function AccountHeader(props: {
const logout = useCallback(async () => {
try {
await doLogout();
resetAnalytics();
if (wallet) {
disconnect(wallet);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { confirmEmailWithOTP } from "@/actions/confirmEmail";
import { apiServerProxy } from "@/actions/proxies";
import { updateAccount } from "@/actions/updateAccount";
import { resetAnalytics } from "@/analytics/reset";
import { useDashboardRouter } from "@/lib/DashboardRouter";
import type { Account } from "@3rdweb-sdk/react/hooks/useApi";
import type { ThirdwebClient } from "thirdweb";
Expand Down Expand Up @@ -46,6 +47,7 @@ export function AccountSettingsPage(props: {
}}
onAccountDeleted={async () => {
await doLogout();
resetAnalytics();
if (activeWallet) {
disconnect(activeWallet);
}
Expand Down
6 changes: 5 additions & 1 deletion apps/dashboard/src/app/(app)/login/LoginPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import { getRawAccountAction } from "@/actions/getAccount";
import { resetAnalytics } from "@/analytics/reset";
import { ClientOnly } from "@/components/blocks/client-only";
import { GenericLoadingPage } from "@/components/blocks/skeletons/GenericLoadingPage";
import { ToggleThemeButton } from "@/components/color-mode-toggle";
Expand Down Expand Up @@ -313,7 +314,10 @@ function CustomConnectEmbed(props: {
throw e;
}
},
doLogout,
doLogout: async () => {
await doLogout();
resetAnalytics();
},
isLoggedIn: async (x) => {
const isLoggedInResult = await isLoggedIn(x);
if (isLoggedInResult) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"use client";

import { createTeam } from "@/actions/createTeam";
import { useIdentifyAccount } from "@/analytics/hooks/identify-account";
import { useIdentifyTeam } from "@/analytics/hooks/identify-team";
import { resetAnalytics } from "@/analytics/reset";
import type { Project } from "@/api/projects";
import type { Team } from "@/api/team";
import { useDashboardRouter } from "@/lib/DashboardRouter";
Expand All @@ -12,7 +15,6 @@ import { toast } from "sonner";
import type { ThirdwebClient } from "thirdweb";
import { useActiveWallet, useDisconnect } from "thirdweb/react";
import { doLogout } from "../../../login/auth-actions";

import {
type TeamHeaderCompProps,
TeamHeaderDesktopUI,
Expand All @@ -27,6 +29,16 @@ export function TeamHeaderLoggedIn(props: {
accountAddress: string;
client: ThirdwebClient;
}) {
// identify the account
useIdentifyAccount({
accountId: props.account.id,
email: props.account.email,
});

// identify the team
useIdentifyTeam({
teamId: props.currentTeam.id,
});
const [createProjectDialogState, setCreateProjectDialogState] = useState<
{ team: Team; isOpen: true } | { isOpen: false }
>({ isOpen: false });
Expand All @@ -38,6 +50,7 @@ export function TeamHeaderLoggedIn(props: {
// log out the user
try {
await doLogout();
resetAnalytics();
if (activeWallet) {
disconnect(activeWallet);
}
Expand Down
1 change: 1 addition & 0 deletions apps/dashboard/src/instrumentation-client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line no-restricted-imports
import posthog from "posthog-js";

const NEXT_PUBLIC_POSTHOG_KEY = process.env.NEXT_PUBLIC_POSTHOG_KEY;
Expand Down
Loading