Which features are available in this library?
This is an optional library you can install if you're working with PHP. 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 the following to composer.json:
{"require": {"posthog/posthog-php": "2.1.*"}}
And then install the dependencies with the command: php composer.phar install
In your app, set your API key before making any calls.
PostHog::init("<ph_project_api_key>",array('host' => '<ph_instance_address>') // You can remove this line if you're using app.posthog.com);
Note: As a general rule of thumb, we do not recommend having API keys in plaintext. Setting it as an environment variable would be best.
You can find your key in the 'Project Settings' page in PostHog.
Making calls
Capture
Capture allows you to capture anything a user does within your system, which you can later use in PostHog to find patterns in usage, work out which features to improve or where people are giving up.
A capture call requires:
distinct idwhich uniquely identifies your userevent nameto specify the event
- We recommend naming events with "[noun][verb]", such as
movie playedormovie updated, in order to easily identify what your events mean later on (we know this from experience).
Optionally you can submit:
properties, which is an array with any information you'd like to add
For example:
PostHog::capture(array('distinctId' => 'user:123','event' => 'movie played','properties' => array('movieId' => '123','category' => 'romcom')));
Setting user properties via an event
To set properties on your users via an event, you can leverage the event properties $set and $set_once.
$set
Example
PostHog::capture(array('distinctId' => 'user:123','event' => 'movie played','properties' => array('$set' => array('userProperty' => 'value'))));
Usage
When capturing an event, you can pass a property called $set as an event property, and specify its value to be an object with properties to be set on the user that will be associated with the user who triggered the event.
$set_once
Example
PostHog::capture(array('distinctId' => 'user:123','event' => 'movie played','properties' => array('$set_once' => array('userProperty' => 'value'))));
Usage
$set_once works just like $set, except that it will only set the property if the user doesn't already have that property set.
Identify
We highly recommend reading our section on Identifying users to better understand how to correctly use this method.
Identify lets you add metadata to your users so you can easily identify who they are in PostHog, as well as do things like segment users by these properties.
An identify call requires:
distinct idwhich uniquely identifies your userpropertieswith a dict with any key: value pairs
For example:
PostHog::identify(array('distinctId' => 'user:123','properties' => array('email' => 'john@doe.com','proUser' => false)));
The most obvious place to make this call is whenever a user signs up, or when they update their information.
Alias
To connect whatever a user does before they sign up or log in with what they do after you need to make an alias call. This will allow you to answer questions like "Which marketing channels leads to users churning after a month?" or "What do users do on our website before signing up?"
In a purely back-end implementation, this means whenever an anonymous user does something, you'll want to send a session ID with the capture call. Then, when that users signs up, you want to do an alias call with the session ID and the newly created user ID.
The same concept applies for when a user logs in.
If you're using PostHog in the front-end and back-end, doing the identify call in the frontend will be enough.
An alias call requires:
distinct idthe current unique idaliasthe unique ID of the user before
For example:
PostHog::alias(array('distinctId' => 'user:123','alias' => 'user:12345'));
Sending page views
If you're aiming for a full back-end implementation of PostHog, you can send pageviews from your backend
PostHog::capture(array('distinctId' => 'user:123','event' => '$pageview','properties' => array('$current_url' => 'https://example.com')));
Feature flags
To check if a feature flag is enabled for a given user, use isFeatureEnabled, like so:
if (PostHog::isFeatureEnabled('my-amazing-flag', 'some distinct id')) {// do something here}
Group analytics
PostHog 1.31.0 introduced support for group analytics, which allows you to associate users and events with larger groups (teams, organizations, etc.). This feature requires a posthog-php version of 2.1.0 or above.
Note: This is a paid feature and is not available on the open-source or free cloud plan. Learn more here.
- Capture an event and associate it with a group
PostHog::capture(array('distinctId' => '[distinct id]','event' => 'some event','$groups' => array("company" => "id:5")));
- Update properties on a group
PostHog::groupIdentify(array('groupType' => 'company','groupKey' => 'id:5','properties' => array("company_name" => "Awesome Inc", "employees" => 11)));
Thank you
This library is largely based on the analytics-php package.