|
| 1 | +# Analytics Tracking Helpers |
| 2 | + |
| 3 | +This folder contains **type-safe wrappers** around our analytics provider (PostHog). |
| 4 | +Instead of calling `posthog.capture` directly, feature code should import the |
| 5 | +pre-defined helpers from here. This guarantees that: |
| 6 | + |
| 7 | +1. Event names are consistent. |
| 8 | +2. Payloads adhere to a strict schema (validated at runtime via Zod and typed at |
| 9 | + compile time). |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Quick start |
| 14 | + |
| 15 | +```ts |
| 16 | +import { reportContractDeployed } from "@/analytics/track"; |
| 17 | + |
| 18 | +// Contract deployment example |
| 19 | +reportContractDeployed({ |
| 20 | + address: "0x…", |
| 21 | + chainId: 1, |
| 22 | +}); |
| 23 | +``` |
| 24 | + |
| 25 | +> **Note** Ensure that PostHog is initialised *before* you emit events. Our |
| 26 | +> bootstrapping code does this automatically during app start-up. |
| 27 | +
|
| 28 | +--- |
| 29 | + |
| 30 | +## Project structure |
| 31 | + |
| 32 | +``` |
| 33 | +track/ # ← you are here |
| 34 | +├─ __internal.ts # low-level wrapper around posthog.capture (do NOT use) |
| 35 | +├─ contract.ts # "contract" event category helpers |
| 36 | +├─ README.md # this file |
| 37 | +└─ … # future categories live here |
| 38 | +``` |
| 39 | + |
| 40 | +* `__internal.ts` exposes `__internal__reportEvent` which performs the actual |
| 41 | + PostHog call and safeguards against the SDK not being ready. |
| 42 | +* Every **category file** (such as `contract.ts`) groups together related events. |
| 43 | + Each event is represented by a `report<Something>` function. |
| 44 | +* `track.ts` sits one directory up and **re-exports all helper functions** so |
| 45 | + consumers can import the ones they need directly: |
| 46 | + |
| 47 | + ```ts |
| 48 | + import { reportContractDeployed /*, reportOtherEvent */ } from "@/analytics/track"; |
| 49 | + ``` |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## Adding a **new event** to an existing category |
| 54 | + |
| 55 | +1. Open the relevant category file (e.g. `contract.ts`). |
| 56 | +2. Define a new Zod schema describing the payload: |
| 57 | + |
| 58 | +```ts |
| 59 | +const ContractUpgradeSchema = z.object({ |
| 60 | + address: z.string(), |
| 61 | + oldVersion: z.string(), |
| 62 | + newVersion: z.string(), |
| 63 | +}); |
| 64 | +``` |
| 65 | + |
| 66 | +3. Export a reporting helper that forwards the validated payload: |
| 67 | + |
| 68 | +```ts |
| 69 | +export function reportContractUpgraded( |
| 70 | + payload: z.infer<typeof ContractUpgradeSchema>, |
| 71 | +) { |
| 72 | + __internal__reportEvent("contract upgraded", payload); |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +That's it – consumers can now call `track.contract.reportContractUpgraded(...)`. |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## Adding a **new category** |
| 81 | + |
| 82 | +1. Create a new file `track/<category>.ts`. |
| 83 | +2. Follow the same pattern as in `contract.ts` (define schemas + export helper |
| 84 | + functions). |
| 85 | +3. Open `track.ts` (one directory up) and add a star-export so the helpers are |
| 86 | + surfaced at the package root: |
| 87 | + |
| 88 | + ```ts |
| 89 | + export * from "./track/<category>"; |
| 90 | + ``` |
| 91 | + |
| 92 | + Your new helpers can now be imported directly: |
| 93 | + |
| 94 | + ```ts |
| 95 | + import { reportMyNewEvent } from "@/analytics/track"; |
| 96 | + ``` |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## Conventions & best practices |
| 101 | + |
| 102 | +* **Lowercase, space-separated event names** – keep them human-readable. |
| 103 | +* **Small, focused payloads** – only include properties that are useful for |
| 104 | + analytics. |
| 105 | +* **Avoid calling PostHog directly** – always go through `__internal__reportEvent` |
| 106 | + so we keep a single choke-point. |
0 commit comments