Skip to content

Send different base currency in Google analytics #10508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/.metadata
/.project
/.settings
/.vscode
atlassian*
/nbproject
/robots.txt
Expand Down
18 changes: 10 additions & 8 deletions app/code/Magento/GoogleAnalytics/Block/Ga.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public function getOrdersTrackingCode()
$result[] = "ga('require', 'ec', 'ec.js');";

foreach ($collection as $order) {
$result[] = "ga('set', 'currencyCode', '" . $order->getOrderCurrencyCode() . "');";
foreach ($order->getAllVisibleItems() as $item) {
$result[] = sprintf(
"ga('ec:addProduct', {
Expand All @@ -130,7 +131,7 @@ public function getOrdersTrackingCode()
});",
$this->escapeJs($item->getSku()),
$this->escapeJs($item->getName()),
$item->getBasePrice(),
$item->getPrice(),
$item->getQtyOrdered()
);
}
Expand All @@ -145,9 +146,9 @@ public function getOrdersTrackingCode()
});",
$order->getIncrementId(),
$this->escapeJs($this->_storeManager->getStore()->getFrontendName()),
$order->getBaseGrandTotal(),
$order->getBaseTaxAmount(),
$order->getBaseShippingAmount()
$order->getGrandTotal(),
$order->getTaxAmount(),
$order->getShippingAmount()
);

$result[] = "ga('send', 'pageview');";
Expand Down Expand Up @@ -232,17 +233,18 @@ public function getOrdersTrackingData()
$result['products'][] = [
'id' => $this->escapeJs($item->getSku()),
'name' => $this->escapeJs($item->getName()),
'price' => $item->getBasePrice(),
'price' => $item->getPrice(),
'quantity' => $item->getQtyOrdered(),
];
}
$result['orders'][] = [
'id' => $order->getIncrementId(),
'affiliation' => $this->escapeJs($this->_storeManager->getStore()->getFrontendName()),
'revenue' => $order->getBaseGrandTotal(),
'tax' => $order->getBaseTaxAmount(),
'shipping' => $order->getBaseShippingAmount(),
'revenue' => $order->getGrandTotal(),
'tax' => $order->getTaxAmount(),
'shipping' => $order->getShippingAmount(),
];
$result['currency'] = $order->getOrderCurrencyCode();
}
return $result;
}
Expand Down
13 changes: 8 additions & 5 deletions app/code/Magento/GoogleAnalytics/Test/Unit/Block/GaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public function testOrderTrackingCode()
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);

$expectedCode = "ga('require', 'ec', 'ec.js');
ga('set', 'currencyCode', 'USD');
ga('ec:addProduct', {
'id': 'sku0',
'name': 'testName0',
Expand Down Expand Up @@ -165,7 +166,8 @@ public function testOrderTrackingData()
'price' => 0.00,
'quantity' => 1
]
]
],
'currency' => 'USD'
];

$this->gaBlock->setOrderIds([1, 2]);
Expand Down Expand Up @@ -202,17 +204,18 @@ protected function createOrderMock($orderItemCount = 1)
->getMock();
$orderItemMock->expects($this->once())->method('getSku')->willReturn('sku' . $i);
$orderItemMock->expects($this->once())->method('getName')->willReturn('testName' . $i);
$orderItemMock->expects($this->once())->method('getBasePrice')->willReturn($i . '.00');
$orderItemMock->expects($this->once())->method('getPrice')->willReturn($i . '.00');
$orderItemMock->expects($this->once())->method('getQtyOrdered')->willReturn($i + 1);
$orderItems[] = $orderItemMock;
}

$orderMock = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
$orderMock->expects($this->once())->method('getIncrementId')->willReturn(100);
$orderMock->expects($this->once())->method('getAllVisibleItems')->willReturn($orderItems);
$orderMock->expects($this->once())->method('getBaseGrandTotal')->willReturn(10);
$orderMock->expects($this->once())->method('getBaseTaxAmount')->willReturn(2);
$orderMock->expects($this->once())->method('getBaseShippingAmount')->willReturn($orderItemCount);
$orderMock->expects($this->once())->method('getGrandTotal')->willReturn(10);
$orderMock->expects($this->once())->method('getTaxAmount')->willReturn(2);
$orderMock->expects($this->once())->method('getShippingAmount')->willReturn($orderItemCount);
$orderMock->expects($this->once())->method('getOrderCurrencyCode')->willReturn('USD');
return $orderMock;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ define([
if (config.ordersTrackingData) {
ga('require', 'ec', 'ec.js');

ga('set', 'currencyCode', config.ordersTrackingData.currency);

// Collect product data for GA
if (config.ordersTrackingData.products) {
$.each(config.ordersTrackingData.products, function (index, value) {
Expand Down