Popular Frameworks
Integrating Scoby Analytics with Laravel
Here's a step-by-step guide to creating a Laravel Middleware using the Scoby Analytics PHP SDK:
Prerequisites
You need your License Key and Salt to use Scoby Analytics in Laravel.
Setting up Environment Variables in Laravel
Open the
.envfile located in the root directory of your Laravel project.Add the following lines:
SCOBY_API_KEY=your_api_key_here SCOBY_SALT=your_generated_salt_hereReplace
your_api_key_herewith the License Key you obtained earlier andyour_generated_salt_herewith the salt you generated.In your Laravel application, you can access these values using the
envfunction:$apiKey = env('SCOBY_API_KEY'); $salt = env('SCOBY_SALT');
Creating the Laravel Middleware
Create a new middleware using Laravel's artisan command:
php artisan make:middleware ScobyAnalyticsMiddlewareIn the generated
ScobyAnalyticsMiddleware.phpfile, import the necessary classes at the top:use Scoby\Analytics\SDK as ScobySDK;In the
handlemethod of the middleware:public function handle($request, Closure $next) { $apiKey = env('SCOBY_API_KEY'); $salt = env('SCOBY_SALT'); $scoby = new ScobySDK($apiKey, $salt); // Collect necessary data $ipAddress = $request->ip(); $userAgent = $request->header('User-Agent'); $requestedUrl = $request->fullUrl(); $referringUrl = $request->header('referer'); // or 'HTTP_REFERER' // Use the client to log the page view // Adapt this based on the methods available in the SDK and the data you wish to track $client->logPageViewAsync([ 'ipAddress' => $ipAddress, 'userAgent' => $userAgent, 'requestedUrl' => $requestedUrl, 'referringUrl' => $referringUrl, ]); return $next($request); }Register your middleware in the
Kernel.phpfile located in theapp/Httpdirectory. You can either register it as a global middleware or assign it to a specific route/middleware group.protected $middlewareGroups = [ 'web' => [ // ... other middlewares \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.
Final Steps
With the middleware set up and the environment variables configured, you can now use the Scoby Analytics SDK's features throughout your application using the License Key and salt securely stored in the
.envfile.Make sure to clear the configuration cache after updating the
.envfile:php artisan config:clear