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 bool |
18
|
|
|
*/ |
19
|
|
|
protected $enabled; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Spatie\GoogleTagManager\DataLayer |
23
|
|
|
*/ |
24
|
|
|
protected $dataLayer; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Spatie\GoogleTagManager\DataLayer |
28
|
|
|
*/ |
29
|
|
|
protected $flashDataLayer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Illuminate\Support\Collection |
33
|
|
|
*/ |
34
|
|
|
protected $pushDataLayer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $id |
38
|
|
|
*/ |
39
|
|
|
public function __construct($id) |
40
|
|
|
{ |
41
|
|
|
$this->id = $id; |
42
|
|
|
$this->dataLayer = new DataLayer(); |
43
|
|
|
$this->flashDataLayer = new DataLayer(); |
44
|
|
|
$this->pushDataLayer = new \Illuminate\Support\Collection(); |
45
|
|
|
|
46
|
|
|
$this->enabled = true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return the Google Tag Manager id. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function id() |
55
|
|
|
{ |
56
|
|
|
return $this->id; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set the Google Tag Manager id. |
61
|
|
|
* |
62
|
|
|
* @param string $id |
63
|
|
|
*/ |
64
|
|
|
public function setId($id) |
65
|
|
|
{ |
66
|
|
|
$this->id = $id; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Check whether script rendering is enabled. |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function isEnabled() |
75
|
|
|
{ |
76
|
|
|
return $this->enabled; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Enable Google Tag Manager scripts rendering. |
81
|
|
|
*/ |
82
|
|
|
public function enable() |
83
|
|
|
{ |
84
|
|
|
$this->enabled = true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Disable Google Tag Manager scripts rendering. |
89
|
|
|
*/ |
90
|
|
|
public function disable() |
91
|
|
|
{ |
92
|
|
|
$this->enabled = false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Add data to the data layer. |
97
|
|
|
* |
98
|
|
|
* @param array|string $key |
99
|
|
|
* @param mixed $value |
100
|
|
|
*/ |
101
|
|
|
public function set($key, $value = null) |
102
|
|
|
{ |
103
|
|
|
$this->dataLayer->set($key, $value); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Retrieve the data layer. |
108
|
|
|
* |
109
|
|
|
* @return \Spatie\GoogleTagManager\DataLayer |
110
|
|
|
*/ |
111
|
|
|
public function getDataLayer() |
112
|
|
|
{ |
113
|
|
|
return $this->dataLayer; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Add data to the data layer for the next request. |
118
|
|
|
* |
119
|
|
|
* @param array|string $key |
120
|
|
|
* @param mixed $value |
121
|
|
|
*/ |
122
|
|
|
public function flash($key, $value = null) |
123
|
|
|
{ |
124
|
|
|
$this->flashDataLayer->set($key, $value); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Retrieve the data layer's data for the next request. |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function getFlashData() |
133
|
|
|
{ |
134
|
|
|
return $this->flashDataLayer->toArray(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Add data to be pushed to the data layer. |
139
|
|
|
* |
140
|
|
|
* @param array|string $key |
141
|
|
|
* @param mixed $value |
142
|
|
|
*/ |
143
|
|
|
public function push($key, $value = null) |
144
|
|
|
{ |
145
|
|
|
$pushItem = new DataLayer(); |
146
|
|
|
$pushItem->set($key, $value); |
147
|
|
|
$this->pushDataLayer->push($pushItem); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Retrieve the data layer's data for the next request. |
152
|
|
|
* |
153
|
|
|
* @return \Illuminate\Support\Collection |
154
|
|
|
*/ |
155
|
|
|
public function getPushData() |
156
|
|
|
{ |
157
|
|
|
return $this->pushDataLayer; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Clear the data layer. |
162
|
|
|
*/ |
163
|
|
|
public function clear() |
164
|
|
|
{ |
165
|
|
|
$this->dataLayer = new DataLayer(); |
166
|
|
|
$this->pushDataLayer = new \Illuminate\Support\Collection(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Utility function to dump an array as json. |
171
|
|
|
* |
172
|
|
|
* @param array $data |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
public function dump($data) |
176
|
|
|
{ |
177
|
|
|
return (new DataLayer($data))->toJson(); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|