diff --git a/packages/keychain/src/components/ErrorBoundary.tsx b/packages/keychain/src/components/ErrorBoundary.tsx
index fe6bf3072..6a5211f0d 100644
--- a/packages/keychain/src/components/ErrorBoundary.tsx
+++ b/packages/keychain/src/components/ErrorBoundary.tsx
@@ -4,7 +4,7 @@ import { AlertIcon, ExternalIcon, Button } from "@cartridge/ui-next";
import { useConnection } from "@/hooks/connection";
import { CARTRIDGE_DISCORD_LINK } from "@/const";
import { Link } from "react-router-dom";
-import { usePostHog } from "@/context/posthog";
+import { usePostHog } from "@/hooks/posthog";
import { useEffect } from "react";
export class ErrorBoundary extends React.Component<
diff --git a/packages/keychain/src/components/connect/create/CreateController.tsx b/packages/keychain/src/components/connect/create/CreateController.tsx
index aa7d2e6fc..f0cc0c4df 100644
--- a/packages/keychain/src/components/connect/create/CreateController.tsx
+++ b/packages/keychain/src/components/connect/create/CreateController.tsx
@@ -10,7 +10,7 @@ import { useCreateController } from "./useCreateController";
import { ErrorAlert } from "@/components/ErrorAlert";
import { VerifiableControllerTheme } from "@/context/theme";
import InAppSpy from "inapp-spy";
-import { usePostHog } from "@/context/posthog";
+import { usePostHog } from "@/hooks/posthog";
interface CreateControllerViewProps {
theme: VerifiableControllerTheme;
diff --git a/packages/keychain/src/components/home.tsx b/packages/keychain/src/components/home.tsx
index 91310482b..8851fdec0 100644
--- a/packages/keychain/src/components/home.tsx
+++ b/packages/keychain/src/components/home.tsx
@@ -13,7 +13,7 @@ import { Settings } from "./settings";
import { SignMessage } from "./SignMessage";
import { PageLoading } from "./Loading";
import { execute } from "@/utils/connection/execute";
-import { usePostHog } from "@/context/posthog";
+import { usePostHog } from "@/hooks/posthog";
export function Home() {
const { context, setContext, controller, error, policies, upgrade } =
diff --git a/packages/keychain/src/components/provider/index.tsx b/packages/keychain/src/components/provider/index.tsx
index 915c1d94f..14e28cde9 100644
--- a/packages/keychain/src/components/provider/index.tsx
+++ b/packages/keychain/src/components/provider/index.tsx
@@ -4,12 +4,12 @@ import { useConnectionValue } from "@/hooks/connection";
import { CartridgeAPIProvider } from "@cartridge/utils/api/cartridge";
import { ENDPOINT } from "@/utils/graphql";
import { ConnectionContext } from "./connection";
+import { PostHogProvider } from "./posthog";
import { ControllerThemeProvider } from "./theme";
import { jsonRpcProvider, StarknetConfig, voyager } from "@starknet-react/core";
import { sepolia, mainnet } from "@starknet-react/chains";
import { constants, num } from "starknet";
import { BrowserRouter } from "react-router-dom";
-import { PostHogProvider } from "@/context/posthog";
export function Provider({ children }: PropsWithChildren) {
const connection = useConnectionValue();
diff --git a/packages/keychain/src/components/provider/posthog.tsx b/packages/keychain/src/components/provider/posthog.tsx
new file mode 100644
index 000000000..22dec6c75
--- /dev/null
+++ b/packages/keychain/src/components/provider/posthog.tsx
@@ -0,0 +1,16 @@
+import { PropsWithChildren } from "react";
+import { PostHogContext, PostHogWrapper } from "@/context/posthog";
+
+export function PostHogProvider({ children }: PropsWithChildren) {
+ const posthog = new PostHogWrapper(import.meta.env.VITE_POSTHOG_KEY!, {
+ host: import.meta.env.VITE_POSTHOG_HOST,
+ persistence: "memory",
+ autocapture: false,
+ });
+
+ return (
+
+ {children}
+
+ );
+}
diff --git a/packages/keychain/src/context/posthog.tsx b/packages/keychain/src/context/posthog.tsx
index 9f73e20be..bc590bc87 100644
--- a/packages/keychain/src/context/posthog.tsx
+++ b/packages/keychain/src/context/posthog.tsx
@@ -1,10 +1,10 @@
-import { createContext, useContext, PropsWithChildren } from "react";
+import { createContext } from "react";
import PostHog from "posthog-js-lite";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Properties = Record;
-class PostHogWrapper extends PostHog {
+export class PostHogWrapper extends PostHog {
isLocal =
typeof window !== "undefined" &&
window.location.hostname.includes("localhost");
@@ -87,26 +87,6 @@ interface PostHogContextType {
posthog: PostHogWrapper;
}
-const PostHogContext = createContext(undefined);
-
-export function PostHogProvider({ children }: PropsWithChildren) {
- const posthog = new PostHogWrapper(import.meta.env.VITE_POSTHOG_KEY!, {
- host: import.meta.env.VITE_POSTHOG_HOST,
- persistence: "memory",
- autocapture: false,
- });
-
- return (
-
- {children}
-
- );
-}
-
-export function usePostHog() {
- const context = useContext(PostHogContext);
- if (!context) {
- return undefined;
- }
- return context.posthog;
-}
+export const PostHogContext = createContext(
+ undefined,
+);
diff --git a/packages/keychain/src/hooks/connection.ts b/packages/keychain/src/hooks/connection.ts
index 7c7151c09..8a59fc00b 100644
--- a/packages/keychain/src/hooks/connection.ts
+++ b/packages/keychain/src/hooks/connection.ts
@@ -18,7 +18,7 @@ import {
ConnectionContextValue,
} from "@/components/provider/connection";
import { UpgradeInterface, useUpgrade } from "./upgrade";
-import { usePostHog } from "@/context/posthog";
+import { usePostHog } from "@/hooks/posthog";
import { Policies } from "@cartridge/presets";
import {
defaultTheme,
diff --git a/packages/keychain/src/hooks/posthog.tsx b/packages/keychain/src/hooks/posthog.tsx
new file mode 100644
index 000000000..9b42db60f
--- /dev/null
+++ b/packages/keychain/src/hooks/posthog.tsx
@@ -0,0 +1,10 @@
+import { useContext } from "react";
+import { PostHogContext } from "@/context/posthog";
+
+export function usePostHog() {
+ const context = useContext(PostHogContext);
+ if (!context) {
+ return undefined;
+ }
+ return context.posthog;
+}