-
-
Save irazasyed/544d613149886ee812ab54404687cc0d 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