1
|
|
|
<?php namespace Arcanesoft\Tracker\ViewComposers\Dashboard; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Tracker\Models\VisitorActivity; |
4
|
|
|
use Arcanesoft\Tracker\Support\DateRange; |
5
|
|
|
use Arcanesoft\Tracker\ViewComposers\AbstractViewComposer; |
6
|
|
|
use Illuminate\Contracts\View\View; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ReferersListComposer |
11
|
|
|
* |
12
|
|
|
* @package Arcanesoft\Tracker\ViewComposers\Dashboard |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class ReferersListComposer extends AbstractViewComposer |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Constants |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
const VIEW = 'tracker::admin._composers.dashboard.referers-ratio-list'; |
23
|
|
|
|
24
|
|
|
/* ----------------------------------------------------------------- |
25
|
|
|
| Main Methods |
26
|
|
|
| ----------------------------------------------------------------- |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Compose the view. |
31
|
|
|
* |
32
|
|
|
* @param \Illuminate\Contracts\View\View $view |
33
|
|
|
*/ |
34
|
|
|
public function compose(View $view) |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var \Carbon\Carbon $start |
38
|
|
|
* @var \Carbon\Carbon $end |
39
|
|
|
* @var \Illuminate\Support\Collection $range |
40
|
|
|
*/ |
41
|
|
|
extract(DateRange::getCurrentMonthDaysRange()); |
|
|
|
|
42
|
|
|
|
43
|
|
|
$view->with('referersRatio', $this->calculateLanguagesPercentage( |
44
|
|
|
$this->getReferersCountFromSessionActivities($start, $end) |
45
|
|
|
)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/* ----------------------------------------------------------------- |
49
|
|
|
| Other Methods |
50
|
|
|
| ----------------------------------------------------------------- |
51
|
|
|
*/ |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the languages count from sessions. |
55
|
|
|
* |
56
|
|
|
* @param \Carbon\Carbon $start |
57
|
|
|
* @param \Carbon\Carbon $end |
58
|
|
|
* |
59
|
|
|
* @return \Illuminate\Support\Collection |
60
|
|
|
*/ |
61
|
|
|
private function getReferersCountFromSessionActivities($start, $end) |
62
|
|
|
{ |
63
|
|
|
return $this->getVisitsFilteredByDateRange($start, $end) |
64
|
|
|
->transform(function (VisitorActivity $visit) { |
65
|
|
|
return [ |
66
|
|
|
'name' => $visit->hasReferer() |
67
|
|
|
? $visit->referer->domain->name |
68
|
|
|
: trans('tracker::generals.undefined'), |
69
|
|
|
]; |
70
|
|
|
}) |
71
|
|
|
->groupBy('name') |
72
|
|
|
->transform(function (Collection $items, $key) { |
73
|
|
|
return [ |
74
|
|
|
'name' => $key, |
75
|
|
|
'count' => $items->count(), |
76
|
|
|
]; |
77
|
|
|
}) |
78
|
|
|
->sortByDesc('count'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Calculate the referers percentage. |
83
|
|
|
* |
84
|
|
|
* @param \Illuminate\Support\Collection $referers |
85
|
|
|
* |
86
|
|
|
* @return \Illuminate\Support\Collection |
87
|
|
|
*/ |
88
|
|
|
private function calculateLanguagesPercentage($referers) |
89
|
|
|
{ |
90
|
|
|
$total = $referers->sum('count'); |
91
|
|
|
|
92
|
|
|
return $referers->transform(function ($item) use ($total) { |
93
|
|
|
return $item + [ |
94
|
|
|
'percentage' => round(($item['count'] / $total) * 100, 2) |
95
|
|
|
]; |
96
|
|
|
}); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|