references/ruby-on-rails.md
Ruby on Rails error tracking installation - Docs
-
1
Install the gems
Required
Add the
posthog-rubyandposthog-railsgems to your Gemfile:Gemfile
PostHog AI
gem "posthog-ruby" gem "posthog-rails"Then run:
Terminal
PostHog AI
bundle install -
2
Generate the initializer
Required
Run the install generator to create the PostHog initializer:
Terminal
PostHog AI
rails generate posthog:installThis will create
config/initializers/posthog.rbwith sensible defaults and documentation. -
3
Configure PostHog
Required
Update
config/initializers/posthog.rbwith your project token and host:config/initializers/posthog.rb
PostHog AI
PostHog.init do |config| config.api_key = '<ph_project_token>' config.host = 'https://us.i.posthog.com' end -
4
Send events
Recommended
Once installed, you can manually send events to test your integration:
Ruby
PostHog AI
PostHog.capture({ distinct_id: 'user_123', event: 'button_clicked', properties: { button_name: 'signup' } }) -
5
Configure error tracking
Required
Update
config/initializers/posthog.rbto enable automatic exception capture:config/initializers/posthog.rb
PostHog AI
PostHog::Rails.configure do |config| config.auto_capture_exceptions = true config.report_rescued_exceptions = true config.auto_instrument_active_job = true config.capture_user_context = true config.current_user_method = :current_user end -
6
Automatic exception capture
Recommended
With
auto_capture_exceptionsenabled, exceptions are automatically captured from your controllers:app/controllers/posts_controller.rb
PostHog AI
class PostsController < ApplicationController def show @post = Post.find(params[:id]) # Any exception here is automatically captured end end -
7
Background jobs
Optional
When
auto_instrument_active_jobis enabled, ActiveJob exceptions are automatically captured:app/jobs/email_job.rb
PostHog AI
class EmailJob < ApplicationJob def perform(user_id) user = User.find(user_id) UserMailer.welcome(user).deliver_now # Exceptions are automatically captured with job context end end -
8
Manually capture exceptions
Optional
You can also manually capture exceptions that you handle in your application:
Ruby
PostHog AI
PostHog.capture_exception( exception, current_user.id, { custom_property: 'value' } ) -
Verify error tracking
Recommended
Confirm events are being sent to PostHog
Before proceeding, let's make sure exception events are being captured and sent to PostHog. You should see events appear in the activity feed.
Activity feed with eventsActivity feed with events
Community questions
Ask a question
Was this page useful?
HelpfulCould be better