Skip to content

Commit 4df9181

Browse files
authored
feat(cli): add PostHog analytics, remove Vercel Analytics (#114)
* Add PostHog Node.js SDK documentation and initialize PostHog client in CLI utils - Created a new documentation file for the PostHog Node.js SDK detailing various methods and usage examples. - Implemented a utility file for PostHog integration in the CLI, including a function to get a distinct ID based on the hostname and initialized the PostHog client with configuration settings. * feat(analytics): add PostHog integration for tracking CLI command usage and errors * feat(analytics): implement shutdownPosthog function for graceful PostHog shutdown
1 parent 5944730 commit 4df9181

22 files changed

Lines changed: 2826 additions & 47 deletions

.changeset/rare-streets-report.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"pdfx-cli": patch
3+
---
4+
5+
Add PostHog analytics to track CLI command usage and errors
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
name: integration-javascript_node
3+
description: PostHog integration for server-side Node.js applications using posthog-node
4+
metadata:
5+
author: PostHog
6+
version: 1.9.5
7+
---
8+
9+
# PostHog integration for JavaScript Node
10+
11+
This skill helps you add PostHog analytics to JavaScript Node applications.
12+
13+
## Workflow
14+
15+
Follow these steps in order to complete the integration:
16+
17+
1. `basic-integration-1.0-begin.md` - PostHog Setup - Begin ← **Start here**
18+
2. `basic-integration-1.1-edit.md` - PostHog Setup - Edit
19+
3. `basic-integration-1.2-revise.md` - PostHog Setup - Revise
20+
4. `basic-integration-1.3-conclude.md` - PostHog Setup - Conclusion
21+
22+
## Reference files
23+
24+
- `references/node.md` - Node.js - docs
25+
- `references/posthog-node.md` - PostHog Node.js SDK
26+
- `references/identify-users.md` - Identify users - docs
27+
- `references/basic-integration-1.0-begin.md` - PostHog setup - begin
28+
- `references/basic-integration-1.1-edit.md` - PostHog setup - edit
29+
- `references/basic-integration-1.2-revise.md` - PostHog setup - revise
30+
- `references/basic-integration-1.3-conclude.md` - PostHog setup - conclusion
31+
32+
The example project shows the target implementation pattern. Consult the documentation for API details.
33+
34+
## Key principles
35+
36+
- **Environment variables**: Always use environment variables for PostHog keys. Never hardcode them.
37+
- **Minimal changes**: Add PostHog code alongside existing integrations. Don't replace or restructure existing code.
38+
- **Match the example**: Your implementation should follow the example project's patterns as closely as possible.
39+
40+
## Framework guidelines
41+
42+
- posthog-node is the Node.js server-side SDK package name – do NOT use posthog-js on the server
43+
- Include enableExceptionAutocapture: true in the PostHog constructor options
44+
- Add posthog.capture() calls in route handlers for meaningful user actions – every route that creates, updates, or deletes data should track an event with contextual properties
45+
- Add posthog.captureException(err, distinctId) in the application's error handler (e.g., Express error middleware, Fastify setErrorHandler, Koa app.on('error'))
46+
- In long-running servers, the SDK batches events automatically – do NOT set flushAt or flushInterval unless you have a specific reason to
47+
- For short-lived processes (scripts, CLIs, serverless), set flushAt to 1 and flushInterval to 0 to send events immediately
48+
- Reverse proxy is NOT needed for server-side Node.js – only client-side JavaScript needs a proxy to avoid ad blockers
49+
- Remember that source code is available in the node_modules directory
50+
- Check package.json for type checking or build scripts to validate changes
51+
52+
## Identifying users
53+
54+
Identify users during login and signup events. Refer to the example code and documentation for the correct identify pattern for this framework. If both frontend and backend code exist, pass the client-side session and distinct ID using `X-POSTHOG-DISTINCT-ID` and `X-POSTHOG-SESSION-ID` headers to maintain correlation.
55+
56+
## Error tracking
57+
58+
Add PostHog error tracking to relevant files, particularly around critical user flows and API boundaries.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: PostHog Setup - Begin
3+
description: Start the event tracking setup process by analyzing the project and creating an event tracking plan
4+
---
5+
6+
We're making an event tracking plan for this project.
7+
8+
Before proceeding, find any existing `posthog.capture()` code. Make note of event name formatting.
9+
10+
From the project's file list, select between 10 and 15 files that might have interesting business value for event tracking, especially conversion and churn events. Also look for additional files related to login that could be used for identifying users, along with error handling. Read the files. If a file is already well-covered by PostHog events, replace it with another option. Do not spawn subagents.
11+
12+
Look for opportunities to track client-side events.
13+
14+
**IMPORTANT: Server-side events are REQUIRED** if the project includes any instrumentable server-side code. If the project has API routes (e.g., `app/api/**/route.ts`) or Server Actions, you MUST include server-side events for critical business operations like:
15+
16+
- Payment/checkout completion
17+
- Webhook handlers
18+
- Authentication endpoints
19+
20+
Do not skip server-side events - they capture actions that cannot be tracked client-side.
21+
22+
Create a new file with a JSON array at the root of the project: .posthog-events.json. It should include one object for each event we want to add: event name, event description, and the file path we want to place the event in. If events already exist, don't duplicate them; supplement them.
23+
24+
Track actions only, not pageviews. These can be captured automatically. Exceptions can be made for "viewed"-type events that correspond to the top of a conversion funnel.
25+
26+
As you review files, make an internal note of opportunities to identify users and catch errors. We'll need them for the next step.
27+
28+
## Status
29+
30+
Before beginning a phase of the setup, you will send a status message with the exact prefix '[STATUS]', as in:
31+
32+
[STATUS] Checking project structure.
33+
34+
Status to report in this phase:
35+
36+
- Checking project structure
37+
- Verifying PostHog dependencies
38+
- Generating events based on project
39+
40+
41+
---
42+
43+
**Upon completion, continue with:** [basic-integration-1.1-edit.md](basic-integration-1.1-edit.md)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: PostHog Setup - Edit
3+
description: Implement PostHog event tracking in the identified files, following best practices and the example project
4+
---
5+
6+
For each of the files and events noted in .posthog-events.json, make edits to capture events using PostHog. Make sure to set up any helper files needed. Carefully examine the included example project code: your implementation should match it as closely as possible. Do not spawn subagents.
7+
8+
Use environment variables for PostHog keys. Do not hardcode PostHog keys.
9+
10+
If a file already has existing integration code for other tools or services, don't overwrite or remove that code. Place PostHog code below it.
11+
12+
For each event, add useful properties, and use your access to the PostHog source code to ensure correctness. You also have access to documentation about creating new events with PostHog. Consider this documentation carefully and follow it closely before adding events. Your integration should be based on documented best practices. Carefully consider how the user project's framework version may impact the correct PostHog integration approach.
13+
14+
Remember that you can find the source code for any dependency in the node_modules directory. This may be necessary to properly populate property names. There are also example project code files available via the PostHog MCP; use these for reference.
15+
16+
Where possible, add calls for PostHog's identify() function on the client side upon events like logins and signups. Use the contents of login and signup forms to identify users on submit. If there is server-side code, pass the client-side session and distinct ID to the server-side code to identify the user. On the server side, make sure events have a matching distinct ID where relevant.
17+
18+
It's essential to do this in both client code and server code, so that user behavior from both domains is easy to correlate.
19+
20+
You should also add PostHog exception capture error tracking to these files where relevant.
21+
22+
Remember: Do not alter the fundamental architecture of existing files. Make your additions minimal and targeted.
23+
24+
Remember the documentation and example project resources you were provided at the beginning. Read them now.
25+
26+
## Status
27+
28+
Status to report in this phase:
29+
30+
- Inserting PostHog capture code
31+
- A status message for each file whose edits you are planning, including a high level summary of changes
32+
- A status message for each file you have edited
33+
34+
35+
---
36+
37+
**Upon completion, continue with:** [basic-integration-1.2-revise.md](basic-integration-1.2-revise.md)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: PostHog Setup - Revise
3+
description: Review and fix any errors in the PostHog integration implementation
4+
---
5+
6+
Check the project for errors. Read the package.json file for any type checking or build scripts that may provide input about what to fix. Remember that you can find the source code for any dependency in the node_modules directory. Do not spawn subagents.
7+
8+
Ensure that any components created were actually used.
9+
10+
Once all other tasks are complete, run any linter or prettier-like scripts found in the package.json, but ONLY on the files you have edited or created during this session. Do not run formatting or linting across the entire project's codebase.
11+
12+
## Status
13+
14+
Status to report in this phase:
15+
16+
- Finding and correcting errors
17+
- Report details of any errors you fix
18+
- Linting, building and prettying
19+
20+
---
21+
22+
**Upon completion, continue with:** [basic-integration-1.3-conclude.md](basic-integration-1.3-conclude.md)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: PostHog Setup - Conclusion
3+
description: Review and fix any errors in the PostHog integration implementation
4+
---
5+
6+
Use the PostHog MCP to create a new dashboard named "Analytics basics" based on the events created here. Make sure to use the exact same event names as implemented in the code. Populate it with up to five insights, with special emphasis on things like conversion funnels, churn events, and other business critical insights.
7+
8+
Search for a file called `.posthog-events.json` and read it for available events. Do not spawn subagents.
9+
10+
Create the file posthog-setup-report.md. It should include a summary of the integration edits, a table with the event names, event descriptions, and files where events were added, along with a list of links for the dashboard and insights created. Follow this format:
11+
12+
<wizard-report>
13+
# PostHog post-wizard report
14+
15+
The wizard has completed a deep integration of your project. [Detailed summary of changes]
16+
17+
[table of events/descriptions/files]
18+
19+
## Next steps
20+
21+
We've built some insights and a dashboard for you to keep an eye on user behavior, based on the events we just instrumented:
22+
23+
[links]
24+
25+
### Agent skill
26+
27+
We've left an agent skill folder in your project. You can use this context for further agent development when using Claude Code. This will help ensure the model provides the most up-to-date approaches for integrating PostHog.
28+
29+
</wizard-report>
30+
31+
Upon completion, remove .posthog-events.json.
32+
33+
## Status
34+
35+
Status to report in this phase:
36+
37+
- Configured dashboard: [insert PostHog dashboard URL]
38+
- Created setup report: [insert full local file path]

0 commit comments

Comments
 (0)