PHPackages                             nietthijmen/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. nietthijmen/laravel-posthog

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nietthijmen/laravel-posthog
===========================

Integrate Posthog into your laravel application

v1.0.0(3mo ago)10[2 PRs](https://github.com/NietThijmen/laravel-posthog/pulls)MITPHPPHP ^8.1CI passing

Since Mar 10Pushed 2w agoCompare

[ Source](https://github.com/NietThijmen/laravel-posthog)[ Packagist](https://packagist.org/packages/nietthijmen/laravel-posthog)[ Docs](https://github.com/nietthijmen/laravel-posthog)[ Fund](https://www.buymeacoffee.com/nietthijmen)[ RSS](/packages/nietthijmen-laravel-posthog/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (3)Dependencies (16)Versions (9)Used By (0)

Laravel PostHog
===============

[](#laravel-posthog)

[![Latest Version on Packagist](https://camo.githubusercontent.com/bb312ba7ccb0689a7c596b51db133c02660b67359561056b82a9d830950e20a5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e6965747468696a6d656e2f6c61726176656c2d706f7374686f672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nietthijmen/laravel-posthog)[![GitHub Tests Action Status](https://camo.githubusercontent.com/f8468d0de8923aef5b57bfee32cce4aedcc23eba64e9484b353571b0b9686877/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6e6965747468696a6d656e2f6c61726176656c2d706f7374686f672f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/nietthijmen/laravel-posthog/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/7d197f42ffc9a524ef0daab342ab1692c42cc13da1ba24f2b3e72fb7ba509318/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6e6965747468696a6d656e2f6c61726176656c2d706f7374686f672f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/nietthijmen/laravel-posthog/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/eb9b530cf550024648731944e3af303c1df0010d5d058e3be0ee4e626738140a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e6965747468696a6d656e2f6c61726176656c2d706f7374686f672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nietthijmen/laravel-posthog)

Integrate PostHog product analytics into your Laravel application. We can handle exception handling, pushing events from your user and more.

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

[](#installation)

You can install the package via composer:

```
composer require nietthijmen/laravel-posthog
```

You can then install all package parts using

```
php artisan posthog:install
```

Usage
-----

[](#usage)

Events
------

[](#events)

You can use the package to push events to PostHog. For example, you can push an event when a user logs in:

```
use NietThijmen\LaravelPostHog\LaravelPosthog;
LaravelPosthog::capture(
    distinctId: LaravelPosthog::getAuthIdentifier(),
    event: 'User Logged In',
    properties: [
        'email' => auth()->user()->email,
    ]
);
```

There is a shorthand trait for this as well, which you can use in your User model:

```
use NietThijmen\LaravelPostHog\Traits\HasEvents;
class User extends Authenticatable
{
    use HasEvents;
}
```

Then you can push events like this:

```
use App\Models\User;
$user = User::find(1);
$user->sendEvent(
    event: 'User Logged In',
    properties: [
        'email' => $user->email,
    ]
);
```

Exception Handling
------------------

[](#exception-handling)

You can also use the package to handle exceptions and push them to PostHog. To handle application exception you can use our `CaptureExceptions` Class inside your `bootstrap/app.php` file:

```
use Nietthijmen\LaravelPosthog\Helpers\CaptureExceptions;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        //...
    )
    ->withMiddleware(function (Middleware $middleware): void {
        //...
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        CaptureExceptions::captureExceptions($exceptions);
    })->create();
```

You can also manually capture exceptions and push them to PostHog:

```
use Nietthijmen\LaravelPosthog\LaravelPosthog;
LaravelPosthog::captureException(
    new \Exception("Something went wrong!")
);
```

Feature Flags
-------------

[](#feature-flags)

This package fully integrates with [Pennant](https://laravel.com/docs/12.x/pennant) to provide feature flag support. You can use the `posthog` driver to fetch feature flags from PostHog. To use the `posthog` driver, you need to add it to your `config/pennant.php` configuration file:

```
    'default' => env('PENNANT_STORE', 'posthog'),
    'stores' => [
        'posthog' => [
            'driver' => 'posthog',
        ],
    ],
```

Then you can use the `posthog` driver to check if a feature flag is enabled:

```
use Laravel\Pennant\Feature;
$is_active = Feature::active("My-Test-Feature");

dd($is_active ? "Feature is active" : "Feature is not active");
```

Laravel/AI tracing
------------------

[](#laravelai-tracing)

The package also integrates with [Laravel/AI](https://laravel.com/docs/12.x/ai-sdk) to provide tracing support for your AI interactions. This tracing is done fully automatically and transparent to your AI interactions, so you don't have to do anything to enable it.

Middleware
----------

[](#middleware)

This package has a middleware that should be added to your `bootstrap/app.php` file to automatically capture the authenticated user's distinct id and push it to PostHog:

```
use Nietthijmen\LaravelPosthog\Http\Middleware\WithPosthog;
use \Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        //...
    )
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->web(append: [
            WithPosthog::class,
        ])
    })
    ->withExceptions(function (Exceptions $exceptions): void {

    })->create();
```

Commands
--------

[](#commands)

The package also has 2 commands, `install` and `test`The `install` command will publish the configuration file:

```
php artisan posthog:install
```

The `test` command will send a test event to PostHog to verify that the integration is working correctly:

```
php artisan posthog:test
```

Credits
-------

[](#credits)

- [NietThijmen](https://github.com/NietThijmen)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance89

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.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 ~2 days

Total

2

Last Release

105d ago

Major Versions

v0.0.2 → v1.0.02026-03-13

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/53520119?v=4)[Thijmen](/maintainers/NietThijmen)[@NietThijmen](https://github.com/NietThijmen)

---

Top Contributors

[![NietThijmen](https://avatars.githubusercontent.com/u/53520119?v=4)](https://github.com/NietThijmen "NietThijmen (40 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (4 commits)")

---

Tags

a-b-testingai-metricsanalyticslaravelposthoglaraveleventsInsightsanalyticsfeature-flagsposthogproduct-analysisai-analytics

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.3M42](/packages/spatie-laravel-pdf)[codewithdennis/filament-select-tree

The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.

328482.0k25](/packages/codewithdennis-filament-select-tree)[nativephp/desktop

NativePHP for Desktop

38133.6k8](/packages/nativephp-desktop)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

124581.3k](/packages/worksome-exchange)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3913.7k](/packages/rawilk-profile-filament-plugin)[qodenl/laravel-posthog

Laravel implementation for Posthog

3591.8k](/packages/qodenl-laravel-posthog)

PHPackages © 2026

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