Popular Frameworks

Integrating Scoby Analytics with Symfony

Here's a step-by-step guide to creating a Symfony Event Listener using the Scoby Analytics PHP SDK:

Prerequisites

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

Setting up Configuration Variables in Symfony

  • Open the .env file in the root directory of your Symfony project.

  • Add the following lines:

    SCOBY_API_KEY=your_api_key_here
    SCOBY_SALT=your_generated_salt_here
    
  • You can access these values using Symfony's configuration system:

    parameters:
      scoby_api_key: '%env(resolve:SCOBY_API_KEY)%'
      scoby_salt: '%env(resolve:SCOBY_SALT)%'
    

Creating the Symfony Event Listener

  • Generate a new event listener:

    bin/console make:subscriber ScobyAnalyticsSubscriber
    
  • In the generated ScobyAnalyticsSubscriber.php file, import necessary classes and services:

    use Symfony\Component\HttpKernel\Event\RequestEvent;
    use Scoby\Analytics\SDK as ScobySDK;
    
  • Update the getSubscribedEvents method to listen to the kernel.request event:

    public static function getSubscribedEvents()
    {
        return [
            RequestEvent::class => 'onKernelRequest',
        ];
    }
    
  • Implement the onKernelRequest method:

    public function onKernelRequest(RequestEvent $event)
    {
        $apiKey = $this->getParameter('scoby_api_key');
        $salt = $this->getParameter('scoby_salt');
    
        $scoby = new ScobySDK($apiKey, $salt);
    
        // Collect necessary data
        $request = $event->getRequest();
        $ipAddress = $request->getClientIp();
        $userAgent = $request->headers->get('User-Agent');
        $requestedUrl = $request->getUri();
        $referringUrl = $request->headers->get('referer');
    
        // Use the Scoby SDK to log the page view
        $scoby->logPageViewAsync([
            'ipAddress' => $ipAddress,
            'userAgent' => $userAgent,
            'requestedUrl' => $requestedUrl,
            'referringUrl' => $referringUrl,
        ]);
    }
    

Final Steps

  • With the event listener set up and the environment variables configured, Scoby Analytics can now log page views on every request to your Symfony application.

  • Clear the cache to ensure the changes take effect:

    bin/console cache:clear
    

By following this guide, you'll successfully integrate Scoby Analytics with your Symfony application using an Event Listener.

Previous
Express

Please note:Scoby does not provide legal advice. The information provided in this documentation is for general informational purposes only and does not constitute legal consultation. You should always consult your legal counsel or Data Protection Officer (DPO) to assess how applicable laws and regulations apply to your specific situation. If your DPO or legal team has questions about Scoby Analytics, they can contact us. We’re happy to provide detailed technical explanations.