Low-code Integrations

Integrating Scoby Analytics with Drupal

Here's a step-by-step guide to creating a Drupal Middleware using the Scoby Analytics PHP SDK:

Prerequisites

You need your License Key and Salt to use Scoby Analytics in Drupal.

Setting up Configuration Variables in Drupal

  • Navigate to the Configuration menu in your Drupal admin dashboard.

  • Under the System section, click Site information.

  • Under Site Details, add your Scoby License Key and salt. You might need a custom configuration or a module if such fields do not exist by default.

  • In your Drupal application, use Drupal's configuration API to access these values:

    $apiKey = \Drupal::config('system.site')->get('scoby_api_key');
    $salt = \Drupal::config('system.site')->get('scoby_salt');
    

Creating a Drupal Middleware (Middleware Module)

  • Create a custom module, e.g., scoby_analytics.

  • Inside the module, create a scoby_analytics.services.yml file with the following content:

    services:
      scoby_analytics.middleware:
        class: Drupal\scoby_analytics\ScobyAnalyticsMiddleware
        tags:
          - { name: http_middleware, priority: 200, responder: true }
    
  • Create the middleware class at src/ScobyAnalyticsMiddleware.php:

    namespace Drupal\scoby_analytics;
    
    use Symfony\Component\HttpFoundation\RequestStack;
    use Scoby\Analytics\SDK as ScobySDK;
    
    class ScobyAnalyticsMiddleware {
    
        protected $requestStack;
    
        public function __construct(RequestStack $requestStack) {
            $this->requestStack = $requestStack;
        }
    
        public function handle() {
            $apiKey = \Drupal::config('system.site')->get('scoby_api_key');
            $salt = \Drupal::config('system.site')->get('scoby_salt');
    
            $scoby = new ScobySDK($apiKey, $salt);
    
            // Collect necessary data
            $currentRequest = $this->requestStack->getCurrentRequest();
            $ipAddress = $currentRequest->getClientIp();
            $userAgent = $currentRequest->headers->get('User-Agent');
            $requestedUrl = $currentRequest->getUri();
            $referringUrl = $currentRequest->headers->get('referer');
    
            // Log the page view
            $scoby->logPageViewAsync([
                'ipAddress' => $ipAddress,
                'userAgent' => $userAgent,
                'requestedUrl' => $requestedUrl,
                'referringUrl' => $referringUrl,
            ]);
        }
    }
    
  • Enable your custom module: Navigate to the Extend menu in the Drupal admin dashboard and enable scoby_analytics.

Final Steps

  • With the middleware set up and the configuration variables set, you can now use the Scoby Analytics SDK's features throughout your Drupal site.

  • Clear the cache for changes to take effect:

    drush cache:rebuild
    

By following this guide, you'll successfully integrate Scoby Analytics with your Drupal website.

Previous
Symfony