forked from magepal/magento2-google-tag-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataLayerAbstract.php
executable file
·294 lines (258 loc) · 6.75 KB
/
DataLayerAbstract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
* Copyright © MagePal LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | [email protected]
*/
namespace MagePal\GoogleTagManager\Block;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use MagePal\GoogleTagManager\Helper\Data as GtmHelper;
/**
* @method setBlockName($name)
* @method getBlockName()
* @method setList($name)
* @method getList()
* @method setListType($type)
* @method getListType()
* @method setItemListName($name)
* @method getItemListName()
* @method setUseWidgetTitle($title)
* @method getUseWidgetTitle()
* @method getShowCategory()
* @method getCatalogWidgetBlockList()
*/
class DataLayerAbstract extends Template
{
/**
* @var GtmHelper
*/
protected $_gtmHelper;
/**
* @var string
*/
protected $dataLayerEventName = 'magepal_datalayer';
/**
* @var array
*/
protected $_additionalVariables = [];
/**
* Push elements last to the data layer
* @var array
*/
protected $customVariables = [];
/**
* @var array
*/
protected $_variables = [];
/**
* @param Context $context
* @param GtmHelper $gtmHelper
* @param array $data
* @throws NoSuchEntityException
*/
public function __construct(
Context $context,
GtmHelper $gtmHelper,
array $data = []
) {
$this->_gtmHelper = $gtmHelper;
parent::__construct($context, $data);
$this->_init();
}
/**
* @return $this
* @throws NoSuchEntityException
*/
protected function _init()
{
$this->addVariable('ecommerce', ['currencyCode' => $this->getStoreCurrencyCode()]);
$this->addVariable('pageType', $this->_request->getFullActionName());
$this->addVariable('list', 'other');
return $this;
}
/**
* Return data layer json
*
* @return array
*/
public function getDataLayer()
{
$this->_eventManager->dispatch(
$this->dataLayerEventName,
['dataLayer' => $this]
);
return $this->processDataLayer();
}
/**
* @return array
*/
public function processDataLayer()
{
$result = [];
if (!empty($this->getVariables())) {
$result[] = $this->getVariables();
}
if (!empty($this->_additionalVariables) && is_array($this->_additionalVariables)) {
foreach ($this->_additionalVariables as $custom) {
$result[] = $custom;
}
}
if (!empty($this->customVariables)) {
ksort($this->customVariables);
foreach ($this->customVariables as $priorityVariable) {
foreach ($priorityVariable as $data) {
$result[] = $data;
}
}
}
return $result;
}
/**
* @return string
*/
public function getDataLayerJson()
{
return json_encode($this->getDataLayer());
}
/**
* @return string
*/
public function getDataLayerJs()
{
$result = [];
foreach ($this->getDataLayer() as $data) {
$result[] = sprintf("window.%s.push(%s);\n", $this->getDataLayerName(), json_encode($data));
}
return implode("\n", $result);
}
/**
* Add Variables
* @param string $name
* @param string|array $value
* @return $this
*/
public function addVariable($name, $value)
{
if (!empty($name)) {
$this->_variables[$name] = $value;
}
return $this;
}
/**
* Return Data Layer Variables
*
* @return array
*/
public function getVariables()
{
return $this->_variables;
}
/**
* Add variable to the custom push data layer
*
* @deprecated - use addCustomDataLayer and addCustomDataLayerByEvent
* @param $name
* @param null $value
* @return $this
*/
public function addAdditionalVariable($name, $value = null)
{
if (is_array($name)) {
$this->_additionalVariables[] = $name;
} else {
$this->_additionalVariables[] = [$name => $value];
}
return $this;
}
/**
* Add variable to the custom push data layer
*
* @param array $data
* @param int $priority
* @param null $group
* @return $this
*/
public function addCustomDataLayer($data, $priority = 0, $group = null)
{
$priority = (int) $priority;
if (is_array($data) && empty($group)) {
$this->customVariables[$priority][] = $data;
} elseif (is_array($data) && !empty($group)) {
if (array_key_exists($priority, $this->customVariables)
&& array_key_exists($group, $this->customVariables[$priority])
) {
$this->customVariables[$priority][$group] = array_merge(
$this->customVariables[$priority][$group],
$data
);
} else {
$this->customVariables[$priority][$group] = $data;
}
}
return $this;
}
/**
* @param $event
* @param $data
* @param int $priority
* @return $this
*/
public function addCustomDataLayerByEvent($event, $data, $priority = 20)
{
if (!empty($event)) {
$data['event'] = $event;
$this->addCustomDataLayer($data, $priority, $event);
}
return $this;
}
/**
* Format Price
*
* @param $price
* @return float
*/
public function formatPrice($price)
{
return $this->_gtmHelper->formatPrice($price);
}
/**
* @return string
*/
public function getDataLayerName()
{
if (!$this->getData('data_layer_name')) {
$this->setData('data_layer_name', $this->_gtmHelper->getDataLayerName());
}
return $this->getData('data_layer_name');
}
/**
* @return string
* @throws NoSuchEntityException
*/
public function getStoreCurrencyCode()
{
return $this->_storeManager->getStore()->getCurrentCurrency()->getCode();
}
/**
* Return cookie restriction mode value.
*
* @return int
*/
public function isCookieRestrictionModeEnabled()
{
return (int) $this->_gtmHelper->isCookieRestrictionModeEnabled();
}
/**
* Return current website id.
*
* @return int
* @throws LocalizedException
*/
public function getCurrentWebsiteId()
{
return $this->_storeManager->getWebsite()->getId();
}
}