Skip to content

Instantly share code, notes, and snippets.

@irazasyed
Forked from mkwsra/Kernel.php
Created January 6, 2022 21:38
Show Gist options
  • Save irazasyed/544d613149886ee812ab54404687cc0d to your computer and use it in GitHub Desktop.
Save irazasyed/544d613149886ee812ab54404687cc0d to your computer and use it in GitHub Desktop.
Laravel middleware to store marketing related query string params
// Usage
class Kernel extends HttpKernel
{
// .....
// .....
// .....
protected $routeMiddleware = [
// .....
'store-marketing-query-params' => \App\Http\Middleware\StoreMarketingQueryParamsMiddleware::class,
];
}
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class StoreMarketingQueryParamsMiddleware
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$utmQueryParams = [
'utm_source',
'utm_medium',
'utm_campaign',
'utm_term',
'utm_content',
];
foreach ($utmQueryParams as $utmQueryParam) {
if ($request->has($utmQueryParam)) {
session()->put($utmQueryParam, $request->input($utmQueryParam));
}
}
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment