88namespace Magento \GoogleGtag \Block ;
99
1010use Magento \Cookie \Helper \Cookie ;
11- use Magento \Framework \App \ObjectManager ;
1211use Magento \Framework \View \Element \Template ;
1312use Magento \Framework \View \Element \Template \Context ;
14- use Magento \GoogleGtag \Helper \Data ;
13+ use Magento \GoogleGtag \Helper \GtagConfiguration ;
1514use Magento \Sales \Model \ResourceModel \Order \CollectionFactory ;
1615use Magento \Store \Model \ScopeInterface ;
1716
2322class Ga extends Template
2423{
2524 /**
26- * @var Data
25+ * @var GtagConfiguration
2726 */
28- protected $ _googleGtagData = null ;
27+ private $ googleGtagConfig ;
2928
3029 /**
3130 * @var CollectionFactory
3231 */
33- protected $ _salesOrderCollection ;
32+ private $ salesOrderCollection ;
3433
3534 /**
3635 * @var Cookie
@@ -40,20 +39,20 @@ class Ga extends Template
4039 /**
4140 * @param Context $context
4241 * @param CollectionFactory $salesOrderCollection
43- * @param Data $googleGtagData
42+ * @param GtagConfiguration $googleGtagConfig
4443 * @param array $data
45- * @param Cookie|null $cookieHelper
44+ * @param Cookie $cookieHelper
4645 */
4746 public function __construct (
4847 Context $ context ,
4948 CollectionFactory $ salesOrderCollection ,
50- Data $ googleGtagData ,
51- array $ data = [] ,
52- Cookie $ cookieHelper = null
49+ GtagConfiguration $ googleGtagConfig ,
50+ Cookie $ cookieHelper ,
51+ array $ data = []
5352 ) {
54- $ this ->_googleGtagData = $ googleGtagData ;
55- $ this ->_salesOrderCollection = $ salesOrderCollection ;
56- $ this ->cookieHelper = $ cookieHelper ?: ObjectManager:: getInstance ()-> get (Cookie::class) ;
53+ $ this ->googleGtagConfig = $ googleGtagConfig ;
54+ $ this ->salesOrderCollection = $ salesOrderCollection ;
55+ $ this ->cookieHelper = $ cookieHelper ;
5756 parent ::__construct ($ context , $ data );
5857 }
5958
@@ -63,27 +62,27 @@ public function __construct(
6362 * @param string $path
6463 * @return mixed
6564 */
66- public function getConfig ($ path )
65+ public function getConfig ($ path ): string
6766 {
6867 return $ this ->_scopeConfig ->getValue ($ path , ScopeInterface::SCOPE_STORE );
6968 }
7069
7170 /**
7271 * Get helper
7372 *
74- * @return Data|null
73+ * @return GtagConfiguration
7574 */
76- public function getHelper ()
75+ public function getHelper (): GtagConfiguration
7776 {
78- return $ this ->_googleGtagData ;
77+ return $ this ->googleGtagConfig ;
7978 }
8079
8180 /**
8281 * Get a specific page name (may be customized via layout)
8382 *
8483 * @return string|null
8584 */
86- public function getPageName ()
85+ public function getPageName (): ? string
8786 {
8887 return $ this ->_getData ('page_name ' );
8988 }
@@ -95,7 +94,7 @@ public function getPageName()
9594 */
9695 protected function _toHtml ()
9796 {
98- if (!$ this ->_googleGtagData ->isGoogleAnalyticsAvailable ()) {
97+ if (!$ this ->googleGtagConfig ->isGoogleAnalyticsAvailable ()) {
9998 return '' ;
10099 }
101100
@@ -107,19 +106,19 @@ protected function _toHtml()
107106 *
108107 * @return bool
109108 */
110- public function isCookieRestrictionModeEnabled ()
109+ public function isCookieRestrictionModeEnabled (): bool
111110 {
112- return $ this ->cookieHelper ->isCookieRestrictionModeEnabled ();
111+ return ( bool ) $ this ->cookieHelper ->isCookieRestrictionModeEnabled ();
113112 }
114113
115114 /**
116115 * Return current website id.
117116 *
118117 * @return int
119118 */
120- public function getCurrentWebsiteId ()
119+ public function getCurrentWebsiteId (): int
121120 {
122- return $ this ->_storeManager ->getWebsite ()->getId ();
121+ return ( int ) $ this ->_storeManager ->getWebsite ()->getId ();
123122 }
124123
125124 /**
@@ -128,14 +127,14 @@ public function getCurrentWebsiteId()
128127 * @link https://developers.google.com/analytics/devguides/collection/gtagjs
129128 * @link https://developers.google.com/analytics/devguides/collection/ga4
130129 *
131- * @param string $accountId
130+ * @param string $measurementId
132131 * @return array
133132 */
134- public function getPageTrackingData ($ accountId )
133+ public function getPageTrackingData ($ measurementId ): array
135134 {
136135 return [
137136 'optPageUrl ' => $ this ->getOptPageUrl (),
138- 'accountId ' => $ this ->escapeHtmlAttr ($ accountId , false )
137+ 'measurementId ' => $ this ->escapeHtmlAttr ($ measurementId , false )
139138 ];
140139 }
141140
@@ -150,32 +149,32 @@ public function getPageTrackingData($accountId)
150149 * @return array
151150 * @since 100.2.0
152151 */
153- public function getOrdersTrackingData ()
152+ public function getOrdersTrackingData (): array
154153 {
155154 $ result = [];
156155 $ orderIds = $ this ->getOrderIds ();
157156 if (empty ($ orderIds ) || !is_array ($ orderIds )) {
158157 return $ result ;
159158 }
160159
161- $ collection = $ this ->_salesOrderCollection ->create ();
160+ $ collection = $ this ->salesOrderCollection ->create ();
162161 $ collection ->addFieldToFilter ('entity_id ' , ['in ' => $ orderIds ]);
163162
164163 foreach ($ collection as $ order ) {
165164 foreach ($ order ->getAllVisibleItems () as $ item ) {
166165 $ result ['products ' ][] = [
167166 'item_id ' => $ this ->escapeJsQuote ($ item ->getSku ()),
168167 'item_name ' => $ this ->escapeJsQuote ($ item ->getName ()),
169- 'price ' => $ this ->_googleGtagData ->formatToDec ((float ) $ item ->getPrice ()),
168+ 'price ' => $ this ->googleGtagConfig ->formatToDec ((float ) $ item ->getPrice ()),
170169 'quantity ' => (int )$ item ->getQtyOrdered (),
171170 ];
172171 }
173172 $ result ['orders ' ][] = [
174173 'transaction_id ' => $ order ->getIncrementId (),
175174 'affiliation ' => $ this ->escapeJsQuote ($ this ->_storeManager ->getStore ()->getFrontendName ()),
176- 'value ' => $ this ->_googleGtagData ->formatToDec ((float ) $ order ->getGrandTotal ()),
177- 'tax ' => $ this ->_googleGtagData ->formatToDec ((float ) $ order ->getTaxAmount ()),
178- 'shipping ' => $ this ->_googleGtagData ->formatToDec ((float ) $ order ->getShippingAmount ()),
175+ 'value ' => $ this ->googleGtagConfig ->formatToDec ((float ) $ order ->getGrandTotal ()),
176+ 'tax ' => $ this ->googleGtagConfig ->formatToDec ((float ) $ order ->getTaxAmount ()),
177+ 'shipping ' => $ this ->googleGtagConfig ->formatToDec ((float ) $ order ->getShippingAmount ()),
179178 ];
180179 $ result ['currency ' ] = $ order ->getOrderCurrencyCode ();
181180 }
@@ -187,7 +186,7 @@ public function getOrdersTrackingData()
187186 *
188187 * @return string
189188 */
190- private function getOptPageUrl ()
189+ private function getOptPageUrl (): string
191190 {
192191 $ optPageURL = '' ;
193192 $ pageName = $ this ->getPageName () !== null ? trim ($ this ->getPageName ()) : '' ;
0 commit comments