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
.envfile in the root directory of your Symfony project.Add the following lines:
SCOBY_API_KEY=your_api_key_here SCOBY_SALT=your_generated_salt_hereYou 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 ScobyAnalyticsSubscriberIn the generated
ScobyAnalyticsSubscriber.phpfile, import necessary classes and services:use Symfony\Component\HttpKernel\Event\RequestEvent; use Scoby\Analytics\SDK as ScobySDK;Update the
getSubscribedEventsmethod to listen to thekernel.requestevent:public static function getSubscribedEvents() { return [ RequestEvent::class => 'onKernelRequest', ]; }Implement the
onKernelRequestmethod: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.