<?php declare(strict_types=1);
namespace Biloba\AdFacebookPixel\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Core\Content\Category\Event\NavigationLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Biloba\AdFacebookPixel\Service\ConversionApiService;
use Biloba\AdFacebookPixel\Structs\StoreFrontPageCollection;
use Exception;
class PageLoadedSubscriber implements EventSubscriberInterface
{
private $conversionApiService;
private $eventIds = [];
private $extensionData;
public function __construct(ConversionApiService $conversionApiService)
{
$this->conversionApiService = $conversionApiService;
}
public static function getSubscribedEvents(): array
{
return [
FooterPageletLoadedEvent::class => 'onPageLoaded',
ProductPageLoadedEvent::class => 'onProductPageLoaded',
NavigationLoadedEvent::class => 'onNavigationLoaded',
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded',
CheckoutRegisterPageLoadedEvent::class => 'onInitiateCheckoutPageLoaded',
CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoaded',
AfterLineItemAddedEvent::class => 'onAfterLineItemAddedEvent'
];
}
public function onAfterLineItemAddedEvent(AfterLineItemAddedEvent $event) :void
{
$contextToken = $event->getSalesChannelContext()->getToken();
if(array_key_exists(0, $event->getLineItems())) {
$lineItemArrayId = $event->getLineItems()[0]->getId();
if($event->getCart()->getLineItems()->getPayload() &&
array_key_exists($lineItemArrayId, $event->getCart()->getLineItems()->getPayload()) &&
array_key_exists('productNumber', $event->getCart()->getLineItems()->getPayload()[$lineItemArrayId])) {
$productNumber = $event->getCart()->getLineItems()->getPayload()[$lineItemArrayId]['productNumber'];
$this->extensionData = new StoreFrontPageCollection();
$this->conversionApiService->setData($event);
// use order nr and token as event_id for server and client AddToCart event
$this->eventIds["AddToCart"] = $productNumber . $contextToken;
// adding eventIds array to context
$this->extensionData->setValue('BilobaAdFacebookPixelEventIds', $this->eventIds);
$event->getContext()->addExtension('BilobaAdFacebookPixel', $this->extensionData);
try {
$this->conversionApiService->send("AddToCart", $productNumber . $contextToken);
} catch(Exception $e) {
// ignoring exception
}
}
}
}
public function onCheckoutFinishPageLoaded(CheckoutFinishPageLoadedEvent $event) :void
{
$this->extensionData = new StoreFrontPageCollection();
$this->conversionApiService->setData($event);
$this->eventIds["Purchase"] = $this->conversionApiService->getDataForConversion()['fbEventId'];
// adding eventIds array to context
$this->extensionData->setValue('BilobaAdFacebookPixelEventIds', $this->eventIds);
$event->getContext()->addExtension('BilobaAdFacebookPixel', $this->extensionData);
try {
$this->conversionApiService->send("Purchase");
} catch(Exception $e) {
// ignoring exception
}
}
public function onInitiateCheckoutPageLoaded(CheckoutRegisterPageLoadedEvent $event) :void
{
$this->extensionData = new StoreFrontPageCollection();
$this->conversionApiService->setData($event);
$this->eventIds["InitiateCheckout"] = $this->conversionApiService->getDataForConversion()['fbEventId'];
// adding eventIds array to context
$this->extensionData->setValue('BilobaAdFacebookPixelEventIds', $this->eventIds);
$event->getContext()->addExtension('BilobaAdFacebookPixel', $this->extensionData);
try {
$this->conversionApiService->send("InitiateCheckout");
} catch(Exception $e) {
// ignoring exception
}
}
public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event) :void
{
// $this->extensionData = new StoreFrontPageCollection(); Customer Fix
// setData sets neccesaryDataForConversion, userData and customData
$this->conversionApiService->setData($event);
$this->eventIds["CompleteRegistration"] = $this->conversionApiService->getDataForConversion()['fbEventId'];
// adding eventIds array to context
$this->extensionData->setValue('BilobaAdFacebookPixelEventIds', $this->eventIds);
$event->getContext()->addExtension('BilobaAdFacebookPixel', $this->extensionData);
try {
$this->conversionApiService->send("CompleteRegistration");
} catch(Exception $e) {
// ignoring exception
}
}
public function onNavigationLoaded(NavigationLoadedEvent $event) :void
{
$this->extensionData = new StoreFrontPageCollection();
$name = $event->getNavigation()->getActive()->getName();
$categoryId = $event->getNavigation()->getActive()->getId();
$this->conversionApiService->setData($event);
$this->conversionApiService->setCustomData(["name" => $name, "id" => $categoryId]);
$this->eventIds["ViewCategory"] = $this->conversionApiService->getDataForConversion()['fbEventId'];
// adding eventIds array to context
$this->extensionData->setValue('BilobaAdFacebookPixelEventIds', $this->eventIds);
$event->getContext()->addExtension('BilobaAdFacebookPixel', $this->extensionData);
try {
$this->conversionApiService->send("ViewCategory");
} catch(Exception $e) {
// ignoring exception
}
}
public function onProductPageLoaded(ProductPageLoadedEvent $event) :void
{
$this->extensionData = new StoreFrontPageCollection();
$currencyShortName = $event->getSalesChannelContext()->getCurrency()->getShortName();
$name = $event->getPage()->getProduct()->get('translated')['name'];
$productId = $event->getPage()->getProduct()->get('id');
$unitPrice = $event->getPage()->getProduct()->getCalculatedPrice()->getUnitPrice();
$this->conversionApiService->setData($event);
// setting custom data this way because ViewContent needs just these specific parameters
$this->conversionApiService->setCustomData(["name" => $name, "currency" => $currencyShortName, "conten_id" => $productId, "value" => $unitPrice]);
$this->eventIds["ViewContent"] = $this->conversionApiService->getDataForConversion()['fbEventId'];
// adding eventIds array to context
$this->extensionData->setValue('BilobaAdFacebookPixelEventIds', $this->eventIds);
$event->getContext()->addExtension('BilobaAdFacebookPixel', $this->extensionData);
try {
$this->conversionApiService->send("ViewContent");
} catch(Exception $e) {
// ignoring exception
}
}
public function onPageLoaded(FooterPageletLoadedEvent $event) :void
{
$this->extensionData = new StoreFrontPageCollection();
$this->conversionApiService->setData($event);
// setting custom data empty because FooterPageletLoadedEvent gives no payload
$this->conversionApiService->setCustomData([]);
$this->eventIds["PageView"] = $this->conversionApiService->getDataForConversion()['fbEventId'];
// adding eventIds array to context
$this->extensionData->setValue('BilobaAdFacebookPixelEventIds', $this->eventIds);
$event->getContext()->addExtension('BilobaAdFacebookPixel', $this->extensionData);
try {
$this->conversionApiService->send("PageView");
} catch(Exception $e) {
// ignoring exception
}
}
}