1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\Referrers;
use Exception;
use Piwik\Common;
use Piwik\Config;
use Piwik\DataArray;
use Piwik\Metrics;
class Archiver extends \Piwik\Plugin\Archiver
{
const SEARCH_ENGINES_RECORD_NAME = 'Referrers_keywordBySearchEngine';
const KEYWORDS_RECORD_NAME = 'Referrers_searchEngineByKeyword';
const CAMPAIGNS_RECORD_NAME = 'Referrers_keywordByCampaign';
const WEBSITES_RECORD_NAME = 'Referrers_urlByWebsite';
const REFERRER_TYPE_RECORD_NAME = 'Referrers_type';
const METRIC_DISTINCT_SEARCH_ENGINE_RECORD_NAME = 'Referrers_distinctSearchEngines';
const METRIC_DISTINCT_KEYWORD_RECORD_NAME = 'Referrers_distinctKeywords';
const METRIC_DISTINCT_CAMPAIGN_RECORD_NAME = 'Referrers_distinctCampaigns';
const METRIC_DISTINCT_WEBSITE_RECORD_NAME = 'Referrers_distinctWebsites';
const METRIC_DISTINCT_URLS_RECORD_NAME = 'Referrers_distinctWebsitesUrls';
protected $columnToSortByBeforeTruncation;
protected $maximumRowsInDataTableLevelZero;
protected $maximumRowsInSubDataTable;
/**
* @var DataArray[] $arrays
*/
protected $arrays = array();
protected $distinctUrls = array();
function __construct($processor)
{
parent::__construct($processor);
$this->columnToSortByBeforeTruncation = Metrics::INDEX_NB_VISITS;
// Reading pre 2.0 config file settings
$this->maximumRowsInDataTableLevelZero = @Config::getInstance()->General['datatable_archiving_maximum_rows_referers'];
$this->maximumRowsInSubDataTable = @Config::getInstance()->General['datatable_archiving_maximum_rows_subtable_referers'];
if (empty($this->maximumRowsInDataTableLevelZero)) {
$this->maximumRowsInDataTableLevelZero = Config::getInstance()->General['datatable_archiving_maximum_rows_referrers'];
$this->maximumRowsInSubDataTable = Config::getInstance()->General['datatable_archiving_maximum_rows_subtable_referrers'];
}
}
public function aggregateDayReport()
{
foreach ($this->getRecordNames() as $record) {
$this->arrays[$record] = new DataArray();
}
$this->aggregateFromVisits(array("referer_type", "referer_name", "referer_keyword", "referer_url"));
$this->aggregateFromConversions(array("referer_type", "referer_name", "referer_keyword"));
$this->insertDayReports();
}
protected function getRecordNames()
{
return array(
self::REFERRER_TYPE_RECORD_NAME,
self::KEYWORDS_RECORD_NAME,
self::SEARCH_ENGINES_RECORD_NAME,
self::WEBSITES_RECORD_NAME,
self::CAMPAIGNS_RECORD_NAME,
);
}
protected function makeReferrerTypeNonEmpty(&$row)
{
if (empty($row['referer_type'])) {
$row['referer_type'] = Common::REFERRER_TYPE_DIRECT_ENTRY;
}
}
protected function aggregateVisitRow($row)
{
switch ($row['referer_type']) {
case Common::REFERRER_TYPE_SEARCH_ENGINE:
if (empty($row['referer_keyword'])) {
$row['referer_keyword'] = API::LABEL_KEYWORD_NOT_DEFINED;
}
$searchEnginesArray = $this->getDataArray(self::SEARCH_ENGINES_RECORD_NAME);
$searchEnginesArray->sumMetricsVisits($row['referer_name'], $row);
$searchEnginesArray->sumMetricsVisitsPivot($row['referer_name'], $row['referer_keyword'], $row);
$keywordsDataArray = $this->getDataArray(self::KEYWORDS_RECORD_NAME);
$keywordsDataArray->sumMetricsVisits($row['referer_keyword'], $row);
$keywordsDataArray->sumMetricsVisitsPivot($row['referer_keyword'], $row['referer_name'], $row);
break;
case Common::REFERRER_TYPE_WEBSITE:
$this->getDataArray(self::WEBSITES_RECORD_NAME)->sumMetricsVisits($row['referer_name'], $row);
$this->getDataArray(self::WEBSITES_RECORD_NAME)->sumMetricsVisitsPivot($row['referer_name'], $row['referer_url'], $row);
$urlHash = substr(md5($row['referer_url']), 0, 10);
if (!isset($this->distinctUrls[$urlHash])) {
$this->distinctUrls[$urlHash] = true;
}
break;
case Common::REFERRER_TYPE_CAMPAIGN:
if (!empty($row['referer_keyword'])) {
$this->getDataArray(self::CAMPAIGNS_RECORD_NAME)->sumMetricsVisitsPivot($row['referer_name'], $row['referer_keyword'], $row);
}
$this->getDataArray(self::CAMPAIGNS_RECORD_NAME)->sumMetricsVisits($row['referer_name'], $row);
break;
case Common::REFERRER_TYPE_DIRECT_ENTRY:
// direct entry are aggregated below in $this->metricsByType array
break;
default:
throw new Exception("Non expected referer_type = " . $row['referer_type']);
break;
}
$this->getDataArray(self::REFERRER_TYPE_RECORD_NAME)->sumMetricsVisits($row['referer_type'], $row);
}
/**
* @param string $name
* @return DataArray
*/
protected function getDataArray($name)
{
return $this->arrays[$name];
}
protected function aggregateFromConversions($dimensions)
{
$query = $this->getLogAggregator()->queryConversionsByDimension($dimensions);
if ($query === false) {
return;
}
while ($row = $query->fetch()) {
$this->makeReferrerTypeNonEmpty($row);
$skipAggregateByType = $this->aggregateConversionRow($row);
if (!$skipAggregateByType) {
$this->getDataArray(self::REFERRER_TYPE_RECORD_NAME)->sumMetricsGoals($row['referer_type'], $row);
}
}
foreach ($this->arrays as $dataArray) {
$dataArray->enrichMetricsWithConversions();
}
}
protected function aggregateConversionRow($row)
{
$skipAggregateByType = false;
switch ($row['referer_type']) {
case Common::REFERRER_TYPE_SEARCH_ENGINE:
if (empty($row['referer_keyword'])) {
$row['referer_keyword'] = API::LABEL_KEYWORD_NOT_DEFINED;
}
$this->getDataArray(self::SEARCH_ENGINES_RECORD_NAME)->sumMetricsGoals($row['referer_name'], $row);
$this->getDataArray(self::KEYWORDS_RECORD_NAME)->sumMetricsGoals($row['referer_keyword'], $row);
break;
case Common::REFERRER_TYPE_WEBSITE:
$this->getDataArray(self::WEBSITES_RECORD_NAME)->sumMetricsGoals($row['referer_name'], $row);
break;
case Common::REFERRER_TYPE_CAMPAIGN:
if (!empty($row['referer_keyword'])) {
$this->getDataArray(self::CAMPAIGNS_RECORD_NAME)->sumMetricsGoalsPivot($row['referer_name'], $row['referer_keyword'], $row);
}
$this->getDataArray(self::CAMPAIGNS_RECORD_NAME)->sumMetricsGoals($row['referer_name'], $row);
break;
case Common::REFERRER_TYPE_DIRECT_ENTRY:
// Direct entry, no sub dimension
break;
default:
// The referer type is user submitted for goal conversions, we ignore any malformed value
// Continue to the next while iteration
$skipAggregateByType = true;
break;
}
return $skipAggregateByType;
}
/**
* Records the daily stats (numeric or datatable blob) into the archive tables.
*/
protected function insertDayReports()
{
$this->insertDayNumericMetrics();
// insert DataTable reports
foreach ($this->getRecordNames() as $recordName) {
$blob = $this->getDataArray($recordName)->asDataTable()->getSerialized($this->maximumRowsInDataTableLevelZero, $this->maximumRowsInSubDataTable, $this->columnToSortByBeforeTruncation);
$this->getProcessor()->insertBlobRecord($recordName, $blob);
}
}
protected function insertDayNumericMetrics()
{
$numericRecords = array(
self::METRIC_DISTINCT_SEARCH_ENGINE_RECORD_NAME => count($this->getDataArray(self::SEARCH_ENGINES_RECORD_NAME)->getDataArray()),
self::METRIC_DISTINCT_KEYWORD_RECORD_NAME => count($this->getDataArray(self::KEYWORDS_RECORD_NAME)->getDataArray()),
self::METRIC_DISTINCT_CAMPAIGN_RECORD_NAME => count($this->getDataArray(self::CAMPAIGNS_RECORD_NAME)->getDataArray()),
self::METRIC_DISTINCT_WEBSITE_RECORD_NAME => count($this->getDataArray(self::WEBSITES_RECORD_NAME)->getDataArray()),
self::METRIC_DISTINCT_URLS_RECORD_NAME => count($this->distinctUrls),
);
$this->getProcessor()->insertNumericRecords($numericRecords);
}
public function aggregateMultipleReports()
{
$dataTableToSum = $this->getRecordNames();
$nameToCount = $this->getProcessor()->aggregateDataTableRecords($dataTableToSum, $this->maximumRowsInDataTableLevelZero, $this->maximumRowsInSubDataTable, $this->columnToSortByBeforeTruncation);
$mappingFromArchiveName = array(
self::METRIC_DISTINCT_SEARCH_ENGINE_RECORD_NAME =>
array('typeCountToUse' => 'level0',
'nameTableToUse' => self::SEARCH_ENGINES_RECORD_NAME,
),
self::METRIC_DISTINCT_KEYWORD_RECORD_NAME =>
array('typeCountToUse' => 'level0',
'nameTableToUse' => self::KEYWORDS_RECORD_NAME,
),
self::METRIC_DISTINCT_CAMPAIGN_RECORD_NAME =>
array('typeCountToUse' => 'level0',
'nameTableToUse' => self::CAMPAIGNS_RECORD_NAME,
),
self::METRIC_DISTINCT_WEBSITE_RECORD_NAME =>
array('typeCountToUse' => 'level0',
'nameTableToUse' => self::WEBSITES_RECORD_NAME,
),
self::METRIC_DISTINCT_URLS_RECORD_NAME =>
array('typeCountToUse' => 'recursive',
'nameTableToUse' => self::WEBSITES_RECORD_NAME,
),
);
foreach ($mappingFromArchiveName as $name => $infoMapping) {
$nameTableToUse = $infoMapping['nameTableToUse'];
if ($infoMapping['typeCountToUse'] == 'recursive') {
$countValue = $nameToCount[$nameTableToUse]['recursive'] - $nameToCount[$nameTableToUse]['level0'];
} else {
$countValue = $nameToCount[$nameTableToUse]['level0'];
}
$this->getProcessor()->insertNumericRecord($name, $countValue);
}
}
/**
* @param $fields
*/
private function aggregateFromVisits($fields)
{
$query = $this->getLogAggregator()->queryVisitsByDimension($fields);
while ($row = $query->fetch()) {
$this->makeReferrerTypeNonEmpty($row);
$this->aggregateVisitRow($row);
}
}
}
|