Passed
Push — 5.0.0 ( 49e1c0...87aae2 )
by Fèvre
06:22
created

AnalyticsComponent::getBrowserColor()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 19
nc 6
nop 1
dl 0
loc 23
rs 9.0111
c 2
b 0
f 0
1
<?php
2
namespace Xetaravel\Http\Components;
3
4
use Carbon\Carbon;
5
use Illuminate\Support\Collection;
6
use Spatie\Analytics\Facades\Analytics;
7
use Spatie\Analytics\Period;
8
9
trait AnalyticsComponent
10
{
11
    /**
12
     * Build the today visitors metric.
13
     *
14
     * @codeCoverageIgnore
15
     *
16
     * @return Collection
17
     */
18
    public function buildTodayVisitors(): Collection
19
    {
20
        $startDate = Carbon::today();
21
        $endDate = Carbon::now();
22
23
        return Analytics::fetchVisitorsAndPageViews(Period::create($startDate, $endDate));
24
    }
25
26
    /**
27
     * Build the today visitors metric.
28
     *
29
     * @codeCoverageIgnore
30
     *
31
     * @return string
32
     */
33
    public function buildAllTimeVisitors(): string
34
    {
35
        $startDate = Carbon::createFromFormat('Y-m-d', config('analytics.start_date'));
36
        $endDate = Carbon::now();
37
38
        $visitorsData = AnalyticsFacade::performQuery(Period::create($startDate, $endDate), 'ga:sessions');
0 ignored issues
show
Bug introduced by
The type Xetaravel\Http\Components\AnalyticsFacade was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
40
        return $visitorsData->totalsForAllResults['ga:sessions'];
41
    }
42
43
    /**
44
     * Build the visitors graph for the last 7 days.
45
     *
46
     * @codeCoverageIgnore
47
     *
48
     * @return \Google_Service_Analytics_GaData
49
     */
50
    public function buildVisitorsGraph(): \Google_Service_Analytics_GaData
0 ignored issues
show
Bug introduced by
The type Google_Service_Analytics_GaData was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
51
    {
52
        $startDate = Carbon::now()->subDays(7);
53
54
        $visitorsData = AnalyticsFacade::performQuery(
55
            Period::create($startDate, $this->endDate),
56
            'ga:sessions,ga:pageviews',
57
            [
58
                'dimensions' => 'ga:date',
59
                'sort' => 'ga:date'
60
            ]
61
        );
62
63
        $visitorsGraph = [];
64
        foreach ($visitorsData->rows as $row) {
65
            $row[0] = Carbon::createFromFormat('Ymd', $row[0]);
66
67
            array_push($visitorsGraph, $row);
68
        }
69
        $visitorsData->rows = array_reverse($visitorsGraph);
70
71
        return $visitorsData;
72
    }
73
74
    /**
75
     * Build the browsers graph from the beginning.
76
     *
77
     * @codeCoverageIgnore
78
     *
79
     * @return \Google_Service_Analytics_GaData
80
     */
81
    public function buildBrowsersGraph(): \Google_Service_Analytics_GaData
82
    {
83
        $startDate = Carbon::createFromFormat('Y-m-d', config('analytics.start_date'));
84
85
        $browsersData = AnalyticsFacade::performQuery(
86
            Period::create($startDate, $this->endDate),
87
            'ga:pageviews',
88
            [
89
                'dimensions' => 'ga:browser',
90
                'sort' => 'ga:pageviews',
91
                'filters' => 'ga:browser==Chrome,'
92
                    .'ga:browser==Firefox,'
93
                    .'ga:browser==Internet Explorer,'
94
                    .'ga:browser==Safari,'
95
                    .'ga:browser==Opera'
96
            ]
97
        );
98
99
        $browsersGraph = [];
100
        foreach ($browsersData->rows as $browser) {
101
            $browser[] = $this->getPercentage($browser[1], $browsersData->totalsForAllResults['ga:pageviews']);
102
            $browser[] = $this->getBrowserColor($browser[0]);
103
104
            array_push($browsersGraph, $browser);
105
        }
106
        $browsersData->rows = array_reverse($browsersGraph);
107
108
        return $browsersData;
109
    }
110
111
    /**
112
     * Build the devices graph from the last 7 months.
113
     *
114
     * @codeCoverageIgnore
115
     *
116
     * @return array
117
     */
118
    public function buildDevicesGraph(): array
119
    {
120
        $startDate = Carbon::now()->subMonths(7);
121
122
        $devicesData = AnalyticsFacade::performQuery(
123
            Period::create($startDate, $this->endDate),
124
            'ga:pageviews',
125
            [
126
                'dimensions' => 'ga:mobileDeviceBranding,ga:yearMonth',
127
                'sort' => '-ga:mobileDeviceBranding',
128
                'filters' => 'ga:mobileDeviceBranding==Apple,'
129
                    .'ga:mobileDeviceBranding==Samsung,'
130
                    .'ga:mobileDeviceBranding==Google,'
131
                    .'ga:mobileDeviceBranding==HTC,'
132
                    .'ga:mobileDeviceBranding==Microsoft'
133
            ]
134
        );
135
136
        $devicesGraph = [];
137
        for ($i = 0; $i < 8; $i++) {
138
            $date = Carbon::now()->subMonths($i);
139
140
            $devicesGraph[$date->format('Ym')] = [
141
                'period' => $date->format('Y-m-d'),
142
                'Apple' => 0,
143
                'Samsung' => 0,
144
                'Google' => 0,
145
                'HTC' => 0,
146
                'Microsoft' => 0
147
            ];
148
        }
149
150
        foreach ($devicesData->rows as $device) {
151
            $devicesGraph[$device[1]][$device[0]] = $device[2];
152
        }
153
        $devicesGraph = array_reverse($devicesGraph);
154
155
        return $devicesGraph;
156
    }
157
158
    /**
159
     * Build the operating system graph from the last 7 months.
160
     *
161
     * @codeCoverageIgnore
162
     *
163
     * @return array
164
     */
165
    public function buildOperatingSystemGraph(): array
166
    {
167
        $startDate = Carbon::now()->subMonths(7);
168
169
        $operatingSystemData = AnalyticsFacade::performQuery(
170
            Period::create($startDate, $this->endDate),
171
            'ga:pageviews',
172
            [
173
                'dimensions' => 'ga:operatingSystem,ga:yearMonth',
174
                'sort' => '-ga:operatingSystem',
175
                'filters' => 'ga:operatingSystem==Windows,'
176
                    .'ga:operatingSystem==Macintosh,'
177
                    .'ga:operatingSystem==Linux'
178
            ]
179
        );
180
181
        $operatingSystemGraph = [];
182
        for ($i = 0; $i < 8; $i++) {
183
            $date = Carbon::now()->subMonths($i);
184
185
            $operatingSystemGraph[$date->format('Ym')] = [
186
                'period' => $date->format('Y-m-d'),
187
                'Windows' => 0,
188
                'Macintosh' => 0,
189
                'Linux' => 0
190
            ];
191
        }
192
193
        foreach ($operatingSystemData->rows as $os) {
194
            $operatingSystemGraph[$os[1]][$os[0]] = $os[2];
195
        }
196
        $operatingSystemGraph = array_reverse($operatingSystemGraph);
197
198
        return $operatingSystemGraph;
199
    }
200
201
    /**
202
     * Get the percentage relative to the total page views.
203
     *
204
     * @param int $pageviews The page views.
205
     * @param int $total The total page views.
206
     *
207
     * @return float
208
     */
209
    public function getPercentage($pageviews, $total): float
210
    {
211
        $percentage = ($pageviews * 100) / $total;
212
213
        return round($percentage, 1);
214
    }
215
216
    /**
217
     * Get the browser color by his name
218
     *
219
     * @param string $browser The name of the browser.
220
     *
221
     * @return string
222
     */
223
    public function getBrowserColor(string $browser): string
224
    {
225
        switch ($browser) {
226
            case 'Chrome':
227
                $color = '#00acac';
228
                break;
229
            case 'Firefox':
230
                $color = '#f4645f';
231
                break;
232
            case 'Safari':
233
                $color = '#727cb6';
234
                break;
235
            case 'Opera':
236
                $color = '#348fe2';
237
                break;
238
            case 'Internet Explorer':
239
                $color = '#75e376';
240
                break;
241
            default:
242
                $color = '#ddd';
243
        }
244
245
        return $color;
246
    }
247
}
248