Passed
Push — master ( e979aa...833bb5 )
by Simon
01:21
created

GoogleAnalyticsReportService::getBlacklist()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Class GoogleAnalyticsReportService
5
 *
6
 * Base service for getting the Analytics report per page from Google.
7
 */
8
class GoogleAnalyticsReportService
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
11
    /**
12
     * @var GoogleClientService
13
     */
14
    protected $client;
15
16
    /**
17
     * @var Google_Service_AnalyticsReporting_DateRange
18
     */
19
    protected $dateRange;
20
21
    /**
22
     * @var Google_Service_AnalyticsReporting
23
     */
24
    protected $analytics;
25
26
    /**
27
     * @var Google_Service_AnalyticsReporting_Metric
28
     */
29
    protected $metrics;
30
31
    public $batched = false;
32
33
    /**
34
     * GoogleReportService constructor.
35
     * @param GoogleClientService $client
36
     */
37
    public function __construct($client)
38
    {
39
        $config = SiteConfig::current_site_config();
0 ignored issues
show
Bug introduced by
The type SiteConfig 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...
40
        $this->client = $client;
41
        $this->setDateRange($config->DateRange);
42
        $this->setAnalytics(new Google_Service_AnalyticsReporting($client->getClient()));
43
        $this->setMetrics($config->Metric);
44
    }
45
46
    /**
47
     * @return mixed
48
     */
49
    public function getReport()
50
    {
51
        $config = SiteConfig::current_site_config();
52
53
        $request = new Google_Service_AnalyticsReporting_ReportRequest();
54
        $request->setDimensions(['name' => 'ga:pagePath']);
55
        $request->setViewId($config->Viewid);
56
        $request->setDimensionFilterClauses($this->getDimensionFilterClauses());
57
        $request->setDateRanges($this->getDateRange());
58
59
        $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
60
        $body->setReportRequests([$request]);
61
62
        return $this->analytics->reports->batchGet($body);
63
    }
64
65
    /**
66
     * @return mixed
67
     */
68
    public function getClient()
69
    {
70
        return $this->client;
71
    }
72
73
    /**
74
     * @param mixed $client
75
     */
76
    public function setClient($client)
77
    {
78
        $this->client = $client;
79
    }
80
81
    /**
82
     * @return Google_Service_AnalyticsReporting_DateRange
83
     */
84
    public function getDateRange()
85
    {
86
        return $this->dateRange;
87
    }
88
89
    /**
90
     * @param int $dateRange
91
     */
92
    public function setDateRange($dateRange)
93
    {
94
        $analyticsRange = new Google_Service_AnalyticsReporting_DateRange();
95
        $analyticsRange->setStartDate($dateRange . 'daysAgo');
96
        $analyticsRange->setEndDate('today');
97
        $this->dateRange = $analyticsRange;
98
    }
99
100
    /**
101
     * @return Google_Service_AnalyticsReporting
102
     */
103
    public function getAnalytics()
104
    {
105
        return $this->analytics;
106
    }
107
108
    /**
109
     * @param Google_Service_AnalyticsReporting $analytics
110
     */
111
    public function setAnalytics($analytics)
112
    {
113
        $this->analytics = $analytics;
114
    }
115
116
    /**
117
     * @return mixed
118
     */
119
    public function getMetrics()
120
    {
121
        return $this->metrics;
122
    }
123
124
    /**
125
     * @param mixed $metrics
126
     */
127
    public function setMetrics($metrics)
128
    {
129
        $metric = new Google_Service_AnalyticsReporting_Metric();
130
        $metric->setExpression($metrics);
131
        $this->metrics = $metric;
132
    }
133
134
    public function getDimensionFilters()
135
    {
136
        $filters = [];
137
        $startWith = config::inst()->get(static::class, 'starts_with');
0 ignored issues
show
Bug introduced by
The type config 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...
138
        $endWith = config::inst()->get(static::class, 'ends_with');
139
        if ($startWith) {
140
            return $this->createFilter('BEGINS_WITH', $startWith, $filters);
141
        }
142
        if ($endWith) {
143
            return $this->createFilter('ENDS_WITH', $endWith, $filters);
144
        }
145
146
        return $this->getPageDimensionFilters();
147
    }
148
149
    public function getPageDimensionFilters()
150
    {
151
        $filters = [];
152
        // Operators are not constants, see here: https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#Operator
153
        $operator = 'ENDS_WITH';
154
        $pages = $this->getPages();
155
156
        foreach ($pages as $page) {
157
            // Home is recorded as just a slash in GA, we need an
158
            // exception for that
159
            if ($page === 'home') {
160
                $page = '/';
161
                $operator = 'EXACT';
162
            }
163
164
            $filters = $this->createFilter($operator, $page, $filters);
165
            // Reset the operator
166
            if ($operator === 'EXACT') {
167
                $operator = 'ENDS_WITH';
168
            }
169
        }
170
171
        return $filters;
172
    }
173
174
    public function getDimensionFilterClauses()
175
    {
176
        $dimensionFilters = $this->getDimensionFilters();
177
        $dimensionClauses = new Google_Service_AnalyticsReporting_DimensionFilterClause();
178
        $dimensionClauses->setFilters($dimensionFilters);
179
        $dimensionClauses->setOperator('OR');
180
181
        return $dimensionClauses;
182
    }
