You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> **Tip:** You can define event schemas with typed properties and generate type-safe code using [schema management](/docs/product-analytics/schema-management).
By default, PostHog automatically captures pageviews and pageleaves as well as clicks, change of inputs, and form submissions associated with `a`, `button`, `form`, `input`, `select`, `textarea`, and `label` tags. See our [autocapture docs](/docs/product-analytics/autocapture) for more details on this.
Copy file name to clipboardExpand all lines: contents/docs/product-analytics/schema-management.mdx
+81-16Lines changed: 81 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ Schema management lets you define and enforce the structure of your events using
17
17
You can create event definitions before any events are actually captured. This is useful when planning your analytics implementation upfront, allowing you to:
18
18
19
19
- Define your event schema before writing instrumentation code
20
-
- Generate TypeScript types for events that haven't been captured yet
20
+
- Generate typed definitions for events that haven't been captured yet
21
21
- Document expected events for your team
22
22
- Ensure consistency across your codebase from day one
23
23
@@ -88,13 +88,7 @@ Events can have multiple property groups, and each group's properties will be in
88
88
89
89
## Downloading your schema with the CLI
90
90
91
-
The PostHog CLI generates TypeScript definitions from your configured schemas.
TypeScript is currently the only supported language for schema generation.
96
-
97
-
</CalloutBox>
91
+
The PostHog CLI generates language definitions from your configured schemas.
98
92
99
93
### Install the CLI
100
94
@@ -114,19 +108,21 @@ This opens your browser to authorize the CLI with your PostHog account.
114
108
115
109
### Download your schema
116
110
111
+
> **Note:** Schema management is currently an experimental feature. The `exp` prefix will be removed once the feature is stable.
112
+
117
113
```bash
118
114
posthog-cli exp schema pull
119
115
```
120
116
121
117
The CLI will:
122
118
1. Fetch your event schemas
123
-
2.Generate TypeScript definitions
124
-
3.Save them to a file (default: `posthog-typed.ts`)
119
+
2.Prompt you for the output file path
120
+
3.Generate typed definitions and save them to the specified file
125
121
4. Update `posthog.json` with the schema hash
126
122
127
-
When prompted for the output path, choose a location that's accessible from your application code (e.g., `src/lib/posthog-typed.ts` or `app/lib/posthog-typed.ts`).
123
+
When prompted for the output path, choose a location that's accessible from your application code (e.g., `src/lib/posthog-typed.ts` or `app/lib/posthog-typed.ts` for TypeScript).
128
124
129
-
**Important:** Commit both `posthog.json` and `posthog-typed.ts` to your git repository. This ensures your team has consistent types and tracks schema changes over time.
125
+
**Important:** Commit both `posthog.json` and `posthog-typed.<extension>` to your git repository. This ensures your team has consistent types and tracks schema changes over time.
130
126
131
127
On subsequent runs, the CLI only updates the file if your schema has changed.
132
128
@@ -142,7 +138,11 @@ This shows your current sync status, including the schema hash, last updated tim
142
138
143
139
Once you've downloaded your schema, import and use the generated PostHog client:
144
140
145
-
```typescript
141
+
142
+
<MultiLanguageselector="tabs">
143
+
144
+
```typescript file=TypeScript
145
+
146
146
importposthogfrom'./posthog-typed'
147
147
148
148
// Use typed events with autocomplete and type safety on known events:
-**Type safety**: Properties are validated against your schema
161
226
-**Autocomplete**: Your IDE suggests event names and properties
162
227
-**Documentation**: Inline types show required properties and descriptions
163
-
-**Flexibility**: `capture()`remains available for dynamic events
228
+
-**Flexibility**: The standard SDK functionality remains available, allowing gradual migrations
164
229
165
230

166
231
@@ -170,4 +235,4 @@ The generated client provides:
170
235
-**Start with your most important events**: Define schemas for critical conversion events first
171
236
-**Use descriptive property groups**: Name groups by their purpose (e.g., "E-commerce Properties", "User Context")
172
237
-**Don't over-schema**: Not every event needs a schema—use them where type safety adds value
173
-
-**Commit generated types**: Always commit `posthog-typed.ts` and `posthog.json` to version control so your team stays in sync
238
+
-**Commit generated types**: Always commit `posthog.json` and `posthog-typed.<extension>` to version control so your team stays in sync
0 commit comments