Low-code Integrations

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 .env file 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_here
    

    Replace your_api_key_here with the License Key you obtained earlier and your_generated_salt_here with the salt you generated.

  • In your Laravel application, you can access these values using the env function:

    $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 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);
        
         // 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.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' => [
         // ... 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 .env file.

  • Make sure to clear the configuration cache after updating the .env file:

    php artisan config:clear
    
Previous
Express