temperbit/larahog 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

temperbit/larahog

最新稳定版本:v1.0.0

Composer 安装命令:

composer require temperbit/larahog

包简介

The Laravel-native PostHog experience.

README 文档

README

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

The Laravel-native PostHog experience. LaraHog wraps the PostHog PHP SDK into a first-class Laravel package with multi-connection support, queue-based dispatch, and Octane compatibility out of the box.

Requirements

  • PHP 8.3+
  • Laravel 11, 12, or 13

Support us

We invest a lot of resources into creating awesome software. You can support us by sponsoring us on GitHub.

Installation

composer require temperbit/larahog

Publish the config file:

php artisan vendor:publish --tag="larahog-config"

Add your PostHog project token to .env:

POSTHOG_PROJECT_TOKEN=phc_your_project_token

Configuration

The published config file will be at config/larahog.php.

Variable Default Description
POSTHOG_CONNECTION default Default connection name
POSTHOG_ENABLED true Enable/disable the default connection
POSTHOG_PROJECT_TOKEN "" Your PostHog project API key
POSTHOG_HOST https://us.i.posthog.com PostHog instance URL
POSTHOG_DISPATCH_MODE sync sync or queue
POSTHOG_QUEUE_CONNECTION null Laravel queue connection (when using queue mode)
POSTHOG_QUEUE_NAME default Laravel queue name (when using queue mode)

Multi-connection support

You can define multiple PostHog connections for different projects:

// config/larahog.php
'connections' => [
    'default' => [
        'project_token' => env('POSTHOG_PROJECT_TOKEN'),
        // ...
    ],
    'marketing' => [
        'project_token' => env('POSTHOG_MARKETING_TOKEN'),
        // ...
    ],
],

Then target a specific connection:

LaraHog::connection('marketing')->capture('user-123', 'campaign_clicked');

Usage

Capturing events

use TemperBit\LaraHog\Facades\LaraHog;

// Basic event
LaraHog::capture('user-123', 'page_viewed');

// With properties
LaraHog::capture('user-123', 'purchase_completed', [
    'amount' => 49.99,
    'currency' => 'USD',
]);

// With group association
LaraHog::capture('user-123', 'report_exported', [], [
    'company' => 'company-456',
]);

// Anonymous event
LaraHog::capture(null, 'landing_page_viewed');

Identifying users

LaraHog::identify('user-123', [
    'name' => 'Jane Doe',
    'email' => 'jane@example.com',
    'plan' => 'enterprise',
]);

Aliasing identities

LaraHog::alias('user-123', 'anonymous-session-abc');

Group identities

LaraHog::groupIdentify('company', 'company-456', [
    'name' => 'Acme Corp',
    'industry' => 'SaaS',
]);

Flushing

LaraHog automatically flushes pending events when the application terminates. You can also flush manually:

LaraHog::flush();    // Flush the default connection
LaraHog::flushAll(); // Flush all connections

Checking status

if (LaraHog::isEnabled()) {
    // ...
}

Dispatch Modes

Sync (default)

Events are buffered in memory by the PostHog SDK and sent in batches at the end of the request lifecycle. This is the simplest setup and works well for most applications.

POSTHOG_DISPATCH_MODE=sync

Queue

Events are dispatched to a Laravel queue for asynchronous processing. This moves PostHog API calls out of the request path entirely.

POSTHOG_DISPATCH_MODE=queue
POSTHOG_QUEUE_CONNECTION=redis
POSTHOG_QUEUE_NAME=analytics

Octane Compatibility

LaraHog uses a PostHog\Client instance (not the static PostHog::init() method), so each request gets a clean state. Multi-connection support is handled through the LaraHogManager singleton, which lazily resolves connections. This design is safe to use with Laravel Octane.

Artisan Commands

larahog:status

Displays the current configuration and tests connectivity to PostHog:

php artisan larahog:status
php artisan larahog:status --connection=marketing

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固