guiassemany/laravel-utm-forwarder

追踪原始的UTM参数

1.0.0 2021-01-29 15:59 UTC

This package is auto-updated.

Last update: 2024-09-08 10:15:27 UTC


README

Latest Version on Packagist Total Downloads

跨域分析很困难。这个包可以帮助您追踪访客的原始UTM参数、引用头和其他分析参数。然后您可以提交这些参数与表单提交一起,或将它们添加到您跟踪的另一个域的链接中。

安装

您可以通过Composer安装此包

composer require guiassemany/laravel-utm-forwarder

该包通过一个中间件工作,需要添加到您的kernel.php文件中的web栈。请确保在StartSession中间件之后注册此中间件。

// app/Http/Kernel.php

protected $middlewareGroups = [
    'web' => [
        // ...
        \Illuminate\Session\Middleware\StartSession::class,
        // ...

        \GuiAssemany\UtmForwarder\Middleware\TrackAnalyticsParametersMiddleware::class,
    ],
];

要配置要追踪的参数或它们在URL参数上的映射方式,您可以使用以下命令发布配置文件:

php artisan vendor:publish --provider="GuiAssemany\UtmForwarder\UtmForwarderServiceProvider"

这是发布配置文件的内容

return [
    /*
     * 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 `\GuiAssemany\UtmForwarder\Sources` namespace.
     */
    'tracked_parameters' => [
        [
            'key' => 'utm_source',
            'source' => GuiAssemany\UtmForwarder\Sources\RequestParameter::class,
        ],
        [
            'key' => 'utm_medium',
            'source' => GuiAssemany\UtmForwarder\Sources\RequestParameter::class,
        ],
        [
            'key' => 'utm_campaign',
            'source' => GuiAssemany\UtmForwarder\Sources\RequestParameter::class,
        ],
        [
            'key' => 'utm_term',
            'source' => GuiAssemany\UtmForwarder\Sources\RequestParameter::class,
        ],
        [
            'key' => 'utm_content',
            'source' => GuiAssemany\UtmForwarder\Sources\RequestParameter::class,
        ],
        [
            'key' => 'referer',
            'source' => GuiAssemany\UtmForwarder\Sources\CrossOriginRequestHeader::class,
        ],
    ],

    /**
     * We'll put the tracked parameters in the session using this key.
     */
    'session_key' => 'tracked_analytics_parameters',

    /*
     * 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',
    ],
];

用法

检索追踪参数的最简单方法是解析TrackedAnalyticsParameters类

use GuiAssemany\UtmForwarder\AnalyticsBag;

app(AnalyticsBag::class)->get(); // returns an array of tracked parameters

您还可以使用追踪参数装饰现有的URL。这在将分析转发到您运行的另一个域非常有用。

<a href="{{ app(\GuiAssemany\UtmForwarder\AnalyticsTracker::class)->decorateUrl('https://testing.com/') }}">
    Buy this product on our webshop
</a>

Will link to https://testing.com?utm_source=facebook&utm_campaign=blogpost

测试

composer test

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

贡献

有关详细信息,请参阅CONTRIBUTING

致谢

  • 此包由spatie和Alex Vanderbist及所有贡献者构建。
  • 我只是把它放在这里,因为我想让它出现在packagist上,并使其通过composer可用。

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件