Skip to content

Commit

Permalink
Merge pull request #226 from berachain/posthog-setup
Browse files Browse the repository at this point in the history
Posthog Analytics
  • Loading branch information
PaulMcInnis authored Dec 12, 2024
2 parents 7f09cee + b293d32 commit f231077
Show file tree
Hide file tree
Showing 20 changed files with 356 additions and 212 deletions.
5 changes: 5 additions & 0 deletions .env.bartio
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,8 @@ NEXT_PUBLIC_POL_SUBGRAPH_URL="https://api.goldsky.com/api/public/project_clq1h5c

# in seconds
NEXT_PUBLIC_FALLBACK_BLOCKTIME="1.9"


# Posthog ===================================================================
NEXT_PUBLIC_POSTHOG_KEY=phc_waPF31wbuiBwbfIeiV0z3GPfg3KAHE1WPMk6UMn8Kax
NEXT_PUBLIC_POSTHOG_HOST=https://eu.i.posthog.com
5 changes: 2 additions & 3 deletions .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ NEXT_PUBLIC_SENTRY_DSN=""
SENTRY_AUTH_TOKEN=""
SENTRY_ORG=""
SENTRY_PROJECT=""
NEXT_PUBLIC_MIXPANEL_PROJECT_NAME=""
# this one is deprecated in favor of NEXT_PUBLIC_MIXPANEL_PROJECT_NAME

NEXT_PUBLIC_PROJECT_NAME=""


Expand All @@ -22,4 +21,4 @@ NEXT_PUBLIC_PROJECT_NAME=""

# IPFS ===================================================================
# Uncomment the following line to build for IPFS
# NEXT_PUBLIC_HOST="ipfs"
# NEXT_PUBLIC_HOST="ipfs"
2 changes: 1 addition & 1 deletion .github/workflows/ipfs-static-build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 18.18.2
node-version: 20.11.1
cache: 'pnpm'

- name: Install
Expand Down
1 change: 1 addition & 0 deletions apps/hub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"next": "^14.2.11",
"next-auth": "^4.24.5",
"next-themes": "^0.2.1",
"posthog-js": "^1.189.0",
"react": "18.2.0",
"react-datepicker": "^4.16.0",
"react-day-picker": "^8.7.1",
Expand Down
25 changes: 25 additions & 0 deletions apps/hub/src/app/PostHogPageView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use client";

import { usePathname, useSearchParams } from "next/navigation";
import { useEffect } from "react";
import { usePostHog } from "posthog-js/react";

export default function PostHogPageView(): null {
const pathname = usePathname();
const searchParams = useSearchParams();
const posthog = usePostHog();
useEffect(() => {
// Track pageviews
if (pathname && posthog) {
let url = window.origin + pathname;
if (searchParams.toString()) {
url = `${url}?${searchParams.toString()}`;
}
posthog.capture("$pageview", {
$current_url: url,
});
}
}, [pathname, searchParams, posthog]);

return null;
}
48 changes: 32 additions & 16 deletions apps/hub/src/app/Providers.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
"use client";
import React, { type PropsWithChildren } from "react";

import React, { useEffect, type PropsWithChildren } from "react";
import { BeraJsProvider, BlockTimeProvider } from "@bera/berajs";
import { BeraWagmi } from "@bera/wagmi";
import posthog from "posthog-js";
import { PostHogProvider } from "posthog-js/react";

import { ThemeProvider } from "~/components/theme-provider";
import { BeraJsProvider, BlockTimeProvider } from "@bera/berajs";

export default function Providers({ children }: PropsWithChildren<any>) {
useEffect(() => {
if (!process.env.NEXT_PUBLIC_POSTHOG_KEY) {
return;
}
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
person_profiles: "identified_only",
capture_pageview: false, // Disable automatic pageview capture, as we capture manually
});
}, []);
return (
<BeraWagmi>
<BeraJsProvider configOverride={undefined}>
<BlockTimeProvider>
<ThemeProvider
attribute="class"
defaultTheme="dark"
forcedTheme="dark"
themes={["dark"]}
>
{children}
</ThemeProvider>
</BlockTimeProvider>
</BeraJsProvider>
</BeraWagmi>
<PostHogProvider client={posthog}>
<BeraWagmi>
<BeraJsProvider configOverride={undefined}>
<BlockTimeProvider>
<ThemeProvider
attribute="class"
defaultTheme="dark"
forcedTheme="dark"
themes={["dark"]}
>
{children}
</ThemeProvider>
</BlockTimeProvider>
</BeraJsProvider>
</BeraWagmi>
</PostHogProvider>
);
}
25 changes: 17 additions & 8 deletions apps/hub/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import "../styles/globals.css";
import { Metadata } from "next";
import dynamic from "next/dynamic";
import { IBM_Plex_Sans } from "next/font/google";
import Script from "next/script";
import { hubName, hubUrl } from "@bera/config";
Expand All @@ -15,7 +17,6 @@ import { Toaster } from "react-hot-toast";

import Providers from "./Providers";
import { navItems } from "./config";
import { Metadata } from "next";

