Skip to content

Commit 469c0c3

Browse files
committed
Merge branch 'ACP2E-1880' of https://github.com/magento-l3/magento2ce into PR-L3-2023-05-11
2 parents 53e9aeb + 4f6514c commit 469c0c3

File tree

2 files changed

+46
-29
lines changed

2 files changed

+46
-29
lines changed

app/code/Magento/GoogleAnalytics/Block/Ga.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function getPageName()
8282
* @link https://developers.google.com/analytics/devguides/collection/analyticsjs/method-reference#set
8383
* @link https://developers.google.com/analytics/devguides/collection/analyticsjs/method-reference#gaObjectMethods
8484
* @deprecated 100.2.0 please use getPageTrackingData method
85+
* @see getPageTrackingData method
8586
*/
8687
public function getPageTrackingCode($accountId)
8788
{
@@ -103,6 +104,7 @@ public function getPageTrackingCode($accountId)
103104
*
104105
* @return string|void
105106
* @deprecated 100.2.0 please use getOrdersTrackingData method
107+
* @see getOrdersTrackingData method
106108
*/
107109
public function getOrdersTrackingCode()
108110
{
@@ -120,33 +122,35 @@ public function getOrdersTrackingCode()
120122
foreach ($collection as $order) {
121123
$result[] = "ga('set', 'currencyCode', '" . $order->getOrderCurrencyCode() . "');";
122124
foreach ($order->getAllVisibleItems() as $item) {
125+
$quantity = $item->getQtyOrdered() * 1;
126+
$format = fmod($quantity, 1) !== 0.00 ? '%.2f' : '%d';
123127
$result[] = sprintf(
124128
"ga('ec:addProduct', {
125129
'id': '%s',
126130
'name': '%s',
127-
'price': '%s',
128-
'quantity': %s
131+
'price': %.2f,
132+
'quantity': $format
129133
});",
130134
$this->escapeJsQuote($item->getSku()),
131135
$this->escapeJsQuote($item->getName()),
132-
$item->getPrice(),
133-
$item->getQtyOrdered()
136+
(float)$item->getPrice(),
137+
$quantity
134138
);
135139
}
136140

137141
$result[] = sprintf(
138142
"ga('ec:setAction', 'purchase', {
139143
'id': '%s',
140144
'affiliation': '%s',
141-
'revenue': '%s',
142-
'tax': '%s',
143-
'shipping': '%s'
145+
'revenue': %.2f,
146+
'tax': %.2f,
147+
'shipping': %.2f
144148
});",
145149
$order->getIncrementId(),
146150
$this->escapeJsQuote($this->_storeManager->getStore()->getFrontendName()),
147-
$order->getGrandTotal(),
148-
$order->getTaxAmount(),
149-
$order->getShippingAmount()
151+
(float)$order->getGrandTotal(),
152+
(float)$order->getTaxAmount(),
153+
(float)$order->getShippingAmount(),
150154
);
151155

152156
$result[] = "ga('send', 'pageview');";
@@ -232,19 +236,20 @@ public function getOrdersTrackingData()
232236

233237
foreach ($collection as $order) {
234238
foreach ($order->getAllVisibleItems() as $item) {
239+
$quantity = $item->getQtyOrdered() * 1;
235240
$result['products'][] = [
236241
'id' => $this->escapeJsQuote($item->getSku()),
237242
'name' => $this->escapeJsQuote($item->getName()),
238-
'price' => $item->getPrice(),
239-
'quantity' => $item->getQtyOrdered(),
243+
'price' => (float)$item->getPrice(),
244+
'quantity' => $quantity,
240245
];
241246
}
242247
$result['orders'][] = [
243248
'id' => $order->getIncrementId(),
244249
'affiliation' => $this->escapeJsQuote($this->_storeManager->getStore()->getFrontendName()),
245-
'revenue' => $order->getGrandTotal(),
246-
'tax' => $order->getTaxAmount(),
247-
'shipping' => $order->getShippingAmount(),
250+
'revenue' => (float)$order->getGrandTotal(),
251+
'tax' => (float)$order->getTaxAmount(),
252+
'shipping' => (float)$order->getShippingAmount(),
248253
];
249254
$result['currency'] = $order->getOrderCurrencyCode();
250255
}