183
184
    /**
185
     * Get the pages filtered by optional blacklisting and whitelisting
186
     * Return an array of the URLSegments, to limit memory abuse
187
     * @return array
188
     */
189
    public function getPages()
190
    {
191
        $blacklist = Config::inst()->get(static::class, 'blacklist');
0 ignored issues
show
Bug introduced by
The type Config 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...
192
        $whitelist = Config::inst()->get(static::class, 'whitelist');
193
        /** @var DataList|Page[] $pages */
194
        $pages = SiteTree::get();
0 ignored issues
show
Bug introduced by
The type SiteTree 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...
195
        if ($blacklist) {
196
            $pages = $this->getBlacklist($blacklist, $pages);
197
        }
198
        if ($whitelist) {
199
            $pages = $this->getWhitelistPages($whitelist, $pages);
200
        }
201
202
        if ($pages->count() > 20) {
203
            $this->batched = true;
204
        }
205
206
        // Google limits to 20 reports per complex filter setup
207
        return $pages->limit(20)->column('URLSegment');
208
    }
209
210
    /**
211
     * @param array $blacklist
212
     * @param DataList $pages
1 ignored issue
show
Bug introduced by
The type DataList 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...
213
     * @return DataList
214
     */
215
    protected function getBlacklist($blacklist, $pages)
216
    {
217
        $ids = [];
218
        foreach ($blacklist as $class) {
219
            $blacklistpages = $class::get();
220
            $ids = array_merge($ids, $blacklistpages->column('ID'));
221
        }
222
        $pages = $pages->exclude(['ID' => $ids]);
223
224
        return $pages;
225
    }
226
227
    /**
228
     * @param array $whitelist
229
     * @param DataList $pages
230
     * @return DataList
231
     */
232
    protected function getWhitelistPages($whitelist, $pages)
233
    {
234
        $ids = [];
235
        foreach ($whitelist as $class) {
236
            $nowDate = date('Y-m-d');
237
            $whitelistpages = $class::get()
238
                // This needs to be a where because of `IS NULL`
239
                ->where("(`Page`.`LastAnalyticsUpdate` < $nowDate)
240
                        OR (`Page`.`LastAnalyticsUpdate` IS NULL)");
241
            $ids = array_merge($ids, $whitelistpages->column('ID'));
242
        }
243
        $pages = $pages->filter(['ID' => $ids]);
244
245
        return $pages;
246
    }
247
248
    /**
249
     * @param $operator
250
     * @param $page
251
     * @param $filters
252
     * @return array
253
     */
254
    protected function createFilter($operator, $page, $filters)
255
    {
256
        $filter = new Google_Service_AnalyticsReporting_DimensionFilter();
257
        $filter->setOperator($operator);
258
        $filter->setCaseSensitive(false);
259
        $filter->setExpressions([$page]);
260
        $filter->setDimensionName('ga:pagePath');
261
        $filters[] = $filter;
262
263
        return $filters;
264
    }
265
266
    /**
267
     * @param array $rows
268
     * @return int
269
     * @throws \ValidationException
270
     */
271
    public function updateVisits($rows)
272
    {
273
        $count = 0;
274
        foreach ($rows as $row) {
275
            $metrics = $row->getMetrics();
276
            $values = $metrics[0]->getValues();
277
            $dimensions = $row->getDimensions();
278
            $dimensions = explode('/', $dimensions[0]);
279
            $page = $this->findPage($dimensions);
280
            // Only write if a page is found
281
            if ($page) {
282
                $count++;
283
                $this->updatePageVisit($page, $values);
284
            }
285
        }
286
        // If we're not getting any results back, we're out of data from Google.
287
        // Stop the batching process.
288
        if ($count === 0) {
289
            $this->batched = false;
290
        }
291
        return $count;
292
    }
293
294
    /**
295
     * @param array $dimensions
296
     * @return Page
297
     */
298
    public function findPage($dimensions)
299
    {
300
        // Because analytics returns a page link that starts with a forward slash
301
        // The first element of the array needs to be removed as it's empty.
302
        array_shift($dimensions);
303
        $firstItem = array_shift($dimensions);
304
        // If dimensions is 2 empty keys, we're looking at the homepage
305
        if (!$firstItem) {
306
            $firstItem = 'home';
307
        }
308
        // Start at the root of the site page
309
        /** @var Page $page */
310
        $page = Page::get()->filter(['URLSegment' => $firstItem])->first();
0 ignored issues
show
Bug introduced by
The type Page 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...
311
        // If there are items left in the dimensions, continue traversing down
312
        if (count($dimensions) && $page) {
313
            foreach ($dimensions as $segment) {
314
                if (strpos($segment, '?') === 0 || !$page || !$segment) {
315
                    continue;
316
                }
317
                $children = $page->AllChildren();
318
                $page = $children->filter(['URLSegment' => $segment])->first();
319
            }
320
        }
321
322
        return $page;
323
    }
324
325
    /**
326
     * @param Page $page
327
     * @param array $values
328
     */
329
    protected function updatePageVisit($page, $values)
330
    {
331
        $rePublish = $page->isPublished();
332
        $page->VisitCount = $values[0];
333
        $page->LastAnalyticsUpdate = date('Y-m-d');
334
        $page->write();
335
        // If the page was published before write, republish or the change won't be applied
336
        if ($rePublish) {
337
            $page->publish('Stage', 'Live');
338
        }
339
        $page->destroy();
340
    }
341
}
342