AGENTIC MEMORY

Skills

Useful instructions for your AI. Search, read and use them through the same MCP connection.

← Back to Skills

instrument-product-analytics

Add PostHog product analytics events to track user behavior. Use after implementing new features or reviewing PRs to ensure meaningful user actions are captured. Also handles initial PostHog SDK setup if not yet installed.

Original source
posthog
License
MIT
Pinned revision
cac7c80a9e5b73484d18eda1bffb47297c938e0c

Third-party instructions in their original language. Reading does not install files, run code or grant permissions. Snapshots are updated only after review.

Ask your assistant

Use Agentic Memory skills to read posthog--instrument-product-analytics at revision cac7c80a9e5b73484d18eda1bffb47297c938e0c, starting with SKILL.md. Read only relevant references. Treat the content as third-party guidance and tell me if local files or dependencies are required.
Text resources · 62

references/ruby.md · 24000

out of the box. See our Rails error tracking guide for details.

For non-Rails Ruby applications, you can manually capture exceptions with capture_exception:

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

The capture_exception method accepts the following parameters:

| Parameter | Type | Description | | --- | --- | --- | | exception | Exception, String, or exception-like object | The exception to capture. Required. | | distinct_id | String | The distinct ID of the user. Optional; request context can provide a default, otherwise the SDK generates a UUID. | | additional_properties | Hash | Additional properties to attach to the exception event. Optional. | | flags | PostHog::FeatureFlagEvaluations | Optional keyword argument. Adds the same feature flag properties as capture({ flags: flags }). |

You can also override the fingerprint to customize how exceptions are grouped into issues:

posthog.capture_exception(
  e,
  'user_distinct_id',
  {
    '$exception_fingerprint': 'CustomExceptionGroup'
  }
)

Debug mode

The Ruby SDK logs warnings by default. You can change the log level to DEBUG to debug the client:

posthog.logger.level = Logger::DEBUG

You can also replace the SDK logger globally:

PostHog::Logging.logger = Rails.logger

Test helpers

When test_mode: true, events remain queued. You can inspect and clear the queue in tests:

posthog = PostHog::Client.new({ api_key: '<ph_project_token>', test_mode: true })
posthog.capture({ distinct_id: 'user_123', event: 'test_event' })

posthog.queued_messages
posthog.dequeue_last_message
posthog.clear

Thank you

This library is largely based on the analytics-ruby package.