Ruby error tracking installation

  1. Install PostHog Ruby SDK

    Required

    Add the posthog-ruby gem to your Gemfile:

    Terminal
    gem "posthog-ruby"

    In your app, import the posthog-ruby library and set your project API key and host. You can find these in the project settings page in PostHog.

    Ruby
    require 'posthog'
    posthog = PostHog::Client.new(
    api_key: '<ph_project_api_key>',
    host: 'https://us.i.posthog.com',
    )

    Note: We do not recommend hardcoding API keys. Setting it as an environment variable is preferred.

  2. Verify PostHog is initialized

    Checkpoint
    Confirm you can capture events with PostHog

    Before proceeding, call posthog.capture(...) to make sure you can capture events.

  3. 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:

    ParameterTypeDescription
    exceptionExceptionThe exception object to capture (required)
    distinct_idStringThe distinct ID of the user (optional)
    propertiesHashAdditional properties to attach to the exception event (optional)
  4. Verify error tracking

    Checkpoint
    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

Community questions

Was this page useful?

Questions about this page? or post a community question.