1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\GoogleTagManager; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Traits\Macroable; |
6
|
|
|
use Spatie\GoogleTagManager\Exceptions\EnvironmentParametersNotSetException; |
7
|
|
|
|
8
|
|
|
class GoogleTagManager |
9
|
|
|
{ |
10
|
|
|
use Macroable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $id; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
protected $enabled; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool true if the environments are used in Google Tag Manager |
24
|
|
|
*/ |
25
|
|
|
protected $environmentsEnabled; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string the value to use as gtm_auth parameter (for environments in Google Tag Manager) |
29
|
|
|
*/ |
30
|
|
|
protected $gtmAuth; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string the value to use as gtm_preview parameter (for environments in Google Tag Manager) |
34
|
|
|
*/ |
35
|
|
|
protected $gtmPreview; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \Spatie\GoogleTagManager\DataLayer |
39
|
|
|
*/ |
40
|
|
|
protected $dataLayer; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \Spatie\GoogleTagManager\DataLayer |
44
|
|
|
*/ |
45
|
|
|
protected $flashDataLayer; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Illuminate\Support\Collection |
49
|
|
|
*/ |
50
|
|
|
protected $pushDataLayer; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $id |
54
|
|
|
*/ |
55
|
|
|
public function __construct($id) |
56
|
|
|
{ |
57
|
|
|
$this->id = $id; |
58
|
|
|
$this->dataLayer = new DataLayer(); |
59
|
|
|
$this->flashDataLayer = new DataLayer(); |
60
|
|
|
$this->pushDataLayer = new \Illuminate\Support\Collection(); |
61
|
|
|
|
62
|
|
|
$this->enabled = true; |
63
|
|
|
$this->environmentsEnabled = false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Enable the use of environments for Google Tag Manager. |
68
|
|
|
* @param string $gtmAuth the value to use as gtm_auth |
69
|
|
|
* @param string $gtmPreview the value to use as gtm_preview |
70
|
|
|
*/ |
71
|
|
|
public function enableEnvironmentWithParameters($gtmAuth, $gtmPreview) |
72
|
|
|
{ |
73
|
|
|
$this->enableEnvironments(); |
74
|
|
|
$this->gtmAuth = $gtmAuth; |
75
|
|
|
$this->gtmPreview = $gtmPreview; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return the Google Tag Manager id. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function id() |
84
|
|
|
{ |
85
|
|
|
return $this->id; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check whether script rendering is enabled. |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function isEnabled() |
94
|
|
|
{ |
95
|
|
|
return $this->enabled; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check whether environments are enabled. |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function isEnvironmentsEnabled() |
104
|
|
|
{ |
105
|
|
|
return $this->environmentsEnabled; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string the environments parameters to use or an empty string if environments are not enabled. |
110
|
|
|
* |
111
|
|
|
* @throws EnvironmentParametersNotSetException if the gtmAuth and gtmPreview are not set and environments are used. |
112
|
|
|
*/ |
113
|
|
|
public function getEnvironmentParameters() |
114
|
|
|
{ |
115
|
|
|
$environmentParameters = ''; |
116
|
|
|
// Verify GTM is enabled as well, to prevent generating exception while disabled completely. |
117
|
|
|
if ($this->isEnabled() && $this->isEnvironmentsEnabled()) { |
118
|
|
|
$gtmAuth = $this->getGtmAuth(); |
119
|
|
|
$gtmPreview = $this->getGtmPreview(); |
120
|
|
|
if (empty($gtmAuth) || empty($gtmPreview)) { |
121
|
|
|
throw new EnvironmentParametersNotSetException( |
122
|
|
|
'Both parameters (gtmAuth and gtmPreview) are required.' |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
$environmentParameters = '>m_auth=' . $gtmAuth . '>m_preview=' . $gtmPreview; |
126
|
|
|
} |
127
|
|
|
return $environmentParameters; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Enable Google Tag Manager scripts rendering. |
132
|
|
|
*/ |
133
|
|
|
public function enable() |
134
|
|
|
{ |
135
|
|
|
$this->enabled = true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Disable Google Tag Manager scripts rendering. |
140
|
|
|
*/ |
141
|
|
|
public function disable() |
142
|
|
|
{ |
143
|
|
|
$this->enabled = false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Enable Google Tag Manager environments parameters rendering. |
148
|
|
|
*/ |
149
|
|
|
public function enableEnvironments() |
150
|
|
|
{ |
151
|
|
|
$this->environmentsEnabled = true; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Disable Google Tag Manager environments parameters rendering. |
156
|
|
|
*/ |
157
|
|
|
public function disableEnvironments() |
158
|
|
|
{ |
159
|
|
|
$this->environmentsEnabled = false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Return the value to use for the gtm_auth parameter. |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public function getGtmAuth() |
168
|
|
|
{ |
169
|
|
|
return $this->gtmAuth; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Return the value to use for the gtm_preview parameter. |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function getGtmPreview() |
178
|
|
|
{ |
179
|
|
|
return $this->gtmPreview; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Add data to the data layer. |
184
|
|
|
* |
185
|
|
|
* @param array|string $key |
186
|
|
|
* @param mixed $value |
187
|
|
|
*/ |
188
|
|
|
public function set($key, $value = null) |
189
|
|
|
{ |
190
|
|
|
$this->dataLayer->set($key, $value); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Retrieve the data layer. |
195
|
|
|
* |
196
|
|
|
* @return \Spatie\GoogleTagManager\DataLayer |
197
|
|
|
*/ |
198
|
|
|
public function getDataLayer() |
199
|
|
|
{ |
200
|
|
|
return $this->dataLayer; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Add data to the data layer for the next request. |
205
|
|
|
* |
206
|
|
|
* @param array|string $key |
207
|
|
|
* @param mixed $value |
208
|
|
|
*/ |
209
|
|
|
public function flash($key, $value = null) |
210
|
|
|
{ |
211
|
|
|
$this->flashDataLayer->set($key, $value); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Retrieve the data layer's data for the next request. |
216
|
|
|
* |
217
|
|
|
* @return array |
218
|
|
|
*/ |
219
|
|
|
public function getFlashData() |
220
|
|
|
{ |
221
|
|
|
return $this->flashDataLayer->toArray(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Add data to be pushed to the data layer. |
226
|
|
|
* |
227
|
|
|
* @param array|string $key |
228
|
|
|
* @param mixed $value |
229
|
|
|
*/ |
230
|
|
|
public function push($key, $value = null) |
231
|
|
|
{ |
232
|
|
|
$pushItem = new DataLayer(); |
233
|
|
|
$pushItem->set($key, $value); |
234
|
|
|
$this->pushDataLayer->push($pushItem); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Retrieve the data layer's data for the next request. |
239
|
|
|
* |
240
|
|
|
* @return \Illuminate\Support\Collection |
241
|
|
|
*/ |
242
|
|
|
public function getPushData() |
243
|
|
|
{ |
244
|
|
|
return $this->pushDataLayer; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Clear the data layer. |
249
|
|
|
*/ |
250
|
|
|
public function clear() |
251
|
|
|
{ |
252
|
|
|
$this->dataLayer = new DataLayer(); |
253
|
|
|
$this->pushDataLayer = new \Illuminate\Support\Collection(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Utility function to dump an array as json. |
258
|
|
|
* |
259
|
|
|
* @param array $data |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
|
|
public function dump($data) |
263
|
|
|
{ |
264
|
|
|
return (new DataLayer($data))->toJson(); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|