88namespace Magento \GoogleGtag \Block ;
99
1010use Magento \Cookie \Helper \Cookie ;
11+ use Magento \Framework \Api \SearchCriteriaBuilder ;
12+ use Magento \Framework \Serialize \SerializerInterface ;
1113use Magento \Framework \View \Element \Template ;
1214use 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}
0 commit comments