This guide helps you set up the example script to demonstrate all PostHog Ruby SDK features, including the new complex cohort evaluation capabilities.
-
Set up credentials:
cp .env.example .env # Edit .env with your actual PostHog credentials -
Run the example:
ruby example.rb
-
Choose an example to run from the interactive menu
Create a .env file with your PostHog credentials:
# Your PostHog project API key (found on the /setup page in PostHog)
POSTHOG_API_KEY=phc_your_project_key_here
# Your personal API key (required for local feature flag evaluation)
POSTHOG_PERSONAL_API_KEY=phx_your_personal_key_here
# PostHog host URL
POSTHOG_HOST=https://us.posthog.com # or https://eu.posthog.com for EU, or your self-hosted URLFor the complex cohort evaluation example (option 3), you need to create a specific cohort and feature flag in your PostHog instance:
Go to: PostHog → People → Cohorts → New Cohort
- Name:
posthog-team(or any name) - Conditions:
OR Group: ├── email contains "@posthog.com" └── $host equals "localhost:8010"
Go to: PostHog → People → Cohorts → New Cohort
- Name:
complex-cohort - Description:
Complex cohort for Ruby SDK demo - Conditions:
OR Group (Top Level): ├── AND Group 1: │ ├── email contains "@example.com" │ └── is_email_verified = "true" └── OR Group 2: └── User belongs to cohort "posthog-team"
Go to: PostHog → Feature Flags → New Feature Flag
- Name:
test-complex-cohort-flag - Key:
test-complex-cohort-flag - Release Conditions:
- User belongs to cohort
complex-cohort - Rollout percentage: 100%
- User belongs to cohort
- Event tracking with properties
- User identification and aliases
- Group identification
- Property setting ($set, $set_once)
- Basic feature flag evaluation
- Location-based flags
- Local-only evaluation
- Bulk flag retrieval
This demonstrates the Ruby SDK's new capability to evaluate complex cohorts locally:
Test Cases:
- ✅
user@example.comwithis_email_verified=true→ should match (first AND group) - ✅
dev@posthog.com→ should match (nested cohort reference) - ❌
user@other.com→ should not match (no conditions met)
Key Features Showcased:
- Multi-level nested logic (OR → AND → properties)
- Cohort-within-cohort references
- Mixed property operators (
icontains,exact) - Zero API calls for complex evaluation
- Ruby SDK feature parity with Python/Node.js SDKs
- Getting payloads for specific flags
- Bulk payload retrieval
- Remote config payloads
- Verify your API keys are correct
- Ensure personal API key has proper permissions
- Check that your host URL is correct
If test-complex-cohort-flag doesn't exist, the example will still run but return false for all tests. Follow the setup steps above to create the required cohort and flag.
- Ensure you have a personal API key (not just project API key)
- Check that feature flags are active in your PostHog instance
- Verify cohort conditions are properly configured
When working correctly, option 3 should output:
✅ Verified @example.com user: true
✅ @posthog.com user: true
❌ Regular user: false
🎯 Results Summary:
- Complex nested cohorts evaluated locally: ✅ YES
- Zero API calls needed: ✅ YES (all evaluated locally)
- Ruby SDK now has cohort parity: ✅ YES
This demonstrates that the Ruby SDK can now evaluate complex, nested cohorts entirely locally - a major new capability that brings it to feature parity with other PostHog server-side SDKs!