Skip to content
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

Posthog Analytics #226

Merged
merged 34 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
dcc2755
chore(hub): adds posthog
bearpong Nov 26, 2024
70cf81c
fix: adds posthog page view
bearpong Nov 26, 2024
fcfb027
feat(bex): add support for PostHog analytics inside the useAnalytics …
PaulMcInnis Nov 28, 2024
40dc9f4
remove NEXT_PUBLIC_DEVELOPMENT_ANALYTICS which belongs in the .env.lo…
PaulMcInnis Nov 28, 2024
a55e6ac
Merge branch 'v2' into posthog-setup
PaulMcInnis Dec 4, 2024
edf8de9
fix(bex): rebuild lockfile w/ posthog deps + update secrets to includ…
PaulMcInnis Dec 4, 2024
9dff1a2
Merge branch 'v2' into posthog-setup
PaulMcInnis Dec 4, 2024
f6239b4
feat(bex): add support for tracking redeem events in analytics
PaulMcInnis Dec 4, 2024
743218a
fix(bex): move to a swap_token event with the data inside.
PaulMcInnis Dec 4, 2024
2022dd9
feat(bex): add tracking for redeem swaps
PaulMcInnis Dec 4, 2024
17c5b99
fix(bex): swap_redeem --> redeem
PaulMcInnis Dec 4, 2024
76557ec
feat(bex): add coverage for pool withdraw event
PaulMcInnis Dec 5, 2024
b8e7461
feat(bex): add coverage for pool deposit
PaulMcInnis Dec 5, 2024
37ac136
feat(bex): add tracking support for stake/unstake + properly track fa…
PaulMcInnis Dec 5, 2024
a767beb
Merge branch 'v2' into posthog-setup
PaulMcInnis Dec 5, 2024
3961952
feat(bex): properly handle errors when sending events to posthog + gr…
PaulMcInnis Dec 6, 2024
ac8e990
fix(bex): include the token symbols in add/withdraw + formatting
PaulMcInnis Dec 6, 2024
4d6f3cb
fix(bex): just track token symbols not addresses
PaulMcInnis Dec 6, 2024
7ba2de4
fix(bex): map the token symbols in add/withdraw using the pool token …
PaulMcInnis Dec 6, 2024
1fc9692
fix(bex): update secrets
PaulMcInnis Dec 6, 2024
336854e
Merge branch 'v2' into posthog-setup
PaulMcInnis Dec 6, 2024
8ff0258
fix(bex): rename swap_token -> swap
PaulMcInnis Dec 6, 2024
7a63b80
feat(bex): record the withdraw liquidity kind (proportional/single)
PaulMcInnis Dec 6, 2024
41f0a37
Merge branch 'v2' into posthog-setup
PaulMcInnis Dec 9, 2024
286b60f
fix(bex): dump the mixpanel hooks and config in analytics
PaulMcInnis Dec 10, 2024
b9e7eeb
fix(bex): remove mixpanel dependencies and dodge upgrading to react 1…
PaulMcInnis Dec 10, 2024
77d091e
fix(bex): update secrets
PaulMcInnis Dec 10, 2024
59ca174
Merge branch 'v2' into posthog-setup
PaulMcInnis Dec 10, 2024
9f44ea2
Merge branch 'v2' into posthog-setup
PaulMcInnis Dec 11, 2024
d00e20d
fix(bex): merge update + dump mixpanel from env.local.example
PaulMcInnis Dec 11, 2024
ddd583a
fix(bex): throw away lock on react version
PaulMcInnis Dec 11, 2024
d978342
fix(bex): reduce pnpm churn
PaulMcInnis Dec 11, 2024
385c855
chore(bex): update static build node version from 18 to 20
PaulMcInnis Dec 11, 2024
b293d32
Merge branch 'v2' into posthog-setup
PaulMcInnis Dec 12, 2024
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
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", {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this kinda sucks, but the reason is that we dont have the token symbols inside the queryOutput, so we have to match the address to get it back out from the pool data itself.

If there's a better way lmk, we need to maintain the ordering so that the amounts correspond.

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
Loading