Skip to content

Commit 1d74769

Browse files
MAGETWO-71539: Send different base currency in Google analytics #10508
2 parents f2314aa + a43e111 commit 1d74769

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/.metadata
44
/.project
55
/.settings
6+
/.vscode
67
atlassian*
78
/nbproject
89
/robots.txt

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public function getOrdersTrackingCode()
120120
$result[] = "ga('require', 'ec', 'ec.js');";
121121

122122
foreach ($collection as $order) {
123+
$result[] = "ga('set', 'currencyCode', '" . $order->getOrderCurrencyCode() . "');";
123124
foreach ($order->getAllVisibleItems() as $item) {
124125
$result[] = sprintf(
125126
"ga('ec:addProduct', {
@@ -130,7 +131,7 @@ public function getOrdersTrackingCode()
130131
});",
131132
$this->escapeJs($item->getSku()),
132133
$this->escapeJs($item->getName()),
133-
$item->getBasePrice(),
134+
$item->getPrice(),
134135
$item->getQtyOrdered()
135136
);
136137
}
@@ -145,9 +146,9 @@ public function getOrdersTrackingCode()
145146
});",
146147
$order->getIncrementId(),
147148
$this->escapeJs($this->_storeManager->getStore()->getFrontendName()),
148-
$order->getBaseGrandTotal(),
149-
$order->getBaseTaxAmount(),
150-
$order->getBaseShippingAmount()
149+
$order->getGrandTotal(),
150+
$order->getTaxAmount(),
151+
$order->getShippingAmount()
151152
);
152153

153154
$result[] = "ga('send', 'pageview');";
@@ -236,17 +237,18 @@ public function getOrdersTrackingData()
236237
$result['products'][] = [
237238
'id' => $this->escapeJs($item->getSku()),
238239
'name' => $this->escapeJs($item->getName()),
239-
'price' => $item->getBasePrice(),
240+
'price' => $item->getPrice(),
240241
'quantity' => $item->getQtyOrdered(),
241242
];
242243
}
243244
$result['orders'][] = [
244245
'id' => $order->getIncrementId(),
245246
'affiliation' => $this->escapeJs($this->_storeManager->getStore()->getFrontendName()),
246-
'revenue' => $order->getBaseGrandTotal(),
247-
'tax' => $order->getBaseTaxAmount(),
248-
'shipping' => $order->getBaseShippingAmount(),
247+
'revenue' => $order->getGrandTotal(),
248+
'tax' => $order->getTaxAmount(),
249+
'shipping' => $order->getShippingAmount(),
249250
];
251+
$result['currency'] = $order->getOrderCurrencyCode();
250252
}
251253
return $result;
252254
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public function testOrderTrackingCode()
103103
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
104104

105105
$expectedCode = "ga('require', 'ec', 'ec.js');
106+
ga('set', 'currencyCode', 'USD');
106107
ga('ec:addProduct', {
107108
'id': 'sku0',
108109
'name': 'testName0',
@@ -165,7 +166,8 @@ public function testOrderTrackingData()
165166
'price' => 0.00,
166167
'quantity' => 1
167168
]
168-
]
169+
],
170+
'currency' => 'USD'
169171
];
170172

171173
$this->gaBlock->setOrderIds([1, 2]);
@@ -202,17 +204,18 @@ protected function createOrderMock($orderItemCount = 1)
202204
->getMock();
203205
$orderItemMock->expects($this->once())->method('getSku')->willReturn('sku' . $i);
204206
$orderItemMock->expects($this->once())->method('getName')->willReturn('testName' . $i);
205-
$orderItemMock->expects($this->once())->method('getBasePrice')->willReturn($i . '.00');
207+
$orderItemMock->expects($this->once())->method('getPrice')->willReturn($i . '.00');
206208
$orderItemMock->expects($this->once())->method('getQtyOrdered')->willReturn($i + 1);
207209
$orderItems[] = $orderItemMock;
208210
}
209211

210212
$orderMock = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
211213
$orderMock->expects($this->once())->method('getIncrementId')->willReturn(100);
212214
$orderMock->expects($this->once())->method('getAllVisibleItems')->willReturn($orderItems);
213-
$orderMock->expects($this->once())->method('getBaseGrandTotal')->willReturn(10);
214-
$orderMock->expects($this->once())->method('getBaseTaxAmount')->willReturn(2);
215-
$orderMock->expects($this->once())->method('getBaseShippingAmount')->willReturn($orderItemCount);
215+
$orderMock->expects($this->once())->method('getGrandTotal')->willReturn(10);
216+
$orderMock->expects($this->once())->method('getTaxAmount')->willReturn(2);
217+
$orderMock->expects($this->once())->method('getShippingAmount')->willReturn($orderItemCount);
218+
$orderMock->expects($this->once())->method('getOrderCurrencyCode')->willReturn('USD');
216219
return $orderMock;
217220
}
218221

app/code/Magento/GoogleAnalytics/view/frontend/web/js/google-analytics.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ define([
5757
if (config.ordersTrackingData) {
5858
ga('require', 'ec', 'ec.js');
5959

60+
ga('set', 'currencyCode', config.ordersTrackingData.currency);
61+
6062
// Collect product data for GA
6163
if (config.ordersTrackingData.products) {
6264
$.each(config.ordersTrackingData.products, function (index, value) {

0 commit comments

Comments
 (0)