Instrument Product Analytics
PostHogOfficialCreated by PostHog
- Skill
- Tool integration
- Data and AI
- MIT
- Low risk
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a structured, multi-step workflow to integrate PostHog product analytics into a wide variety of software projects. It guides the user through detecting the project's framework, installing the appropriate SDK, initializing the service, and strategically placing event capture calls. The skill emphasizes non-intrusive implementation, requiring that new analytics code be added alongside existing logic without altering the project's fundamental architecture. It also covers user identification, error tracking, and secure environment variable management.
What it does
- Detects the project's framework and language to determine the correct PostHog SDK.
- Handles initial SDK installation and initialization based on framework-specific patterns.
- Identifies key user actions and business-critical events for tracking.
- Implements event capture calls and user identification logic.
- Configures environment variables securely for API tokens and host URLs.
- Verifies the integration and performs targeted linting on modified files.
When to use it
- After implementing new features that require tracking for product analytics.
- When reviewing pull requests to ensure meaningful user behaviors are captured.
- During the initial setup of a new project that needs product analytics.
- When migrating or adding analytics to an existing application.
Advantages
- Supports a broad range of frontend, backend, and mobile frameworks.
- Ensures consistent event naming and property usage across the codebase.
- Prioritizes non-breaking changes and minimal impact on existing code structure.
- Automates the complex setup of environment variables and SDK initialization.
Usage examples
- “Add PostHog analytics to my Next.js project.”
- The agent will analyze the project structure, install the PostHog SDK, initialize it in the appropriate client/server files, and prompt you to identify key events to track.
- “I need to track user signups and errors in my Django backend.”
- The agent will locate your authentication logic, add identify calls, implement exception capture, and ensure the PostHog project tokens are managed via environment variables.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/posthog/instrument-product-analytics/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip instrument-product-analytics.zip -d .agents/skills
Documentation by PostHog
Add PostHog product analytics events
Use this skill to add product analytics events (capture calls) that track meaningful user actions in new or changed code. Use it after implementing features or reviewing PRs to ensure key user behaviors are captured. If PostHog is not yet installed, this skill also covers initial SDK setup. Supports any framework or language.
Supported frameworks and languages: Next.js, React Router, Nuxt, Vue, TanStack Start, SvelteKit, Astro, Angular, Django, Flask, FastAPI, Laravel, PHP, Ruby on Rails, Go, Elixir, Android, iOS, Flutter, React Native, Expo, and more.
Instructions
Follow these steps IN ORDER:
STEP 1: Analyze the codebase and detect the platform.
Look for dependency files (package.json, pubspec.yaml, Podfile, Package.swift, requirements.txt, Gemfile, composer.json, go.mod, mix.exs, etc.) to determine the framework and language.
Look for lockfiles (pnpm-lock.yaml, package-lock.json, yarn.lock, bun.lockb, go.sum, pubspec.lock, Podfile.lock, Package.resolved, mix.lock) to determine the package manager.
Check for existing PostHog setup. If PostHog is already installed and initialized, skip to STEP 5.
STEP 2: Research integration. (Skip if PostHog is already set up.) 2.1. Find the reference file below that matches the detected framework — it is the source of truth for SDK initialization, provider setup, and event capture patterns. Read it now. 2.2. If no reference matches, fall back to your general knowledge and web search. Use posthog.com/docs as the primary search source.
STEP 3: Install the PostHog SDK. (Skip if PostHog is already set up.)
Add the PostHog SDK package for the detected platform. Do not manually edit package.json — use the package manager's install command.
STEP 4: Initialize PostHog. (Skip if PostHog is already set up.)
Follow the framework reference for where and how to initialize. This varies significantly by framework (e.g., instrumentation-client.ts for Next.js 15.3+, AppConfig.ready() for Django, create_app() for Flask).
STEP 5: Plan event tracking.
From the project's file list, select between 10 and 15 files that might have interesting business value for event tracking, especially conversion and churn events.
Also look for files related to login that could be used for identifying users, along with error handling.
Find any existing
posthog.capture()code. Make note of event name formatting. Don't duplicate existing events; supplement them.Track actions only, not pageviews (those can be captured automatically). Exceptions can be made for "viewed"-type events at the top of a conversion funnel.
Server-side events are REQUIRED if the project includes any instrumentable server-side code (API routes, server actions, webhook handlers, payment/checkout completion, authentication endpoints).
STEP 6: Implement event capture.
For each planned event, add
posthog.capture()calls with useful properties.If a file already has existing integration code for other tools or services, don't overwrite or remove that code. Place PostHog code below it.
Do not alter the fundamental architecture of existing files. Make additions minimal and targeted.
You must read a file immediately before attempting to write it.
STEP 7: Identify users.
Add PostHog
identify()calls on the client side during login and signup events. Use the contents of login and signup forms to identify users on submit.If both frontend and backend exist, pass the client-side session and distinct ID using
X-POSTHOG-DISTINCT-IDandX-POSTHOG-SESSION-IDheaders to the server-side code. On the server side, make sure events have a matching distinct ID.
STEP 8: Add error tracking.
Add PostHog exception capture error tracking to relevant files, particularly around critical user flows and API boundaries.
STEP 9: Set up environment variables.
Check if the project already has PostHog environment variables configured (e.g. in
.env,.env.local, or framework-specific env files). If valid values already exist, skip this step.If the PostHog project token is missing, use the PostHog MCP server's
projects-gettool to retrieve the project'sapi_token. If multiple projects are returned, ask the user which project to use. If the MCP server is not connected or not authenticated, ask the user for their PostHog project token instead.For the PostHog host URL: check the
projects-getMCP response for aregionfield —USmaps tohttps://us.i.posthog.com,EUmaps tohttps://eu.i.posthog.com. If the region is not available from the MCP response or from existing project configuration, ask the user: "Are you on PostHog US Cloud or EU Cloud?" Do not assume US Cloud.Write these values to the appropriate env file using the framework's naming convention.
Reference these environment variables in code instead of hardcoding them.
STEP 10: Verify and clean up.
Check the project for errors. Look for type checking or build scripts in package.json.
Ensure any components created were actually used.
Run any linter or prettier-like scripts found in the package.json, but ONLY on the files you have edited or created during this session. Never run formatting or linting across the entire project's codebase.
Reference files
references/EXAMPLE-next-app-router.md- next-app-router example project codereferences/EXAMPLE-next-pages-router.md- next-pages-router example project codereferences/EXAMPLE-react-react-router-6.md- react-react-router-6 example project codereferences/EXAMPLE-react-react-router-7-framework.md- react-react-router-7-framework example project codereferences/EXAMPLE-react-react-router-7-data.md- react-react-router-7-data example project codereferences/EXAMPLE-react-react-router-7-declarative.md- react-react-router-7-declarative example project codereferences/EXAMPLE-nuxt-3-6.md- nuxt-3-6 example project codereferences/EXAMPLE-nuxt-4.md- nuxt-4 example project codereferences/EXAMPLE-vue-3.md- vue-3 example project codereferences/EXAMPLE-react-tanstack-router-file-based.md- react-tanstack-router-file-based example project codereferences/EXAMPLE-react-tanstack-router-code-based.md- react-tanstack-router-code-based example project codereferences/EXAMPLE-tanstack-start.md- tanstack-start example project codereferences/EXAMPLE-sveltekit.md- sveltekit example project codereferences/EXAMPLE-astro-static.md- astro-static example project codereferences/EXAMPLE-astro-view-transitions.md- astro-view-transitions example project codereferences/EXAMPLE-astro-ssr.md- astro-ssr example project codereferences/EXAMPLE-astro-hybrid.md- astro-hybrid example project codereferences/EXAMPLE-angular.md- angular example project codereferences/EXAMPLE-django.md- django example project codereferences/EXAMPLE-flask.md- flask example project codereferences/EXAMPLE-fastapi.md- fastapi example project codereferences/EXAMPLE-python.md- python example project codereferences/EXAMPLE-laravel.md- laravel example project codereferences/EXAMPLE-php.md- php example project codereferences/EXAMPLE-ruby-on-rails.md- ruby-on-rails example project codereferences/EXAMPLE-ruby.md- ruby example project codereferences/EXAMPLE-android.md- android example project codereferences/EXAMPLE-swift.md- swift example project codereferences/EXAMPLE-react-native.md- react-native example project codereferences/EXAMPLE-expo.md- expo example project codereferences/next-js.md- Next.jsreferences/react-router-v6.md- React router v6references/react-router-v7-framework-mode.md- React router v7 framework mode (remix v3)references/react-router-v7-data-mode.md- React router v7 data modereferences/react-router-v7-declarative-mode.md- React router v7 declarative modereferences/nuxt-js-3-6.md- Nuxt.js (v3.0 to v3.6)references/nuxt-js.md- Nuxt.jsreferences/vue-js.md- Vue.jsreferences/tanstack-start.md- Tanstack startreferences/svelte.md- Sveltereferences/astro.md- Astroreferences/angular.md- Angularreferences/django.md- Djangoreferences/flask.md- Flaskreferences/python.md- Pythonreferences/posthog-python.md- PostHog python SDKreferences/dotnet.md- .netreferences/elixir.md- Elixirreferences/go.md- Goreferences/laravel.md- Laravelreferences/php.md- Phpreferences/ruby-on-rails.md- Ruby on railsreferences/ruby.md- Rubyreferences/android.md- Androidreferences/ios.md- Iosreferences/usage.md- Ios SDK usagereferences/configuration.md- Ios SDK configurationreferences/flutter.md- Flutterreferences/react-native.md- React nativereferences/identify-users.md- Identify usersreferences/COMMANDMENTS.md- Framework-specific rules the integration must follow
Each framework reference contains SDK-specific installation, initialization, and usage patterns. Find the one matching the user's stack.
Key principles
Environment variables: Always use environment variables for PostHog keys. Never hardcode them.
Minimal changes: Add PostHog code alongside existing integrations. Don't replace or restructure existing code.
Match the docs: Follow the framework reference's initialization and capture patterns exactly.
Files
- ATTRIBUTION.mdAdded by AutoGineer970 B
- LICENSELicence file added by AutoGineer1.0 KiB
- SKILL.md9.6 KiB
- references/COMMANDMENTS.md1.5 KiB
- references/EXAMPLE-android.md49.8 KiB
- references/EXAMPLE-angular.md23.1 KiB
- references/EXAMPLE-astro-hybrid.md27.5 KiB
- references/EXAMPLE-astro-ssr.md27.6 KiB
- references/EXAMPLE-astro-static.md19.6 KiB
- references/EXAMPLE-astro-view-transitions.md23.3 KiB
- references/EXAMPLE-django.md30.7 KiB
- references/EXAMPLE-expo.md35.1 KiB
- references/EXAMPLE-fastapi.md40.9 KiB
- references/EXAMPLE-flask.md32.4 KiB
- references/EXAMPLE-laravel.md59.3 KiB
- references/EXAMPLE-next-app-router.md17.6 KiB
- references/EXAMPLE-next-pages-router.md19.1 KiB
- references/EXAMPLE-nuxt-3-6.md22.3 KiB
- references/EXAMPLE-nuxt-4.md28.9 KiB
- references/EXAMPLE-php.md13.0 KiB
- references/EXAMPLE-python.md11.6 KiB
- references/EXAMPLE-react-native.md46.0 KiB
- references/EXAMPLE-react-react-router-6.md13.5 KiB
- references/EXAMPLE-react-react-router-7-data.md20.1 KiB
- references/EXAMPLE-react-react-router-7-declarative.md15.0 KiB
- references/EXAMPLE-react-react-router-7-framework.md32.5 KiB
- references/EXAMPLE-react-tanstack-router-code-based.md19.6 KiB
- references/EXAMPLE-react-tanstack-router-file-based.md18.3 KiB
- references/EXAMPLE-ruby-on-rails.md38.0 KiB
- references/EXAMPLE-ruby.md10.0 KiB
- references/EXAMPLE-sveltekit.md18.6 KiB
- references/EXAMPLE-swift.md18.8 KiB
- references/EXAMPLE-tanstack-start.md29.9 KiB
- references/EXAMPLE-vue-3.md17.9 KiB
- references/android.md33.3 KiB
- references/angular.md16.5 KiB
- references/astro.md15.8 KiB
- references/configuration.md17.2 KiB
- references/django.md14.9 KiB
- references/dotnet.md25.8 KiB
- references/elixir.md17.6 KiB
- references/flask.md8.1 KiB
- references/flutter.md42.8 KiB
- references/go.md20.1 KiB
- references/identify-users.md12.2 KiB
- references/ios.md8.0 KiB
- references/laravel.md13.1 KiB
- references/next-js.md18.3 KiB
- references/nuxt-js-3-6.md11.4 KiB
- references/nuxt-js.md15.1 KiB
- references/php.md23.7 KiB
- references/posthog-python.md69.3 KiB
- references/python.md52.2 KiB
- references/react-native.md59.9 KiB
- references/react-router-v6.md13.6 KiB
- references/react-router-v7-data-mode.md13.5 KiB
- references/react-router-v7-declarative-mode.md13.7 KiB
- references/react-router-v7-framework-mode.md20.2 KiB
- references/ruby-on-rails.md19.7 KiB
- references/ruby.md25.5 KiB
- references/svelte.md14.6 KiB
- references/tanstack-start.md12.8 KiB
- references/usage.md29.2 KiB
- references/vue-js.md13.4 KiB
references/EXAMPLE-ruby-on-rails.md
PostHog ruby-on-rails Example Project
Repository: https://github.com/PostHog/context-mill Path: example-apps/ruby-on-rails
README.md
PostHog Ruby on Rails example
This is a Ruby on Rails example demonstrating PostHog integration with product analytics, error tracking (auto-instrumentation), feature flags, user identification, and ActiveJob instrumentation via the posthog-rails gem.
Features
Product analytics: Track user events and behaviors with
PostHog.captureError tracking (auto): Unhandled exceptions captured automatically by
posthog-railsError tracking (manual): Handled errors captured with
PostHog.capture_exceptionRails.error integration: Rails 7+ error reporting captured automatically
ActiveJob instrumentation: Background job failures captured automatically
User identification: Associate events with authenticated users via
PostHog.identifyFeature flags: Control feature rollouts with
PostHog.is_feature_enabledUser context: Exceptions automatically associated with
current_userFrontend tracking: posthog-js captures pageviews and session replay alongside backend events
Getting started
1. Install dependencies
bundle install2. Configure environment variables
cp .env.example .env
# Edit .env and add your PostHog project tokenGet your PostHog project token from your PostHog project settings.
3. Setup database
bin/rails db:create db:migrate db:seed4. Run the development server
bin/rails serverOpen http://localhost:3000 with your browser. Login with [email protected] / admin.
Project structure
ruby-on-rails/
├── config/
│ ├── routes.rb # URL routing
│ └── initializers/
│ └── posthog.rb # PostHog + posthog-rails configuration
├── app/
│ ├── controllers/
│ │ ├── application_controller.rb # Base controller with current_user
│ │ ├── sessions_controller.rb # Login/logout with PostHog identify
│ │ ├── registrations_controller.rb # Signup with PostHog identify
│ │ ├── dashboard_controller.rb # Feature flags + ActiveJob demo
│ │ ├── burritos_controller.rb # Custom event tracking
│ │ ├── profiles_controller.rb # Page view tracking
│ │ └── errors_controller.rb # Error tracking demos
│ ├── jobs/
│ │ └── example_job.rb # ActiveJob auto-instrumentation demo
│ ├── models/
│ │ └── user.rb # posthog_distinct_id + posthog_properties
│ └── views/
│ ├── layouts/application.html.erb # Base layout with posthog-js snippet
│ ├── sessions/new.html.erb # Login page
│ ├── registrations/new.html.erb # Signup page
│ ├── dashboard/show.html.erb # Feature flags demo
│ ├── burritos/show.html.erb # Event tracking demo
│ └── profiles/show.html.erb # Error tracking demo
├── db/
│ ├── migrate/ # Database migrations
│ └── seeds.rb # Default admin user
├── .env.example # Environment variable template
├── Gemfile # Ruby dependencies
└── README.md # This fileKey integration points
PostHog initialization (config/initializers/posthog.rb)
# Rails-specific auto-instrumentation
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
config.user_id_method = :posthog_distinct_id
end
PostHog.init do |config|
config.api_key = ENV.fetch('POSTHOG_PROJECT_TOKEN', nil)
config.host = ENV.fetch('POSTHOG_HOST', 'https://us.i.posthog.com')
endUser model (app/models/user.rb)
class User < ApplicationRecord
has_secure_password
# Called by posthog-rails for automatic user association in error reports.
# The primary key, not the email — an email can change, which splits one
# person's history in two, and it is PII on every event's identity.
def posthog_distinct_id
id.to_s
end
def posthog_properties
{ email: email, is_staff: is_staff, date_joined: created_at&.iso8601 }
end
endUser identification (app/controllers/sessions_controller.rb)
# Identify the user and capture login event
PostHog.identify(
distinct_id: user.posthog_distinct_id,
properties: user.posthog_properties
)
PostHog.capture(
distinct_id: user.posthog_distinct_id,
event: 'user_logged_in',
properties: { login_method: 'email' }
)Feature flags (app/controllers/dashboard_controller.rb)
# Check if a feature flag is enabled
@show_new_feature = PostHog.is_feature_enabled(
'new-dashboard-feature',
user.posthog_distinct_id,
person_properties: user.posthog_properties
)
# Get feature flag payload for configuration
@feature_config = PostHog.get_feature_flag_payload(
'new-dashboard-feature',
user.posthog_distinct_id
)Error tracking — auto-capture
With auto_capture_exceptions: true, unhandled exceptions in controllers are captured automatically. No code needed:
# This exception is automatically captured by posthog-rails
# with the current_user's posthog_distinct_id attached
def show
raise "Something went wrong" # Captured automatically!
endError tracking — manual capture
begin
risky_operation
rescue => e
PostHog.capture_exception(e, current_user.posthog_distinct_id)
endError tracking — Rails.error integration
# posthog-rails subscribes to Rails.error automatically
Rails.error.handle(context: { user_id: user.id }) do
risky_operation
endActiveJob instrumentation
# config: auto_instrument_active_job = true
# Job failures are captured automatically.
# Use the posthog_distinct_id DSL to associate errors with a user.
class ExampleJob < ApplicationJob
posthog_distinct_id ->(distinct_id, *) { distinct_id }
def perform(distinct_id, should_fail: false)
raise "Job failed" # Captured automatically with user context
end
end
# In the controller, pass the distinct_id when enqueuing:
ExampleJob.perform_later(current_user.posthog_distinct_id, should_fail: true)Frontend + Backend integration
This example includes the posthog-js snippet in the layout template to demonstrate how frontend and backend tracking work together.
How it works
posthog-js (frontend) captures pageviews, clicks, and session replay
posthog-ruby + posthog-rails (backend) captures business logic events, errors, and feature flag evaluations
Shared distinct_id — frontend and backend events are linked when the same
distinct_idis used on both sides. Callposthog.identify(user.id.to_s)in posthog-js after login, matching theposthog_distinct_idused on the backendSession replay lets you watch user sessions where errors occurred
Note: Unlike the Django SDK, posthog-rails does not include a context middleware that reads X-POSTHOG-SESSION-ID or X-POSTHOG-DISTINCT-ID tracing headers. Frontend and backend events are correlated through the shared distinct_id.
When to track frontend vs backend
Frontend: UI interactions, client-side errors, session replay, pageviews
Backend: Business logic (signups, purchases), server errors, feature flag evaluations, background jobs
Learn more
.env.example
# PostHog Configuration
POSTHOG_PROJECT_TOKEN=phc_your_project_token_here
POSTHOG_HOST=https://us.i.posthog.com
# Optional: Enable debug mode to see PostHog requests
# POSTHOG_DEBUG=true
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
private
def current_user
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
end
helper_method :current_user
def require_login
unless current_user
redirect_to login_path
end
end
end
app/controllers/burritos_controller.rb
class BurritosController < ApplicationController
before_action :require_login
def show
@burrito_count = session[:burrito_count] || 0
end
def consider
count = (session[:burrito_count] || 0) + 1
session[:burrito_count] = count
user = current_user
# PostHog: Track custom event
PostHog.identify(
distinct_id: user.posthog_distinct_id,
properties: user.posthog_properties
)
PostHog.capture(
distinct_id: user.posthog_distinct_id,
event: 'burrito_considered',
properties: { total_considerations: count }
)
render json: { success: true, count: count }
end
end
app/controllers/dashboard_controller.rb
class DashboardController < ApplicationController
before_action :require_login
def show
user = current_user
# PostHog: Track dashboard view
PostHog.capture(
distinct_id: user.posthog_distinct_id,
event: 'dashboard_viewed',
properties: { is_staff: user.is_staff }
)
# PostHog: Check feature flag
@show_new_feature = PostHog.is_feature_enabled(
'new-dashboard-feature',
user.posthog_distinct_id,
person_properties: user.posthog_properties
)
# PostHog: Get feature flag payload for configuration
@feature_config = PostHog.get_feature_flag_payload(
'new-dashboard-feature',
user.posthog_distinct_id
)
end
def enqueue_test_job
# Enqueue a job that will fail — posthog-rails captures the error automatically.
# The distinct_id is passed so the posthog_distinct_id DSL can associate the error with this user.
ExampleJob.perform_later(current_user.posthog_distinct_id, should_fail: true)
render json: {
success: true,
message: 'Job enqueued. The job will fail and posthog-rails will capture the error automatically.'
}
end
end
app/controllers/errors_controller.rb
class ErrorsController < ApplicationController
before_action :require_login
def test
# Manual exception capture — catch the error and report it explicitly
begin
raise StandardError, 'Test exception from critical operation'
rescue StandardError => e
# PostHog: Manually capture the exception
PostHog.capture_exception(e, current_user.posthog_distinct_id)
PostHog.capture(
distinct_id: current_user.posthog_distinct_id,
event: 'error_triggered',
properties: {
error_type: e.class.name,
error_message: e.message
}
)
render json: {
success: false,
error: e.message,
message: 'Error has been captured by PostHog'
}, status: :internal_server_error
end
end
def test_rails_error
# Rails.error.handle — Rails 7+ error reporting integration.
# posthog-rails subscribes to Rails.error, so exceptions reported
# via Rails.error.handle are automatically captured in PostHog.
Rails.error.handle(context: { user_id: current_user.id }) do
raise StandardError, 'Test error via Rails.error.handle — captured automatically by posthog-rails'
end
render json: {
success: true,
message: 'Error was handled via Rails.error.handle and captured by posthog-rails'
}
end
end
app/controllers/profiles_controller.rb
class ProfilesController < ApplicationController
before_action :require_login
def show
# PostHog: Track profile view
PostHog.capture(
distinct_id: current_user.posthog_distinct_id,
event: 'profile_viewed'
)
end
end
app/controllers/registrations_controller.rb
class RegistrationsController < ApplicationController
def new
redirect_to dashboard_path if current_user
end
def create
user = User.new(
email: params[:email],
password: params[:password],
password_confirmation: params[:password_confirmation]
)
if user.save
session[:user_id] = user.id
# PostHog: Identify the new user and capture signup event
PostHog.identify(
distinct_id: user.posthog_distinct_id,
properties: user.posthog_properties
)
PostHog.capture(
distinct_id: user.posthog_distinct_id,
event: 'user_signed_up',
properties: { signup_method: 'form' }
)
redirect_to dashboard_path
else
flash[:error] = user.errors.full_messages.join(', ')
render :new, status: :unprocessable_entity
end
end
end
app/controllers/sessions_controller.rb
class SessionsController < ApplicationController
def new
redirect_to dashboard_path if current_user
end
def create
user = User.find_by(email: params[:email])
if user&.authenticate(params[:password])
session[:user_id] = user.id
# PostHog: Identify the user and capture login event
PostHog.identify(
distinct_id: user.posthog_distinct_id,
properties: user.posthog_properties
)
PostHog.capture(
distinct_id: user.posthog_distinct_id,
event: 'user_logged_in',
properties: { login_method: 'email' }
)
redirect_to dashboard_path
else
flash[:error] = 'Invalid email or password'
render :new, status: :unprocessable_entity
end
end
def destroy
if current_user
# PostHog: Track logout before session ends
PostHog.capture(
distinct_id: current_user.posthog_distinct_id,
event: 'user_logged_out'
)
end
session.delete(:user_id)
redirect_to login_path
end
end
app/jobs/application_job.rb
class ApplicationJob < ActiveJob::Base
end
app/jobs/example_job.rb
# Example ActiveJob demonstrating posthog-rails auto-instrumentation.
#
# When auto_instrument_active_job is enabled in the PostHog config,
# posthog-rails automatically captures exceptions from failed jobs.
# The job class name, queue, and arguments are included as properties
# on the error event.
#
# Use the posthog_distinct_id DSL to associate job errors with a user.
# The proc receives the same arguments as perform and should return
# the distinct_id string. Without this, job errors have no user context.
class ExampleJob < ApplicationJob
queue_as :default
# Extract distinct_id from the first argument so posthog-rails
# can associate the error with the user who triggered the job.
posthog_distinct_id ->(distinct_id, *) { distinct_id }
def perform(distinct_id, should_fail: false)
if should_fail
raise StandardError, 'Example job failure - this error is automatically captured by posthog-rails'
end
Rails.logger.info "ExampleJob completed successfully for #{distinct_id}"
end
end
app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
end
app/models/user.rb
class User < ApplicationRecord
has_secure_password
validates :email, presence: true, uniqueness: true
# Called by posthog-rails for automatic user association in error reports.
# When auto_capture_exceptions and capture_user_context are enabled,
# posthog-rails calls this method on current_user to get the distinct_id.
# The primary key, not the email: an email can change, which would split one
# person's history in two, and it is PII on every event's identity.
def posthog_distinct_id
id.to_s
end
# Helper used by controllers when calling PostHog.identify to set person properties.
# These properties appear on the person profile in PostHog.
def posthog_properties
{
email: email,
is_staff: is_staff,
date_joined: created_at&.iso8601
}
end
end
app/views/burritos/show.html.erb
<% content_for(:title) { 'Burrito - PostHog Rails example' } %>
<div class="card">
<h1>Burrito consideration tracker</h1>
<p>This page demonstrates custom event tracking with PostHog.</p>
</div>
<div class="card" style="text-align: center;">
<h2>Times considered</h2>
<div class="count" id="burrito-count"><%= @burrito_count %></div>
<button onclick="considerBurrito()" style="font-size: 18px; padding: 15px 30px;">
Consider a burrito
</button>
</div>
<div class="card">
<h3>How event tracking works</h3>
<p>Each time you click the button, a <code>burrito_considered</code> event is sent to PostHog:</p>
<pre style="background: #f3f4f6; padding: 15px; border-radius: 5px; overflow-x: auto; margin-top: 15px;"><code>PostHog.capture(
distinct_id: user.posthog_distinct_id,
event: 'burrito_considered',
properties: { total_considerations: count }
)</code></pre>
</div>
<% content_for :scripts do %>
<script>
async function considerBurrito() {
try {
const response = await fetch('/api/burrito/consider', {
method: 'POST',
headers: {
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content,
'Content-Type': 'application/json',
},
});
const data = await response.json();
if (data.success) {
document.getElementById('burrito-count').textContent = data.count;
}
} catch (error) {
console.error('Error:', error);
}
}
</script>
<% end %>
app/views/dashboard/show.html.erb
<% content_for(:title) { 'Dashboard - PostHog Rails example' } %>
<div class="card">
<h1>Dashboard</h1>
<p>Welcome back, <strong><%= current_user.email %></strong>!</p>
</div>
<div class="card">
<h2>Feature flags</h2>
<p>Feature flags allow you to control feature rollouts and run A/B tests.</p>
<% if @show_new_feature %>
<div class="feature-flag">
<h3>New feature enabled!</h3>
<p>
This section is only visible because the <code>new-dashboard-feature</code>
flag is enabled for your user.
</p>
<% if @feature_config %>
<p><strong>Feature config:</strong> <%= @feature_config %></p>
<% end %>
</div>
<% else %>
<div style="background: #f3f4f6; padding: 15px; border-radius: 8px; margin-top: 15px;">
<p>
The <code>new-dashboard-feature</code> flag is not enabled for your user.
Create this flag in your PostHog project to see it in action.
</p>
</div>
<% end %>
</div>
<div class="card">
<h2>ActiveJob instrumentation</h2>
<p>
Click below to enqueue a background job that will fail.
<code>posthog-rails</code> automatically captures the exception — no extra code needed.
</p>
<button onclick="enqueueTestJob()" style="margin-top: 10px;">Enqueue failing job</button>
<div id="job-result" style="margin-top: 15px; display: none;"></div>
</div>
<div class="card">
<h3>How feature flags work</h3>
<pre style="background: #f3f4f6; padding: 15px; border-radius: 5px; overflow-x: auto;"><code># Check if a feature flag is enabled
show_feature = PostHog.is_feature_enabled(
'new-dashboard-feature',
user.posthog_distinct_id,
person_properties: user.posthog_properties
)
# Get feature flag payload for configuration
config = PostHog.get_feature_flag_payload(
'new-dashboard-feature',
user.posthog_distinct_id
)</code></pre>
</div>
<% content_for :scripts do %>
<script>
async function enqueueTestJob() {
const resultDiv = document.getElementById('job-result');
try {
const response = await fetch('/api/test-job', {
method: 'POST',
headers: {
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content,
'Content-Type': 'application/json',
},
});
const data = await response.json();
resultDiv.style.display = 'block';
resultDiv.innerHTML = '<div class="flash success">' + data.message + '</div>';
} catch (error) {
resultDiv.style.display = 'block';
resultDiv.innerHTML = '<div class="flash error">Request failed: ' + error + '</div>';
}
}
</script>
<% end %>
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= content_for?(:title) ? yield(:title) : 'PostHog Rails example' %></title>
<%= csrf_meta_tags %>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
line-height: 1.6;
background-color: #f5f5f5;
color: #333;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
nav {
background: #1d4ed8;
padding: 15px 20px;
margin-bottom: 30px;
}
nav a {
color: white;
text-decoration: none;
margin-right: 20px;
}
nav a:hover {
text-decoration: underline;
}
.card {
background: white;
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h1, h2, h3 {
margin-bottom: 15px;
color: #1d4ed8;
}
button, .btn {
background: #1d4ed8;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
display: inline-block;
text-decoration: none;
}
button:hover, .btn:hover {
background: #1e40af;
}
button.danger {
background: #dc2626;
}
button.danger:hover {
background: #b91c1c;
}
input {
width: 100%;
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ddd;
border-radius: 5px;
font-size: 14px;
}
.flash {
padding: 10px 15px;
border-radius: 5px;
margin-bottom: 20px;
}
.flash.error {
background: #fee2e2;
color: #dc2626;
}
.flash.success {
background: #d1fae5;
color: #059669;
}
.feature-flag {
background: #fef3c7;
border: 2px dashed #f59e0b;
padding: 15px;
border-radius: 8px;
margin: 20px 0;
}
code {
background: #f3f4f6;
padding: 2px 6px;
border-radius: 3px;
font-family: monospace;
}
.count {
font-size: 48px;
font-weight: bold;
color: #1d4ed8;
text-align: center;
padding: 20px;
}
</style>
<!-- PostHog frontend tracking (posthog-js) -->
<script>
// POSTHOG_BROWSER_SNIPPET_START
!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.crossOrigin="anonymous",p.async=!0,p.src=s.api_host.replace(".i.posthog.com","-assets.i.posthog.com")+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],Object.defineProperty(u,"toString",{configurable:!0,enumerable:!0,writable:!0,value:function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e}}),Object.defineProperty(u.people,"toString",{configurable:!0,enumerable:!0,writable:!0,value:function(){return u.toString(1)+".people (stub)"}}),o="init capture register register_once register_for_session unregister unregister_for_session getFeatureFlag getFeatureFlagPayload isFeatureEnabled reloadFeatureFlags updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures on onFeatureFlags onSessionId getSurveys getActiveMatchingSurveys renderSurvey canRenderSurvey identify setPersonProperties group resetGroups setPersonPropertiesForFlags resetPersonPropertiesForFlags setGroupPropertiesForFlags resetGroupPropertiesForFlags reset get_distinct_id getGroups get_session_id get_session_replay_url alias set_config startSessionRecording stopSessionRecording sessionRecordingStarted captureException loadToolbar get_property getSessionProperty createPersonProfile opt_in_capturing opt_out_capturing has_opted_in_capturing has_opted_out_capturing clear_opt_in_out_capturing debug getPageViewId".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
// POSTHOG_BROWSER_SNIPPET_END
posthog.init('<%= ENV["POSTHOG_PROJECT_TOKEN"] %>', {
api_host: '<%= ENV.fetch("POSTHOG_HOST", "https://us.i.posthog.com") %>',
person_profiles: 'identified_only'
})
</script>
</head>
<body>
<% if current_user %>
<nav style="display: flex; align-items: center;">
<a href="<%= dashboard_path %>">Dashboard</a>
<a href="<%= burrito_path %>">Burrito</a>
<a href="<%= profile_path %>">Profile</a>
<%= button_to 'Logout (' + current_user.email + ')', logout_path, method: :delete, form: { style: 'margin-left: auto;' }, style: 'background: transparent; border: none; color: white; cursor: pointer; font-size: inherit; padding: 0;' %>
</nav>
<% end %>
<div class="container">
<% if flash[:error] %>
<div class="flash error"><%= flash[:error] %></div>
<% end %>
<% if flash[:notice] %>
<div class="flash success"><%= flash[:notice] %></div>
<% end %>
<%= yield %>
</div>
<%= yield :scripts %>
</body>
</html>
app/views/profiles/show.html.erb
<% content_for(:title) { 'Profile - PostHog Rails example' } %>
<div class="card">
<h1>Profile</h1>
<p>This page demonstrates error tracking with PostHog and <code>posthog-rails</code>.</p>
</div>
<div class="card">
<h2>User information</h2>
<table style="width: 100%; border-collapse: collapse;">
<tr>
<td style="padding: 10px; border-bottom: 1px solid #eee;"><strong>Email:</strong></td>
<td style="padding: 10px; border-bottom: 1px solid #eee;"><%= current_user.email %></td>
</tr>
<tr>
<td style="padding: 10px; border-bottom: 1px solid #eee;"><strong>Date Joined:</strong></td>
<td style="padding: 10px; border-bottom: 1px solid #eee;"><%= current_user.created_at %></td>
</tr>
<tr>
<td style="padding: 10px;"><strong>Staff Status:</strong></td>
<td style="padding: 10px;"><%= current_user.is_staff ? 'Yes' : 'No' %></td>
</tr>
</table>
</div>
<div class="card">
<h2>Error tracking demo</h2>
<p>Click the buttons below to trigger different types of errors and see how PostHog captures them.</p>
<div style="margin-top: 20px;">
<button class="danger" onclick="triggerError('manual')">
Manual capture_exception
</button>
<button class="danger" onclick="triggerError('rails')" style="margin-left: 10px;">
Rails.error.handle
</button>
</div>
<div id="error-result" style="margin-top: 20px; display: none;"></div>
</div>
<div class="card">
<h3>How error tracking works</h3>
<p><strong>Auto-capture (no code needed):</strong></p>
<pre style="background: #f3f4f6; padding: 15px; border-radius: 5px; overflow-x: auto;"><code># config/initializers/posthog.rb
PostHog::Rails.configure do |config|
config.auto_capture_exceptions = true
config.capture_user_context = true
end
# That's it! Unhandled exceptions are captured automatically.</code></pre>
<p style="margin-top: 15px;"><strong>Manual capture:</strong></p>
<pre style="background: #f3f4f6; padding: 15px; border-radius: 5px; overflow-x: auto;"><code>begin
risky_operation
rescue => e
PostHog.capture_exception(e, user.posthog_distinct_id)
end</code></pre>
<p style="margin-top: 15px;"><strong>Rails.error integration:</strong></p>
<pre style="background: #f3f4f6; padding: 15px; border-radius: 5px; overflow-x: auto;"><code># posthog-rails subscribes to Rails.error automatically
Rails.error.handle(context: { user_id: user.id }) do
risky_operation
end</code></pre>
</div>
<% content_for :scripts do %>
<script>
async function triggerError(type) {
const resultDiv = document.getElementById('error-result');
const url = type === 'rails' ? '/api/test-rails-error' : '/api/test-error';
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content,
'Content-Type': 'application/json',
},
});
const data = await response.json();
resultDiv.style.display = 'block';
if (data.success) {
resultDiv.innerHTML = '<div class="flash success">' + data.message + '</div>';
} else {
resultDiv.innerHTML = '<div class="flash error"><strong>Error captured:</strong> ' + data.error + '<br><small>' + data.message + '</small></div>';
}
} catch (error) {
resultDiv.style.display = 'block';
resultDiv.innerHTML = '<div class="flash error">Request failed: ' + error + '</div>';
}
}
</script>
<% end %>
app/views/registrations/new.html.erb
<% content_for(:title) { 'Sign Up - PostHog Rails example' } %>
<div class="card">
<h1>Sign Up</h1>
<p>Create an account to see PostHog analytics in action.</p>
<form action="<%= signup_path %>" method="post" style="margin-top: 20px;">
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="password" placeholder="Password" required>
<input type="password" name="password_confirmation" placeholder="Confirm Password" required>
<button type="submit">Sign Up</button>
</form>
<p style="margin-top: 15px; color: #666; font-size: 14px;">
Already have an account? <a href="<%= login_path %>">Login</a>
</p>
</div>
app/views/sessions/new.html.erb
<% content_for(:title) { 'Login - PostHog Rails example' } %>
<div class="card">
<h1>PostHog Rails example</h1>
<p>Welcome! This example demonstrates PostHog integration with Ruby on Rails, including automatic error tracking via <code>posthog-rails</code>.</p>
</div>
<div class="card">
<h2>Login</h2>
<p>Login to see PostHog analytics in action.</p>
<form action="<%= login_path %>" method="post" style="margin-top: 20px;">
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Login</button>
</form>
<p style="margin-top: 15px; color: #666; font-size: 14px;">
Don't have an account? <a href="<%= signup_path %>">Sign up</a><br>
Tip: Run <code>bin/rails db:seed</code> to create [email protected] / admin
</p>
</div>
<div class="card">
<h3>What this example demonstrates</h3>
<ul style="padding-left: 20px;">
<li><strong>User identification</strong> — Users are identified with <code>PostHog.identify</code> on login</li>
<li><strong>Event tracking</strong> — Custom events captured with <code>PostHog.capture</code></li>
<li><strong>Feature flags</strong> — Conditional features with <code>PostHog.is_feature_enabled</code></li>
<li><strong>Error tracking (auto)</strong> — Unhandled exceptions captured automatically by <code>posthog-rails</code></li>
<li><strong>Error tracking (manual)</strong> — Handled errors captured with <code>PostHog.capture_exception</code></li>
<li><strong>ActiveJob instrumentation</strong> — Background job failures captured automatically</li>
<li><strong>Rails.error integration</strong> — Rails 7+ error reporting captured by <code>posthog-rails</code></li>
<li><strong>Frontend tracking</strong> — posthog-js captures pageviews and session replay</li>
</ul>
</div>
config.ru
require_relative 'config/environment'
run Rails.application
config/application.rb
require_relative 'boot'
require 'rails/all'
Bundler.require(*Rails.groups)
module PosthogExample
class Application < Rails::Application
config.load_defaults 7.1
# Use SQLite for all stores
config.active_job.queue_adapter = :async
end
end
config/boot.rb
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup'
config/environment.rb
require_relative 'application'
Rails.application.initialize!
config/environments/development.rb
require 'active_support/core_ext/integer/time'
Rails.application.configure do
config.enable_reloading = true
config.eager_load = false
config.consider_all_requests_local = true
config.server_timing = true
# Secret key for development (not used in production)
config.secret_key_base = 'dev-secret-key-for-posthog-example-only'
config.action_controller.perform_caching = false
config.cache_store = :memory_store
config.active_support.deprecation = :log
config.active_support.disallowed_deprecation = :raise
config.active_support.disallowed_deprecation_warnings = []
config.active_record.migration_error = :page_load
config.active_record.verbose_query_logs = true
end
config/initializers/posthog.rb
# PostHog configuration with posthog-rails auto-instrumentation
#
# The posthog-rails gem provides:
# - Automatic exception capture for unhandled controller errors
# - ActiveJob instrumentation for background job failures
# - User context detection from current_user
# - Rails.error integration for rescued exceptions
PostHog.init do |config|
config.api_key = ENV.fetch('POSTHOG_PROJECT_TOKEN', nil)
config.host = ENV.fetch('POSTHOG_HOST', 'https://us.i.posthog.com')
end
PostHog::Rails.configure do |config|
# Auto-capture unhandled exceptions in controllers
config.auto_capture_exceptions = true
# Also capture exceptions that Rails rescues (e.g. ActiveRecord::RecordNotFound)
config.report_rescued_exceptions = true
# Auto-instrument ActiveJob failures
config.auto_instrument_active_job = true
# Automatically associate errors with the current user
config.capture_user_context = true
config.current_user_method = :current_user
config.user_id_method = :posthog_distinct_id
end
config/routes.rb
Rails.application.routes.draw do
# Auth
get 'login', to: 'sessions#new'
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#destroy'
get 'signup', to: 'registrations#new'
post 'signup', to: 'registrations#create'
# App
get 'dashboard', to: 'dashboard#show'
get 'burrito', to: 'burritos#show'
post 'api/burrito/consider', to: 'burritos#consider'
get 'profile', to: 'profiles#show'
# Error tracking demos
post 'api/test-error', to: 'errors#test'
post 'api/test-rails-error', to: 'errors#test_rails_error'
# Background job demo
post 'api/test-job', to: 'dashboard#enqueue_test_job'
root 'sessions#new'
end
db/migrate/20240101000000_create_users.rb
class CreateUsers < ActiveRecord::Migration[7.1]
def change
create_table :users do |t|
t.string :email, null: false
t.string :password_digest, null: false
t.boolean :is_staff, default: false
t.timestamps
end
add_index :users, :email, unique: true
end
end
db/schema.rb
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.2].define(version: 2024_01_01_000000) do
create_table "users", force: :cascade do |t|
t.string "email", null: false
t.string "password_digest", null: false
t.boolean "is_staff", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
end
end
db/seeds.rb
# Create a default admin user for testing
User.find_or_create_by!(email: '[email protected]') do |user|
user.password = 'admin'
user.password_confirmation = 'admin'
user.is_staff = true
end
puts 'Seed data created: [email protected] / admin'
Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 7.1'
gem 'sqlite3', '~> 1.7'
gem 'puma', '~> 6.0'
gem 'bcrypt', '~> 3.1'
gem 'dotenv-rails', '~> 3.0'
# PostHog
gem 'posthog-ruby', '~> 3.0'
gem 'posthog-rails'
Rakefile
require_relative 'config/application'
Rails.application.load_tasks
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Needs an MCP server, Needs Python, Needs Node.js
Security
What our scanner noticed· Low risk
- Instructions that use the network
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- PostHog
- Publisher
- PostHog (Official)
- Source
- PostHog/skills · skills/omnibus/instrument-product-analytics · commit a5d30e7
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by PostHog or PostHog.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download instrument-product-analytics.zip