The leanbase-ruby library provides tracking functionality on the server-side for applications built in Ruby.
It uses an internal queue to make calls fast and non-blocking. It also batches requests and flushes asynchronously, making it perfect to use in any part of your web app or other server-side application that needs performance.
Installation
Add this to your Gemfile:
In your app, set your API key before making any calls.
If setting a custom host, make sure to include the protocol (e.g., https://).
You can find your project API key and instance address in your Leanbase project settings.
Debug logging
The log level by default is set to WARN. You can change it to DEBUG if you want to debug the client by running leanbase.logger.level = Logger::DEBUG, where leanbaseis your initialized LeanBase::Client instance.
Capturing events
You can send custom events using capture:
Tip: Use a [object] [verb] format for event names, e.g. project created, user signed up, or invite sent.
Setting event properties
Optionally, include additional information with the event by adding a properties object:
Sending pageviews
If you're aiming for a backend-only implementation of PostHog and won't be capturing events from your frontend, you can send pageviews from your backend like so:
Person profiles and properties
The Ruby SDK captures identified events by default, creating person profiles.
To set person properties, include them when capturing an event:
To capture anonymous events without creating person profiles:
Alias
You can assign multiple distinct IDs to a single user — for example, when a frontend distinct ID is not available on the backend.
Group Analytics
Group analytics let you associate an event with an organization, team, or company.
Capture event and group
Update group properties
The name is a special property which is used in the Leanbase UI for the name of the group. If you don't specify a name property, the group ID will be used instead.
If the optional distinct_id is not provided in the group identify call, it defaults to ${groupType}_${groupKey} (e.g., $company_company_id_in_your_db in the example above). This default behavior will result in each group appearing as a separate person in Leanbase. To avoid this, it's often more practical to use a consistent distinct_id, such as group_identifier.