Skip to content

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

added posthog for analytics

Type of Change

  • New feature

Testing

Tested manually by omitting events.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link

vercel bot commented Oct 2, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
sim Ready Ready Preview Comment Oct 2, 2025 3:13am
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
docs Skipped Skipped Oct 2, 2025 3:13am

Copy link
Contributor

@greptile-apps greptile-apps bot left a 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.ts where NEXT_PUBLIC_POSTHOG_HOST is 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
Loading

5 files reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

@vercel vercel bot temporarily deployed to Preview – docs October 2, 2025 03:09 Inactive
@waleedlatif1 waleedlatif1 merged commit 7aae108 into staging Oct 2, 2025
3 of 5 checks passed
@waleedlatif1 waleedlatif1 deleted the sim-150 branch October 2, 2025 03:12
Copy link
Collaborator Author

waleedlatif1 commented Oct 2, 2025

1 Job Failed:

CI / Test and Build / Test and Build failed on "Run tests with coverage"
[...]
   Start at  03:12:20
   Duration  11.89s (transform 6.76s, setup 5.08s, collect 26.14s, tests 21.52s, environment 1.68s, prepare 6.46s)




 ❯ lib/email/validation.test.ts:6:8
If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout".
Error: Error: Test timed out in 5000ms.
error: script "test" exited with code 1
[ERROR] command finished with error: command (/home/runner/_work/sim/sim/apps/sim) /home/runner/.bun/bin/bun run test exited (1)
 ERROR  run failed: command  exited (1)
error: script "test" exited with code 1

 Tasks:    1 successful, 2 total
Cached:    0 cached, 2 total
  Time:    12.5s 
Failed:    sim#test

Error: Process completed with exit code 1.

Summary: 1 failed workflow
  • CI (1 job failed)

Last updated: 2025-10-02 03:13:10 UTC

waleedlatif1 added a commit that referenced this pull request Oct 2, 2025
* feat(posthog): added posthog for analytics

* added envvars to env.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants