Skip to content

Commit

Permalink
chore: move analytics hook to util for api usage
Browse files Browse the repository at this point in the history
  • Loading branch information
chambaz committed Dec 3, 2023
1 parent e903055 commit 6770695
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
VersionedTransaction,
} from "@solana/web3.js";
import { useConnection } from "~/hooks/useConnection";
import { useAnalytics } from "~/hooks/useAnalytics";
import { capture } from "~/utils/analytics";
import { SwapMode, useJupiter } from "@jup-ag/react-hook";
import JSBI from "jsbi";
import { StakeData, usePrevious } from "~/utils";
Expand Down Expand Up @@ -73,7 +73,6 @@ export const StakingCard: FC = () => {
const router = useRouter();
const { connection } = useConnection();
const { connected, wallet, walletAddress } = useWalletContext();
const { capture } = useAnalytics();

const [setIsWalletAuthDialogOpen] = useUiStore((state) => [state.setIsWalletAuthDialogOpen]);
const [
Expand Down
3 changes: 1 addition & 2 deletions apps/marginfi-v2-ui/src/components/common/Wallet/Wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useMrgnlendStore, useUiStore } from "~/store";
import { useConnection } from "~/hooks/useConnection";
import { useWalletContext } from "~/hooks/useWalletContext";
import { useIsMobile } from "~/hooks/useIsMobile";
import { useAnalytics } from "~/hooks/useAnalytics";
import { setPersonProperties } from "~/utils/analytics";

import { MrgnTooltip } from "~/components/common/MrgnTooltip";
import {
Expand All @@ -33,7 +33,6 @@ export const Wallet = () => {
const { connection } = useConnection();
const { wallet, connected, logout, pfp, requestPrivateKey, web3AuthPk, web3AuthConncected } = useWalletContext();
const isMobile = useIsMobile();
const { setPersonProperties } = useAnalytics();

const [isWalletAddressCopied, setIsWalletAddressCopied] = React.useState(false);
const [isFundingAddressCopied, setIsFundingAddressCopied] = React.useState(false);
Expand Down
33 changes: 0 additions & 33 deletions apps/marginfi-v2-ui/src/hooks/useAnalytics.ts

This file was deleted.

4 changes: 1 addition & 3 deletions apps/marginfi-v2-ui/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useLstStore } from "./stake";
import { Desktop, Mobile } from "~/mediaQueries";
import { WalletProvider as MrgnWalletProvider } from "~/hooks/useWalletContext";
import { ConnectionProvider } from "~/hooks/useConnection";
import { useAnalytics } from "~/hooks/useAnalytics";
import { init as initAnalytics } from "~/utils/analytics";

import { MobileNavbar } from "~/components/mobile/MobileNavbar";
import { Tutorial } from "~/components/common/Tutorial";
Expand Down Expand Up @@ -55,8 +55,6 @@ const MyApp = ({ Component, pageProps }: AppProps) => {
state.isRefreshingStore,
]);

const { init: initAnalytics } = useAnalytics();

// enable matomo heartbeat
React.useEffect(() => {
if (process.env.NEXT_PUBLIC_MARGINFI_ENVIRONMENT === "alpha") {
Expand Down
3 changes: 1 addition & 2 deletions apps/marginfi-v2-ui/src/pages/api/user/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
STATUS_OK,
firebaseApi,
} from "@mrgnlabs/marginfi-v2-ui-state";
import { useAnalytics } from "~/hooks/useAnalytics";
import { capture, identify } from "~/utils/analytics";

initFirebaseIfNeeded();

Expand All @@ -26,7 +26,6 @@ export interface LoginRequest {

export default async function handler(req: NextApiRequest<LoginRequest>, res: any) {
const { walletAddress } = req.body;
const { capture, identify } = useAnalytics();

/* signing logic
let signer;
Expand Down
4 changes: 1 addition & 3 deletions apps/marginfi-v2-ui/src/pages/api/user/signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
STATUS_OK,
firebaseApi,
} from "@mrgnlabs/marginfi-v2-ui-state";
import { useAnalytics } from "~/hooks/useAnalytics";
import { capture, identify } from "~/utils/analytics";

initFirebaseIfNeeded();

Expand All @@ -27,8 +27,6 @@ export interface SignupRequest {
export default async function handler(req: NextApiRequest<SignupRequest>, res: any) {
const { walletAddress, payload } = req.body;

const { capture, identify } = useAnalytics();

Sentry.setContext("signup_args", {
walletAddress,
});
Expand Down
24 changes: 24 additions & 0 deletions apps/marginfi-v2-ui/src/utils/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import posthog from "posthog-js";

const _checkEnv = () =>
!process.env.NEXT_PUBLIC_POSTHOG_API_KEY || process.env.NEXT_PUBLIC_MARGINFI_ENVIRONMENT !== "production";

export const init = () => {
if (_checkEnv()) return;
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_API_KEY!, { api_host: "https://app.posthog.com" });
};

export const capture = (event: string, properties?: Record<string, any>) => {
if (_checkEnv()) return;
posthog.capture(event, properties);
};

export const identify = (userId: string, properties?: Record<string, any>) => {
if (_checkEnv()) return;
posthog.identify(userId, properties);
};

export const setPersonProperties = (properties: Record<string, any>) => {
if (_checkEnv()) return;
posthog.setPersonProperties(properties);
};
15 changes: 1 addition & 14 deletions apps/marginfi-v2-ui/src/utils/mrgnActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Connection, PublicKey, Transaction } from "@solana/web3.js";
import { Wallet, processTransaction } from "@mrgnlabs/mrgn-common";
import { WalletContextState } from "@solana/wallet-adapter-react";
import { WalletContextStateOverride } from "~/hooks/useWalletContext";
import { useAnalytics } from "~/hooks/useAnalytics";
import { capture } from "~/utils/analytics";
import { MultiStepToastHandle, showErrorToast } from "./toastUtils";

export type MarginfiActionParams = {
Expand Down Expand Up @@ -79,8 +79,6 @@ async function createAccountAndDeposit({
return;
}

const { capture } = useAnalytics();

const multiStepToast = new MultiStepToastHandle("Initial deposit", [
{ label: "Creating account" },
{ label: `Depositing ${amount} ${bank.meta.tokenSymbol}` },
Expand Down Expand Up @@ -126,8 +124,6 @@ export async function deposit({
bank: ExtendedBankInfo;
amount: number;
}) {
const { capture } = useAnalytics();

const multiStepToast = new MultiStepToastHandle("Deposit", [
{ label: `Depositing ${amount} ${bank.meta.tokenSymbol}` },
]);
Expand Down Expand Up @@ -159,8 +155,6 @@ export async function borrow({
bank: ExtendedBankInfo;
amount: number;
}) {
const { capture } = useAnalytics();

const multiStepToast = new MultiStepToastHandle("Borrow", [
{ label: `Borrowing ${amount} ${bank.meta.tokenSymbol}` },
]);
Expand Down Expand Up @@ -192,8 +186,6 @@ export async function withdraw({
bank: ExtendedBankInfo;
amount: number;
}) {
const { capture } = useAnalytics();

const multiStepToast = new MultiStepToastHandle("Withdrawal", [
{ label: `Withdrawing ${amount} ${bank.meta.tokenSymbol}` },
]);
Expand Down Expand Up @@ -225,8 +217,6 @@ export async function repay({
bank: ExtendedBankInfo;
amount: number;
}) {
const { capture } = useAnalytics();

const multiStepToast = new MultiStepToastHandle("Repayment", [
{ label: `Repaying ${amount} ${bank.meta.tokenSymbol}` },
]);
Expand Down Expand Up @@ -255,7 +245,6 @@ export async function collectRewardsBatch(
marginfiAccount: MarginfiAccountWrapper,
bankAddresses: PublicKey[]
) {
const { capture } = useAnalytics();
const multiStepToast = new MultiStepToastHandle("Collect rewards", [{ label: "Collecting rewards" }]);
multiStepToast.start();

Expand Down Expand Up @@ -300,8 +289,6 @@ export const closeBalance = async ({
return;
}

const { capture } = useAnalytics();

const multiStepToast = new MultiStepToastHandle("Closing balance", [
{ label: `Closing ${bank.position.isLending ? "lending" : "borrow"} balance for ${bank.meta.tokenSymbol}` },
]);
Expand Down

1 comment on commit 6770695

@vercel
Copy link

@vercel vercel bot commented on 6770695 Dec 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.