8
8
namespace Magento \GoogleGtag \Block ;
9
9
10
10
use Magento \Cookie \Helper \Cookie ;
11
- use Magento \Framework \App \ObjectManager ;
12
11
use Magento \Framework \View \Element \Template ;
13
12
use Magento \Framework \View \Element \Template \Context ;
14
- use Magento \GoogleGtag \Helper \Data ;
13
+ use Magento \GoogleGtag \Helper \GtagConfiguration ;
15
14
use Magento \Sales \Model \ResourceModel \Order \CollectionFactory ;
16
15
use Magento \Store \Model \ScopeInterface ;
17
16
23
22
class Ga extends Template
24
23
{
25
24
/**
26
- * @var Data
25
+ * @var GtagConfiguration
27
26
*/
28
- protected $ _googleGtagData = null ;
27
+ private $ googleGtagConfig ;
29
28
30
29
/**
31
30
* @var CollectionFactory
32
31
*/
33
- protected $ _salesOrderCollection ;
32
+ private $ salesOrderCollection ;
34
33
35
34
/**
36
35
* @var Cookie
@@ -40,20 +39,20 @@ class Ga extends Template
40
39
/**
41
40
* @param Context $context
42
41
* @param CollectionFactory $salesOrderCollection
43
- * @param Data $googleGtagData
42
+ * @param GtagConfiguration $googleGtagConfig
44
43
* @param array $data
45
- * @param Cookie|null $cookieHelper
44
+ * @param Cookie $cookieHelper
46
45
*/
47
46
public function __construct (
48
47
Context $ context ,
49
48
CollectionFactory $ salesOrderCollection ,
50
- Data $ googleGtagData ,
51
- array $ data = [] ,
52
- Cookie $ cookieHelper = null
49
+ GtagConfiguration $ googleGtagConfig ,
50
+ Cookie $ cookieHelper ,
51
+ array $ data = []
53
52
) {
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 ;
57
56
parent ::__construct ($ context , $ data );
58
57
}
59
58
@@ -63,27 +62,27 @@ public function __construct(
63
62
* @param string $path
64
63
* @return mixed
65
64
*/
66
- public function getConfig ($ path )
65
+ public function getConfig ($ path ): string
67
66
{
68
67
return $ this ->_scopeConfig ->getValue ($ path , ScopeInterface::SCOPE_STORE );
69
68
}
70
69
71
70
/**
72
71
* Get helper
73
72
*
74
- * @return Data|null
73
+ * @return GtagConfiguration
75
74
*/
76
- public function getHelper ()
75
+ public function getHelper (): GtagConfiguration
77
76
{
78
- return $ this ->_googleGtagData ;
77
+ return $ this ->googleGtagConfig ;
79
78
}
80
79
81
80
/**
82
81
* Get a specific page name (may be customized via layout)
83
82
*
84
83
* @return string|null
85
84
*/
86
- public function getPageName ()
85
+ public function getPageName (): ? string
87
86
{
88
87
return $ this ->_getData ('page_name ' );
89
88
}
@@ -95,7 +94,7 @@ public function getPageName()
95
94
*/
96
95
protected function _toHtml ()
97
96
{
98
- if (!$ this ->_googleGtagData ->isGoogleAnalyticsAvailable ()) {
97
+ if (!$ this ->googleGtagConfig ->isGoogleAnalyticsAvailable ()) {
99
98
return '' ;
100
99
}
101
100
@@ -107,19 +106,19 @@ protected function _toHtml()
107
106
*
108
107
* @return bool
109
108
*/
110
- public function isCookieRestrictionModeEnabled ()
109
+ public function isCookieRestrictionModeEnabled (): bool
111
110
{
112
- return $ this ->cookieHelper ->isCookieRestrictionModeEnabled ();
111
+ return ( bool ) $ this ->cookieHelper ->isCookieRestrictionModeEnabled ();
113
112
}
114
113
115
114
/**
116
115
* Return current website id.
117
116
*
118
117
* @return int
119
118
*/
120
- public function getCurrentWebsiteId ()
119
+ public function getCurrentWebsiteId (): int
121
120
{
122
- return $ this ->_storeManager ->getWebsite ()->getId ();
121
+ return ( int ) $ this ->_storeManager ->getWebsite ()->getId ();
123
122
}
124
123
125
124
/**
@@ -128,14 +127,14 @@ public function getCurrentWebsiteId()
128
127
* @link https://developers.google.com/analytics/devguides/collection/gtagjs
129
128
* @link https://developers.google.com/analytics/devguides/collection/ga4
130
129
*
131
- * @param string $accountId
130
+ * @param string $measurementId
132
131
* @return array
133
132
*/
134
- public function getPageTrackingData ($ accountId )
133
+ public function getPageTrackingData ($ measurementId ): array
135
134
{
136
135
return [
137
136
'optPageUrl ' => $ this ->getOptPageUrl (),
138
- 'accountId ' => $ this ->escapeHtmlAttr ($ accountId , false )
137
+ 'measurementId ' => $ this ->escapeHtmlAttr ($ measurementId , false )
139
138
];
140
139
}
141
140
@@ -150,32 +149,32 @@ public function getPageTrackingData($accountId)
150
149
* @return array
151
150
* @since 100.2.0
152
151
*/
153
- public function getOrdersTrackingData ()
152
+ public function getOrdersTrackingData (): array
154
153
{
155
154
$ result = [];
156
155
$ orderIds = $ this ->getOrderIds ();
157
156
if (empty ($ orderIds ) || !is_array ($ orderIds )) {
158
157
return $ result ;
159
158
}
160
159
161
- $ collection = $ this ->_salesOrderCollection ->create ();
160
+ $ collection = $ this ->salesOrderCollection ->create ();
162
161
$ collection ->addFieldToFilter ('entity_id ' , ['in ' => $ orderIds ]);
163
162
164
163
foreach ($ collection as $ order ) {
165
164
foreach ($ order ->getAllVisibleItems () as $ item ) {
166
165
$ result ['products ' ][] = [
167
166
'item_id ' => $ this ->escapeJsQuote ($ item ->getSku ()),
168
167
'item_name ' => $ this ->escapeJsQuote ($ item ->getName ()),
169
- 'price ' => $ this ->_googleGtagData ->formatToDec ((float ) $ item ->getPrice ()),
168
+ 'price ' => $ this ->googleGtagConfig ->formatToDec ((float ) $ item ->getPrice ()),
170
169
'quantity ' => (int )$ item ->getQtyOrdered (),
171
170
];
172
171
}
173
172
$ result ['orders ' ][] = [
174
173
'transaction_id ' => $ order ->getIncrementId (),
175
174
'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 ()),
179
178
];
180
179
$ result ['currency ' ] = $ order ->getOrderCurrencyCode ();
181
180
}
@@ -187,7 +186,7 @@ public function getOrdersTrackingData()
187
186
*
188
187
* @return string
189
188
*/
190
- private function getOptPageUrl ()
189
+ private function getOptPageUrl (): string
191
190
{
192
191
$ optPageURL = '' ;
193
192
$ pageName = $ this ->getPageName () !== null ? trim ($ this ->getPageName ()) : '' ;
0 commit comments