Skip to content

Commit

Permalink
Make the sending of server side events possible with the current impl…
Browse files Browse the repository at this point in the history
…ementation
  • Loading branch information
loevgaard committed Aug 7, 2023
1 parent c285ce6 commit e77ac08
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/Event/ServerSideEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Setono\GoogleAnalyticsBundle\Event;

use Setono\GoogleAnalyticsBundle\ValueObject\Property;
use Setono\GoogleAnalyticsMeasurementProtocol\Request\Body\Event\Event;
use Symfony\Contracts\EventDispatcher\Event as StoppableEvent;

Expand All @@ -14,14 +15,18 @@ final class ServerSideEvent extends StoppableEvent
{
public Event $event;

public ?string $clientId = null;
public ?string $clientId;

public ?Property $property;

/**
* @param string|null $clientId If you do not provide a client id, the client id from the _ga cookie will be used
* @param Property|null $property If you do not provide a property, the property will be resolved from the configured properties or containers
*/
public function __construct(Event $event, string $clientId = null)
public function __construct(Event $event, string $clientId = null, Property $property = null)
{
$this->event = $event;
$this->clientId = $clientId;
$this->property = $property;
}
}
34 changes: 30 additions & 4 deletions src/EventSubscriber/HandleServerSideEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Psr\Log\NullLogger;
use Setono\GoogleAnalyticsBundle\Event\ServerSideEvent;
use Setono\GoogleAnalyticsBundle\Message\Command\SendRequest;
use Setono\GoogleAnalyticsBundle\Provider\ContainerProviderInterface;
use Setono\GoogleAnalyticsBundle\Provider\PropertyProviderInterface;
use Setono\GoogleAnalyticsBundle\ValueObject\Property;
use Setono\GoogleAnalyticsMeasurementProtocol\Request\Body\Body;
use Setono\GoogleAnalyticsMeasurementProtocol\Request\Request;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand All @@ -26,11 +28,17 @@ final class HandleServerSideEventSubscriber implements EventSubscriberInterface,

private PropertyProviderInterface $propertyProvider;

public function __construct(MessageBusInterface $commandBus, PropertyProviderInterface $propertyProvider)
private ContainerProviderInterface $containerProvider;

private bool $gtagEnabled;

public function __construct(MessageBusInterface $commandBus, PropertyProviderInterface $propertyProvider, ContainerProviderInterface $containerProvider, bool $gtagEnabled)
{
$this->logger = new NullLogger();
$this->commandBus = $commandBus;
$this->propertyProvider = $propertyProvider;
$this->containerProvider = $containerProvider;
$this->gtagEnabled = $gtagEnabled;
}

public static function getSubscribedEvents(): array
Expand All @@ -48,9 +56,7 @@ public function dispatch(ServerSideEvent $serverSideEvent): void
return;
}

$properties = $this->propertyProvider->getProperties();

foreach ($properties as $property) {
foreach ($this->getProperties() as $property) {
if (null === $property->apiSecret) {
$this->logger->error(sprintf(
'You tried to send an event server side, but the API secret is not set on the property with measurement id %s',
Expand All @@ -64,6 +70,26 @@ public function dispatch(ServerSideEvent $serverSideEvent): void
}
}

/**
* @return list<Property>
*/
private function getProperties(): array
{
$properties = [];

if ($this->gtagEnabled) {
return $this->propertyProvider->getProperties();
}

foreach ($this->containerProvider->getContainers() as $container) {
if (null !== $container->property) {
$properties[] = $container->property;
}
}

return $properties;
}

public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services/event_subscriber.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
class="Setono\GoogleAnalyticsBundle\EventSubscriber\HandleServerSideEventSubscriber">
<argument type="service" id="setono_google_analytics.command_bus"/>
<argument type="service" id="setono_google_analytics.property_provider.default"/>
<argument type="service" id="setono_google_analytics.container_provider.default"/>
<argument>%setono_google_analytics.gtag_enabled%</argument>
<call method="setLogger">
<argument type="service" id="logger"/>
</call>
Expand Down

0 comments on commit e77ac08

Please sign in to comment.