Leanbase Help Center

Your Guide to Building Products Customers Love

Home How to capture PHP events?

How to capture PHP events?

You can send custom events using capture:

PHP

Leanbase::capture(array(

'distinctId' => 'distinct_id_of_the_user',

'event' => 'user_signed_up'

));

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:

PHP

Leanbase:::capture(array(

'distinctId' => 'distinct_id_of_the_user',

'event' => 'user_signed_up',

'properties' => array(

'login_type' => 'email',

'is_free_trial' => 'true'

)

));

Sending page views

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:

PHP

Leanbase::capture(array(

'distinctId' => 'distinct_id_of_the_user',

'event' => '$pageview',

'properties' => array(

'$current_url' => 'https://example.com'

)

));

Event ingestion

It's a priority for us that events are fully processed and saved as soon as possible. Typically, events will be usable in queries within a few minutes.

Advanced: Anonymous vs identified events

PostHog captures two types of events: anonymous and identified

Identified events enable you to attribute events to specific users, and attach person properties. They're best suited for logged-in users.

Scenarios where you want to capture identified events are:

  • Tracking logged-in users in B2B and B2C SaaS apps

  • Doing user segmented product analysis

  • Growth and marketing teams wanting to analyze the complete conversion lifecycle

Anonymous events are events without individually identifiable data. They're best suited for web analytics or apps where users aren't logged in.

Scenarios where you want to capture anonymous events are:

  • Tracking a marketing website

  • Content-focused sites

  • B2C apps where users don't sign up or log in

Under the hood, the key difference between identified and anonymous events is that for identified events we create a person profile for the user, whereas for anonymous events we do not.

Important: Due to the reduced cost of processing them, anonymous events can be up to 4x cheaper than identified ones, so we recommended you only capture identified events when needed.

How to capture anonymous events

  • Web

  • Backend

  • Android

  • iOS

  • Flutter

The JavaScript Web SDK captures anonymous events by default. However, this may change depending on your person_profiles config when initializing PostHog:

  1. person_profiles: 'identified_only' (recommended) (default) - Anonymous events are captured by default. PostHog only captures identified events for users where person profiles have already been created.

  2. person_profiles: 'always' - Capture identified events for all events.

For example:

Web

Leanbase.init('phc_CTMqwSYhUFLHhkWWDKZWUkDtFH025P3OwkWfb5h1Y2v', {

api_host: 'https://us.i.leanbase.com',

defaults: '2025-05-24',

person_profiles: 'always'

})

How to capture identified events

  • Web

  • Backend

  • Android

  • iOS

  • Flutter

If you've set the personProfiles config to IDENTIFIED_ONLY (the default option), anonymous events are captured by default. To capture identified events, call any of the following functions:

  • identify()

  • alias()

  • group()

  • setPersonProperties()

  • setPersonPropertiesForFlags()

  • setGroupPropertiesForFlags()

When you call any of these functions, it creates a person profile for the user. Once this profile is created, all subsequent events for this user will be captured as identified events.

Alternatively, you can set personProfiles to ALWAYS to capture identified events by defa