suarez/laravel-utm-parameter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

suarez/laravel-utm-parameter

Composer 安装命令:

composer require suarez/laravel-utm-parameter

包简介

A little helper to store and handle utm-parameter

README 文档

README

Latest Version on Packagist StyleCI Test PHP 8.x Packagist Downloads GitHub Statamic Addon

A lightweight way to handle UTM parameters session-based in your Laravel Application.

@hasUtm('source', 'newsletter')
  <p>Special Content for Newsletter-Subscriber.</p>
@endhasUtm

Installation

Follow these steps to install the Laravel UTM-Parameters package. Guide for Laravel 10 and below.

Open your terminal and navigate to your Laravel project directory. Then, use Composer to install the package:

$ composer require suarez/laravel-utm-parameter

Optionally, you can publish the config file of this package with this command:

php artisan vendor:publish --tag="utm-parameter"

Middleware Configuration

Once the package is installed, you can add the UtmParameters middleware to your Laravel application. Open the bootstrap/app.php file and append the UtmParameters::class inside the web-group.

# Laravel 11+
return Application::configure(basePath: dirname(__DIR__))
  ...
  ->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [
      Suarez\UtmParameter\Middleware\UtmParameters::class,
      /* ... keep the existing middleware here */
    ]);
  })
  ...

Also, take a look at how to set an alias for the middleware.

Use as Facade

If you prefer not to use it as middleware, you can utilize the UtmParameter Facade directly in your controllers or other parts of your application. Once the boot($request)-method is called, you have access to it at any place. For example:

use Suarez\UtmParameter\Facades\UtmParameter;

// Inside a controller method
class IndexController {
  public function index(Request $request)
  {
      UtmParameter::boot($request);
  }
}

class SomeDifferentController {
  public function index(Request $request)
  {
      $source = UtmParameter::get('source');
  }
}

Configuration

The configuration file config/utm-parameter.php allows you to control the behavior of the UTM parameters handling.

<?php

return [
  /*
   * Control Overwriting UTM Parameters (default: false)
   *
   * This setting determines how UTM parameters are handled within a user's session.
   *
   * - Enabled (true): New UTM parameters will overwrite existing ones during the session.
   * - Disabled (false): The initial UTM parameters will persist throughout the session.
   */
  'override_utm_parameters' => false,

  /*
   * Session Key for UTM Parameters (default: 'utm')
   *
   * This key specifies the name used to access and store UTM parameters within the session data.
   *
   * If you're already using 'utm' for another purpose in your application,
   * you can customize this key to avoid conflicts.
   * Simply provide your preferred key name as a string value.
   */
  'session_key' => 'utm',

  /*
    * Allowed UTM Parameters (default: ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'utm_campaign_id'])
    *
    * This setting defines the UTM parameters that are allowed within your application.
    *
    * In this array, you can specify a list of allowed UTM parameter names. Each parameter should be listed as a string.
    * Only parameters from this list will be stored and processed in the session.
    * and any parameter without the 'utm_' prefix will be ignored regardless of its inclusion in this list.
    *
    * Example: To only allow the basic UTM parameters (source, medium, and campaign), you could update the array like this:
    *
    * 'allowed_utm_parameters' => [
    *     'utm_source',
    *     'utm_medium',
    *     'utm_campaign',
    * ],
    */
    'allowed_utm_parameters' => [
        'utm_source',
        'utm_medium',
        'utm_campaign',
        'utm_term',
        'utm_content',
        'utm_campaign_id'
    ],
];

Usage

get_all_utm()

To get an array of all UTM parameters, use this helper: get_all_utm().

$parameter = get_all_utm();

get_utm()

If you need to retrieve certain UTM parameters, use get_utm('source|medium|campaign|term|content').

 <p>You came from {{ get_utm('source') }}</p>
// Some Task in your Class
public function someTask()
{
  return match(get_utm('source')) {
    'bing' => Bing::class,
    'google' => Google::class,
    'duckduckgo' => DuckDuckGo::class,
    'newsletter' => Newsletter::class,
    default => Default::class
  };
}

// Render a view based on an utm_source
Route::get('/', function () {
  return match(get_utm('source')) {
        'newsletter' => view('newsletter'),
        default => view('welcome')
    };
});

has_utm()

Sometimes you want to show or do something, if user might have some or specific utm-parameters.

Simply use:

  • has_utm('source|medium|campaign|term|content', 'optional-value')
  • has_not_utm('source|medium|campaign|term|content', 'optional-value')
@hasUtm('source', 'corporate-partner')
  <div>Some corporate partner related stuff</div>
@endhasUtm

@hasNotUtm('term')
  <p>You have any term.</p>
@endhasNotUtm
 if (has_utm('campaign', 'special-sale')) {
   redirect('to/special-sale/page');
 }

 if (has_not_utm('campaign', 'newsletter')) {
   session()->flash('Did you know, we have a newsletter?');
 }

contains_utm()

You can conditionally show or perform actions based on the presence or absence of a portion of an UTM parameters using contains.

Simply use:

  • contains_utm('source|medium|campaign|term|content', 'value')
  • contains_not_utm('source|medium|campaign|term|content', 'value')
@containsUtm('campaign', 'weekly')
  <div>Some Weekly related stuff</div>
@endcontainsUtm

@containsNotUtm('campaign', 'sales')
  <p>Some not Sales stuff</p>
@endcontainsNotUtm
 if (contains_utm('campaign', 'weekly')) {
   redirect('to/special/page');
 }

 if (contains_not_utm('campaign', 'sale')) {
   session()->flash('Did you know, we have a newsletter?');
 }

Extending the Middleware

You can extend the middleware to customize the behavior of accepting UTM parameters. For example, you can override the shouldAcceptUtmParameter method.

First, create a new middleware using Artisan:

php artisan make:middleware CustomMiddleware

Then, update the new middleware to extend UtmParameters and override the shouldAcceptUtmParameter method:

<?php

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Suarez\UtmParameter\Middleware\UtmParameters;

class CustomMiddleware extends UtmParameters
{
    /**
     * Determines whether the given request/response pair should accept UTM-Parameters.
     *
     * @param \Illuminate\Http\Request  $request
     *
     * @return bool
     */
    protected function shouldAcceptUtmParameter(Request $request)
    {
        return $request->isMethod('GET') || $request->isMethod('POST');
    }
}

Finally, update your bootstrap/app.php to use the CustomMiddleware:

# bootstrap/app.php
use App\Http\Middleware\CustomMiddleware;

->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [
        CustomMiddleware::class,
        // other middleware...
    ]);
})

Resources

Explore additional use cases and resources on the wiki pages

Inspirations

License

The Laravel UTM-Parameters package is open-sourced software licensed under the MIT license.

suarez/laravel-utm-parameter 适用场景与选型建议

suarez/laravel-utm-parameter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.91k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2022 年 01 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「laravel」 「social-media」 「UTM」 「utm-parameter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 suarez/laravel-utm-parameter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 suarez/laravel-utm-parameter 我们能提供哪些服务?
定制开发 / 二次开发

基于 suarez/laravel-utm-parameter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 16.91k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 28
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-15