1 | <?php |
||
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) |
||
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) |
||
77 | |||
78 | /** |
||
79 | * Return the Google Tag Manager id. |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | public function id() |
||
87 | |||
88 | /** |
||
89 | * Check whether script rendering is enabled. |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | public function isEnabled() |
||
97 | |||
98 | /** |
||
99 | * Check whether environments are enabled. |
||
100 | * |
||
101 | * @return bool |
||
102 | */ |
||
103 | public function isEnvironmentsEnabled() |
||
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() |
||
129 | |||
130 | /** |
||
131 | * Enable Google Tag Manager scripts rendering. |
||
132 | */ |
||
133 | public function enable() |
||
137 | |||
138 | /** |
||
139 | * Disable Google Tag Manager scripts rendering. |
||
140 | */ |
||
141 | public function disable() |
||
145 | |||
146 | /** |
||
147 | * Enable Google Tag Manager environments parameters rendering. |
||
148 | */ |
||
149 | public function enableEnvironments() |
||
153 | |||
154 | /** |
||
155 | * Disable Google Tag Manager environments parameters rendering. |
||
156 | */ |
||
157 | public function disableEnvironments() |
||
161 | |||
162 | /** |
||
163 | * Return the value to use for the gtm_auth parameter. |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getGtmAuth() |
||
171 | |||
172 | /** |
||
173 | * Return the value to use for the gtm_preview parameter. |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | public function getGtmPreview() |
||
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) |
||
192 | |||
193 | /** |
||
194 | * Retrieve the data layer. |
||
195 | * |
||
196 | * @return \Spatie\GoogleTagManager\DataLayer |
||
197 | */ |
||
198 | public function getDataLayer() |
||
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) |
||
213 | |||
214 | /** |
||
215 | * Retrieve the data layer's data for the next request. |
||
216 | * |
||
217 | * @return array |
||
218 | */ |
||
219 | public function getFlashData() |
||
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) |
||
236 | |||
237 | /** |
||
238 | * Retrieve the data layer's data for the next request. |
||
239 | * |
||
240 | * @return \Illuminate\Support\Collection |
||
241 | */ |
||
242 | public function getPushData() |
||
246 | |||
247 | /** |
||
248 | * Clear the data layer. |
||
249 | */ |
||
250 | public function clear() |
||
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) |
||
266 | } |
||
267 |