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