|
| 1 | +--- |
| 2 | +title: Ruby |
| 3 | +--- |
| 4 | + |
| 5 | +import { Step, Steps } from 'fumadocs-ui/components/steps'; |
| 6 | +import { Callout } from 'fumadocs-ui/components/callout'; |
| 7 | +import CommonSdkConfig from '@/components/common-sdk-config.mdx'; |
| 8 | + |
| 9 | +The OpenPanel Ruby SDK allows you to track user behavior in your Ruby applications. This guide provides instructions for installing and using the Ruby SDK in your project. |
| 10 | + |
| 11 | +<Callout> |
| 12 | +View the [Ruby SDK on GitHub](https://github.com/tstaetter/openpanel-ruby-sdk) for the latest updates and source code. |
| 13 | +</Callout> |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +<Steps> |
| 18 | +### Install dependencies |
| 19 | + |
| 20 | +If you're using Bundler, add to your `Gemfile`: |
| 21 | + |
| 22 | +```bash |
| 23 | +bundle add openpanel-sdk |
| 24 | +``` |
| 25 | + |
| 26 | +Or install the gem directly: |
| 27 | + |
| 28 | +```bash |
| 29 | +gem install openpanel-sdk |
| 30 | +``` |
| 31 | + |
| 32 | +### Set environment variables |
| 33 | + |
| 34 | +Set your environment variables in a `.env` file: |
| 35 | + |
| 36 | +```bash |
| 37 | +OPENPANEL_TRACK_URL=https://api.openpanel.dev/track |
| 38 | +OPENPANEL_CLIENT_ID=<YOUR_CLIENT_ID> |
| 39 | +OPENPANEL_CLIENT_SECRET=<YOUR_CLIENT_SECRET> |
| 40 | +``` |
| 41 | + |
| 42 | +### Initialize |
| 43 | + |
| 44 | +Require and initialize the OpenPanel SDK: |
| 45 | + |
| 46 | +```ruby |
| 47 | +require 'openpanel-sdk' |
| 48 | + |
| 49 | +tracker = OpenPanel::SDK::Tracker.new |
| 50 | +``` |
| 51 | + |
| 52 | +### Configuration Options |
| 53 | + |
| 54 | +<CommonSdkConfig /> |
| 55 | + |
| 56 | +Additional Ruby-specific options: |
| 57 | + |
| 58 | +- `disabled` - Set to `true` to disable all event tracking |
| 59 | +- `env` - Environment name (e.g., `Rails.env.to_s`) |
| 60 | + |
| 61 | +```ruby |
| 62 | +tracker = OpenPanel::SDK::Tracker.new( |
| 63 | + { env: Rails.env.to_s }, |
| 64 | + disabled: Rails.env.development? |
| 65 | +) |
| 66 | +``` |
| 67 | + |
| 68 | +</Steps> |
| 69 | + |
| 70 | +## Usage |
| 71 | + |
| 72 | +### Tracking Events |
| 73 | + |
| 74 | +To track an event, use the `track` method: |
| 75 | + |
| 76 | +```ruby |
| 77 | +tracker.track('test_event', payload: { name: 'test' }) |
| 78 | +``` |
| 79 | + |
| 80 | +### Identifying Users |
| 81 | + |
| 82 | +Create an `IdentifyUser` object and pass it to the `identify` method: |
| 83 | + |
| 84 | +```ruby |
| 85 | +identify_user = OpenPanel::SDK::IdentifyUser.new |
| 86 | +identify_user.profile_id = 'user_123' |
| 87 | +identify_user.email = 'user@example.com' |
| 88 | +identify_user.first_name = 'John' |
| 89 | +identify_user.last_name = 'Doe' |
| 90 | +identify_user.properties = { tier: 'premium', company: 'Acme Inc' } |
| 91 | + |
| 92 | +response = tracker.identify(identify_user) |
| 93 | +``` |
| 94 | + |
| 95 | +### Incrementing Properties |
| 96 | + |
| 97 | +To increment a numeric property on a user profile: |
| 98 | + |
| 99 | +```ruby |
| 100 | +tracker.increment_property(identify_user, 'visits', 1) |
| 101 | +``` |
| 102 | + |
| 103 | +### Decrementing Properties |
| 104 | + |
| 105 | +To decrement a numeric property on a user profile: |
| 106 | + |
| 107 | +```ruby |
| 108 | +tracker.decrement_property(identify_user, 'credits', 1) |
| 109 | +``` |
| 110 | + |
| 111 | +### Filtering Events |
| 112 | + |
| 113 | +Filters are used to prevent sending events to OpenPanel in certain cases. You can filter events by passing a `filter` lambda to the `track` method: |
| 114 | + |
| 115 | +```ruby |
| 116 | +filter = lambda { |payload| |
| 117 | + # Return true to send the event, false to skip it |
| 118 | + payload[:name] == 'test' |
| 119 | +} |
| 120 | + |
| 121 | +response = tracker.track('test_event', payload: { name: 'test' }, filter: filter) |
| 122 | +# If filter returns false, response will be nil |
| 123 | +``` |
| 124 | + |
| 125 | +## Rails Integration |
| 126 | + |
| 127 | +### Setting up the Tracker |
| 128 | + |
| 129 | +Add the following to your `application_controller.rb`: |
| 130 | + |
| 131 | +```ruby |
| 132 | +before_action :set_openpanel_tracker |
| 133 | + |
| 134 | +protected |
| 135 | + |
| 136 | +def set_openpanel_tracker |
| 137 | + @openpanel_tracker = OpenPanel::SDK::Tracker.new( |
| 138 | + { env: Rails.env.to_s }, |
| 139 | + disabled: Rails.env.development? |
| 140 | + ) |
| 141 | + @openpanel_tracker.set_header 'x-client-ip', request.ip |
| 142 | + @openpanel_tracker.set_header 'user-agent', request.user_agent |
| 143 | +end |
| 144 | +``` |
| 145 | + |
| 146 | +### Tracking Events in Controllers |
| 147 | + |
| 148 | +Use `@openpanel_tracker` in your controllers to track events: |
| 149 | + |
| 150 | +```ruby |
| 151 | +def create |
| 152 | + @user = User.create(user_params) |
| 153 | + @openpanel_tracker.track('user_created', payload: { user_id: @user.id }) |
| 154 | + redirect_to @user |
| 155 | +end |
| 156 | +``` |
| 157 | + |
| 158 | +### Identifying Users |
| 159 | + |
| 160 | +Create a helper method to convert your app's user model to an `IdentifyUser`: |
| 161 | + |
| 162 | +```ruby |
| 163 | +def identify_user_from_app_user(user, properties: {}) |
| 164 | + iu = OpenPanel::SDK::IdentifyUser.new |
| 165 | + iu.profile_id = user.id.to_s |
| 166 | + iu.email = user.email |
| 167 | + iu.first_name = user.first_name |
| 168 | + iu.last_name = user.last_name |
| 169 | + iu.properties = properties |
| 170 | + iu |
| 171 | +end |
| 172 | + |
| 173 | +# Usage in controller |
| 174 | +def show |
| 175 | + iu = identify_user_from_app_user(current_user) |
| 176 | + @openpanel_tracker.identify(iu) |
| 177 | +end |
| 178 | +``` |
| 179 | + |
| 180 | +## Advanced Usage |
| 181 | + |
| 182 | +### Setting Custom Headers |
| 183 | + |
| 184 | +You can set custom headers for requests: |
| 185 | + |
| 186 | +```ruby |
| 187 | +tracker.set_header 'x-client-ip', request.ip |
| 188 | +tracker.set_header 'user-agent', request.user_agent |
| 189 | +``` |
| 190 | + |
| 191 | +### Error Handling |
| 192 | + |
| 193 | +The SDK returns a `Faraday::Response` object. Check the response status: |
| 194 | + |
| 195 | +```ruby |
| 196 | +response = tracker.track('event', payload: { name: 'test' }) |
| 197 | +if response&.status == 200 |
| 198 | + puts 'Event tracked successfully' |
| 199 | +else |
| 200 | + puts "Failed to track event: #{response&.status}" |
| 201 | +end |
| 202 | +``` |
| 203 | + |
| 204 | +### Disabling Tracking |
| 205 | + |
| 206 | +You can disable tracking during initialization or in specific environments: |
| 207 | + |
| 208 | +```ruby |
| 209 | +# Disable during initialization |
| 210 | +tracker = OpenPanel::SDK::Tracker.new({}, disabled: true) |
| 211 | + |
| 212 | +# Or disable in development |
| 213 | +tracker = OpenPanel::SDK::Tracker.new( |
| 214 | + { env: Rails.env.to_s }, |
| 215 | + disabled: Rails.env.development? |
| 216 | +) |
| 217 | +``` |
| 218 | + |
0 commit comments