Setting the file. One moment.
Chapter 145 · Integration Ruby
Subchapter 145.5
references/EXAMPLE.mdMarkdown10 KBView on GitHub
Repository: https://github.com/PostHog/context-mill Path: basics/ruby
A simple command-line todo application built with plain Ruby (no frameworks) demonstrating PostHog integration for CLIs, scripts, data pipelines, and non-web Ruby applications.
This example serves as:
PostHog::Client.new(...) for explicit client managementshutdown in ensure block to flush events before exitdistinct_id and propertiesidentify# Install bundler if needed
gem install bundler
# Install dependencies
bundle install# Copy environment template
cp .env.example .env
# Edit .env and add your PostHog project token
# POSTHOG_PROJECT_TOKEN=phc_your_project_token_here
# POSTHOG_HOST=https://us.i.posthog.com# Add a todo
ruby todo.rb add "Buy groceries"
# List all todos
ruby todo.rb list
# Complete a todo
ruby todo.rb complete 1
# Delete a todo
ruby todo.rb delete 1
# Show statistics
ruby todo.rb statsThe app tracks these events in PostHog:
| Event | Properties | Purpose |
|---|---|---|
todo_added | todo_id, todo_length, total_todos | When user adds a new todo |
todos_viewed | total_todos, completed_todos | When user lists todos |
todo_completed | todo_id, time_to_complete_hours | When user completes a todo |
todo_deleted | todo_id, was_completed | When user deletes a todo |
stats_viewed | total_todos, completed_todos, pending_todos | When user views stats |
basics/ruby/
├── todo.rb # Main CLI application
├── Gemfile # Ruby dependencies
├── .env.example # Environment variable template
├── .gitignore # Git ignore rules
└── README.md # This filerequire 'posthog-ruby'
posthog = PostHog::Client.new(
api_key: api_key,
host: 'https://us.i.posthog.com',
on_error: proc { |status, msg| puts "PostHog error: #{status} - #{msg}" }
)# Track events with distinct_id
posthog.capture(
distinct_id: 'user_123',
event: 'event_name',
properties: { key: 'value' }
)begin
# Your application code
ensure
# Always call shutdown to flush events and close connections
posthog&.shutdown
end# Identify users (optional - adds user properties)
posthog.identify(
distinct_id: 'user_123',
properties: { email: 'user@example.com', plan: 'pro' }
)The app works fine without PostHog configured - it simply won’t track analytics. You’ll see a warning message but the app continues to function normally.
todo.rb to experiment with PostHog trackingposthog.is_feature_enabled('flag-name', 'user_id')# 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
source 'https://rubygems.org'
gem 'posthog-ruby', '~> 3.0'
gem 'dotenv', '~> 3.0'
#!/usr/bin/env ruby
# frozen_string_literal: true
# Simple CLI Todo App with PostHog Analytics
#
# A minimal plain Ruby CLI application demonstrating PostHog integration
# for non-framework Ruby projects (CLIs, scripts, data pipelines, etc.).
require 'json'
require 'securerandom'
require 'time'
require 'dotenv/load'
require 'posthog'
# Data file location
DATA_FILE = File.join(Dir.home, '.todo_app.json')
def initialize_posthog