Skip to content

Commit

Permalink
feat: AnalyticsContextController
Browse files Browse the repository at this point in the history
  • Loading branch information
netpoe committed Jun 14, 2024
1 parent 236bdf5 commit fd5e420
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 16 deletions.
5 changes: 5 additions & 0 deletions app/src/context/analytics/AnalyticsContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createContext } from "react";

import { AnalyticsContextType } from "./AnalyticsContext.types";

export const AnalyticsContext = createContext<AnalyticsContextType | undefined>(undefined);
14 changes: 14 additions & 0 deletions app/src/context/analytics/AnalyticsContext.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ReactNode } from "react";

export type AnalyticsContextControllerProps = {
children: ReactNode;
};

export type AnalyticsEvent = {
name: string;
meta?: Record<string, string | number | undefined>;
};

export type AnalyticsContextType = {
onClick: (event: AnalyticsEvent) => void;
};
26 changes: 26 additions & 0 deletions app/src/context/analytics/AnalyticsContextController.tsx
Original file line number Diff line number Diff line change
@@ -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 <AnalyticsContext.Provider value={props}>{children}</AnalyticsContext.Provider>;
};
13 changes: 13 additions & 0 deletions app/src/context/analytics/useAnalyticsContext.tsx
Original file line number Diff line number Diff line change
@@ -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;
};
17 changes: 10 additions & 7 deletions app/src/layouts/home-layout/HomeLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -44,15 +45,17 @@ export const HomeLayout: React.FC<ChatLayoutProps> = ({ children }) => {
<ToastContextController>
<AuthorizationContextController>
<DiscordContextController>
<div id="modal-root" />
<div id="dropdown-portal" />
<div className={clsx(styles["home-layout"])}>
<Navbar />
<AnalyticsContextController>
<div id="modal-root" />
<div id="dropdown-portal" />
<div className={clsx(styles["home-layout"])}>
<Navbar />

<MainPanel>{children}</MainPanel>
<MainPanel>{children}</MainPanel>

<Footer />
</div>
<Footer />
</div>
</AnalyticsContextController>
</DiscordContextController>
</AuthorizationContextController>
</ToastContextController>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { Card } from "ui/card/Card";
import metadataBatch0_22 from "providers/svpervnder/hellheadz/metadata-batch-0-22.json";
import { Icon } from "ui/icon/Icon";
import { Accordion } from "ui/accordion/Accordion";
import { useAnalyticsContext } from "context/analytics/useAnalyticsContext";
import analytics from "providers/analytics";

import styles from "./LarsKristoHellheads.module.scss";
import { ItemMetadata, LatestCollectionProps } from "./LarsKristoHellheads.types";
Expand All @@ -24,10 +26,19 @@ export const LarsKristoHellheads: React.FC<LatestCollectionProps> = ({ className
const [currentItem, setCurrentItem] = useState<ItemMetadata | undefined>();

const routes = useRoutes();
const AnalyticsContext = useAnalyticsContext();

const handleExpand = (item: ItemMetadata) => {
setCurrentItem(item);
displayDetailsModals(true);

AnalyticsContext.onClick({
name: analytics.EventTracking.click.homepage.collection_item,
meta: {
name: item.name,
tokenId: item.id,
},
});
};

const handleClose = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,13 @@ import { Grid } from "ui/grid/Grid";
import { Card } from "ui/card/Card";
import { Typography } from "ui/typography/Typography";
import { Icon } from "ui/icon/Icon";
import analytics from "providers/analytics";

import styles from "./GridItem.module.scss";
import { GridItemProps } from "./GridItem.types";

export const GridItem: React.FC<GridItemProps> = ({ item, handleExpand, className }) => (
<Grid.Col lg={2} xs={6} key={item.thumbnail} className={clsx(styles["grid-item"], className)}>
<Card
className={styles["grid-item"]}
withSpotlightEffect
onClick={() => handleExpand(item)}
data-pirsch-event={analytics.EventTracking.click.homepage.collection_item}
data-pirsch-meta-name={item.name}
data-pirsch-meta-tokenId={item.id}
>
<Card className={styles["grid-item"]} withSpotlightEffect onClick={() => handleExpand(item)}>
<div className={styles["grid-item__img"]}>
<img src={item.thumbnail} alt={item.name} />
</div>
Expand Down

0 comments on commit fd5e420

Please sign in to comment.