Skip to content

Commit

Permalink
Analytics Tracking (nand2tetris#179)
Browse files Browse the repository at this point in the history
* Tracking acceptance banner.

* Tracking for some chips buttons.

Deploying and seeing what happens?

prettier scss
  • Loading branch information
DavidSouther authored Jan 1, 2023
1 parent b2bce38 commit 8171681
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 39 deletions.
65 changes: 40 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"license": "ISC",
"homepage": "https://nand2tetris.github.io/web-ide",
"devDependencies": {
"@nand2tetris/components": "*",
"@nand2tetris/projects": "*",
"@nand2tetris/simulator": "*",
"@davidsouther/jiffies": "^2.0.5",
"@lingui/cli": "^3.14.0",
"@lingui/macro": "^3.14.0",
"@lingui/react": "^3.14.0",
"@monaco-editor/react": "^4.4.5",
"@nand2tetris/components": "*",
"@nand2tetris/projects": "*",
"@nand2tetris/simulator": "*",
"@rollup/plugin-node-resolve": "^13.2.1",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
Expand Down Expand Up @@ -95,5 +95,8 @@
"transformIgnorePatterns": [
"node_modules/(?!@davidsouther)"
]
},
"dependencies": {
"react-ga4": "^1.4.1"
}
}
18 changes: 18 additions & 0 deletions web/src/App.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createContext, useCallback, useState } from "react";
import { FileSystem } from "@davidsouther/jiffies/lib/esm/fs.js";
import { useDialog } from "./shell/dialog";
import { useFilePicker } from "./shell/file_select";
import { useTracking } from "./tracking";

export type Theme = "light" | "dark" | "system";

