定制 nietthijmen/laravel-posthog 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nietthijmen/laravel-posthog

最新稳定版本:v1.0.0

Composer 安装命令:

composer require nietthijmen/laravel-posthog

包简介

Integrate Posthog into your laravel application

README 文档

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.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固