const fontSans = IBM_Plex_Sans({
weight: ["400", "500", "600", "700"],
Expand All @@ -30,6 +31,9 @@ export const metadata: Metadata = {
default: hubName,
},
};
const PostHogPageView = dynamic(() => import("./PostHogPageView"), {
ssr: false,
});

export default function RootLayout(props: { children: React.ReactNode }) {
return (
Expand All @@ -48,11 +52,16 @@ export default function RootLayout(props: { children: React.ReactNode }) {
})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');`,
}}
/>
<body
className={cn("min-h-screen font-sans antialiased", fontSans.variable)}
>
<TermOfUseModal />
<Providers>
<Providers>
<body
className={cn(
"min-h-screen font-sans antialiased",
fontSans.variable,
)}
>
<TermOfUseModal />

<PostHogPageView />
{/* Note: This div previously had overflow-hidden, but it was removed as it interferes with sticky elements */}
<div className="relative flex min-h-screen w-full flex-col ">
<div className="z-[100]">
Expand All @@ -74,8 +83,8 @@ export default function RootLayout(props: { children: React.ReactNode }) {
</div>
<TailwindIndicator />
<Analytics />
</Providers>
</body>
</body>
</Providers>
</html>
);
}
49 changes: 37 additions & 12 deletions apps/hub/src/app/pools/[poolId]/deposit/AddLiquidityContent.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import { useCallback, useEffect, useMemo, useState } from "react";
import Link from "next/link";
import { notFound, useRouter } from "next/navigation";
import {
ADDRESS_ZERO,
Expand All @@ -15,6 +16,7 @@ import {
import { balancerVaultAddress, cloudinaryUrl } from "@bera/config";
import {
ActionButton,
AddLiquiditySuccess,
ApproveButton,
PreviewToken,
Spinner,
Expand All @@ -23,31 +25,30 @@ import {
TokenList,
TxnPreview,
getRewardsVaultUrl,
useAnalytics,
useSlippage,
useTxn,
} from "@bera/shared-ui";
import { cn } from "@bera/ui";
import { Button } from "@bera/ui/button";
import { Switch } from "@bera/ui/switch";
import { Card, CardContent, CardHeader, CardTitle } from "@bera/ui/card";
import { Icons } from "@bera/ui/icons";
import { Address, formatEther } from "viem";

import { SettingsPopover } from "~/components/settings-popover";
import { Skeleton } from "@bera/ui/skeleton";
import { AddLiquiditySuccess } from "@bera/shared-ui";
import Link from "next/link";
import useMultipleTokenApprovalsWithSlippage from "~/hooks/useMultipleTokenApprovalsWithSlippage";
import { Switch } from "@bera/ui/switch";
import { beraToken, wBeraToken } from "@bera/wagmi";
import {
AddLiquidityKind,
vaultV2Abi,
} from "@berachain-foundation/berancer-sdk";
import { AddLiquidityDetails } from "./AddLiquidiyDetails";
import { Address, formatEther, formatUnits } from "viem";

import { SettingsPopover } from "~/components/settings-popover";
import { usePool } from "~/b-sdk/usePool";
import useMultipleTokenApprovalsWithSlippage from "~/hooks/useMultipleTokenApprovalsWithSlippage";
import { getPoolUrl } from "../../fetchPools";
import { beraToken, wBeraToken } from "@bera/wagmi";
import { useAddLiquidity } from "./useAddLiquidity";
import AddLiquidityError from "./AddLiquidityError";
import { usePool } from "~/b-sdk/usePool";
import { AddLiquidityDetails } from "./AddLiquidiyDetails";
import { useAddLiquidity } from "./useAddLiquidity";

interface IAddLiquidityContent {
poolId: string;
Expand Down Expand Up @@ -128,12 +129,36 @@ export default function AddLiquidityContent({ poolId }: IAddLiquidityContent) {
setInput([]);
};
const { refresh } = usePollWalletBalances();
const { captureException, track } = useAnalytics();
const { write, ModalPortal } = useTxn({
message: `Add liquidity to ${pool?.name}`,
onSuccess: async () => {
try {
track("pool_deposit", {
poolId: pool?.id,
poolName: pool?.name,
tokensIn: queryOutput?.amountsIn.map((a) =>
pool?.tokens
? pool.tokens.find(
(t) =>
t.address.toLowerCase() === a.token.address.toLowerCase(),
)?.symbol
: a.token.address,
),
amountsIn: queryOutput?.amountsIn.map((a) =>
formatUnits(a.amount, a.token.decimals),
),
});
} catch (e) {
captureException(e);
}
reset();
refresh();
},
onError: (e) => {
track("pool_deposit_failed");
captureException(e);
},
CustomSuccessModal: pool?.address ? AddLiquiditySuccess : undefined,
customSuccessModalProps: pool?.address
? {
Expand Down Expand Up @@ -165,7 +190,7 @@ export default function AddLiquidityContent({ poolId }: IAddLiquidityContent) {
return (
<div className="mt-16 flex w-full flex-col items-center justify-center gap-4">
{ModalPortal}
<Card className="mx-6 w-full items-center bg-background p-4 sm:mx-0 sm:w-[480px] flex flex-col">
<Card className="mx-6 flex w-full flex-col items-center bg-background p-4 sm:mx-0 sm:w-[480px]">
{!pool && isLoading ? (
<Skeleton className="h-8 w-40 self-center" />
) : (
Expand Down
Loading

0 comments on commit f231077

Please sign in to comment.