diff --git a/app/src/context/analytics/AnalyticsContext.tsx b/app/src/context/analytics/AnalyticsContext.tsx new file mode 100644 index 0000000..a789231 --- /dev/null +++ b/app/src/context/analytics/AnalyticsContext.tsx @@ -0,0 +1,5 @@ +import { createContext } from "react"; + +import { AnalyticsContextType } from "./AnalyticsContext.types"; + +export const AnalyticsContext = createContext(undefined); diff --git a/app/src/context/analytics/AnalyticsContext.types.ts b/app/src/context/analytics/AnalyticsContext.types.ts new file mode 100644 index 0000000..8043088 --- /dev/null +++ b/app/src/context/analytics/AnalyticsContext.types.ts @@ -0,0 +1,14 @@ +import { ReactNode } from "react"; + +export type AnalyticsContextControllerProps = { + children: ReactNode; +}; + +export type AnalyticsEvent = { + name: string; + meta?: Record; +}; + +export type AnalyticsContextType = { + onClick: (event: AnalyticsEvent) => void; +}; diff --git a/app/src/context/analytics/AnalyticsContextController.tsx b/app/src/context/analytics/AnalyticsContextController.tsx new file mode 100644 index 0000000..ffdd6a3 --- /dev/null +++ b/app/src/context/analytics/AnalyticsContextController.tsx @@ -0,0 +1,26 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import React from "react"; + +import { AnalyticsContext } from "./AnalyticsContext"; +import { AnalyticsContextControllerProps, AnalyticsContextType, AnalyticsEvent } from "./AnalyticsContext.types"; + +const onClick = (event: AnalyticsEvent) => { + (window as any) + .pirsch(event.name, { + meta: event.meta, + }) + .then(() => { + console.log(`AnalyticsContextController.onClick: ${event.name}, ${JSON.stringify(event.meta)}`); + }) + .catch((error: Error) => { + console.error(error); + }); +}; + +export const AnalyticsContextController = ({ children }: AnalyticsContextControllerProps) => { + const props: AnalyticsContextType = { + onClick, + }; + + return {children}; +}; diff --git a/app/src/context/analytics/useAnalyticsContext.tsx b/app/src/context/analytics/useAnalyticsContext.tsx new file mode 100644 index 0000000..ef28871 --- /dev/null +++ b/app/src/context/analytics/useAnalyticsContext.tsx @@ -0,0 +1,13 @@ +import { useContext } from "react"; + +import { AnalyticsContext } from "./AnalyticsContext"; + +export const useAnalyticsContext = () => { + const context = useContext(AnalyticsContext); + + if (context === undefined) { + throw new Error("useAnalyticsContext must be used within a AnalyticsContext"); + } + + return context; +}; diff --git a/app/src/layouts/home-layout/HomeLayout.tsx b/app/src/layouts/home-layout/HomeLayout.tsx index efb1a2c..4293351 100644 --- a/app/src/layouts/home-layout/HomeLayout.tsx +++ b/app/src/layouts/home-layout/HomeLayout.tsx @@ -11,6 +11,7 @@ import { EvmWalletSelectorContextController } from "context/evm/wallet-selector/ import { Footer } from "ui/footer/Footer"; import { AuthorizationContextController } from "context/authorization/AuthorizationContextController"; import { DiscordContextController } from "context/discord/DiscordContextController"; +import { AnalyticsContextController } from "context/analytics/AnalyticsContextController"; import { ChatLayoutProps } from "./HomeLayout.types"; import styles from "./HomeLayout.module.scss"; @@ -44,15 +45,17 @@ export const HomeLayout: React.FC = ({ children }) => { -