<?php declare(strict_types=1);
namespace Biloba\AdFacebookPixel\Service;
use Biloba\AdFacebookPixel\Service\BuildFromEventData;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
use GuzzleHttp\Client;
use Shopware\Core\Framework\Uuid\Uuid;
/**
* Fetching neccesary data from BuildFromEventData, SystemConfigService classes and summarizing the data in the below methods which return arrays
*/
class ConversionApiService {
private $systemConfigService;
private $buildFromEventData;
private $neccesaryDataForConversion;
private $userData;
private $customData;
private $url = 'https://graph.facebook.com/v11.0/';
private $endpoint = '/events';
public function __construct(SystemConfigService $systemConfigService = null, BuildFromEventData $buildFromEventData)
{
$this->systemConfigService = $systemConfigService;
$this->buildFromEventData = $buildFromEventData;
}
public function setData($event) {
$this->neccesaryDataForConversion = $this->getNeccesaryDataForConversion($event);
$this->userData = $this->getUserData($event);
$this->customData = $this->buildFromEventData->getCustomDataForFacebook($event);
}
// method is neccesary because we have two conversion with specific custom data parameters
public function setCustomData($customData) {
$this->customData = $customData;
}
public function getNeccesaryDataForConversion($event) {
$neccesaryDataForConversion = [];
$neccesaryDataForConversion['disableFBConversions'] = $this->systemConfigService->get('BilobaAdFacebookPixel.config.DisableFBConversions', $event->getSalesChannelContext()->getSalesChannel()->getId());
$neccesaryDataForConversion['customerloggedIn'] = $this->buildFromEventData->isCustomerLoggedIn($event);
$neccesaryDataForConversion['fbEventTimeStamp'] = (new \DateTime())->getTimestamp();
$neccesaryDataForConversion['fbEventId'] = Uuid::randomHex();
$neccesaryDataForConversion['fbPixelId'] = $this->systemConfigService->get('BilobaAdFacebookPixel.config.FbqID', $event->getSalesChannelContext()->getSalesChannel()->getId());
$neccesaryDataForConversion['url'] = $this->url;
$neccesaryDataForConversion['endpoint'] = $this->endpoint;
$neccesaryDataForConversion['access_token'] = $this->systemConfigService->get('BilobaAdFacebookPixel.config.FbqAccessToken', $event->getSalesChannelContext()->getSalesChannel()->getId());
return $neccesaryDataForConversion;
}
public function getDataForConversion() {
return $this->neccesaryDataForConversion;
}
public function getUserData($event)
{
$neccesaryDataForConversion = $this->getNeccesaryDataForConversion($event);
return $this->buildFromEventData->getUserDataForFacebook($event, $neccesaryDataForConversion['customerloggedIn']);
}
// send the conversion with payload
public function send($action, $productNumber = null) {
if(!$this->neccesaryDataForConversion['disableFBConversions'] && $this->neccesaryDataForConversion['access_token'] != null) {
$client = new Client([
'headers'=>[
'Content-type' => 'application/json',
'Authorization' => 'Bearer ' . $this->neccesaryDataForConversion['access_token']
]
]);
$customData = null;
if(array_key_exists('custom_data', $this->customData)) {
$customData = $this->customData['custom_data'];
}
if($action != 'AddToCart') {
$client->post($this->neccesaryDataForConversion['url'].$this->neccesaryDataForConversion['fbPixelId'].$this->neccesaryDataForConversion['endpoint'], [
'json' => [
"data" => [[
"event_name"=> $action,
"event_time"=> $this->neccesaryDataForConversion['fbEventTimeStamp'],
"event_id" => str_replace('"',"'", $this->neccesaryDataForConversion['fbEventId']),
"action_source"=> "website",
"user_data" => [
"em" => [
$this->userData['em']
],
"fn"=> [
$this->userData['fn']
],
"ln"=> [
$this->userData['ln']
],
"ct" => [
$this->userData['ct']
],
"st" => [
$this->userData['st']
]
],
"custom_data"=>
$customData
]]
]
]);
}else {
$client->post($this->neccesaryDataForConversion['url'].$this->neccesaryDataForConversion['fbPixelId'].$this->neccesaryDataForConversion['endpoint'], [
'json' => [
"data" => [[
"event_name"=> $action,
"event_time"=> $this->neccesaryDataForConversion['fbEventTimeStamp'],
"event_id" => str_replace('"',"'", $productNumber),
"action_source"=> "website",
"user_data" => [
"em" => [
$this->userData['em']
],
"fn"=> [
$this->userData['fn']
],
"ln"=> [
$this->userData['ln']
],
"ct" => [
$this->userData['ct']
],
"st" => [
$this->userData['st']
]
],
"custom_data"=>
$customData
]]
]
]);
}
}
}
}