Ruby error tracking installation

  1. Install the gem

    Required

    Add the PostHog Ruby gem to your Gemfile:

    Gemfile
    gem "posthog-ruby"
  2. Configure PostHog

    Required

    Initialize the PostHog client with your API key and host:

    Ruby
    require 'posthog'
    posthog = PostHog::Client.new({
    api_key: "<ph_project_api_key>",
    host: "https://us.i.posthog.com",
    on_error: Proc.new { |status, msg| print msg }
    })
  3. Send events

    Recommended

    Once installed, you can manually send events to test your integration:

    Ruby
    posthog.capture({
    distinct_id: 'user_123',
    event: 'button_clicked',
    properties: {
    button_name: 'signup'
    }
    })
  4. Manually capture exceptions

    Required

    Note: The Ruby SDK currently only supports manual exception capture. Automatic exception capture is not yet available.

    To capture exceptions in your Ruby application, use the capture_exception method:

    Ruby
    begin
    # Code that might raise an exception
    raise StandardError, "Something went wrong"
    rescue => e
    posthog.capture_exception(
    e,
    distinct_id: 'user_distinct_id',
    properties: {
    custom_property: 'custom_value'
    }
    )
    end

    The capture_exception method accepts the following parameters:

    ParamTypeDescription
    exceptionExceptionThe exception object to capture (required)
    distinct_idStringThe distinct ID of the user (optional)
    propertiesHashAdditional properties to attach to the exception event (optional)
  5. 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 events
    Check for exceptions in PostHog
  6. Upload source maps

    Required

    Great, you're capturing exceptions! If you serve minified bundles, the next step is to upload source maps to generate accurate stack traces.

    Let's continue to the next section.

    Upload source maps

Community questions

Was this page useful?

Questions about this page? or post a community question.