alifahmmed/metrics-tracker
Composer 安装命令:
composer require alifahmmed/metrics-tracker
包简介
A Laravel helper package for store impression, click of a post with filter and ctr.
README 文档
README
A Laravel package to track impressions, clicks, and CTR (Click-Through Rate) for any model like pages, posts, banners, or other content. Inspired by platforms like Fiverr and Upwork.
✨ Features
- ✅ Track impressions (when content is viewed)
- ✅ Track clicks (when user interacts)
- ✅ Automatically generates CTR (Click-Through Rate)
- ✅ Supports polymorphic relationships
- ✅ Artisan command to clean old records
📦 Installation
composer require alifahmmed/metrics-tracker
Then publish the migration:
php artisan vendor:publish --tag=public php artisan migrate
🔧 Setup
🎮 1. How to Use in Controller You can easily integrate metrics tracking (impressions and clicks) directly in your controllers.
👁️ Store Impressions (e.g., in index() method) To track impressions when showing a list of items:
use AlifAhmmed\MetricsTracker\Helpers\MetricsTracker; $data = YourModel::all(); // Replace with your model MetricsTracker::trackImpressionsForCollection($data);
This will:
Store an impression only once per user/IP per day
Automatically attach a metric_token to each model for tracking clicks
🖱️ Store Clicks (e.g., in show() method) To track clicks when a user views or interacts with a single item:
use AlifAhmmed\MetricsTracker\Helpers\MetricsTracker; $data = YourModel::findOrFail($id); // Replace with your model MetricsTracker::trackClickAndGenerateToken($data);
This will:
Store a click only if an impression exists for today
Attach the metric_token to the model for reference
🔍 2. Filter Metrics by Days You can filter impressions and clicks by a specific range of days using this snippet:
$request->validate([ 'days' => 'required|in:1,7,14,30,60,90,180,365', ]); $days = $request->days; $metrics = MetricsTracker::getMetricsByDays($days);
Accepted values for days: 1, 7, 14, 30, 60, 90, 180, 365
This will return all metrics grouped by:
trackable_type
type (impression or click)
date
📈 3. Calculate CTR (Click-Through Rate) To calculate CTR for a specific model, ID, and day range, use:
use App\Models\YourModel; $ctr = MetricsTracker::calculateCTR(YourModel::class, $trackableId, 30);
Replace YourModel::class with your actual model class.
Replace $trackableId with the model ID.
Replace 30 with any day value (1, 7, 14, 30, 60, 90, 180, 365).
If there are no impressions, the CTR will return 0.
📊 How It Works 👁️ Impression is tracked when an element becomes visible in the viewport.
🖱️ Click is tracked when an element is clicked.
📈 CTR (Click-Through Rate) is calculated based on total clicks / total impressions * 100.
🛠 Artisan Commands Clean up metrics older than 90 days (adjustable):
php artisan metrics:clean
🧪 Example Usage (Controller or API) If you're building a SPA or headless app, you can also generate tokens server-side:
use AlifAhmmed\MetricsTracker\Helpers\MetricsTracker; $token = MetricsTracker::generateMetricToken($post);
🧑💻 Author S M Alif Ahmmed
⚖ License MIT License. Use it freely in personal and commercial projects.
⭐ Like it? Give it a ⭐ on GitHub and share it with your Laravel friends!
alifahmmed/metrics-tracker 适用场景与选型建议
alifahmmed/metrics-tracker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「helper」 「laravel」 「activity」 「count」 「click」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alifahmmed/metrics-tracker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alifahmmed/metrics-tracker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alifahmmed/metrics-tracker 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Datetime helpers for timezone handling
Library with classes to help you do batch.
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
HTML and form generation
Simple ASCII output of array data
统计信息
- 总下载量: 24
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-20