Here's a step-by-step guide to creating a Laravel Middleware using the Scoby Analytics PHP SDK:
Create a new middleware using Laravel's artisan command:
php artisan make:middleware ScobyAnalyticsMiddleware
In the generated ScobyAnalyticsMiddleware.php
file, import the necessary classes at the top:
use Scoby\Analytics\SDK as ScobySDK;
In the handle
method of the middleware:
public function handle($request, Closure $next)
{
$apiKey = env('SCOBY_API_KEY');
$salt = env('SCOBY_SALT');
$scoby = new ScobySDK($apiKey, $salt);
$ipAddress = $request->ip();
$userAgent = $request->header('User-Agent');
$requestedUrl = $request->fullUrl();
$referringUrl = $request->header('referer');
$client->logPageViewAsync([
'ipAddress' => $ipAddress,
'userAgent' => $userAgent,
'requestedUrl' => $requestedUrl,
'referringUrl' => $referringUrl,
]);
return $next($request);
}
Register your middleware in the Kernel.php
file located in the app/Http
directory. You can either register it as a global middleware or assign it to a specific route/middleware group.
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\ScobyAnalyticsMiddleware::class,
],
];
This will attach the middleware to all routes in the 'web' group. If you want more granular control, you can register it as a route middleware and attach it to specific routes or groups.