Rails error tracking installation
Contents
- 1
Install PostHog Rails gem
RequiredAdd both gems to your Gemfile:
GemfileThen run:
Terminal - 2
Generate the initializer
RequiredRun the install generator to create the PostHog initializer:
TerminalThis creates
config/initializers/posthog.rbwith sensible defaults. - 3
Configure PostHog
RequiredEdit the generated initializer at
config/initializers/posthog.rb:config/initializers/posthog.rbYou can find your project API key and instance address in your project settings.
Note: We recommend using environment variables for API keys rather than hardcoding them.
Verify PostHog is initialized
CheckpointConfirm PostHog is correctly set upIn a Rails console, verify that PostHog is initialized:
Ruby- 4
Enable automatic exception capture
RecommendedYour goal in this step: Enable automatic exception tracking for your Rails application.Automatic exception capture is enabled by setting
auto_capture_exceptions = truein your configuration. When enabled, exceptions are automatically captured:RubyUnderstanding exception tracking options
auto_capture_exceptions- Master switch for all automatic error tracking (default:false)- When
true: All exceptions are automatically captured and sent to PostHog - When
false(default): No automatic error tracking (you must manually callPostHog.capture_exception)
report_rescued_exceptions- Control exceptions that Rails handles gracefully (default:false)- When
true: Capture exceptions that Rails rescues and shows error pages for (404s, 500s, etc.) - When
false(default): Only capture truly unhandled exceptions that crash your app
Recommendation: Enable both options to get complete visibility into all errors.
- When
- 5
Enable ActiveJob exception tracking
OptionalYour goal in this step: Capture exceptions from background jobs.When
auto_instrument_active_jobis enabled, ActiveJob exceptions are automatically captured with job context:RubyAssociating jobs with users
By default, PostHog extracts a
distinct_idfrom job arguments by looking for auser_idkey:RubyFor more control, use the
posthog_distinct_idclass method:RubyNote: Currently only ActiveJob is supported. Support for other job runners (Sidekiq, Resque, Good Job, etc.) is planned for future releases.
- 6
Configure user context
OptionalYour goal in this step: Associate exceptions with users.PostHog Rails automatically captures user information from your controllers. By default, it uses the
current_usermethod.If your user method has a different name:
RubyPostHog auto-detects the user's distinct ID by trying these methods on your user object:
posthog_distinct_id– Define this on your User model for full controldistinct_id– Common analytics conventionid– Standard ActiveRecord primary key
You can configure a specific method:
RubyOr define a method on your User model:
Ruby - 7
Rails 7.0+ error reporter integration
OptionalYour goal in this step: Use Rails' built-in error reporting.PostHog integrates with Rails' built-in error reporting (Rails 7.0+):
RubyPostHog automatically extracts the user's distinct ID from
user_idordistinct_idin the context hash. - 8
Manually capture exceptions
OptionalYour goal in this step: Capture handled exceptions.For exceptions you handle but still want to track:
Ruby - 10
Configure excluded exceptions
OptionalThe following exceptions are not reported by default:
AbstractController::ActionNotFoundActionController::BadRequestActionController::InvalidAuthenticityTokenActionController::RoutingErrorActionController::UnknownFormatActiveRecord::RecordNotFound
Add more with:
Ruby
Configuration reference
| Option | Type | Default | Description |
|---|---|---|---|
auto_capture_exceptions | Boolean | false | Automatically capture exceptions |
report_rescued_exceptions | Boolean | false | Report exceptions Rails rescues |
auto_instrument_active_job | Boolean | false | Instrument ActiveJob |
capture_user_context | Boolean | true | Include user info |
current_user_method | Symbol | :current_user | Controller method for user |
user_id_method | Symbol | nil | Method to extract ID from user object |
excluded_exceptions | Array | [] | Additional exceptions to ignore |
Troubleshooting
Exceptions not being captured
Verify PostHog is initialized:
RubyCheck that
auto_capture_exceptionsis set totrueCheck your excluded exceptions list
Verify middleware is installed:
Ruby
User context not working
- Verify
current_user_methodmatches your controller method - Check that the user object responds to
posthog_distinct_id,distinct_id, orid - If using a custom identifier, set
PostHog::Rails.config.user_id_method = :your_method

