-
Notifications
You must be signed in to change notification settings - Fork 3.3k
feat(posthog): added posthog for analytics #1523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Greptile Overview
Summary
This PR adds PostHog analytics integration to the Sim platform, implementing comprehensive user behavior tracking and session analytics. The implementation includes both client-side and server-side PostHog SDKs with proper environment variable configuration, URL rewriting to bypass ad blockers, and session-based user identification.The changes span multiple key areas: Next.js configuration adds URL rewrites that proxy PostHog requests through /ingest/* paths to avoid ad-blocker interference, environment variables are added for both server-side (POSTHOG_ENABLED) and client-side (NEXT_PUBLIC_POSTHOG_*) configuration, and the analytics system integrates with the existing session management to automatically identify users upon login and reset tracking on logout.
The PostHog configuration enables advanced features including session recording with privacy controls (masking passwords but preserving emails), performance monitoring, heatmaps, autocapture for UI interactions, and dead click detection. The implementation follows a feature flag pattern allowing the analytics to be toggled on/off across different environments, maintaining the existing custom telemetry system alongside PostHog's capabilities.
Changed Files
| Filename | Score | Overview |
|---|---|---|
apps/sim/next.config.ts |
5/5 | Added PostHog URL rewrites to proxy analytics requests through application domain |
apps/sim/package.json |
5/5 | Added posthog-js and posthog-node dependencies for client and server-side analytics |
apps/sim/lib/env.ts |
5/5 | Added environment variables for PostHog configuration with proper validation |
apps/sim/instrumentation-client.ts |
3/5 | Integrated PostHog client initialization with comprehensive tracking features |
apps/sim/lib/session/session-context.tsx |
4/5 | Added user identification and session reset logic for PostHog analytics |
Confidence score: 4/5
- This PR is generally safe to merge with some minor configuration inconsistencies that should be addressed
- Score reflects solid implementation of analytics integration with proper environment controls, but points are deducted for a potential configuration mismatch in the client instrumentation
- Pay close attention to
apps/sim/instrumentation-client.tswhereNEXT_PUBLIC_POSTHOG_HOSTis checked but not used in initialization
Sequence Diagram
sequenceDiagram
participant User
participant Browser
participant PostHog
participant SessionProvider
participant TelemetryAPI as "/api/telemetry"
participant PerformanceObserver
User->>Browser: "Visits application"
Browser->>Browser: "Load instrumentation-client.ts"
alt PostHog enabled
Browser->>PostHog: "posthog.init() with config"
Note over PostHog: Initialize with session recording, heatmaps, autocapture
end
Browser->>Browser: "Check telemetry preferences in localStorage"
alt Telemetry enabled
Browser->>Browser: "Set telemetryEnabled = true"
Browser->>Browser: "Define __SIM_TRACK_EVENT on window"
Browser->>PerformanceObserver: "Setup LCP, CLS, FID observers"
Browser->>Browser: "Add error event listener"
else Telemetry disabled
Browser->>Browser: "Set telemetryEnabled = false"
end
User->>SessionProvider: "Session loads"
SessionProvider->>SessionProvider: "loadSession() called"
alt User authenticated
SessionProvider->>PostHog: "posthog.identify() with user data"
else User not authenticated
SessionProvider->>PostHog: "posthog.reset()"
end
User->>Browser: "Performs actions triggering events"
Browser->>Browser: "Call __SIM_TRACK_EVENT()"
alt Telemetry enabled
Browser->>TelemetryAPI: "POST event data"
end
PerformanceObserver->>Browser: "Observes performance metrics"
alt Telemetry enabled
Browser->>TelemetryAPI: "POST performance data"
end
Browser->>Browser: "JavaScript error occurs"
alt Telemetry enabled
Browser->>TelemetryAPI: "POST error details"
end
5 files reviewed, 3 comments
|
1 Job Failed: CI / Test and Build / Test and Build failed on "Run tests with coverage" |
* feat(posthog): added posthog for analytics * added envvars to env.ts
Summary
added posthog for analytics
Type of Change
Testing
Tested manually by omitting events.
Checklist