nietthijmen/laravel-posthog
Composer 安装命令:
composer require nietthijmen/laravel-posthog
包简介
Integrate Posthog into your laravel application
README 文档
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.
nietthijmen/laravel-posthog 适用场景与选型建议
nietthijmen/laravel-posthog 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「analytics」 「events」 「laravel」 「Insights」 「feature-flags」 「posthog」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nietthijmen/laravel-posthog 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nietthijmen/laravel-posthog 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nietthijmen/laravel-posthog 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Библиотека для реализаций паттерна Pub/Sub
Wireless Cross-Component Communication
A powerful event calendar Tool for Laravel's Nova 5.
Analytics chooser extensions for site settings.
Command bus implementation: Commands and domain events
A Laravel Nova Card to show Fathom Analytics stats.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-10