Expand Down Expand Up @@ -33,6 +34,7 @@ export function useAppContext(fs: FileSystem = new FileSystem()) {
monaco: useMonaco(),
settings: useDialog(),
filePicker: useFilePicker(),
tracking: useTracking(),
theme,
setTheme,
};
Expand Down Expand Up @@ -67,6 +69,22 @@ export const AppContext = createContext<ReturnType<typeof useAppContext>>({
},
isOpen: false,
},
tracking: {
canTrack: false,
haveAsked: false,
accept() {
return undefined;
},
reject() {
return undefined;
},
trackEvent() {
return undefined;
},
trackPage() {
return undefined;
},
},
theme: "system",
setTheme() {
return undefined;
Expand Down
2 changes: 2 additions & 0 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { FilePicker } from "./shell/file_select";

import "./pico/flex.scss";
import "./pico/pico.scss";
import { TrackingBanner } from "./tracking";

i18n.load("en", messages);
i18n.load("en-PL", plMessages);
Expand Down Expand Up @@ -72,6 +73,7 @@ function App() {
</Suspense>
</main>
<Footer />
<TrackingBanner />
</Router>
</AppContext.Provider>
</BaseContext.Provider>
Expand Down
56 changes: 46 additions & 10 deletions web/src/pages/chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,48 @@ import { BaseContext } from "@nand2tetris/components/stores/base.context.js";

export const Chip = () => {
const { fs, setStatus } = useContext(BaseContext);
const { filePicker } = useContext(AppContext);
const { filePicker, tracking } = useContext(AppContext);
const { state, actions, dispatch } = useChipPageStore();

const [hdl, setHdl] = useStateInitializer(state.files.hdl);
const [tst, setTst] = useStateInitializer(state.files.tst);
const [cmp, setCmp] = useStateInitializer(state.files.cmp);
const [out] = useStateInitializer(state.files.out);

useEffect(() => {
tracking.trackPage("/chip");
}, [tracking]);

const saveChip = () => {
actions.saveChip(hdl);
};

useEffect(() => {
tracking.trackEvent("action", "setProject", state.controls.project);
tracking.trackEvent("action", "setChip", state.controls.chipName);
}, []);

const setProject = useCallback(
(project: keyof typeof CHIP_PROJECTS) => {
actions.setProject(project);
tracking.trackEvent("action", "setProject", project);
},
[actions, tracking]
);

const setChip = useCallback(
(chip: string) => {
actions.setChip(chip);
tracking.trackEvent("action", "setChip", chip);
},
[actions, tracking]
);

const doEval = useCallback(() => {
actions.eval();
tracking.trackEvent("action", "eval");
}, [actions, tracking]);

const compile = useRef<(files?: Partial<Files>) => void>(() => undefined);
compile.current = async (files: Partial<Files> = {}) => {
await actions.updateFiles({
Expand Down Expand Up @@ -99,8 +129,10 @@ export const Chip = () => {
() => ({
toggle() {
actions.clock();
tracking.trackEvent("action", "toggleClock");
},
reset() {
tracking.trackEvent("action", "resetClock");
actions.reset();
},
}),
Expand All @@ -124,7 +156,7 @@ export const Chip = () => {
<select
value={state.controls.project}
onChange={({ target: { value } }) => {
actions.setProject(value as keyof typeof CHIP_PROJECTS);
setProject(value as keyof typeof CHIP_PROJECTS);
}}
data-testid="project-picker"
>
Expand All @@ -137,7 +169,7 @@ export const Chip = () => {
<select
value={state.controls.chipName}
onChange={({ target: { value } }) => {
actions.setChip(value);
setChip(value);
}}
data-testid="chip-picker"
>
Expand Down Expand Up @@ -192,11 +224,7 @@ export const Chip = () => {

const chipButtons = (
<fieldset role="group">
<button
onClick={actions.eval}
onKeyDown={actions.eval}
disabled={!state.sim.pending}
>
<button onClick={doEval} onKeyDown={doEval} disabled={!state.sim.pending}>
<Trans>Eval</Trans>
</button>
<button
Expand Down Expand Up @@ -252,8 +280,16 @@ export const Chip = () => {
</Panel>
);

const [selectedTestTab, setSelectedTestTab] = useState<"tst" | "cmp" | "out">(
"tst"
const [selectedTestTab, doSetSelectedTestTab] = useState<
"tst" | "cmp" | "out"
>("tst");

const setSelectedTestTab = useCallback(
(tab: typeof selectedTestTab) => {
doSetSelectedTestTab(tab);
tracking.trackEvent("tab", "change", tab);
},
[tracking]
);

const testPanel = (
Expand Down
26 changes: 25 additions & 1 deletion web/src/shell/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { AppContext } from "../App.context";

import "../pico/button-group.scss";
import "../pico/property.scss";
import { TrackingDisclosure } from "src/tracking";

export const Settings = () => {
const { fs, setStatus } = useContext(BaseContext);
const { settings, monaco, theme, setTheme } = useContext(AppContext);
const { settings, monaco, theme, setTheme, tracking } =
useContext(AppContext);

const writeLocale = useMemo(
() => (locale: string) => {
Expand Down Expand Up @@ -169,6 +171,28 @@ export const Settings = () => {
</label>
</fieldset>
</dd>
<dt>
<Trans>Tracking</Trans>
</dt>
<dd>
<label>
<input
type="checkbox"
name="switch"
role="switch"
checked={tracking.canTrack}
onChange={(e) => {
if (e.target.checked) {
tracking.accept();
} else {
tracking.reject();
}
}}
/>
<Trans>Allow anonymous interaction tracking</Trans>
<TrackingDisclosure />
</label>
</dd>
</dl>
</main>
</article>
Expand Down
21 changes: 21 additions & 0 deletions web/src/tracking.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#trackingBanner {
background-color: ghostwhite;
border-top: 1px solid black;
position: absolute;
padding: var(--spacing);
bottom: 0;
left: 0;
right: 0;

[role="button"] {
&.accept {
color: black;
background-color: lightgreen;
}

&.reject {
color: black;
background-color: lightpink;
}
}
}
Loading

0 comments on commit 8171681

Please sign in to comment.