app/code/Magento/GoogleAnalytics/Test/Unit/Block/GaTest.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,21 @@ public function testOrderTrackingCode()
115115
ga('ec:addProduct', {
116116
'id': 'sku0',
117117
'name': 'testName0',
118-
'price': '0.00',
118+
'price': 0.00,
119119
'quantity': 1
120120
});
121+
ga('ec:addProduct', {
122+
'id': 'sku1',
123+
'name': 'testName1',
124+
'price': 1.00,
125+
'quantity': 1.11
126+
});
121127
ga('ec:setAction', 'purchase', {
122128
'id': '100',
123129
'affiliation': 'test',
124-
'revenue': '10',
125-
'tax': '2',
126-
'shipping': '1'
130+
'revenue': 10.00,
131+
'tax': 2.00,
132+
'shipping': 2.00
127133
});
128134
ga('send', 'pageview');";
129135

@@ -163,9 +169,9 @@ public function testOrderTrackingData()
163169
[
164170
'id' => 100,
165171
'affiliation' => 'test',
166-
'revenue' => 10,
167-
'tax' => 2,
168-
'shipping' => 1
172+
'revenue' => 10.00,
173+
'tax' => 2.00,
174+
'shipping' => 2.0
169175
]
170176
],
171177
'products' => [
@@ -174,6 +180,12 @@ public function testOrderTrackingData()
174180
'name' => 'testName0',
175181
'price' => 0.00,
176182
'quantity' => 1
183+
],
184+
[
185+
'id' => 'sku1',
186+
'name' => 'testName1',
187+
'price' => 1.00,
188+
'quantity' => 1.11
177189
]
178190
],
179191
'currency' => 'USD'
@@ -204,7 +216,7 @@ public function testGetPageTrackingData()
204216
* @param int $orderItemCount
205217
* @return Order|MockObject
206218
*/
207-
protected function createOrderMock($orderItemCount = 1)
219+
protected function createOrderMock($orderItemCount = 2)
208220
{
209221
$orderItems = [];
210222
for ($i = 0; $i < $orderItemCount; $i++) {
@@ -213,8 +225,8 @@ protected function createOrderMock($orderItemCount = 1)
213225
->getMockForAbstractClass();
214226
$orderItemMock->expects($this->once())->method('getSku')->willReturn('sku' . $i);
215227
$orderItemMock->expects($this->once())->method('getName')->willReturn('testName' . $i);
216-
$orderItemMock->expects($this->once())->method('getPrice')->willReturn($i . '.00');
217-
$orderItemMock->expects($this->once())->method('getQtyOrdered')->willReturn($i + 1);
228+
$orderItemMock->expects($this->once())->method('getPrice')->willReturn((float)($i . '.0000'));
229+
$orderItemMock->expects($this->once())->method('getQtyOrdered')->willReturn($i == 1 ? 1.11 : $i + 1);
218230
$orderItems[] = $orderItemMock;
219231
}
220232

@@ -223,9 +235,9 @@ protected function createOrderMock($orderItemCount = 1)
223235
->getMock();
224236
$orderMock->expects($this->once())->method('getIncrementId')->willReturn(100);
225237
$orderMock->expects($this->once())->method('getAllVisibleItems')->willReturn($orderItems);
226-
$orderMock->expects($this->once())->method('getGrandTotal')->willReturn(10);
227-
$orderMock->expects($this->once())->method('getTaxAmount')->willReturn(2);
228-
$orderMock->expects($this->once())->method('getShippingAmount')->willReturn($orderItemCount);
238+
$orderMock->expects($this->once())->method('getGrandTotal')->willReturn(10.00);
239+
$orderMock->expects($this->once())->method('getTaxAmount')->willReturn(2.00);
240+
$orderMock->expects($this->once())->method('getShippingAmount')->willReturn(round((float)$orderItemCount, 2));
229241
$orderMock->expects($this->once())->method('getOrderCurrencyCode')->willReturn('USD');
230242
return $orderMock;
231243
}
@@ -241,7 +253,7 @@ protected function createCollectionMock()
241253

242254
$collectionMock->expects($this->any())
243255
->method('getIterator')
244-
->willReturn(new \ArrayIterator([$this->createOrderMock(1)]));
256+
->willReturn(new \ArrayIterator([$this->createOrderMock(2)]));
245257
return $collectionMock;
246258
}
247259

0 commit comments

Comments
 (0)