Skip to content

Commit cf6baca

Browse files
sachinkumarRPSachin Kumarbgiamarino
authored
Refactor AddToCart Pixel To Use MetaPixelTracker (#311)
* Add Refactor Add to Cart Events * Remove Unwanted Changes * add currency value in lowercase for browser event * Resolve static tests --------- Co-authored-by: Sachin Kumar <[email protected]> Co-authored-by: Ben Giamarino <[email protected]>
1 parent aa2be5f commit cf6baca

File tree

7 files changed

+145
-182
lines changed

7 files changed

+145
-182
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,14 @@ public function getProductInfoUrl()
7979
{
8080
return sprintf('%sfbe/Pixel/ProductInfoForAddToCart', $this->fbeHelper->getBaseUrl());
8181
}
82+
83+
/**
84+
* Returns event name
85+
*
86+
* @return string
87+
*/
88+
public function getEventToObserveName()
89+
{
90+
return 'facebook_businessextension_ssapi_add_to_cart';
91+
}
8292
}

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,13 @@ private function getProductInfo($productSku, $productId = null): array
160160
}
161161
if ($product && $product->getId()) {
162162
return [
163+
'productId'=> $product->getId(),
163164
'id' => $this->magentoDataHelper->getContentId($product),
165+
'currency' => strtolower($this->magentoDataHelper->getCurrency()),
164166
'name' => $product->getName(),
165167
'category' => $this->getCategory($product),
166168
'value' => $this->getPriceValue($product),
169+
'content_type' => $this->magentoDataHelper->getContentType($product)
167170
];
168171
}
169172
return [];
@@ -185,7 +188,6 @@ public function execute()
185188
if (count($responseData) > 0) {
186189
$eventId = EventIdGenerator::guidv4();
187190
$responseData['event_id'] = $eventId;
188-
$this->trackServerEvent($eventId);
189191
$result->setData(array_filter($responseData));
190192
}
191193
} else {
@@ -194,18 +196,4 @@ public function execute()
194196
}
195197
return $result;
196198
}
197-
198-
/**
199-
* Track the server event
200-
*
201-
* @param string $eventId
202-
* @return void
203-
*/
204-
public function trackServerEvent($eventId): void
205-
{
206-
$this->eventManager->dispatch(
207-
'facebook_businessextension_ssapi_add_to_cart',
208-
['eventId' => $eventId]
209-
);
210-
}
211199
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Meta\Conversion\Model\Tracker;
5+
6+
use Magento\Catalog\Api\ProductRepositoryInterface;
7+
use Magento\Framework\Escaper;
8+
use Magento\Framework\Exception\NoSuchEntityException;
9+
use Meta\Conversion\Api\TrackerInterface;
10+
use Meta\Conversion\Helper\MagentoDataHelper;
11+
12+
class AddToCart implements TrackerInterface
13+
{
14+
private const EVENT_TYPE = "AddToCart";
15+
16+
/**
17+
* @var Escaper
18+
*/
19+
private $escaper;
20+
21+
/**
22+
* @var ProductRepositoryInterface
23+
*/
24+
private $productRepository;
25+
26+
/**
27+
* @var MagentoDataHelper
28+
*/
29+
private $magentoDataHelper;
30+
31+
/**
32+
* @param MagentoDataHelper $magentoDataHelper
33+
* @param ProductRepositoryInterface $productRepository
34+
* @param Escaper $escaper
35+
*/
36+
public function __construct(
37+
MagentoDataHelper $magentoDataHelper,
38+
ProductRepositoryInterface $productRepository,
39+
Escaper $escaper
40+
) {
41+
$this->magentoDataHelper = $magentoDataHelper;
42+
$this->productRepository = $productRepository;
43+
$this->escaper = $escaper;
44+
}
45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
public function getPayload(array $params): array
50+
{
51+
$productId = $params['productId'];
52+
try {
53+
$product = $this->productRepository->getById($productId);
54+
$contentId = $this->magentoDataHelper->getContentId($product);
55+
return [
56+
'currency' => $this->magentoDataHelper->getCurrency(),
57+
'value' => $this->magentoDataHelper->getValueForProduct($product),
58+
'content_ids' => [$contentId],
59+
'content_name' => $this->escaper->escapeUrl($product->getName()),
60+
'contents' => [
61+
[
62+
'product_id' => $contentId,
63+
'quantity' => 1
64+
]
65+
],
66+
'content_type' => $this->magentoDataHelper->getContentType($product)
67+
];
68+
} catch (NoSuchEntityException $e) {
69+
return [];
70+
}
71+
}
72+
73+
/**
74+
* @inheritDoc
75+
*/
76+
public function getEventType(): string
77+
{
78+
return self::EVENT_TYPE;
79+
}
80+
}

app/code/Meta/Conversion/Observer/AddToCart.php

Lines changed: 0 additions & 129 deletions
This file was deleted.

