This is an optional library you can install if you're working with PHP. It uses an internal queue to batch requests, flushes at the end of the request, and optionally does so in an async manner.
Installation
Add the following to composer.json:
And then install the dependencies with the command:
In your app, set your project API key before making any calls.
Note: As a rule of thumb, we do not recommend having API keys in plaintext. Setting it as an environment variable is best.
You can find your project API key and instance address in the project settings page in Leanbase.
Capturing events
You can send custom events using capture:
Tip: We recommend using a [object] [verb] format for your event names, where [object] is the entity that the behavior relates to, and [verb] is the behavior itself. For example, project created, user signed up, or invite sent.
Setting event properties
Optionally, you can include additional information with the event by including a properties object:
Sending page views
If you're aiming for a backend-only implementation of Leanbase and won't be capturing events from your frontend, you can send pageviews from your backend like so:
Person profiles and properties
The PHP SDK captures identified events by default. These create person profiles. To set person properties in these profiles, include them when capturing an event:
For more details on the difference between $set and $set_once, see our person properties docs.
To capture anonymous events without person profiles, set the event's $process_person_profile property to false:
Alias
Sometimes, you want to assign multiple distinct IDs to a single user. This is helpful when your primary distinct ID is inaccessible. For example, if a distinct ID used on the frontend is not available in your backend.
In this case, you can use alias to assign another distinct ID to the same user.
We strongly recommend reading our docs on alias to best understand how to correctly use this method.
Overriding GeoIP properties
By default, a user's GeoIP properties are set using the IP address they use to capture events on the frontend. You may want to override the these properties when evaluating feature flags. A common reason to do this is when you're not using Leanbase on your frontend, so the user has no GeoIP properties.
You can override GeoIP properties by including them in the person_properties parameter when evaluating feature flags. This is useful when you're evaluating flags on your backend and want to use the client's location instead of your server's location.
The following GeoIP properties can be overridden:
$geoip_country_code
$geoip_country_name
$geoip_city_name
$geoip_city_confidence
$geoip_continent_code
$geoip_continent_name
$geoip_latitude
$geoip_longitude
$geoip_postal_code
$geoip_subdivision_1_code
$geoip_subdivision_1_name
$geoip_subdivision_2_code
$geoip_subdivision_2_name
$geoip_subdivision_3_code
$geoip_subdivision_3_name
$geoip_time_zone
Simply include any of these properties in the person_properties parameter alongside your other person properties when calling feature flags.
Group analytics
Group analytics allows you to associate an event with a group (e.g. teams, organizations, etc.). This feature requires a Leanbase-php version of 2.1.0 or above. Read the Group Analytics guide for more information.
Capture an event and associate it with a group
Update properties on a group
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.
Config options
When calling LeanBase::init, there are various configuration options you can set apart from the host. Pass them into your client initialisation like so:
All possible options below:
Attribute | Description |
host Type: String Default: app.Leanbase.com | URL of your Leanbase instance. |
ssl Type: Boolean Default: true | Whether to use SSL for API requests or not |
timeout Type: Integer Default: 10000 | Request timeout in milliseconds |
verify_batch_events_request Type: Boolean Default: true | Whether to verify successful delivery of batch events (true, synchronous) or fire and forget (false, asynchronous) with the lib_curl consumer |
feature_flag_request_timeout_ms Type: Integer Default: 3000 | Request timeout for feature flags in milliseconds |
maximum_backoff_duration Type: Integer Default: 10000 | Request retry backoff. Retries will stop after this duration is hit |
consumer Type: String Default: lib_curl | One of socket, file, lib_curl, and fork_curl. Determines what transport option to use for analytics capture. More details here |
debug Type: Boolean Default: false | Output debug logs or not |