1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Dashboard\Panels; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Spatie\Analytics\AnalyticsServiceProvider; |
|
|
|
|
8
|
|
|
use Spatie\Analytics\Period; |
|
|
|
|
9
|
|
|
use Terranet\Administrator\Architect; |
10
|
|
|
use Terranet\Administrator\Dashboard\Panel; |
11
|
|
|
use Terranet\Administrator\Traits\Stringify; |
12
|
|
|
|
13
|
|
|
class GoogleAnalyticsPanel extends Panel |
14
|
|
|
{ |
15
|
|
|
use Stringify; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Widget contents. |
19
|
|
|
* |
20
|
|
|
* @return mixed string|View |
21
|
|
|
*/ |
22
|
|
|
public function render() |
23
|
|
|
{ |
24
|
|
|
$period = $this->period(); |
25
|
|
|
|
26
|
|
|
$dailyStats = $this->dependencyInstalled() && config('analytics.view_id') |
|
|
|
|
27
|
|
|
? \Analytics::fetchTotalVisitorsAndPageViews($period) |
|
|
|
|
28
|
|
|
: $dailyStats = $this->fakeData($period); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$visitors = $dailyStats->sum('visitors'); |
31
|
|
|
$pageViews = $dailyStats->sum('pageViews'); |
32
|
|
|
$maxVisitors = $dailyStats->max('visitors'); |
33
|
|
|
|
34
|
|
|
$labels = $this->dateLabels($dailyStats); |
35
|
|
|
|
36
|
|
|
return view(Architect::template()->dashboard('google_analytics'))->with(compact( |
|
|
|
|
37
|
|
|
'dailyStats', |
38
|
|
|
'labels', |
39
|
|
|
'visitors', |
40
|
|
|
'pageViews', |
41
|
|
|
'maxVisitors', |
42
|
|
|
'period' |
43
|
|
|
)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
protected function abortMessage() |
50
|
|
|
{ |
51
|
|
|
return |
52
|
|
|
<<<'OUT' |
53
|
|
|
<div class="panel"> |
54
|
|
|
<div class="panel-heading"> |
55
|
|
|
<h4 class="panel-title">Google Analytics.</h4> |
56
|
|
|
</div> |
57
|
|
|
<div class="panel-body"> |
58
|
|
|
<p> |
59
|
|
|
Spatie Google Analytics module missing, install it by running: |
60
|
|
|
<code>composer require spatie/laravel-analytics</code>. |
61
|
|
|
<br /><br /> |
62
|
|
|
Then follow the <a href="https://github.com/spatie/laravel-analytics" target="_blank">Setup Instructions</a>. |
63
|
|
|
</p> |
64
|
|
|
</div> |
65
|
|
|
</div> |
66
|
|
|
OUT; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
protected function dependencyInstalled() |
73
|
|
|
{ |
74
|
|
|
return Arr::has( |
75
|
|
|
app()->getLoadedProviders(), |
|
|
|
|
76
|
|
|
AnalyticsServiceProvider::class |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $dailyStats |
82
|
|
|
* |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
protected function dateLabels($dailyStats) |
86
|
|
|
{ |
87
|
|
|
return $dailyStats->pluck('date')->map(function (Carbon $carbon) { |
88
|
|
|
return $carbon->formatLocalized('%a, %e %B %Y'); |
89
|
|
|
})->toArray(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return Period |
94
|
|
|
*/ |
95
|
|
|
protected function period() |
96
|
|
|
{ |
97
|
|
|
$end = Carbon::today(); |
98
|
|
|
$start = Carbon::parse($end)->subMonthNoOverflow(); |
99
|
|
|
|
100
|
|
|
return $this->dependencyInstalled() |
101
|
|
|
? Period::create($start, $end) |
102
|
|
|
: (object) [ |
103
|
|
|
'startDate' => $start, |
104
|
|
|
'endDate' => $end, |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Provide fake analytics data for demo purposes. |
110
|
|
|
* |
111
|
|
|
* @param $period |
112
|
|
|
* |
113
|
|
|
* @return \Illuminate\Support\Collection |
114
|
|
|
*/ |
115
|
|
|
protected function fakeData($period) |
116
|
|
|
{ |
117
|
|
|
$data = collect([]); |
118
|
|
|
|
119
|
|
|
for ($date = Carbon::parse($period->startDate); $date->lte($period->endDate); $date->addDay()) { |
120
|
|
|
$data->push([ |
121
|
|
|
'date' => Carbon::parse($date), |
122
|
|
|
'visitors' => rand(100, 1000), |
123
|
|
|
'pageViews' => rand(100, 1000), |
124
|
|
|
]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $data; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths