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