Skip to content

Commit

Permalink
feat: mixpanel experimentation (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaybuidl committed Oct 3, 2023
1 parent acb94e9 commit fa7c479
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 30 deletions.
4 changes: 4 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,23 @@
"@parcel/watcher": "~2.2.0",
"@types/amqplib": "^0.10.1",
"@types/busboy": "^1.5.0",
"@types/mixpanel-browser": "^2.47.3",
"@types/react": "^18.2.14",
"@types/react-dom": "^18.2.7",
"@types/styled-components": "^5.1.26",
"@typescript-eslint/eslint-plugin": "^5.58.0",
"@typescript-eslint/parser": "^5.61.0",
"@typescript-eslint/utils": "^5.58.0",
"@wagmi/cli": "^1.3.0",
"crypto-browserify": "^3.12.0",
"eslint": "^8.38.0",
"eslint-config-prettier": "^8.8.0",
"eslint-import-resolver-parcel": "^1.10.6",
"eslint-plugin-react": "^7.33.0",
"eslint-plugin-react-hooks": "^4.6.0",
"lru-cache": "^7.18.3",
"parcel": "2.8.3",
"string_decoder": "^1.3.0",
"typescript": "^4.9.5"
},
"dependencies": {
Expand All @@ -81,6 +84,7 @@
"ethers": "^5.7.2",
"graphql": "^16.7.1",
"graphql-request": "~6.1.0",
"mixpanel-browser": "^2.47.0",
"moment": "^2.29.4",
"overlayscrollbars": "^2.3.0",
"overlayscrollbars-react": "^0.5.2",
Expand Down
2 changes: 2 additions & 0 deletions web/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import Cases from "./pages/Cases";
import Dashboard from "./pages/Dashboard";
import Courts from "./pages/Courts";
import DisputeTemplateView from "./pages/DisputeTemplateView";
import mixpanel from "./utils/mixpanel";

const App: React.FC = () => {
mixpanel.track("App");
return (
<StyledComponentsProvider>
<QueryClientProvider>
Expand Down
54 changes: 33 additions & 21 deletions web/src/components/CasesDisplay/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import styled from "styled-components";
import Search from "./Search";
import StatsAndFilters from "./StatsAndFilters";
import CasesGrid, { ICasesGrid } from "./CasesGrid";
import useTracking from "../../hooks/useTracking";

const StyledHR = styled.hr`
margin-top: 24px;
Expand All @@ -22,26 +23,37 @@ const CasesDisplay: React.FC<ICasesDisplay> = ({
casesPerPage,
title = "Cases",
className,
}) => (
<div {...{ className }}>
<h1>{title}</h1>
<Search />
<StatsAndFilters />
<StyledHR />
{disputes.length > 0 ? (
<CasesGrid
{...{
disputes,
currentPage,
setCurrentPage,
numberDisputes,
casesPerPage,
}}
/>
) : (
<h1>wow no cases</h1>
)}
</div>
);
}) => {
useTracking("CasesDisplay", {
disputes,
currentPage,
setCurrentPage,
numberDisputes,
casesPerPage,
title,
className,
});
return (
<div {...{ className }}>
<h1>{title}</h1>
<Search />
<StatsAndFilters />
<StyledHR />
{disputes.length > 0 ? (
<CasesGrid
{...{
disputes,
currentPage,
setCurrentPage,
numberDisputes,
casesPerPage,
}}
/>
) : (
<h1>wow no cases</h1>
)}
</div>
);
};

export default CasesDisplay;
2 changes: 2 additions & 0 deletions web/src/components/ConnectWallet/AccountDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { landscapeStyle } from "styles/landscapeStyle";
import { useAccount, useNetwork, useEnsAvatar, useEnsName } from "wagmi";
import Identicon from "react-identicons";
import { shortenAddress } from "utils/shortenAddress";
import { useIdentify } from "../../hooks/useTracking";

const Container = styled.div`
display: flex;
Expand Down Expand Up @@ -119,6 +120,7 @@ export const AddressOrName: React.FC = () => {
address,
chainId: 1,
});
useIdentify(address);
return <label>{data ?? (address && shortenAddress(address))}</label>;
};

Expand Down
3 changes: 3 additions & 0 deletions web/src/components/ConnectWallet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { useAccount, useNetwork, useSwitchNetwork } from "wagmi";
import { useWeb3Modal } from "@web3modal/react";
import { Button } from "@kleros/ui-components-library";
import { SUPPORTED_CHAINS, DEFAULT_CHAIN } from "consts/chains";
import useTracking from "../../hooks/useTracking";
import AccountDisplay from "./AccountDisplay";

export const SwitchChainButton: React.FC = () => {
const { switchNetwork, isLoading } = useSwitchNetwork();
useTracking("Switch Network");
const handleSwitch = () => {
if (!switchNetwork) {
console.error("Cannot switch network. Please do it manually.");
Expand Down Expand Up @@ -36,6 +38,7 @@ const ConnectButton: React.FC = () => {
const ConnectWallet: React.FC = () => {
const { chain } = useNetwork();
const { isConnected } = useAccount();
useTracking("Connect Wallet", { chain });
if (isConnected) {
if (chain && chain.id !== DEFAULT_CHAIN) {
return <SwitchChainButton />;
Expand Down
23 changes: 23 additions & 0 deletions web/src/hooks/useTracking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// useTracking.ts
import { useEffect } from "react";
import mixpanel from "../utils/mixpanel";
import crypto from "crypto";

const useTracking = (eventName: string, props?: object) => {
useEffect(() => {
mixpanel.track(eventName, {
pathname: window.location.pathname,
...(props ?? {}),
});
}, [eventName, props]);
};

export const useIdentify = (userId: string | undefined, props?: object) => {
useEffect(() => {
if (userId) {
mixpanel.identify(crypto.createHash("sha256").update(userId).digest("hex"));
}
}, [userId, props]);
};

export default useTracking;
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { uploadFormDataToIPFS } from "utils/uploadFormDataToIPFS";
import { useWalletClient, usePublicClient } from "wagmi";
import { EnsureChain } from "components/EnsureChain";
import { prepareWriteDisputeKitClassic } from "hooks/contracts/generated";
import mixpanel from "../../../../utils/mixpanel";

const StyledModal = styled(Modal)`
position: absolute;
Expand Down Expand Up @@ -82,6 +83,11 @@ const SubmitEvidenceModal: React.FC<{
});
await wrapWithToast(async () => await walletClient.writeContract(request), publicClient).then(
() => {
mixpanel.track("submitEvidence", {
pathname: window.location.pathname,
cid,
evidenceGroup,
});
setMessage("");
close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useCourtDetails } from "hooks/queries/useCourtDetails";
import { wrapWithToast } from "utils/wrapWithToast";
import { isUndefined } from "utils/index";
import { EnsureChain } from "components/EnsureChain";
import mixpanel from "../../../../utils/mixpanel";

export enum ActionType {
allowance = "allowance",
Expand Down Expand Up @@ -87,6 +88,10 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
wrapWithToast(async () => await increaseAllowance().then((response) => response.hash), publicClient).finally(
() => {
setIsSending(false);
mixpanel.track("increaseAllowance", {
pathname: window.location.pathname,
amount: increaseAllowanceConfig.request.args[1].toString(),
});
}
);
}
Expand All @@ -104,6 +109,12 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
.then(() => setIsPopupOpen(true))
.finally(() => {
setIsSending(false);
mixpanel.track("setStake", {
pathname: window.location.pathname,
action: isStaking ? "stake" : "withdraw",
courtId: id,
stakeChange: (isStaking ? "" : "-") + parsedAmount.toString(),
});
});
}
};
Expand Down
9 changes: 9 additions & 0 deletions web/src/utils/mixpanel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import mixpanel from "mixpanel-browser";

mixpanel.init(process.env.REACT_APP_MIXPANEL_TOKEN!, {
debug: true,
track_pageview: true,
persistence: "localStorage",
});

export default mixpanel;
Loading

0 comments on commit fa7c479

Please sign in to comment.