8
8
9
9
namespace Magento \GoogleAnalytics \Block ;
10
10
11
+ use Magento \Framework \App \ObjectManager ;
12
+
11
13
/**
12
14
* GoogleAnalytics Page Block
13
15
*
@@ -27,20 +29,28 @@ class Ga extends \Magento\Framework\View\Element\Template
27
29
*/
28
30
protected $ _salesOrderCollection ;
29
31
32
+ /**
33
+ * @var \Magento\Cookie\Helper\Cookie
34
+ */
35
+ private $ cookieHelper ;
36
+
30
37
/**
31
38
* @param \Magento\Framework\View\Element\Template\Context $context
32
39
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $salesOrderCollection
33
40
* @param \Magento\GoogleAnalytics\Helper\Data $googleAnalyticsData
34
41
* @param array $data
42
+ * @param \Magento\Cookie\Helper\Cookie|null $cookieHelper
35
43
*/
36
44
public function __construct (
37
45
\Magento \Framework \View \Element \Template \Context $ context ,
38
46
\Magento \Sales \Model \ResourceModel \Order \CollectionFactory $ salesOrderCollection ,
39
47
\Magento \GoogleAnalytics \Helper \Data $ googleAnalyticsData ,
40
- array $ data = []
48
+ array $ data = [],
49
+ \Magento \Cookie \Helper \Cookie $ cookieHelper = null
41
50
) {
42
51
$ this ->_googleAnalyticsData = $ googleAnalyticsData ;
43
52
$ this ->_salesOrderCollection = $ salesOrderCollection ;
53
+ $ this ->cookieHelper = $ cookieHelper ?: ObjectManager::getInstance ()->get (\Magento \Cookie \Helper \Cookie::class);
44
54
parent ::__construct ($ context , $ data );
45
55
}
46
56
@@ -73,22 +83,17 @@ public function getPageName()
73
83
* @return string
74
84
* @link https://developers.google.com/analytics/devguides/collection/analyticsjs/method-reference#set
75
85
* @link https://developers.google.com/analytics/devguides/collection/analyticsjs/method-reference#gaObjectMethods
86
+ * @deprecated please use getPageTrackingData method
76
87
*/
77
88
public function getPageTrackingCode ($ accountId )
78
89
{
79
- $ pageName = trim ($ this ->getPageName ());
80
- $ optPageURL = '' ;
81
- if ($ pageName && substr ($ pageName , 0 , 1 ) == '/ ' && strlen ($ pageName ) > 1 ) {
82
- $ optPageURL = ", ' " . $ this ->escapeHtmlAttr ($ pageName , false ) . "' " ;
83
- }
84
-
85
90
$ anonymizeIp = "" ;
86
91
if ($ this ->_googleAnalyticsData ->isAnonymizedIpActive ()) {
87
92
$ anonymizeIp = "\nga('set', 'anonymizeIp', true); " ;
88
93
}
89
94
90
95
return "\nga('create', ' " . $ this ->escapeHtmlAttr ($ accountId , false )
91
- . "', 'auto'); {$ anonymizeIp }\nga('send', 'pageview' {$ optPageURL }); \n" ;
96
+ . "', 'auto'); {$ anonymizeIp }\nga('send', 'pageview' {$ this -> getOptPageUrl () }); \n" ;
92
97
}
93
98
94
99
/**
@@ -99,6 +104,7 @@ public function getPageTrackingCode($accountId)
99
104
* @link https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#transaction
100
105
*
101
106
* @return string|void
107
+ * @deprecated please use getOrdersTrackingData method
102
108
*/
103
109
public function getOrdersTrackingCode ()
104
110
{
@@ -162,4 +168,97 @@ protected function _toHtml()
162
168
163
169
return parent ::_toHtml ();
164
170
}
171
+
172
+ /**
173
+ * Return cookie restriction mode value.
174
+ *
175
+ * @return bool
176
+ */
177
+ public function isCookieRestrictionModeEnabled ()
178
+ {
179
+ return $ this ->cookieHelper ->isCookieRestrictionModeEnabled ();
180
+ }
181
+
182
+ /**
183
+ * Return current website id.
184
+ *
185
+ * @return int
186
+ */
187
+ public function getCurrentWebsiteId ()
188
+ {
189
+ return $ this ->_storeManager ->getWebsite ()->getId ();
190
+ }
191
+
192
+ /**
193
+ * Return information about page for GA tracking
194
+ *
195
+ * @link https://developers.google.com/analytics/devguides/collection/analyticsjs/method-reference#set
196
+ * @link https://developers.google.com/analytics/devguides/collection/analyticsjs/method-reference#gaObjectMethods
197
+ *
198
+ * @param string $accountId
199
+ * @return array
200
+ */
201
+ public function getPageTrackingData ($ accountId )
202
+ {
203
+ return [
204
+ 'optPageUrl ' => $ this ->getOptPageUrl (),
205
+ 'isAnonymizedIpActive ' => $ this ->_googleAnalyticsData ->isAnonymizedIpActive (),
206
+ 'accountId ' => $ this ->escapeHtmlAttr ($ accountId , false )
207
+ ];
208
+ }
209
+
210
+ /**
211
+ * Return information about order and items for GA tracking.
212
+ *
213
+ * @link https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#checkout-options
214
+ * @link https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-transactions
215
+ * @link https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#transaction
216
+ *
217
+ * @return array
218
+ */
219
+ public function getOrdersTrackingData ()
220
+ {
221
+ $ result = [];
222
+ $ orderIds = $ this ->getOrderIds ();
223
+ if (empty ($ orderIds ) || !is_array ($ orderIds )) {
224
+ return $ result ;
225
+ }
226
+
227
+ $ collection = $ this ->_salesOrderCollection ->create ();
228
+ $ collection ->addFieldToFilter ('entity_id ' , ['in ' => $ orderIds ]);
229
+
230
+ foreach ($ collection as $ order ) {
231
+ foreach ($ order ->getAllVisibleItems () as $ item ) {
232
+ $ result ['products ' ][] = [
233
+ 'id ' => $ this ->escapeJs ($ item ->getSku ()),
234
+ 'name ' => $ this ->escapeJs ($ item ->getName ()),
235
+ 'price ' => $ item ->getBasePrice (),
236
+ 'quantity ' => $ item ->getQtyOrdered (),
237
+ ];
238
+ }
239
+ $ result ['orders ' ][] = [
240
+ 'id ' => $ order ->getIncrementId (),
241
+ 'affiliation ' => $ this ->escapeJs ($ this ->_storeManager ->getStore ()->getFrontendName ()),
242
+ 'revenue ' => $ order ->getBaseGrandTotal (),
243
+ 'tax ' => $ order ->getBaseTaxAmount (),
244
+ 'shipping ' => $ order ->getBaseShippingAmount (),
245
+ ];
246
+ }
247
+ return $ result ;
248
+ }
249
+
250
+ /**
251
+ * Return page url for tracking.
252
+ *
253
+ * @return string
254
+ */
255
+ private function getOptPageUrl ()
256
+ {
257
+ $ optPageURL = '' ;
258
+ $ pageName = trim ($ this ->getPageName ());
259
+ if ($ pageName && substr ($ pageName , 0 , 1 ) == '/ ' && strlen ($ pageName ) > 1 ) {
260
+ $ optPageURL = ", ' " . $ this ->escapeHtmlAttr ($ pageName , false ) . "' " ;
261
+ }
262
+ return $ optPageURL ;
263
+ }
165
264
}
0 commit comments