PHPackages                             qodenl/laravel-posthog - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. qodenl/laravel-posthog

ActiveLibrary[API Development](/categories/api)

qodenl/laravel-posthog
======================

Laravel implementation for Posthog

1.3.2(3mo ago)3599.7k↓26.2%6[3 issues](https://github.com/QodeNL/Laravel-Posthog/issues)[3 PRs](https://github.com/QodeNL/Laravel-Posthog/pulls)MITPHPPHP ^8.4CI passing

Since Mar 24Pushed 1w ago2 watchersCompare

[ Source](https://github.com/QodeNL/Laravel-Posthog)[ Packagist](https://packagist.org/packages/qodenl/laravel-posthog)[ Fund](https://www.buymeacoffee.com/christianqode)[ RSS](/packages/qodenl-laravel-posthog/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (10)Dependencies (22)Versions (24)Used By (0)

Laravel Posthog implementation
==============================

[](#laravel-posthog-implementation)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c1b7f7bc0b1edd5b45b01e0391e0a01dd77b92643b5bafdda46cc430f439806b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f516f64654e4c2f6c61726176656c2d706f7374686f672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/QodeNL/laravel-posthog)[![Total Downloads](https://camo.githubusercontent.com/c0040458498871024a21ca8e0297efdcd6a7cf808d996b02c16937c85ccbdd10/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f516f64654e4c2f6c61726176656c2d706f7374686f672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/QodeNL/laravel-posthog)

This package provides a simple integration of Posthog in Laravel applications.

The package covers both Identify as Capture (events) requests which can be triggered manual or automatically using an Event Listener.

You can also easily integrate Feature Flags within your application.

This package uses the [PostHog / posthog-php](https://github.com/PostHog/posthog-php) package. For more information about Posthog, check their [documentation](https://posthog.com/docs).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require qodenl/laravel-posthog
```

You can publish the config file with:

```
php artisan vendor:publish --provider="QodeNL\LaravelPosthog\PosthogServiceProvider"
```

After publishing the content, set your API key and Host in your .env file:

```
POSTHOG_KEY=
POSTHOG_HOST=https://posthog.com
POSTHOG_ENABLED=true
```

Make sure you copy the correct host from Posthog.

Posthog is enabled by default, but you can disable it with the POSTHOG\_ENABLED env variable.

Make sure to disable Posthog for local/testing environments.

Usage
-----

[](#usage)

### Manual events

[](#manual-events)

```
use QodeNL\LaravelPosthog\Facades\Posthog;

Posthog::capture('event name', ['property' => 'value']);
```

### Automatic events

[](#automatic-events)

You can add the `PosthogListener::class` listener to your `EventServiceProvider`. The package will create an capture automatically when the event happens.

By default, all `fillable` attributes from a model (available in the event) will be sent to Posthog as properties.

You can specify which attributes you want to send to Posthog by adding a `PosthogAttributes` property to your Model.

```
public $posthogAttributes = [
    'first_name',
    'last_name',
];
```

Attributes in the `hidden` property will always be ignored.

### Identify

[](#identify)

Events will be sent to Posthog with a unique ID for anonymous users. When the user is recognized (usually on log in), you should trigger the `identify` method to link the unique ID to the user.

You can pass additional information about the user to be stored in his profile.

```
use QodeNL\LaravelPosthog\Facades\Posthog;

Posthog::identify('email@user.com', ['first_name' => 'John', 'last_name' => 'Doe']);
```

### Alias

[](#alias)

If you want to assign a session ID to a user (for example a front-end session ID) you can use the `alias` method.

The Session ID argument will be assigned to the auto-generated ID of the user.

```
Posthog::alias('Session ID here');
```

### Group analytics

[](#group-analytics)

You can associate events with a group (e.g. a company, team or project) by setting the group type and key. Once set, all subsequent `capture` calls will include the group automatically.

```
use QodeNL\LaravelPosthog\Facades\Posthog;

Posthog::setGroup('company', 'company_id_5');

Posthog::capture('page_view', ['url' => '/dashboard']); // includes the group
```

You can set multiple groups at once by chaining the method:

```
Posthog::setGroup('company', 'company_id_5')->setGroup('project', 'project_id_8');
```

#### Group identify

[](#group-identify)

To create a new group, or update properties on a group, by using the `updateOrCreateGroup` method:

```
Posthog::updateOrCreateGroup('company', 'company_id_5', ['name' => 'Acme Inc', 'plan' => 'enterprise']);
```

You can also pass group properties as a third argument to `setGroup`. This will automatically call `updateOrCreateGroup` to sync the group properties with PostHog:

```
Posthog::setGroup('company', 'company_id_5', ['name' => 'Acme Inc', 'plan' => 'enterprise']);
```

For more information about group analytics, check the [Posthog PHP documentation](https://posthog.com/docs/libraries/php#group-analytics).

Feature flags
-------------

[](#feature-flags)

### Laravel Pennant

[](#laravel-pennant)

This package also includes a custom driver for the [Laravel Pennant](https://laravel.com/docs/11.x/pennant) package. With this custom driver, you can use Laravel Pennant and listen to the feature flags set in Posthog.

To use this driver, simply add the following to your .env file. You don't need to create the database migration.

```
PENNANT_STORE=posthog

```

Also, add the store to the `stores` array in `config/pennant.php`:

```
    'stores' => [
        'posthog' => [
            'driver' => 'posthog',
        ],
    ],
```

You can use the Laravel Pennant package as you would normally do. However, some features, like enabling a feature for a user, are not supported by the Posthog driver. A `PosthogFeatureException` will be thrown when you try to use these features.

#### Example: Check if feature is enabled

[](#example-check-if-feature-is-enabled)

```
Laravel\Pennant\Feature::active('myFeatureFlagKey'); // true
```

### Check feature flags

[](#check-feature-flags)

If you don't want to use Laravel Pennant, you can also implement the feature flags using our Facade:

#### Get all feature flags

[](#get-all-feature-flags)

```
use QodeNL\LaravelPosthog\Facades\Posthog;

Posthog::getAllFlags();
```

Get all feature flags with boolean if enabled for user or not.

#### Check if feature is enabled

[](#check-if-feature-is-enabled)

```
use QodeNL\LaravelPosthog\Facades\Posthog;

Posthog::isFeatureEnabled('myFeatureFlagKey');
```

Check if feature is enabled for user. Returns boolean.

#### Get feature flag

[](#get-feature-flag)

```
use QodeNL\LaravelPosthog\Facades\Posthog;

Posthog::getFeatureFlag('myFeatureFlagKey');
```

Get feature flag. Returns `false` if feature is disabled. Returns `true` (or `payload` if set).

#### Optional attributes

[](#optional-attributes)

You can pass `groups`, `personProperties` and `groupProperties` to the isFeatureEnabled and getFeatureFlag functions.

Please check the [Posthog PHP documentation](https://posthog.com/docs/libraries/php#advanced-overriding-server-properties) for more information.

In the Posthog config you can configure if [events](https://posthog.com/docs/libraries/php#method-2-set-send_feature_flags-to-true) should be sent to Posthog and if you want to [evaluate events locally](https://posthog.com/docs/libraries/php#local-evaluation).

### Custom Distinct ID

[](#custom-distinct-id)

By default, the package generates a distinct ID based on the authenticated user ID (with an optional prefix) or a hashed session ID for anonymous users.

If you want full control over the distinct ID, you can register a custom resolver. For example, in your `AppServiceProvider`:

```
use QodeNL\LaravelPosthog\Facades\Posthog;

public function boot(): void
{
    Posthog::resolveDistinctIdUsing(function () {
        return auth()->id() ?? session()->getId();
    });
}
```

### Queue / jobs

[](#queue--jobs)

The capture, identify and alias actions are executed by jobs. Be sure you've enabled and configured [queues](https://laravel.com/docs/10.x/queues) for your applications.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

- Christian Schoenmakers (Qode BV - Netherlands) ()

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

59

—

FairBetter than 98% of packages

Maintenance88

Actively maintained with recent releases

Popularity44

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 88.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~84 days

Recently: every ~97 days

Total

14

Last Release

99d ago

PHP version history (2 changes)1.0.0PHP ^8.0

1.3.0PHP ^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/3298f87386861530218057ea51bcf42c11b23f7f1f663df6b0a843a51a4783b2?d=identicon)[QodeNL](/maintainers/QodeNL)

---

Top Contributors

[![christianschoenmakers](https://avatars.githubusercontent.com/u/14851445?v=4)](https://github.com/christianschoenmakers "christianschoenmakers (48 commits)")[![DarLar57](https://avatars.githubusercontent.com/u/106186122?v=4)](https://github.com/DarLar57 "DarLar57 (3 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")[![jordanhavard](https://avatars.githubusercontent.com/u/67948773?v=4)](https://github.com/jordanhavard "jordanhavard (1 commits)")

---

Tags

laraveleventsanalyticsa b testingfeature-flagsposthogproduct analytics

###  Code Quality

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/qodenl-laravel-posthog/health.svg)

```
[![Health](https://phpackages.com/badges/qodenl-laravel-posthog/health.svg)](https://phpackages.com/packages/qodenl-laravel-posthog)
```

###  Alternatives

[defstudio/telegraph

A laravel facade to interact with Telegram Bots

816333.8k3](/packages/defstudio-telegraph)[simplestats-io/laravel-client

Server-side analytics for Laravel that follows the full funnel from visit to registration to payment, attributed to the channel that drove it. Revenue, MRR, churn and ad-spend profit (ROAS/CAC) per channel. GDPR compliant, ad-blocker proof.

5022.0k](/packages/simplestats-io-laravel-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
