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