Skip to content

Commit 6e63230

Browse files
AC-2574: Moved Block to view model, created config Model to remove Helper
1 parent 7b41128 commit 6e63230

File tree

15 files changed

+283
-384
lines changed

15 files changed

+283
-384
lines changed

app/code/Magento/GoogleGtag/Block/Code.php

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

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

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
namespace Magento\GoogleGtag\Block;
99

1010
use Magento\Cookie\Helper\Cookie;
11+
use Magento\Framework\Api\SearchCriteriaBuilder;
12+
use Magento\Framework\Serialize\SerializerInterface;
1113
use Magento\Framework\View\Element\Template;
1214
use Magento\Framework\View\Element\Template\Context;
13-
use Magento\GoogleGtag\Helper\GtagConfiguration;
14-
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
15-
use Magento\Store\Model\ScopeInterface;
15+
use Magento\GoogleGtag\Model\Config\GtagConfig as GtagConfiguration;
16+
use Magento\Sales\Api\OrderRepositoryInterface;
1617

1718
/**
1819
* GoogleAnalytics Page Block
@@ -27,56 +28,51 @@ class Ga extends Template
2728
private $googleGtagConfig;
2829

2930
/**
30-
* @var CollectionFactory
31+
* @var Cookie
3132
*/
32-
private $salesOrderCollection;
33+
private $cookieHelper;
3334

3435
/**
35-
* @var Cookie
36+
* @var SerializerInterface
3637
*/
37-
private $cookieHelper;
38+
private $serializer;
39+
40+
/**
41+
* @var OrderRepositoryInterface
42+
*/
43+
private $orderRepository;
44+
45+
/**
46+
* @var SearchCriteriaBuilder
47+
*/
48+
private $searchCriteriaBuilder;
3849

3950
/**
4051
* @param Context $context
41-
* @param CollectionFactory $salesOrderCollection
4252
* @param GtagConfiguration $googleGtagConfig
43-
* @param array $data
4453
* @param Cookie $cookieHelper
54+
* @param SerializerInterface $serializer
55+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
56+
* @param OrderRepositoryInterface $orderRepository
57+
* @param array $data
4558
*/
4659
public function __construct(
4760
Context $context,
48-
CollectionFactory $salesOrderCollection,
4961
GtagConfiguration $googleGtagConfig,
5062
Cookie $cookieHelper,
63+
SerializerInterface $serializer,
64+
SearchCriteriaBuilder $searchCriteriaBuilder,
65+
OrderRepositoryInterface $orderRepository,
5166
array $data = []
5267
) {
5368
$this->googleGtagConfig = $googleGtagConfig;
54-
$this->salesOrderCollection = $salesOrderCollection;
5569
$this->cookieHelper = $cookieHelper;
70+
$this->serializer = $serializer;
71+
$this->orderRepository = $orderRepository;
72+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
5673
parent::__construct($context, $data);
5774
}
5875

59-
/**
60-
* Get config
61-
*
62-
* @param string $path
63-
* @return mixed
64-
*/
65-
public function getConfig($path): string
66-
{
67-
return $this->_scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE);
68-
}
69-
70-
/**
71-
* Get helper
72-
*
73-
* @return GtagConfiguration
74-
*/
75-
public function getHelper(): GtagConfiguration
76-
{
77-
return $this->googleGtagConfig;
78-
}
79-
8076
/**
8177
* Get a specific page name (may be customized via layout)
8278
*
@@ -147,7 +143,6 @@ public function getPageTrackingData($measurementId): array
147143
* @link https://developers.google.com/gtagjs/reference/event#purchase
148144
*
149145
* @return array
150-
* @since 100.2.0
151146
*/
152147
public function getOrdersTrackingData(): array
153148
{
@@ -156,25 +151,28 @@ public function getOrdersTrackingData(): array
156151
if (empty($orderIds) || !is_array($orderIds)) {
157152
return $result;
158153
}
159-
160-
$collection = $this->salesOrderCollection->create();
161-
$collection->addFieldToFilter('entity_id', ['in' => $orderIds]);
162-
163-
foreach ($collection as $order) {
154+
$this->searchCriteriaBuilder->addFilter(
155+
'entity_id',
156+
$orderIds,
157+
'in'
158+
);
159+
$collection = $this->orderRepository->getList($this->searchCriteriaBuilder->create());
160+
161+
foreach ($collection->getItems() as $order) {
164162
foreach ($order->getAllVisibleItems() as $item) {
165163
$result['products'][] = [
166164
'item_id' => $this->escapeJsQuote($item->getSku()),
167165
'item_name' => $this->escapeJsQuote($item->getName()),
168-
'price' => $this->googleGtagConfig->formatToDec((float) $item->getPrice()),
166+
'price' => number_format((float) $item->getPrice(), 2),
169167
'quantity' => (int)$item->getQtyOrdered(),
170168
];
171169
}
172170
$result['orders'][] = [
173171
'transaction_id' => $order->getIncrementId(),
174172
'affiliation' => $this->escapeJsQuote($this->_storeManager->getStore()->getFrontendName()),
175-
'value' => $this->googleGtagConfig->formatToDec((float) $order->getGrandTotal()),
176-
'tax' => $this->googleGtagConfig->formatToDec((float) $order->getTaxAmount()),
177-
'shipping' => $this->googleGtagConfig->formatToDec((float) $order->getShippingAmount()),
173+
'value' => number_format((float) $order->getGrandTotal(), 2),
174+
'tax' => number_format((float) $order->getTaxAmount(), 2),
175+
'shipping' => number_format((float) $order->getShippingAmount(), 2),
178176
];
179177
$result['currency'] = $order->getOrderCurrencyCode();
180178
}
@@ -195,4 +193,21 @@ private function getOptPageUrl(): string
195193
}
196194
return $optPageURL;
197195
}
196+
197+
/**
198+
* Provide analytics events data
199+
*
200+
* @return bool|string
201+
*/
202+
public function getAnalyticsData()
203+
{
204+
$analyticData = [
205+
'isCookieRestrictionModeEnabled' => $this->isCookieRestrictionModeEnabled(),
206+
'currentWebsite' => $this->getCurrentWebsiteId(),
207+
'cookieName' => Cookie::IS_USER_ALLOWED_SAVE_COOKIE,
208+
'pageTrackingData' => $this->getPageTrackingData($this->googleGtagConfig->getMeasurementId()),
209+
'ordersTrackingData' => $this->getOrdersTrackingData()
210+
];
211+
return $this->serializer->serialize($analyticData);
212+
}
198213
}

app/code/Magento/GoogleGtag/Block/Head.php

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

app/code/Magento/GoogleGtag/Helper/GtagConfiguration.php renamed to app/code/Magento/GoogleGtag/Model/Config/GtagConfig.php

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,47 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\GoogleGtag\Helper;
8+
namespace Magento\GoogleGtag\Model\Config;
99

10-
use Magento\Framework\App\Helper\AbstractHelper;
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
1111
use Magento\Store\Model\ScopeInterface;
1212
use Magento\Store\Model\Store;
1313

14-
/**
15-
* GoogleAnalytics data helper
16-
*
17-
* @api
18-
*/
19-
class GtagConfiguration extends AbstractHelper
14+
class GtagConfig
2015
{
2116
/**
2217
* Config paths for using throughout the code
2318
*/
24-
public const XML_PATH_ACTIVE = 'google/gtag/analytics4/active';
19+
private const XML_PATH_ACTIVE = 'google/gtag/analytics4/active';
2520

26-
public const XML_PATH_MEASUREMENT_ID = 'google/gtag/analytics4/measurement_id';
21+
private const XML_PATH_MEASUREMENT_ID = 'google/gtag/analytics4/measurement_id';
2722

2823
/**
29-
* Anonymize IP Default Yes
30-
*/
31-
public const DEFAULT_YES = 1;
32-
33-
/**#@-*/
34-
35-
/**#@+
3624
* Google AdWords conversion src
3725
*/
38-
public const GTAG_GLOBAL_SITE_TAG_SRC = 'https://www.googletagmanager.com/gtag/js?id=';
26+
private const GTAG_GLOBAL_SITE_TAG_SRC = 'https://www.googletagmanager.com/gtag/js?id=';
3927

4028
/**#@+
4129
* Google AdWords config data
4230
*/
43-
public const XML_PATH_ADWORD_ACTIVE = 'google/gtag/adwords/active';
31+
private const XML_PATH_ADWORD_ACTIVE = 'google/gtag/adwords/active';
32+
33+
private const XML_PATH_CONVERSION_ID = 'google/gtag/adwords/conversion_id';
34+
35+
private const XML_PATH_CONVERSION_LABEL = 'google/gtag/adwords/conversion_label';
4436

45-
public const XML_PATH_CONVERSION_ID = 'google/gtag/adwords/conversion_id';
37+
/**
38+
* @var ScopeConfigInterface
39+
*/
40+
private $scopeConfig;
4641

47-
public const XML_PATH_CONVERSION_LABEL = 'google/gtag/adwords/conversion_label';
42+
/**
43+
* @param ScopeConfigInterface $scopeConfig
44+
*/
45+
public function __construct(ScopeConfigInterface $scopeConfig)
46+
{
47+
$this->scopeConfig = $scopeConfig;
48+
}
4849

4950
/**
5051
* Whether GA is ready to use
@@ -62,7 +63,7 @@ public function isGoogleAnalyticsAvailable($store = null): bool
6263
}
6364

6465
/**
65-
* Get Account Id, depending on property type Tracking Id (UA) or Measurement Id (GA4)
66+
* Get Measurement Id (GA4)
6667
*
6768
* @return string
6869
*/
@@ -139,15 +140,4 @@ public function getConversionLabel(): string
139140
ScopeInterface::SCOPE_STORE
140141
);
141142
}
142-
143-
/**
144-
* Format Data
145-
*
146-
* @param float $numberString
147-
* @return Float
148-
*/
149-
public function formatToDec($numberString)
150-
{
151-
return number_format($numberString, 2);
152-
}
153143
}

0 commit comments

Comments
 (0)