Skip to content

Commit 803dc56

Browse files
authored
Merge pull request #882 from sdinteractive/ADO-313-capi-changes
ADO-313: CAPI refactor
2 parents c1d1dd9 + 571bf69 commit 803dc56

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1823
-99
lines changed

app/code/Meta/Conversion/Block/Pixel/AddToWishlist.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,36 @@
33

44
namespace Meta\Conversion\Block\Pixel;
55

6+
use Magento\Customer\Model\Session as CustomerSession;
7+
use Magento\Checkout\Model\Session as CheckoutSession;
8+
use Magento\Framework\Escaper;
9+
use Magento\Framework\View\Element\Template\Context;
10+
use Meta\BusinessExtension\Helper\FBEHelper;
11+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
12+
use Meta\Conversion\Helper\MagentoDataHelper;
13+
614
/**
715
* @api
816
*/
917
class AddToWishlist extends Common
1018
{
19+
20+
private $customerSession;
21+
22+
public function __construct(
23+
Context $context,
24+
FBEHelper $fbeHelper,
25+
MagentoDataHelper $magentoDataHelper,
26+
SystemConfig $systemConfig,
27+
Escaper $escaper,
28+
CheckoutSession $checkoutSession,
29+
CustomerSession $customerSession,
30+
array $data = []
31+
) {
32+
parent::__construct($context, $fbeHelper, $magentoDataHelper, $systemConfig, $escaper, $checkoutSession, $data);
33+
$this->customerSession = $customerSession;
34+
}
35+
1136
/**
1237
* Returns event name
1338
*
@@ -17,4 +42,14 @@ public function getEventToObserveName()
1742
{
1843
return 'facebook_businessextension_ssapi_add_to_wishlist';
1944
}
45+
46+
public function getEventId(): ?string
47+
{
48+
$eventIds = $this->customerSession->getMetaEventIds();
49+
if (is_array($eventIds) && array_key_exists($this->getEventToObserveName(), $eventIds)) {
50+
return $eventIds[$this->getEventToObserveName()];
51+
}
52+
53+
return null;
54+
}
2055
}

app/code/Meta/Conversion/Block/Pixel/Common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public function getTrackerUrl(): string
274274
*
275275
* @return string
276276
*/
277-
public function getEventId(): string
277+
public function getEventId(): ?string
278278
{
279279
return EventIdGenerator::guidv4();
280280
}

app/code/Meta/Conversion/Block/Pixel/CustomerRegistrationSuccess.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,37 @@
33

44
namespace Meta\Conversion\Block\Pixel;
55

6+
use Magento\Checkout\Model\Session as CheckoutSession;
7+
use Magento\Customer\Model\Session as CustomerSession;
8+
use Magento\Framework\Escaper;
9+
use Magento\Framework\View\Element\Template\Context;
10+
use Meta\BusinessExtension\Helper\FBEHelper;
11+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
12+
use Meta\Conversion\Helper\MagentoDataHelper;
13+
614
/**
715
* @api
816
*/
917
class CustomerRegistrationSuccess extends Common
1018
{
19+
20+
private $customerSession;
21+
22+
23+
public function __construct(
24+
Context $context,
25+
FBEHelper $fbeHelper,
26+
MagentoDataHelper $magentoDataHelper,
27+
SystemConfig $systemConfig,
28+
Escaper $escaper,
29+
CheckoutSession $checkoutSession,
30+
CustomerSession $customerSession,
31+
array $data = []
32+
) {
33+
parent::__construct($context, $fbeHelper, $magentoDataHelper, $systemConfig, $escaper, $checkoutSession, $data);
34+
$this->customerSession = $customerSession;
35+
}
36+
1137
/**
1238
* Returns content type
1339
*
@@ -27,4 +53,14 @@ public function getEventToObserveName()
2753
{
2854
return 'facebook_businessextension_ssapi_customer_registration_success';
2955
}
56+
57+
public function getEventId(): ?string
58+
{
59+
$eventIds = $this->customerSession->getMetaEventIds();
60+
if (is_array($eventIds) && array_key_exists($this->getEventToObserveName(), $eventIds)) {
61+
return $eventIds[$this->getEventToObserveName()];
62+
}
63+
64+
return null;
65+
}
3066
}

app/code/Meta/Conversion/Block/Pixel/InitiateCheckout.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Magento\Framework\Pricing\Helper\Data as PricingHelper;
2929
use Magento\Checkout\Model\Session as CheckoutSession;
3030
use Magento\Quote\Model\Quote;
31+
use Meta\Conversion\Model\CapiEventIdHandler;
3132

3233
/**
3334
* @api
@@ -54,6 +55,8 @@ class InitiateCheckout extends Common
5455
*/
5556
private $checkoutSession;
5657

58+
private $capiEventIdHandler;
59+
5760
/**
5861
* Constructor
5962
*
@@ -64,6 +67,7 @@ class InitiateCheckout extends Common
6467
* @param Escaper $escaper
6568
* @param CheckoutSession $checkoutSession
6669
* @param PricingHelper $pricingHelper
70+
* @param CapiEventIdHandler $capiEventIdHandler
6771
* @param array $data
6872
*/
6973
public function __construct(
@@ -74,6 +78,7 @@ public function __construct(
7478
Escaper $escaper,
7579
CheckoutSession $checkoutSession,
7680
PricingHelper $pricingHelper,
81+
CapiEventIdHandler $capiEventIdHandler,
7782
array $data = []
7883
) {
7984
parent::__construct(
@@ -88,6 +93,7 @@ public function __construct(
8893
$this->pricingHelper = $pricingHelper;
8994
$this->magentoDataHelper = $magentoDataHelper;
9095
$this->checkoutSession = $checkoutSession;
96+
$this->capiEventIdHandler = $capiEventIdHandler;
9197
}
9298

9399
/**
@@ -197,4 +203,9 @@ public function getContentTypeQuote(): string
197203
{
198204
return 'product';
199205
}
206+
207+
public function getEventId(): ?string
208+
{
209+
return $this->capiEventIdHandler->getMetaEventId($this->getEventToObserveName());
210+
}
200211
}

app/code/Meta/Conversion/Block/Pixel/Purchase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Magento\Framework\View\Element\Template\Context;
2929
use Magento\Framework\Escaper;
3030
use Magento\Checkout\Model\Session as CheckoutSession;
31+
use Meta\Conversion\Model\CapiEventIdHandler;
3132

3233
/**
3334
* @api
@@ -44,6 +45,9 @@ class Purchase extends Common
4445
*/
4546
private $fbeHelper;
4647

48+
private $capiEventIdHandler;
49+
50+
4751
/**
4852
* Purchase constructor
4953
*
@@ -62,6 +66,7 @@ public function __construct(
6266
SystemConfig $systemConfig,
6367
Escaper $escaper,
6468
CheckoutSession $checkoutSession,
69+
CapiEventIdHandler $capiEventIdHandler,
6570
array $data = []
6671
) {
6772
parent::__construct(
@@ -75,6 +80,7 @@ public function __construct(
7580
);
7681
$this->fbeHelper = $fbeHelper;
7782
$this->checkoutSession = $checkoutSession;
83+
$this->capiEventIdHandler = $capiEventIdHandler;
7884
}
7985

8086
/**
@@ -194,4 +200,9 @@ public function getLastOrderRealOrderEntityId()
194200
{
195201
return $this->checkoutSession->getLastRealOrder()->getEntityId();
196202
}
203+
204+
public function getEventId(): ?string
205+
{
206+
return $this->capiEventIdHandler->getMetaEventId($this->getEventToObserveName());
207+
}
197208
}

app/code/Meta/Conversion/Controller/Pixel/Tracker.php

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
use Magento\Framework\App\Action\HttpPostActionInterface;
88
use Magento\Framework\App\RequestInterface;
9-
use Meta\Conversion\Helper\ServerSideHelper;
10-
use Meta\Conversion\Helper\ServerEventFactory;
119
use Meta\BusinessExtension\Helper\FBEHelper;
1210
use Magento\Framework\Controller\Result\JsonFactory;
1311
use Magento\Framework\Controller\Result\Json;
12+
use Magento\Framework\MessageQueue\PublisherInterface;
13+
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
14+
use FacebookAds\Object\ServerSide\Util;
1415

1516
class Tracker implements HttpPostActionInterface
1617
{
@@ -19,11 +20,6 @@ class Tracker implements HttpPostActionInterface
1920
*/
2021
private $request;
2122

22-
/**
23-
* @var ServerSideHelper
24-
*/
25-
private $serverSideHelper;
26-
2723
/**
2824
* @var FBEHelper
2925
*/
@@ -40,33 +36,36 @@ class Tracker implements HttpPostActionInterface
4036
private $jsonFactory;
4137

4238
/**
43-
* @var ServerEventFactory
39+
* @var PublisherInterface
4440
*/
45-
private $serverEventFactory;
41+
private $publisher;
42+
43+
/**
44+
* @var JsonSerializer
45+
*/
46+
private $jsonSerializer;
4647

4748
/**
48-
* Constructor
49-
*
5049
* @param RequestInterface $request
51-
* @param ServerSideHelper $serverSideHelper
5250
* @param FBEHelper $fbeHelper
5351
* @param JsonFactory $jsonFactory
54-
* @param ServerEventFactory $serverEventFactory
52+
* @param PublisherInterface $publisher
53+
* @param JsonSerializer $jsonSerializer
5554
* @param array $pixelEvents
5655
*/
5756
public function __construct(
5857
RequestInterface $request,
59-
ServerSideHelper $serverSideHelper,
6058
FBEHelper $fbeHelper,
6159
JsonFactory $jsonFactory,
62-
ServerEventFactory $serverEventFactory,
60+
PublisherInterface $publisher,
61+
JsonSerializer $jsonSerializer,
6362
array $pixelEvents = []
6463
) {
6564
$this->request = $request;
66-
$this->serverSideHelper = $serverSideHelper;
6765
$this->fbeHelper = $fbeHelper;
6866
$this->jsonFactory = $jsonFactory;
69-
$this->serverEventFactory = $serverEventFactory;
67+
$this->publisher = $publisher;
68+
$this->jsonSerializer = $jsonSerializer;
7069
$this->pixelEvents = $pixelEvents;
7170
}
7271

@@ -81,22 +80,17 @@ public function execute(): Json
8180
try {
8281
$params = $this->request->getParams();
8382
$eventName = $params['eventName'];
84-
$eventId = $params['eventId'];
8583

8684
if ($eventName) {
8785
$payload = $this->pixelEvents[$eventName]->getPayload($params);
86+
$payload['event_id'] = $params['eventId'];
87+
$payload['event_type'] = $this->pixelEvents[$eventName]->getEventType();
88+
$payload['request_uri'] = Util::getRequestUri();
89+
$payload['user_agent'] = Util::getHttpUserAgent();
90+
$payload['fbp'] = Util::getFbp();
91+
$payload['fbc'] = Util::getFbc();
8892
if (isset($payload)) {
89-
// Add source and pluginVersion in the payload as custom properties
90-
$payload['custom_properties'] = [];
91-
$payload['custom_properties']['source'] = $this->fbeHelper->getSource();
92-
$payload['custom_properties']['pluginVersion'] = $this->fbeHelper->getPluginVersion();
93-
$eventType = $this->pixelEvents[$eventName]->getEventType();
94-
$event = $this->serverEventFactory->createEvent($eventType, array_filter($payload), $eventId);
95-
if (isset($payload['userDataFromOrder'])) {
96-
$this->serverSideHelper->sendEvent($event, $payload['userDataFromOrder']);
97-
} else {
98-
$this->serverSideHelper->sendEvent($event);
99-
}
93+
$this->publisher->publish('send.conversion.event.to.meta', $this->jsonSerializer->serialize($payload));
10094
$response['success'] = true;
10195
}
10296
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Meta\Conversion\CustomerData;
5+
6+
use Magento\Customer\CustomerData\SectionSourceInterface;
7+
use Magento\Customer\Model\Session as CustomerSession;
8+
9+
class EventData implements SectionSourceInterface
10+
{
11+
12+
public function __construct(
13+
private readonly CustomerSession $customerSession)
14+
{ }
15+
16+
public function getSectionData()
17+
{
18+
return ['eventIds' => $this->customerSession->getMetaEventIds() ?: []];
19+
}
20+
}

app/code/Meta/Conversion/Helper/MagentoDataHelper.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,48 @@ public function hashValue($string): string
289289
return hash('sha256', strtolower($string ?? ""));
290290
}
291291

292+
/**
293+
* @param $quote
294+
* @return array
295+
* @throws LocalizedException
296+
* @throws NoSuchEntityException
297+
*/
298+
public function getCartPayload($quote): array
299+
{
300+
foreach ($quote->getAllItems() as $item) {
301+
$product = $item->getProduct();
302+
$categoryIds[] = $product->getCategoryIds();
303+
array_push($categoryIds, $product->getCategoryIds());
304+
if (!in_array($item['product_type'], ['simple', 'grouped', 'bundle', 'virtual', 'downloadable'])) {
305+
continue;
306+
}
307+
$contents[] = [
308+
'id' => $item->getSku(),
309+
'quantity' => (int) $item->getQty()
310+
];
311+
}
312+
$categoryIds = array_merge([], ...$categoryIds);
313+
$contentCategoriesForItems = explode(
314+
",",
315+
$this->getCategoriesNameById($categoryIds)
316+
);
317+
foreach ($contentCategoriesForItems as $category) {
318+
$contentCategories[] = $category;
319+
}
320+
321+
$contentIds = array_unique(array_map(function ($elem) {
322+
return $elem['id'];
323+
},
324+
$contents));
325+
$contentCategories = array_unique($contentCategories);
326+
return [
327+
'content_category' => implode(', ', $contentCategories),
328+
'content_ids' => $contentIds,
329+
'contents' => $contents,
330+
'currency' => $this->getCurrency(),
331+
'value' => round((float) $quote->getSubtotal(), 2)
332+
];
333+
}
334+
292335
// TODO Remaining user/custom data methods that can be obtained using Magento.
293336
}

0 commit comments

Comments
 (0)