nietthijmen / laravel-posthog
Integrate Posthog into your laravel application
Fund package maintenance!
Requires
- php: ^8.1
- illuminate/contracts: ^11.0||^12.0
- posthog/posthog-php: ^3.7
- spatie/backtrace: ^1.8
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/ai: ^0.2.6
- laravel/pennant: *
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
Suggests
- laravel/ai: Use Laravel AI to easily send insights to Posthog using AI
- laravel/pennant: Use pennant to easily manage feature flags in your application
This package is auto-updated.
Last update: 2026-03-12 06:58:05 UTC
README
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.