tourze/user-track-bundle
Composer 安装命令:
composer require tourze/user-track-bundle
包简介
A Symfony bundle for tracking and recording user behavior across your application
README 文档
README
A Symfony bundle for tracking and recording user behavior across your application. Provides automatic event tracking, manual tracking via JSON-RPC, and built-in log rotation.
Table of Contents
- Features
- Requirements
- Installation
- Quick Start
- Advanced Usage
- Configuration
- JSON-RPC API Reference
- Use Cases
- Testing
- Security
- Contributing
- License
Features
- Automatic Event Tracking: Automatically captures
UserInteractionEventevents - Asynchronous Storage: Uses async insertion to avoid impacting main business performance
- JSON-RPC Interface: Manual behavior tracking through RPC endpoint
- Automatic Log Cleanup: Built-in scheduled cleanup of expired logs (configurable retention period)
- Customer Radar Support: Provides data foundation for CRM customer radar functionality
- Flexible Event Parameters: Support for custom tracking parameters through interfaces
- Distributed Lock Support: Prevents duplicate submissions in distributed environments
Requirements
- PHP 8.1+
- Symfony 6.4+
- tourze/user-event-bundle
- tourze/doctrine-snowflake-bundle
- tourze/doctrine-ip-bundle
Installation
composer require tourze/user-track-bundle
Quick Start
1. Register the Bundle
// config/bundles.php return [ // ... Tourze\UserTrackBundle\UserTrackBundle::class => ['all' => true], ];
2. Automatic Event Tracking
Any UserInteractionEvent dispatched in your application will be automatically tracked:
use Tourze\UserEventBundle\Event\UserInteractionEvent; // This event will be automatically recorded $event = new UserInteractionEvent($sender, $systemUser, 'user.login'); $eventDispatcher->dispatch($event);
3. Manual Tracking via JSON-RPC
// Frontend JavaScript const response = await jsonRpcClient.call('SubmitCrmTrackLog', { event: 'page.view', params: { page: '/products/123', referrer: 'google.com', duration: 30 } }); // Response: { time: 1234567890 } // Backend PHP $result = $jsonRpcClient->call('SubmitCrmTrackLog', [ 'event' => 'order.completed', 'params' => [ 'order_id' => '12345', 'amount' => 99.99 ] ]);
Advanced Usage
Custom Tracking Parameters
Implement TrackContextInterface in your events to provide additional tracking parameters:
use Tourze\UserEventBundle\Event\UserInteractionEvent; use Tourze\UserTrackBundle\Event\TrackContextInterface; class ProductViewEvent extends UserInteractionEvent implements TrackContextInterface { /** @var array<string, mixed> */ private array $trackParams = []; public function __construct($user, $product) { parent::__construct($user, $systemUser, 'product.view'); $this->setTrackingParams([ 'product_id' => $product->getId(), 'category' => $product->getCategory(), 'price' => $product->getPrice() ]); } /** * @return array<string, mixed> */ public function getTrackingParams(): array { return $this->trackParams; } /** * @param array<string, mixed> $params */ public function setTrackingParams(array $params): void { $this->trackParams = $params; } }
Extending RPC Response
Listen to TrackLogReportEvent to modify the RPC response:
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Tourze\UserTrackBundle\Event\TrackLogReportEvent; class TrackLogResponseEnricher implements EventSubscriberInterface { public static function getSubscribedEvents() { return [ TrackLogReportEvent::class => 'onTrackLogReport', ]; } public function onTrackLogReport(TrackLogReportEvent $event) { $result = $event->getResult(); $result['tracking_id'] = $event->getTrackLog()->getId(); $event->setResult($result); } }
Configuration
Environment Variables
# .env # Number of days to keep track logs (default: 30) CLEAN_TRACK_LOG_DAY_NUM=90
Automatic Cleanup
Logs are automatically cleaned up daily at 2:20 AM. The retention period can be configured
via the CLEAN_TRACK_LOG_DAY_NUM environment variable.
JSON-RPC API Reference
SubmitCrmTrackLog
Submit a user behavior tracking log.
Parameters:
event(string, required): Event name (e.g., "page.view", "button.click")params(array, optional): Event parameters as key-value pairs
Response:
{
"time": 1234567890
}
Authentication: Requires authenticated user (IS_AUTHENTICATED)
Use Cases
- User Behavior Analytics: Track page views, clicks, feature usage
- Customer Radar: Real-time customer activity tracking for CRM
- Marketing Automation: Trigger marketing campaigns based on user behavior
- Security Auditing: Record audit logs for sensitive operations
- Product Optimization: Analyze user behavior data to improve features
Testing
Run tests from the monorepo root:
# Run all tests ./vendor/bin/phpunit packages/user-track-bundle/tests # Run specific test ./vendor/bin/phpunit packages/user-track-bundle/tests/Entity/TrackLogTest.php
Test Coverage
- Entity tests - Complete coverage of TrackLog entity
- Event tests - TrackLogReportEvent and TrackContext interface
- Event Subscriber tests - UserTrackListener behavior
- RPC Procedure tests - SubmitCrmTrackLog endpoint
- DI Extension tests - Service registration and configuration
- Bundle tests - Bundle initialization
Security
Authentication
All JSON-RPC endpoints require authenticated users. User tracking data is stored with IP address information for security auditing purposes.
Data Protection
- User tracking data is automatically cleaned up based on configured retention period
- IP addresses are stored for security purposes and follow the same retention policy
- All user input is validated before storage
Best Practices
- Review tracking parameters to avoid storing sensitive information
- Use appropriate retention periods for compliance requirements
- Monitor tracking data for unusual patterns that might indicate security issues
Contributing
Contributions are welcome! Please ensure:
- All tests pass
- Code follows PSR-12 standards
- New features include tests
- Documentation is updated
License
The MIT License (MIT). Please see License File for more information.
tourze/user-track-bundle 适用场景与选型建议
tourze/user-track-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tourze/user-track-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tourze/user-track-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 18
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-17