Openpanel::SDK
OpenPanel SDK for Ruby
Installation
Install the gem and add to the application's Gemfile by executing:
bundle add openpanel-sdkIf bundler is not being used to manage dependencies, install the gem by executing:
gem install openpanel-sdkUsage example
Simple example
tracker = OpenPanel::SDK::Tracker.new
identify_user = OpenPanel::SDK::IdentifyUser.new
# ... set user props
tracker.identify identify_user
tracker.track 'test_event', payload: { name: 'test' }
tracker.increment_property identify_user, 'test_property', 1
tracker.decrement_property identify_user, 'test_property', 1See spec for more.
Rails example
Now imagine you have a Rails app, you can add the following to your application_controller.rb:
before_action :set_openpanel_tracker
protected
def set_openpanel_tracker
@openpanel_tracker = OpenPanel::SDK::Tracker.new({ env: Rails.env.to_s }, disabled: Rails.env.development?)
@openpanel_tracker.set_header "x-client-ip", request.ip
@openpanel_tracker.set_header "user-agent", request.user_agent
endthen you can use @openpanel_tracker in your controllers to track events:
@openpanel_tracker.track 'test_event', payload: { name: 'test' }or to identify users:
def identify_user_from_app_user(user, properties: {})
iu = OpenPanel::SDK::IdentifyUser.new
iu.profile_id = user.id.to_s
iu.email = user.email
iu.first_name = user.first_name
iu.last_name = user.last_name
iu.properties = properties
iu
end
iu = identify_user_from_app_user current_user
response = @openpanel_tracker.identify iu
# Faraday::ResponseDon't forget to set your env vars in .env file:
OPENPANEL_TRACK_URL=https://api.openpanel.dev/track
OPENPANEL_CLIENT_ID=<YOUR_CLIENT_ID>
OPENPANEL_CLIENT_SECRET=<YOUR_CLIENT_SECRET>
as shown in .env_sample
Filtering events
Filters are used to prevent sending events to OpenPanel in certain cases.
You can filter events by passing a filter lambda expression to the track method:
filter = lambda { |payload|
true if payload[:name] == 'test'
}
response = tracker.track('test_event', payload: { name: 'test' }, filter: filter)
# response is nilContributing
Bug reports and pull requests are welcome on GitHub at https://github.com/tstaetter/openpanel-sdk.
License
The gem is available as open source under the terms of the MIT License.