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