Skip to content

Instantly share code, notes, and snippets.

@mkwsra
Last active January 6, 2022 21:38
Show Gist options
  • Save mkwsra/068df48753e3d98e394ef4f0d354e952 to your computer and use it in GitHub Desktop.
Save mkwsra/068df48753e3d98e394ef4f0d354e952 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