1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\GoogleTagManager; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Traits\Macroable; |
6
|
|
|
|
7
|
|
|
class GoogleTagManager |
8
|
|
|
{ |
9
|
|
|
use Macroable; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string[] |
13
|
|
|
*/ |
14
|
|
|
protected $id; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $layer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $enabled; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Spatie\GoogleTagManager\DataLayer |
28
|
|
|
*/ |
29
|
|
|
protected $dataLayer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Spatie\GoogleTagManager\DataLayer |
33
|
|
|
*/ |
34
|
|
|
protected $flashDataLayer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Spatie\GoogleTagManager\DataLayer |
38
|
|
|
*/ |
39
|
|
|
protected $noJSDataLayer; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \Illuminate\Support\Collection |
43
|
|
|
*/ |
44
|
|
|
protected $pushDataLayer; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string[] $id |
48
|
|
|
* @param string $layer |
49
|
|
|
*/ |
50
|
|
|
public function __construct($id, $layer) |
51
|
|
|
{ |
52
|
|
|
$this->id = $id; |
53
|
|
|
$this->layer = $layer; |
54
|
|
|
$this->dataLayer = new DataLayer(); |
55
|
|
|
$this->flashDataLayer = new DataLayer(); |
56
|
|
|
$this->noJSDataLayer = new DataLayer(); |
57
|
|
|
$this->pushDataLayer = new \Illuminate\Support\Collection(); |
58
|
|
|
|
59
|
|
|
$this->enabled = true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the Google Tag Manager id. |
64
|
|
|
* |
65
|
|
|
* @return string[] |
66
|
|
|
*/ |
67
|
|
|
public function id() |
68
|
|
|
{ |
69
|
|
|
return $this->id; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return the Google Tag Manager id. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getLayer() |
78
|
|
|
{ |
79
|
|
|
return $this->layer; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Check whether script rendering is enabled. |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function isEnabled() |
88
|
|
|
{ |
89
|
|
|
return $this->enabled; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Enable Google Tag Manager scripts rendering. |
94
|
|
|
*/ |
95
|
|
|
public function enable() |
96
|
|
|
{ |
97
|
|
|
$this->enabled = true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Disable Google Tag Manager scripts rendering. |
102
|
|
|
*/ |
103
|
|
|
public function disable() |
104
|
|
|
{ |
105
|
|
|
$this->enabled = false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Add data to the data layer. |
110
|
|
|
* |
111
|
|
|
* @param array|string $key |
112
|
|
|
* @param mixed $value |
113
|
|
|
*/ |
114
|
|
|
public function set($key, $value = null) |
115
|
|
|
{ |
116
|
|
|
$this->dataLayer->set($key, $value); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Retrieve the data layer. |
121
|
|
|
* |
122
|
|
|
* @return \Spatie\GoogleTagManager\DataLayer |
123
|
|
|
*/ |
124
|
|
|
public function getDataLayer() |
125
|
|
|
{ |
126
|
|
|
return $this->dataLayer; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Add data to the data layer for the next request. |
131
|
|
|
* |
132
|
|
|
* @param array|string $key |
133
|
|
|
* @param mixed $value |
134
|
|
|
*/ |
135
|
|
|
public function flash($key, $value = null) |
136
|
|
|
{ |
137
|
|
|
$this->flashDataLayer->set($key, $value); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Retrieve the data layer's data for the next request. |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function getFlashData() |
146
|
|
|
{ |
147
|
|
|
return $this->flashDataLayer->toArray(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Add data to the data layer for the next request. |
152
|
|
|
* |
153
|
|
|
* @param array|string $key |
154
|
|
|
* @param mixed $value |
155
|
|
|
*/ |
156
|
|
|
public function noScript($key, $value = null) |
157
|
|
|
{ |
158
|
|
|
$this->noJSDataLayer->set($key, $value); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Retrieve the data layer's data for the next request. |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public function getNoScriptData() |
167
|
|
|
{ |
168
|
|
|
return $this->noJSDataLayer->toArray(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Add data to be pushed to the data layer. |
173
|
|
|
* |
174
|
|
|
* @param array|string $key |
175
|
|
|
* @param mixed $value |
176
|
|
|
*/ |
177
|
|
|
public function push($key, $value = null) |
178
|
|
|
{ |
179
|
|
|
$pushItem = new DataLayer(); |
180
|
|
|
$pushItem->set($key, $value); |
181
|
|
|
$this->pushDataLayer->push($pushItem); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Retrieve the data layer's data for the next request. |
186
|
|
|
* |
187
|
|
|
* @return \Illuminate\Support\Collection |
188
|
|
|
*/ |
189
|
|
|
public function getPushData() |
190
|
|
|
{ |
191
|
|
|
return $this->pushDataLayer; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Clear the data layer. |
196
|
|
|
*/ |
197
|
|
|
public function clear() |
198
|
|
|
{ |
199
|
|
|
$this->dataLayer = new DataLayer(); |
200
|
|
|
$this->pushDataLayer = new \Illuminate\Support\Collection(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Utility function to dump an array as json. |
205
|
|
|
* |
206
|
|
|
* @param array $data |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
public function dump($data) |
210
|
|
|
{ |
211
|
|
|
return (new DataLayer($data))->toJson(); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|