custom/plugins/BilobaAdFacebookPixel/src/Service/ConversionApiService.php line 68

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Biloba\AdFacebookPixel\Service;
  3. use Biloba\AdFacebookPixel\Service\BuildFromEventData;
  4. use Shopware\Core\System\SystemConfig\SystemConfigService;
  5. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  6. use GuzzleHttp\Client;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. /**
  9.  * Fetching neccesary data from BuildFromEventData, SystemConfigService classes and summarizing the data in the below methods which return arrays 
  10.  */
  11. class ConversionApiService {
  12.     private $systemConfigService;
  13.     private $buildFromEventData;
  14.     private $neccesaryDataForConversion;
  15.     private $userData;
  16.     private $customData;
  17.     private $url 'https://graph.facebook.com/v11.0/';
  18.     private $endpoint '/events';
  19.     public function __construct(SystemConfigService $systemConfigService nullBuildFromEventData $buildFromEventData)
  20.     {
  21.         $this->systemConfigService $systemConfigService;
  22.         $this->buildFromEventData $buildFromEventData;
  23.     }
  24.     public function setData($event) {
  25.         $this->neccesaryDataForConversion $this->getNeccesaryDataForConversion($event);
  26.         $this->userData $this->getUserData($event);
  27.         $this->customData $this->buildFromEventData->getCustomDataForFacebook($event);
  28.     }
  29.     // method is neccesary because we have two conversion with specific custom data parameters
  30.     public function setCustomData($customData) {
  31.         $this->customData $customData;
  32.     }
  33.     public function getNeccesaryDataForConversion($event) {
  34.         $neccesaryDataForConversion = [];
  35.         
  36.         $neccesaryDataForConversion['disableFBConversions'] = $this->systemConfigService->get('BilobaAdFacebookPixel.config.DisableFBConversions'$event->getSalesChannelContext()->getSalesChannel()->getId());
  37.         $neccesaryDataForConversion['customerloggedIn'] = $this->buildFromEventData->isCustomerLoggedIn($event);
  38.         $neccesaryDataForConversion['fbEventTimeStamp'] = (new \DateTime())->getTimestamp();
  39.         $neccesaryDataForConversion['fbEventId'] = Uuid::randomHex();
  40.         $neccesaryDataForConversion['fbPixelId'] = $this->systemConfigService->get('BilobaAdFacebookPixel.config.FbqID'$event->getSalesChannelContext()->getSalesChannel()->getId());
  41.         $neccesaryDataForConversion['url'] = $this->url;
  42.         $neccesaryDataForConversion['endpoint'] = $this->endpoint;
  43.         $neccesaryDataForConversion['access_token'] = $this->systemConfigService->get('BilobaAdFacebookPixel.config.FbqAccessToken'$event->getSalesChannelContext()->getSalesChannel()->getId());
  44.         
  45.         return $neccesaryDataForConversion;
  46.     }
  47.     public function getDataForConversion() {
  48.         return $this->neccesaryDataForConversion;
  49.     }
  50.     public function getUserData($event)
  51.     {
  52.         $neccesaryDataForConversion $this->getNeccesaryDataForConversion($event);
  53.         return $this->buildFromEventData->getUserDataForFacebook($event$neccesaryDataForConversion['customerloggedIn']);
  54.     }
  55.     
  56.     // send the conversion with payload
  57.     public function send($action$productNumber null) {
  58.         if(!$this->neccesaryDataForConversion['disableFBConversions'] && $this->neccesaryDataForConversion['access_token'] != null) {
  59.             $client = new Client([
  60.                 'headers'=>[
  61.                     'Content-type'  => 'application/json',
  62.                     'Authorization' => 'Bearer ' $this->neccesaryDataForConversion['access_token']
  63.                 ]
  64.             ]);
  65.             $customData null;
  66.             if(array_key_exists('custom_data'$this->customData)) {
  67.                 $customData $this->customData['custom_data'];
  68.             }
  69.             
  70.             if($action != 'AddToCart') {
  71.                 $client->post($this->neccesaryDataForConversion['url'].$this->neccesaryDataForConversion['fbPixelId'].$this->neccesaryDataForConversion['endpoint'], [
  72.                     'json' => [
  73.                         "data" => [[
  74.                             "event_name"=> $action,
  75.                             "event_time"=> $this->neccesaryDataForConversion['fbEventTimeStamp'],
  76.                             "event_id" => str_replace('"',"'"$this->neccesaryDataForConversion['fbEventId']),
  77.                             "action_source"=> "website",
  78.                             "user_data" => [
  79.                                 "em" => [
  80.                                     $this->userData['em']
  81.                                 ],
  82.                                 "fn"=> [
  83.                                     $this->userData['fn']
  84.                                 ],
  85.                                 "ln"=> [
  86.                                     $this->userData['ln']
  87.                                 ],
  88.                                 "ct" => [
  89.                                     $this->userData['ct']
  90.                                 ],
  91.                                 "st" => [
  92.                                     $this->userData['st']
  93.                                 ]
  94.                             ],
  95.                             "custom_data"=> 
  96.                                 $customData
  97.                         ]]
  98.                     ]
  99.                 ]);
  100.             }else {
  101.                 $client->post($this->neccesaryDataForConversion['url'].$this->neccesaryDataForConversion['fbPixelId'].$this->neccesaryDataForConversion['endpoint'], [
  102.                     'json' => [
  103.                         "data" => [[
  104.                             "event_name"=> $action,
  105.                             "event_time"=> $this->neccesaryDataForConversion['fbEventTimeStamp'],
  106.                             "event_id" => str_replace('"',"'"$productNumber),
  107.                             "action_source"=> "website",
  108.                             "user_data" => [
  109.                                 "em" => [
  110.                                     $this->userData['em']
  111.                                 ],
  112.                                 "fn"=> [
  113.                                     $this->userData['fn']
  114.                                 ],
  115.                                 "ln"=> [
  116.                                     $this->userData['ln']
  117.                                 ],
  118.                                 "ct" => [
  119.                                     $this->userData['ct']
  120.                                 ],
  121.                                 "st" => [
  122.                                     $this->userData['st']
  123.                                 ]
  124.                             ],
  125.                             "custom_data"=> 
  126.                                 $customData
  127.                         ]]
  128.                     ]
  129.                 ]);
  130.             }
  131.         }
  132.     }
  133. }