adzbuck / laravel-utm
跟踪 UTM 参数
v2.0.9
2023-05-26 22:16 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^9.0|^10.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.16
- orchestra/testbench: ^6.27|^7.9|^8.4
- phpunit/phpunit: ^10.0
- vimeo/psalm: ^5.9
README
#### 此包是基于 spatie/laravel-utm-forwarder 更新后的分支
跟踪 UTMs 和/或其他参数
此包允许您通过会话轻松跟踪首次和最后接触的查询参数和头部信息。然后您可以轻松访问这些参数,以便将它们添加到表单提交或您正在跟踪的另一个域的链接中。
安装
您可以通过 composer 安装此包。
composer require adzbuck/laravel-utm
此包通过中间件工作,需要将其添加到 kernel.php
文件中的 web
栈。请确保在 StartSession
中间件之后注册此中间件。
// app/Http/Kernel.php protected $middlewareGroups = [ 'web' => [ // ... \Illuminate\Session\Middleware\StartSession::class, // ... \Adzbuck\LaravelUTM\Middleware\ParameterTrackerMiddleware::class, ], ];
要配置跟踪的参数或它们在 URL 参数上的映射方式,您可以使用以下命令发布配置文件:
php artisan vendor:publish --provider="Adzbuck\LaravelUTM\ServiceProvider"
这是发布配置文件的内容
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', ], ];
用法
有三种跟踪方法
- getFirstTouch
- 这将获取用户首次访问的参数。
- getLastTouch
- 这将获取用户最后访问的参数。
- getCurrent
- 这将获取当前请求的参数。
检索跟踪参数的最简单方法是通过解析 ParameterTracker
类
use Adzbuck\LaravelUTM\ParameterTracker; // returns an array of the first touch tracked parameters $parameterTracker = app(ParameterTracker::class) $parameterTracker->getFirstTouch(); $parameterTracker->getLastTouch(); $parameterTracker->getCurrent();
您还可以使用跟踪参数装饰现有的 URL。这有助于将分析转发到您运行的另一个域。
示例使用三个请求
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
这不会使用会话跟踪,它只是使用提供的参数。
<?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
这添加了用户首次访问的参数。您也可以通过数组作为第二个参数添加额外的参数。
<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
这添加了用户最后访问的参数。您也可以通过数组作为第二个参数添加额外的参数。
<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
这添加了用户最后访问的参数。您也可以通过数组作为第二个参数添加额外的参数。
<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
测试
composer test
更新日志
请参阅 更新日志 以获取有关最近更改的更多信息。
致谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。