app/code/Meta/Conversion/etc/frontend/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<item name = "facebook_businessextension_ssapi_view_category" xsi:type="object">Meta\Conversion\Model\Tracker\ViewCategory</item>
88
<item name = "facebook_businessextension_ssapi_view_content" xsi:type="object">Meta\Conversion\Model\Tracker\ViewContent</item>
99
<item name = "facebook_businessextension_ssapi_search" xsi:type="object">Meta\Conversion\Model\Tracker\Search</item>
10+
<item name = "facebook_businessextension_ssapi_add_to_cart" xsi:type="object">Meta\Conversion\Model\Tracker\AddToCart</item>
1011
</argument>
1112
</arguments>
1213
</type>

app/code/Meta/Conversion/etc/frontend/events.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
3-
<event name="facebook_businessextension_ssapi_add_to_cart">
4-
<observer name="facebook_businessextension_ssapi_add_to_cart_after" instance="Meta\Conversion\Observer\AddToCart"/>
5-
</event>
63
<event name="facebook_businessextension_ssapi_initiate_checkout">
74
<observer name="facebook_businessextension_ssapi_initiate_checkout_after" instance="Meta\Conversion\Observer\InitiateCheckout" />
85
</event>
Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
1-
<?php /** @var \Meta\Conversion\Block\Pixel\AddToCart $block */ ?>
2-
<?php if ($block->getFacebookPixelID()) { ?>
1+
<?php
2+
/** @var \Meta\Conversion\Block\Pixel\AddToCart $block */
3+
$trackerUrl = $block->getTrackerUrl();
4+
?>
5+
<?php if ($block->getFacebookPixelID()): ?>
6+
<!-- Added the new component below to track server events -->
37
<script>
4-
require([
5-
'jquery'
6-
], function ($) {
8+
window.addToCartData =
9+
<?= /* @noEscape */ json_encode([
10+
"url" => $block->escapeUrl($trackerUrl),
11+
"eventName" => $block->getEventToObserveName(),
12+
'fbAgentVersion' => $block->getFacebookAgentVersion(),
13+
'fbPixelId' => $block->getFacebookPixelID(),
14+
'source' => $block->getSource(),
15+
'pluginVersion' => $block->getPluginVersion(),
16+
'track' => 'track',
17+
'event' => 'AddToCart'
718

8-
function trackAddToCart(content_ids, content_name, content_category, value, eventId) {
9-
fbq(
10-
'set',
11-
'agent',
12-
'<?= /* @noEscape */ $block->getFacebookAgentVersion() ?>',
13-
'<?= /* @noEscape */ $block->getFacebookPixelID() ?>'
14-
);
15-
fbq('track', 'AddToCart', {
16-
source: "<?= /* @noEscape */ $block->getSource() ?>",
17-
pluginVersion: "<?= /* @noEscape */ $block->getPluginVersion() ?>",
18-
content_type: "<?= /* @noEscape */ $block->getContentType() ?>",
19-
currency: "<?= /* @noEscape */ $block->getCurrency() ?>",
20-
content_ids: content_ids,
21-
content_name: content_name,
22-
content_category: content_category,
23-
value: value
24-
},
25-
{
26-
eventID: eventId
27-
}
28-
);
29-
}
19+
]); ?>
20+
</script>
21+
<script>
22+
require([
23+
'jquery',
24+
'Meta_Conversion/js/metaPixelTracker'
25+
], function ($, metaPixelTracker) {
3026

3127
var product_info_url = '<?= /* @noEscape */ $block->getProductInfoUrl() ?>';
3228

@@ -65,17 +61,37 @@
6561
type: 'get',
6662
dataType: 'json',
6763
success: function (res) {
68-
trackAddToCart(
69-
[res.id],
70-
res.name,
71-
res.content_category,
72-
res.value,
73-
res.event_id
74-
);
64+
var addToCartConfigPixel = {
65+
"url" : window.addToCartData.url,
66+
"payload": {
67+
"eventName" : window.addToCartData.eventName,
68+
"productId" : res.productId
69+
},
70+
"browserEventData": {
71+
'fbAgentVersion': window.addToCartData.fbAgentVersion,
72+
'fbPixelId': window.addToCartData.fbPixelId,
73+
'source': window.addToCartData.source,
74+
'pluginVersion': window.addToCartData.pluginVersion,
75+
'track': window.addToCartData.track,
76+
'event': window.addToCartData.event,
77+
'payload': {
78+
"content_name": res.name,
79+
"content_ids": [res.id],
80+
"value": res.value,
81+
"currency": res.currency,
82+
"content_type": res.content_type,
83+
"contents": [{
84+
"id": res.id,
85+
"quantity": 1
86+
}]
87+
}
88+
}
89+
};
90+
metaPixelTracker(addToCartConfigPixel);
7591
}
7692
});
7793
}
7894
});
7995
});
8096
</script>
81-
<?php } ?>
97+
<?php endif; ?>

0 commit comments

Comments
 (0)