nietthijmen/laravel-posthog

Integrate Posthog into your laravel application

Maintainers

Package info

github.com/NietThijmen/laravel-posthog

Homepage

pkg:composer/nietthijmen/laravel-posthog

Fund package maintenance!

Buy Me A Coffee

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 1

v0.0.2 2026-03-10 17:19 UTC

This package is auto-updated.

Last update: 2026-03-12 06:58:05 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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

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

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

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

This package fully integrates with 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

The package also integrates with Laravel/AI 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

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

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

License

The MIT License (MIT). Please see License File for more information.