adzbuck/laravel-utm
Composer 安装命令:
composer require adzbuck/laravel-utm
包简介
Keeps track of the UTM parameters
关键字:
README 文档
README
####This package is an updated fork of spatie/laravel-utm-forwarder
Keeps track of UTMs and/or other parameters
Support:
- PHP: 8.0, 8.1, 8.2, 8.3, 8.4
- Laravel: 9, 10, 11, 12
This package allows you to easily track first and last touch query parameters and headers via session. You can then easily access these parameters so you can add them to a form submission or a link to another domain you track.
Installation
You can install the package via composer:
composer require adzbuck/laravel-utm
The package works via a middleware that needs to be added to the web stack in your kernel.php file. Make sure to register this middleware after the StartSession middleware.
// app/Http/Kernel.php protected $middlewareGroups = [ 'web' => [ // ... \Illuminate\Session\Middleware\StartSession::class, // ... \Adzbuck\LaravelUTM\Middleware\ParameterTrackerMiddleware::class, ], ];
To configure the tracked parameters or how they're mapped on the URL parameters, you can publish the config file using:
php artisan vendor:publish --provider="Adzbuck\LaravelUTM\ServiceProvider"
This is the contents of the published config file:
use Adzbuck\LaravelUTM\Sources; return [ /** * How the data will be stored */ 'store' => StoreType::Cookie, /* * These are the analytics parameters that will be tracked when a user first visits * the application. The configuration consists of the parameter's key and the * source to extract this key from. * * Available sources can be found in the `\Adzbuck\LaravelUTM\Sources` namespace. */ 'tracked_parameters' => [ [ 'key' => 'utm_source', 'source' => Sources\RequestParameter::class, ], [ 'key' => 'utm_medium', 'source' => Sources\RequestParameter::class, ], [ 'key' => 'utm_campaign', 'source' => Sources\RequestParameter::class, ], [ 'key' => 'utm_term', 'source' => Sources\RequestParameter::class, ], [ 'key' => 'utm_content', 'source' => Sources\RequestParameter::class, ], [ 'key' => 'referer', 'source' => Sources\CrossOriginRequestHeader::class, ], ], /** * The name of the cooke that will be set */ 'cookie_name' => 'utm_params', /** * We'll put the first touch tracked parameters in the session using this key. */ 'first_touch_store_key' => 'laravel_utm_parameters_first', /** * We'll put the last touch tracked parameters in the session using this key. */ 'last_touch_store_key' => 'laravel_utm_parameters_last', /** * If we should keep track of the first touch utm params */ 'first_touch' => true, /** * If we should keep track of the last touch utm params */ 'last_touch' => true, /* * When formatting an URL to add the tracked parameters we'll use the following * mapping to put tracked parameters in URL parameters. * * This is useful when using an analytics solution that ignores the utm_* parameters. */ 'parameter_url_mapping' => [ 'utm_source' => 'utm_source', 'utm_medium' => 'utm_medium', 'utm_campaign' => 'utm_campaign', 'utm_term' => 'utm_term', 'utm_content' => 'utm_content', 'referer' => 'referer', ], ];
Usage
There are three methods of tracking:
- getFirstTouch
- This will get the parameters from the users first visit.
- getLastTouch
- This will get the parameters from the users last visit.
- getCurrent
- This will get the parameters from the current request.
The easiest way to retrieve the tracked parameters is by resolving the ParameterTracker class:
use Adzbuck\LaravelUTM\ParameterTracker; // returns an array of the first touch tracked parameters $parameterTracker = app(ParameterTracker::class) $parameterTracker->getFirstTouch(); $parameterTracker->getLastTouch(); $parameterTracker->getCurrent();
You can also decorate an existing URL with the tracked parameters. This is useful to forward analytics to another domain you're running analytics on.
The example uses three requests:
First request:
https://mywebshop.com/?utm_source=facebook&utm_campaign=blogpost
2nd Request:
https://mywebshop.com/?utm_source=google&utm_campaign=blogpost
Current Request:
https://mywebshop.com/
decorateUrl/@trackedUrl
This does not use the session tracking, it simply uses the params provided.
<?php use Adzbuck\LaravelUTM\DecorateURL; ?> <a href="{{ DecorateURL::decorateUrl('https://mywebshop.com/', ['utm_source' => 'google']) }}"> Buy this product on our webshop </a> -- or -- <a href="@trackedUrl('https://mywebshop.com/', ['utm_source' => 'google'])"> Buy this product on our webshop </a> Will link to https://mywebshop.com?utm_source=google
decorateUrlFromFirstTouch/@trackedUrlFromFirstTouch
This adds the parameters from the users first visit. You can also add extra params via an array as the second parameter.
<a href="{{ DecorateURL::decorateUrlFromFirstTouch('https://mywebshop.com/', ['extra_param' => 'test']) }}">
Buy this product on our webshop
</a>
-- or --
<a href="@trackedUrlFromFirstTouch('https://mywebshop.com/', ['extra_param' => 'test'])">
Buy this product on our webshop
</a>
Will link to https://mywebshop.com?utm_source=facebook&utm_campaign=blogpost&extra_param=test
decorateUrlFromLastTouch/@trackedUrlFromLastTouch
This adds the parameters from the users Last visit. You can also add extra params via an array as the second parameter.
<a href="{{ DecorateURL::decorateUrlFromLastTouch('https://mywebshop.com/', ['extra_param' => 'test']) }}">
Buy this product on our webshop
</a>
-- or --
<a href="@trackedUrlFromLastTouch('https://mywebshop.com/', ['extra_param' => 'test'])">
Buy this product on our webshop
</a>
Will link to https://mywebshop.com?utm_source=facebook&utm_campaign=blogpost&extra_param=test
decorateUrlFromCurrent/@trackedUrlFromCurrent
This adds the parameters from the users Last visit. You can also add extra params via an array as the second parameter.
<a href="{{ DecorateURL::decorateUrlFromCurrent('https://mywebshop.com/', ['extra_param' => 'test']) }}">
Buy this product on our webshop
</a>
-- or --
<a href="@trackedUrlFromCurrent('https://mywebshop.com/', ['extra_param' => 'test'])">
Buy this product on our webshop
</a>
Will link to https://mywebshop.com?utm_source=google&utm_campaign=blogpost&extra_param=test
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
adzbuck/laravel-utm 适用场景与选型建议
adzbuck/laravel-utm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.55k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 05 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「spatie」 「adzbuck」 「laravel-utm」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 adzbuck/laravel-utm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 adzbuck/laravel-utm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 adzbuck/laravel-utm 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Receive webhooks in Laravel apps
Easily optimize images using PHP
Keeps track of the UTM parameters
Store your application settings
Add tags and taggable behaviour to your Laravel app
Speed up a Laravel application by caching the entire response additions
统计信息
- 总下载量: 2.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-05-25