|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Dashboard module controller |
|
4
|
|
|
* |
|
5
|
|
|
* Controller for module Dashobard |
|
6
|
|
|
* |
|
7
|
|
|
* @category Controller |
|
8
|
|
|
* @subpackage Admin |
|
9
|
|
|
* @package Olapus |
|
10
|
|
|
* @author Jan Drda <[email protected]> |
|
11
|
|
|
* @copyright Jan Drda |
|
12
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace App\Http\Controllers\Admin; |
|
17
|
|
|
|
|
18
|
|
|
use App\Http\Controllers\Controller; |
|
19
|
|
|
use Illuminate\Support\Facades\Auth; |
|
20
|
|
|
use Illuminate\Support\Facades\View; |
|
21
|
|
|
use Spatie\Analytics; |
|
22
|
|
|
|
|
23
|
|
|
class DashboardController extends Controller { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Module basic path |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $moduleBasicRoute = 'admin.dashboard'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* View basic path |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $moduleBasicTemplatePath = 'admin.modules.user'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Google analytics data |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $gaData = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Constructor |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct() { |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Global template variables |
|
53
|
|
|
*/ |
|
54
|
|
|
View::share('moduleBasicRoute', $this->moduleBasicRoute); |
|
55
|
|
|
View::share('moduleBasicTemplatePath', $this->moduleBasicTemplatePath); |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Module name for blade |
|
59
|
|
|
*/ |
|
60
|
|
|
$temp = explode('.', $this->moduleBasicRoute); |
|
61
|
|
|
View::share('moduleNameBlade', $temp[0] . "_module_" . $temp[1]); |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Main Dashboard function |
|
68
|
|
|
* |
|
69
|
|
|
* @return Response |
|
70
|
|
|
*/ |
|
71
|
|
|
public function index() { |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get Google Analytics values if enabled |
|
75
|
|
|
*/ |
|
76
|
|
|
if (env('ANALYTICS_ENABLED') == 1) { |
|
77
|
|
|
|
|
78
|
|
|
$ga = $this->getGAValues(); |
|
79
|
|
|
} else { |
|
80
|
|
|
|
|
81
|
|
|
$ga = []; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$statistics = [ |
|
85
|
|
|
'ga' => $ga |
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
|
|
return view('admin.modules.dashboard.index', ['statistics' => $statistics]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get visitors or pageviews from Google API |
|
93
|
|
|
* |
|
94
|
|
|
* @param integer $days |
|
95
|
|
|
* @param string $type |
|
96
|
|
|
* @return integer |
|
97
|
|
|
*/ |
|
98
|
|
|
public function gaGetVisitorsPageviews($days = 7, $type = 'visitors') { |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get the data |
|
102
|
|
|
*/ |
|
103
|
|
|
$data = \Analytics::getVisitorsAndPageViews($days); |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Calculate total |
|
107
|
|
|
*/ |
|
108
|
|
|
$total = 0; |
|
109
|
|
|
foreach ($data as $value) { |
|
110
|
|
|
|
|
111
|
|
|
$total += $value[$type]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $total; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get percent difference between values |
|
119
|
|
|
* |
|
120
|
|
|
* @param integer $lastValue |
|
121
|
|
|
* @param integer $thisValue |
|
122
|
|
|
* @param integer $round |
|
123
|
|
|
* @return integer |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getPercentDifference($lastValue, $thisValue, $round = 2) { |
|
126
|
|
|
|
|
127
|
|
|
if ($thisValue > 0 && $lastValue > 0) |
|
128
|
|
|
{ |
|
129
|
|
|
return round((($thisValue / ($lastValue / 100)) - 100), $round); |
|
130
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
else { |
|
133
|
|
|
|
|
134
|
|
|
return 0; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get Google Analytics values |
|
140
|
|
|
* |
|
141
|
|
|
* @return object |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getGAValues() { |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Visitors and pageviews this week |
|
147
|
|
|
*/ |
|
148
|
|
|
$visitorsThisWeek = $this->gaGetVisitorsPageviews(7, 'visitors'); |
|
149
|
|
|
$pageviewsThisWeek = $this->gaGetVisitorsPageviews(7, 'pageViews'); |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Visitors and pageviews two weeks |
|
153
|
|
|
*/ |
|
154
|
|
|
$visitorsTwoWeeks = $this->gaGetVisitorsPageviews(14, 'visitors'); |
|
155
|
|
|
$pageviewsTwoWeeks = $this->gaGetVisitorsPageviews(14, 'pageViews'); |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Visitors and pageviews last week |
|
159
|
|
|
*/ |
|
160
|
|
|
$visitorsLastWeek = $visitorsTwoWeeks - $visitorsThisWeek; |
|
161
|
|
|
$pageviewsLastWeek = $pageviewsTwoWeeks - $pageviewsThisWeek; |
|
162
|
|
|
$visitorsPercentThisWeek = $this->getPercentDifference($visitorsLastWeek, $visitorsThisWeek); |
|
163
|
|
|
$pageviewsPercentThisWeek = $this->getPercentDifference($pageviewsLastWeek, $pageviewsThisWeek); |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Visitors and pageviews this month |
|
167
|
|
|
*/ |
|
168
|
|
|
$visitorsThisMonth = $this->gaGetVisitorsPageviews(30, 'visitors'); |
|
169
|
|
|
$pageviewsThisMonth = $this->gaGetVisitorsPageviews(30, 'pageViews'); |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Visitors and pageviews last month |
|
173
|
|
|
*/ |
|
174
|
|
|
$visitorsTwoMonths = $this->gaGetVisitorsPageviews(60, 'visitors'); |
|
175
|
|
|
$pageviewsTwoMonths = $this->gaGetVisitorsPageviews(60, 'pageViews'); |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Visitors and pageviews last month |
|
179
|
|
|
*/ |
|
180
|
|
|
$visitorsLastMonth = $visitorsTwoMonths - $visitorsThisMonth; |
|
181
|
|
|
$pageviewsLastMonth = $pageviewsTwoMonths - $pageviewsThisMonth; |
|
182
|
|
|
$visitorsPercentThisMonth = $this->getPercentDifference($visitorsLastMonth, $visitorsThisMonth); |
|
183
|
|
|
$pageviewsPercentThisMonth = $this->getPercentDifference($pageviewsLastMonth, $pageviewsThisMonth); |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Refefers |
|
187
|
|
|
* |
|
188
|
|
|
*/ |
|
189
|
|
|
$topReferers = \Analytics::getTopReferrers(30, 10); |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Visitors and pageviews for 30 days |
|
193
|
|
|
*/ |
|
194
|
|
|
$visitorsPageviewsChart = \Analytics::getVisitorsAndPageViews(30); |
|
195
|
|
|
|
|
196
|
|
|
$ga = [ |
|
197
|
|
|
'visitors_this_week' => $visitorsThisWeek, |
|
198
|
|
|
'visitors_last_week' => $visitorsLastWeek, |
|
199
|
|
|
'visitors_percent_this_week' => $visitorsPercentThisWeek, |
|
200
|
|
|
'pageviews_this_week' => $pageviewsThisWeek, |
|
201
|
|
|
'pageviews_last_week' => $pageviewsLastWeek, |
|
202
|
|
|
'pageviews_percent_this_week' => $pageviewsPercentThisWeek, |
|
203
|
|
|
'visitors_this_month' => $visitorsThisMonth, |
|
204
|
|
|
'visitors_last_month' => $visitorsLastMonth, |
|
205
|
|
|
'visitors_percent_this_month' => $visitorsPercentThisMonth, |
|
206
|
|
|
'pageviews_this_month' => $pageviewsThisMonth, |
|
207
|
|
|
'pageviews_last_month' => $pageviewsLastMonth, |
|
208
|
|
|
'pageviews_percent_this_month' => $pageviewsPercentThisMonth, |
|
209
|
|
|
'top_referers' => $topReferers, |
|
210
|
|
|
'visitors_pageviews_chart' => $visitorsPageviewsChart |
|
211
|
|
|
]; |
|
212
|
|
|
|
|
213
|
|
|
return $ga; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|