Skip to content

Commit bce0094

Browse files
MAGETWO-80195: [2.2.x] - Send different base currency in Google analytics magento#10508
1 parent 7ca7002 commit bce0094

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

.gitignore

+1
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

+10-8
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public function getOrdersTrackingCode()
121121
$result[] = "ga('require', 'ec', 'ec.js');";
122122

123123
foreach ($collection as $order) {
124+
$result[] = "ga('set', 'currencyCode', '" . $order->getOrderCurrencyCode() . "');";
124125
foreach ($order->getAllVisibleItems() as $item) {
125126
$result[] = sprintf(
126127
"ga('ec:addProduct', {
@@ -131,7 +132,7 @@ public function getOrdersTrackingCode()
131132
});",
132133
$this->escapeJs($item->getSku()),
133134
$this->escapeJs($item->getName()),
134-
$item->getBasePrice(),
135+
$item->getPrice(),
135136
$item->getQtyOrdered()
136137
);
137138
}
@@ -146,9 +147,9 @@ public function getOrdersTrackingCode()
146147
});",
147148
$order->getIncrementId(),
148149
$this->escapeJs($this->_storeManager->getStore()->getFrontendName()),
149-
$order->getBaseGrandTotal(),
150-
$order->getBaseTaxAmount(),
151-
$order->getBaseShippingAmount()
150+
$order->getGrandTotal(),
151+
$order->getTaxAmount(),
152+
$order->getShippingAmount()
152153
);
153154

154155
$result[] = "ga('send', 'pageview');";
@@ -237,17 +238,18 @@ public function getOrdersTrackingData()
237238
$result['products'][] = [
238239
'id' => $this->escapeJs($item->getSku()),
239240
'name' => $this->escapeJs($item->getName()),
240-
'price' => $item->getBasePrice(),
241+
'price' => $item->getPrice(),
241242
'quantity' => $item->getQtyOrdered(),
242243
];
243244
}
244245
$result['orders'][] = [
245246
'id' => $order->getIncrementId(),
246247
'affiliation' => $this->escapeJs($this->_storeManager->getStore()->getFrontendName()),
247-
'revenue' => $order->getBaseGrandTotal(),
248-
'tax' => $order->getBaseTaxAmount(),
249-
'shipping' => $order->getBaseShippingAmount(),
248+
'revenue' => $order->getGrandTotal(),
249+
'tax' => $order->getTaxAmount(),
250+
'shipping' => $order->getShippingAmount(),
250251
];
252+
$result['currency'] = $order->getOrderCurrencyCode();
251253
}
252254
return $result;
253255
}

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

+8-5
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

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ define([
5757
if (config.ordersTrackingData) {
5858
ga('require', 'ec', 'ec.js');
5959

60+
//Set currency code
61+
ga('set', 'currencyCode', config.ordersTrackingData.currency);
62+
6063
// Collect product data for GA
6164
if (config.ordersTrackingData.products) {
6265
$.each(config.ordersTrackingData.products, function (index, value) {

0 commit comments

Comments
 (0)