Last active
January 6, 2022 21:38
-
-
Save mkwsra/068df48753e3d98e394ef4f0d354e952 to your computer and use it in GitHub Desktop.
Laravel middleware to store marketing related query string params
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage | |
class Kernel extends HttpKernel | |
{ | |
// ..... | |
// ..... | |
// ..... | |
protected $routeMiddleware = [ | |
// ..... | |
'store-marketing-query-params' => \App\Http\Middleware\StoreMarketingQueryParamsMiddleware::class, | |
]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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