1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tracking\Services; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\Facades\Schema; |
7
|
|
|
use Tracking\Models\Analytics; |
8
|
|
|
use function parse_user_agent; |
9
|
|
|
|
10
|
|
|
class AnalyticsService |
11
|
|
|
{ |
12
|
|
|
public function __construct(Analytics $model) |
13
|
|
|
{ |
14
|
|
|
$this->model = $model; |
|
|
|
|
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function log($request) |
18
|
|
|
{ |
19
|
|
|
$requestData = json_encode( |
20
|
|
|
[ |
21
|
|
|
'referer' => $request->server('HTTP_REFERER', null), |
22
|
|
|
'user_agent' => $request->server('HTTP_USER_AGENT', null), |
23
|
|
|
'host' => $request->server('HTTP_HOST', null), |
24
|
|
|
'remote_addr' => $request->server('REMOTE_ADDR', null), |
25
|
|
|
'uri' => $request->server('REQUEST_URI', null), |
26
|
|
|
'method' => $request->server('REQUEST_METHOD', null), |
27
|
|
|
'query' => $request->server('QUERY_STRING', null), |
28
|
|
|
'time' => $request->server('REQUEST_TIME', null), |
29
|
|
|
] |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
if (Schema::hasTable('analytics')) { |
33
|
|
|
$data = [ |
34
|
|
|
'data' => $requestData, |
35
|
|
|
]; |
36
|
|
|
if (Schema::hasColumn($this->model->getTable(), 'business_code')) // || Business::isToIgnore()) |
37
|
|
|
{ |
38
|
|
|
$data['business_code'] = \Business::getCode(); |
39
|
|
|
} |
40
|
|
|
$this->model->create( |
41
|
|
|
$data |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
public function topReferers($count) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$analytics = $this->model->where('created_at', '>', Carbon::now()->subDays($count))->get(); |
49
|
|
|
$data = $analytics->pluck('data')->all(); |
50
|
|
|
|
51
|
|
|
return $this->convertDataToItems($data, 'referer', ['unknown' => 0]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function topPages($count) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$analytics = $this->model->where('created_at', '>', Carbon::now()->subDays($count))->get(); |
57
|
|
|
$data = $analytics->pluck('data')->all(); |
58
|
|
|
|
59
|
|
|
return $this->convertDataToItems($data, 'uri'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function topBrowsers($count) |
63
|
|
|
{ |
64
|
|
|
$analytics = $this->model->where('created_at', '>', Carbon::now()->subDays($count))->get(); |
65
|
|
|
$data = $analytics->pluck('data')->all(); |
66
|
|
|
|
67
|
|
|
$browsers = []; |
68
|
|
|
|
69
|
|
|
foreach ($this->convertDataToItems($data, 'user_agent') as $userAgent => $count) { |
70
|
|
|
$browser = parse_user_agent($userAgent); |
|
|
|
|
71
|
|
|
$browsers[$browser['browser'].' ('.$browser['version'].') on '.$browser['platform']] = $count; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $browsers; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function convertDataToItems($data, $key, $conversions = []) |
78
|
|
|
{ |
79
|
|
|
if (!isset($conversions['unknown'])) { |
80
|
|
|
$conversions['unknown'] = 0; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (!isset($conversions['unknown'])) { |
84
|
|
|
$conversions['unknown'] = 0; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
foreach ($data as $item) { |
88
|
|
|
$visit = json_decode($item); |
89
|
|
|
if (!empty($visit->$key) && $visit->$key > '') { |
90
|
|
|
$conversions[$visit->$key] = 0; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
foreach ($data as $item) { |
95
|
|
|
$visit = json_decode($item); |
96
|
|
|
if (!empty($visit->$key) && $visit->$key > '') { |
97
|
|
|
$conversions[$visit->$key] += 1; |
98
|
|
|
} else { |
99
|
|
|
$conversions['unknown'] += 1; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $conversions; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getDays($count) |
107
|
|
|
{ |
108
|
|
|
$analytics = $this->model->where('created_at', '>', Carbon::now()->subDays($count)); |
109
|
|
|
|
110
|
|
|
if ($analytics->first()) { |
111
|
|
|
$endDate = Carbon::now(); |
112
|
|
|
$startDate = Carbon::parse($analytics->first()->created_at->format('Y-m-d')); |
113
|
|
|
|
114
|
|
|
$dateRange = $this->getDateRange($startDate, $endDate); |
115
|
|
|
|
116
|
|
|
foreach ($dateRange as $date) { |
117
|
|
|
$visits[$date] = $this->model->where('created_at', '>', $date.' 00:00:00')->where('created_at', '<', $date.' 23:59:59')->count(); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$visitCollection = collect($visits); |
|
|
|
|
121
|
|
|
} else { |
122
|
|
|
$visitCollection = collect( |
123
|
|
|
[ |
124
|
|
|
Carbon::now()->format('Y-m-d') => 0, |
125
|
|
|
] |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return [ |
130
|
|
|
'dates' => $visitCollection->keys()->toArray(), |
131
|
|
|
'visits' => $visitCollection->values()->toArray(), |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
protected function getDateRange($startDate, $endDate) |
136
|
|
|
{ |
137
|
|
|
$dates = []; |
138
|
|
|
|
139
|
|
|
for ($date = $startDate; $date->lte($endDate); $date->addDay()) { |
140
|
|
|
$dates[] = $date->format('Y-m-d'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $dates; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: