1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\GoogleAPI\Services; |
4
|
|
|
|
5
|
|
|
use Page; |
|
|
|
|
6
|
|
|
use SilverStripe\ORM\DataList; |
7
|
|
|
use SilverStripe\ORM\ValidationException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class PageUpdateService is used to update the count on pages |
11
|
|
|
*/ |
12
|
|
|
class PageUpdateService |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
public $batched = true; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array $rows |
22
|
|
|
* @return int |
23
|
|
|
* @throws ValidationException |
24
|
|
|
*/ |
25
|
|
|
public function updateVisits($rows) |
26
|
|
|
{ |
27
|
|
|
$count = 0; |
28
|
|
|
foreach ($rows as $row) { |
29
|
|
|
$metrics = $row->getMetrics(); |
30
|
|
|
$values = $metrics[0]->getValues(); |
31
|
|
|
$dimensions = $row->getDimensions(); |
32
|
|
|
$dimensions = explode('/', $dimensions[0]); |
33
|
|
|
$page = $this->findPage($dimensions); |
34
|
|
|
// Only write if a page is found |
35
|
|
|
if ($page) { |
36
|
|
|
$count++; |
37
|
|
|
$this->updatePageVisit($page, $values); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
// If we're not getting any results back, we're out of data from Google. |
41
|
|
|
// Stop the batching process. |
42
|
|
|
if ($count === 0 || $count > 20) { |
43
|
|
|
$this->batched = false; |
44
|
|
|
} |
45
|
|
|
return $count; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $dimensions |
50
|
|
|
* @return Page |
51
|
|
|
*/ |
52
|
|
|
public function findPage($dimensions) |
53
|
|
|
{ |
54
|
|
|
// Because analytics returns a page link that starts with a forward slash |
55
|
|
|
// The first element of the array needs to be removed as it's empty. |
56
|
|
|
array_shift($dimensions); |
57
|
|
|
$firstItem = array_shift($dimensions); |
58
|
|
|
// If dimensions is 2 empty keys, we're looking at the homepage |
59
|
|
|
if (!$firstItem) { |
60
|
|
|
$firstItem = 'home'; |
61
|
|
|
} |
62
|
|
|
// Start at the root of the site page |
63
|
|
|
/** @var Page $page */ |
64
|
|
|
$page = Page::get()->filter(['URLSegment' => $firstItem])->first(); |
65
|
|
|
// If there are items left in the dimensions, continue traversing down |
66
|
|
|
if (count($dimensions) && $page) { |
67
|
|
|
$page = $this->findPageChildren($dimensions, $page); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $page; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $dimensions |
75
|
|
|
* @param Page $page |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
protected function findPageChildren($dimensions, $page) |
79
|
|
|
{ |
80
|
|
|
foreach ($dimensions as $segment) { |
81
|
|
|
if (strpos($segment, '?') === 0 || !$page || !$segment) { |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
/** @var DataList|Page[] $children */ |
85
|
|
|
$children = $page->AllChildren(); |
86
|
|
|
$page = $children->filter(['URLSegment' => $segment])->first(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $page; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Page $page |
94
|
|
|
* @param array $values |
95
|
|
|
* @throws ValidationException |
96
|
|
|
*/ |
97
|
|
|
protected function updatePageVisit($page, $values) |
98
|
|
|
{ |
99
|
|
|
$rePublish = $page->isPublished(); |
100
|
|
|
$page->VisitCount = $values[0]; |
101
|
|
|
$page->LastAnalyticsUpdate = date('Y-m-d'); |
102
|
|
|
$page->write(); |
103
|
|
|
// If the page was published before write, republish or the change won't be applied |
104
|
|
|
if ($rePublish) { |
105
|
|
|
$page->copyVersionToStage('Stage', 'Live'); |
106
|
|
|
} |
107
|
|
|
$page->destroy(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths