1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Http\Components; |
3
|
|
|
|
4
|
|
|
use Carbon\Carbon; |
5
|
|
|
use Spatie\Analytics\AnalyticsFacade; |
6
|
|
|
use Spatie\Analytics\Period; |
7
|
|
|
|
8
|
|
|
trait AnalyticsComponent |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The and date used in Analytics requests. |
12
|
|
|
* |
13
|
|
|
* @var \Carbon\Carbon |
14
|
|
|
*/ |
15
|
|
|
protected $endDate; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Build the today visitors metric. |
19
|
|
|
* |
20
|
|
|
* @codeCoverageIgnore |
21
|
|
|
* |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
public function buildTodayVisitors(): string |
25
|
|
|
{ |
26
|
|
|
$startDate = Carbon::today(); |
27
|
|
|
$endDate = Carbon::now(); |
28
|
|
|
|
29
|
|
|
$visitorsData = AnalyticsFacade::performQuery(Period::create($startDate, $endDate), 'ga:users'); |
30
|
|
|
|
31
|
|
|
return $visitorsData->totalsForAllResults['ga:users']; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Build the today visitors metric. |
36
|
|
|
* |
37
|
|
|
* @codeCoverageIgnore |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function buildAllTimeVisitors(): string |
42
|
|
|
{ |
43
|
|
|
$startDate = Carbon::createFromFormat('Y-m-d', config('analytics.start_date')); |
44
|
|
|
$endDate = Carbon::now(); |
45
|
|
|
|
46
|
|
|
$visitorsData = AnalyticsFacade::performQuery(Period::create($startDate, $endDate), 'ga:sessions'); |
|
|
|
|
47
|
|
|
|
48
|
|
|
return $visitorsData->totalsForAllResults['ga:sessions']; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Build the visitors graph for the last 7 days. |
53
|
|
|
* |
54
|
|
|
* @codeCoverageIgnore |
55
|
|
|
* |
56
|
|
|
* @return \Google_Service_Analytics_GaData |
57
|
|
|
*/ |
58
|
|
|
public function buildVisitorsGraph(): \Google_Service_Analytics_GaData |
59
|
|
|
{ |
60
|
|
|
$startDate = Carbon::now()->subDays(7); |
61
|
|
|
|
62
|
|
|
$visitorsData = AnalyticsFacade::performQuery( |
63
|
|
|
Period::create($startDate, $this->endDate), |
64
|
|
|
'ga:sessions,ga:pageviews', |
65
|
|
|
[ |
66
|
|
|
'dimensions' => 'ga:date', |
67
|
|
|
'sort' => 'ga:date' |
68
|
|
|
] |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$visitorsGraph = []; |
72
|
|
|
foreach ($visitorsData->rows as $row) { |
73
|
|
|
$row[0] = Carbon::createFromFormat('Ymd', $row[0]); |
74
|
|
|
|
75
|
|
|
array_push($visitorsGraph, $row); |
76
|
|
|
} |
77
|
|
|
$visitorsData->rows = array_reverse($visitorsGraph); |
78
|
|
|
|
79
|
|
|
return $visitorsData; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Build the browsers graph from the beginning. |
84
|
|
|
* |
85
|
|
|
* @codeCoverageIgnore |
86
|
|
|
* |
87
|
|
|
* @return \Google_Service_Analytics_GaData |
88
|
|
|
*/ |
89
|
|
|
public function buildBrowsersGraph(): \Google_Service_Analytics_GaData |
90
|
|
|
{ |
91
|
|
|
$startDate = Carbon::createFromFormat('Y-m-d', config('analytics.start_date')); |
92
|
|
|
|
93
|
|
|
$browsersData = AnalyticsFacade::performQuery( |
94
|
|
|
Period::create($startDate, $this->endDate), |
|
|
|
|
95
|
|
|
'ga:pageviews', |
96
|
|
|
[ |
97
|
|
|
'dimensions' => 'ga:browser', |
98
|
|
|
'sort' => 'ga:pageviews', |
99
|
|
|
'filters' => 'ga:browser==Chrome,' |
100
|
|
|
.'ga:browser==Firefox,' |
101
|
|
|
.'ga:browser==Internet Explorer,' |
102
|
|
|
.'ga:browser==Safari,' |
103
|
|
|
.'ga:browser==Opera' |
104
|
|
|
] |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$browsersGraph = []; |
108
|
|
|
foreach ($browsersData->rows as $browser) { |
109
|
|
|
$browser[] = $this->getPercentage($browser[1], $browsersData->totalsForAllResults['ga:pageviews']); |
110
|
|
|
$browser[] = $this->getBrowserColor($browser[0]); |
111
|
|
|
|
112
|
|
|
array_push($browsersGraph, $browser); |
113
|
|
|
} |
114
|
|
|
$browsersData->rows = array_reverse($browsersGraph); |
115
|
|
|
|
116
|
|
|
return $browsersData; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Build the devices graph from the last 7 months. |
121
|
|
|
* |
122
|
|
|
* @codeCoverageIgnore |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
public function buildDevicesGraph(): array |
127
|
|
|
{ |
128
|
|
|
$startDate = Carbon::now()->subMonths(7); |
129
|
|
|
|
130
|
|
|
$devicesData = AnalyticsFacade::performQuery( |
131
|
|
|
Period::create($startDate, $this->endDate), |
132
|
|
|
'ga:pageviews', |
133
|
|
|
[ |
134
|
|
|
'dimensions' => 'ga:mobileDeviceBranding,ga:yearMonth', |
135
|
|
|
'sort' => '-ga:mobileDeviceBranding', |
136
|
|
|
'filters' => 'ga:mobileDeviceBranding==Apple,' |
137
|
|
|
.'ga:mobileDeviceBranding==Samsung,' |
138
|
|
|
.'ga:mobileDeviceBranding==Google,' |
139
|
|
|
.'ga:mobileDeviceBranding==HTC,' |
140
|
|
|
.'ga:mobileDeviceBranding==Microsoft' |
141
|
|
|
] |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$devicesGraph = []; |
145
|
|
|
for ($i = 0; $i < 8; $i++) { |
146
|
|
|
$date = Carbon::now()->subMonths($i); |
147
|
|
|
|
148
|
|
|
$devicesGraph[$date->format('Ym')] = [ |
149
|
|
|
'period' => $date->format('Y-m-d'), |
150
|
|
|
'Apple' => 0, |
151
|
|
|
'Samsung' => 0, |
152
|
|
|
'Google' => 0, |
153
|
|
|
'HTC' => 0, |
154
|
|
|
'Microsoft' => 0 |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
foreach ($devicesData->rows as $device) { |
159
|
|
|
$devicesGraph[$device[1]][$device[0]] = $device[2]; |
160
|
|
|
} |
161
|
|
|
$devicesGraph = array_reverse($devicesGraph); |
162
|
|
|
|
163
|
|
|
return $devicesGraph; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Build the operating system graph from the last 7 months. |
168
|
|
|
* |
169
|
|
|
* @codeCoverageIgnore |
170
|
|
|
* |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
|
|
public function buildOperatingSystemGraph(): array |
174
|
|
|
{ |
175
|
|
|
$startDate = Carbon::now()->subMonths(7); |
176
|
|
|
|
177
|
|
|
$operatingSystemData = AnalyticsFacade::performQuery( |
178
|
|
|
Period::create($startDate, $this->endDate), |
179
|
|
|
'ga:pageviews', |
180
|
|
|
[ |
181
|
|
|
'dimensions' => 'ga:operatingSystem,ga:yearMonth', |
182
|
|
|
'sort' => '-ga:operatingSystem', |
183
|
|
|
'filters' => 'ga:operatingSystem==Windows,' |
184
|
|
|
.'ga:operatingSystem==Macintosh,' |
185
|
|
|
.'ga:operatingSystem==Linux' |
186
|
|
|
] |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
$operatingSystemGraph = []; |
190
|
|
|
for ($i = 0; $i < 8; $i++) { |
191
|
|
|
$date = Carbon::now()->subMonths($i); |
192
|
|
|
|
193
|
|
|
$operatingSystemGraph[$date->format('Ym')] = [ |
194
|
|
|
'period' => $date->format('Y-m-d'), |
195
|
|
|
'Windows' => 0, |
196
|
|
|
'Macintosh' => 0, |
197
|
|
|
'Linux' => 0 |
198
|
|
|
]; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
foreach ($operatingSystemData->rows as $os) { |
202
|
|
|
$operatingSystemGraph[$os[1]][$os[0]] = $os[2]; |
203
|
|
|
} |
204
|
|
|
$operatingSystemGraph = array_reverse($operatingSystemGraph); |
205
|
|
|
|
206
|
|
|
return $operatingSystemGraph; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get the percentage relative to the total page views. |
211
|
|
|
* |
212
|
|
|
* @param int $pageviews The page views. |
213
|
|
|
* @param int $total The total page views. |
214
|
|
|
* |
215
|
|
|
* @return float |
216
|
|
|
*/ |
217
|
|
|
public function getPercentage($pageviews, $total): float |
218
|
|
|
{ |
219
|
|
|
$percentage = ($pageviews * 100) / $total; |
220
|
|
|
|
221
|
|
|
return round($percentage, 1); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get the browser color by his name |
226
|
|
|
* |
227
|
|
|
* @param string $browser The name of the browser. |
228
|
|
|
* |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
|
|
public function getBrowserColor(string $browser): string |
232
|
|
|
{ |
233
|
|
|
switch ($browser) { |
234
|
|
|
case 'Chrome': |
235
|
|
|
$color = '#00acac'; |
236
|
|
|
break; |
237
|
|
|
case 'Firefox': |
238
|
|
|
$color = '#f4645f'; |
239
|
|
|
break; |
240
|
|
|
case 'Safari': |
241
|
|
|
$color = '#727cb6'; |
242
|
|
|
break; |
243
|
|
|
case 'Opera': |
244
|
|
|
$color = '#348fe2'; |
245
|
|
|
break; |
246
|
|
|
case 'Internet Explorer': |
247
|
|
|
$color = '#75e376'; |
248
|
|
|
break; |
249
|
|
|
default: |
250
|
|
|
$color = '#ddd'; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $color; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|