integration-laravel
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePostHog integration for Laravel
Laravel应用的PostHog集成
This skill helps you add PostHog analytics to Laravel applications.
本技能可帮助你为Laravel应用添加PostHog分析功能。
Workflow
工作流程
Follow these steps in order to complete the integration:
- - PostHog Setup - Begin ← Start here
basic-integration-1.0-begin.md - - PostHog Setup - Edit
basic-integration-1.1-edit.md - - PostHog Setup - Revise
basic-integration-1.2-revise.md - - PostHog Setup - Conclusion
basic-integration-1.3-conclude.md
按照以下步骤完成集成:
- - PostHog设置 - 开始 ← 从此处开始
basic-integration-1.0-begin.md - - PostHog设置 - 编辑
basic-integration-1.1-edit.md - - PostHog设置 - 修改
basic-integration-1.2-revise.md - - PostHog设置 - 总结
basic-integration-1.3-conclude.md
Reference files
参考文件
- - Laravel example project code
references/EXAMPLE.md - - Laravel - docs
references/laravel.md - - Identify users - docs
references/identify-users.md - - PostHog setup - begin
references/basic-integration-1.0-begin.md - - PostHog setup - edit
references/basic-integration-1.1-edit.md - - PostHog setup - revise
references/basic-integration-1.2-revise.md - - PostHog setup - conclusion
references/basic-integration-1.3-conclude.md
The example project shows the target implementation pattern. Consult the documentation for API details.
- - Laravel示例项目代码
references/EXAMPLE.md - - Laravel - 文档
references/laravel.md - - 用户识别 - 文档
references/identify-users.md - - PostHog设置 - 开始
references/basic-integration-1.0-begin.md - - PostHog设置 - 编辑
references/basic-integration-1.1-edit.md - - PostHog设置 - 修改
references/basic-integration-1.2-revise.md - - PostHog设置 - 总结
references/basic-integration-1.3-conclude.md
示例项目展示了目标实现模式。如需API细节,请查阅相关文档。
Key principles
核心原则
- Environment variables: Always use environment variables for PostHog keys. Never hardcode them.
- Minimal changes: Add PostHog code alongside existing integrations. Don't replace or restructure existing code.
- Match the example: Your implementation should follow the example project's patterns as closely as possible.
- 环境变量:始终使用环境变量存储PostHog密钥,切勿硬编码。
- 最小改动:在现有集成代码旁添加PostHog代码,不要替换或重构现有代码。
- 匹配示例:你的实现应尽可能贴近示例项目的模式。
Framework guidelines
框架指南
- Create a dedicated PostHogService class in app/Services/ - do NOT scatter PostHog::capture calls throughout controllers
- Register PostHog configuration in config/posthog.php using env() for all settings (api_key, host, disabled)
- Do NOT use Laravel's event system or observers for analytics - call capture explicitly where actions occur
- Remember that source code is available in the vendor directory after composer install
- posthog/posthog-php is the PHP SDK package name
- Check composer.json for existing dependencies and autoload configuration before adding new files
- The PHP SDK uses static methods (PostHog::capture, PostHog::identify) - initialize once with PostHog::init()
- PHP SDK methods take associative arrays with 'distinctId', 'event', 'properties' keys - not positional arguments
- 在app/Services/目录下创建专用的PostHogService类 - 不要将PostHog::capture调用分散在各个控制器中
- 在config/posthog.php中注册PostHog配置,所有设置(api_key、host、disabled)均使用env()获取
- 不要使用Laravel的事件系统或观察者来处理分析 - 在操作发生的位置显式调用capture方法
- 请记住,执行composer install后,源代码会存放在vendor目录中
- posthog/posthog-php是PHP SDK的包名
- 添加新文件前,请检查composer.json中的现有依赖和自动加载配置
- PHP SDK使用静态方法(PostHog::capture、PostHog::identify)- 只需通过PostHog::init()初始化一次
- PHP SDK方法接受包含'distinctId'、'event'、'properties'键的关联数组作为参数,而非位置参数
Identifying users
用户识别
Identify users during login and signup events. Refer to the example code and documentation for the correct identify pattern for this framework. If both frontend and backend code exist, pass the client-side session and distinct ID using and headers to maintain correlation.
X-POSTHOG-DISTINCT-IDX-POSTHOG-SESSION-ID在登录和注册事件中识别用户。请参考示例代码和文档,了解适用于该框架的正确识别模式。如果同时存在前端和后端代码,请使用和头传递客户端会话和唯一ID,以保持关联。
X-POSTHOG-DISTINCT-IDX-POSTHOG-SESSION-IDError tracking
错误追踪
Add PostHog error tracking to relevant files, particularly around critical user flows and API boundaries.
为相关文件添加PostHog错误追踪,尤其是关键用户流程和API边界附近的文件。