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
|
<?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\UserSettings;
use Piwik\Common;
use Piwik\DataAccess\LogAggregator;
use Piwik\DataArray;
use Piwik\DataTable;
use Piwik\Metrics;
require_once PIWIK_INCLUDE_PATH . '/plugins/UserSettings/functions.php';
/**
* Archiver for UserSettings Plugin
*
* @see PluginsArchiver
*/
class Archiver extends \Piwik\Plugin\Archiver
{
const LANGUAGE_RECORD_NAME = 'UserSettings_language';
const PLUGIN_RECORD_NAME = 'UserSettings_plugin';
const SCREEN_TYPE_RECORD_NAME = 'UserSettings_wideScreen';
const RESOLUTION_RECORD_NAME = 'UserSettings_resolution';
const CONFIGURATION_RECORD_NAME = 'UserSettings_configuration';
const LANGUAGE_DIMENSION = "log_visit.location_browser_lang";
const RESOLUTION_DIMENSION = "log_visit.config_resolution";
const CONFIGURATION_DIMENSION = "CONCAT(log_visit.config_os, ';', log_visit.config_browser_name, ';', log_visit.config_resolution)";
/**
* Daily archive of User Settings report. Processes reports for Visits by Resolution,
* by Browser, Browser family, etc. Some reports are built from the logs, some reports
* are superset of an existing report (eg. Browser family is built from the Browser report)
*/
public function aggregateDayReport()
{
$this->aggregateByConfiguration();
$this->aggregateByResolutionAndScreenType();
$this->aggregateByPlugin();
$this->aggregateByLanguage();
}
/**
* Period archiving: simply sums up daily archives
*/
public function aggregateMultipleReports()
{
$dataTableRecords = array(
self::CONFIGURATION_RECORD_NAME,
self::RESOLUTION_RECORD_NAME,
self::SCREEN_TYPE_RECORD_NAME,
self::PLUGIN_RECORD_NAME,
self::LANGUAGE_RECORD_NAME,
);
$this->getProcessor()->aggregateDataTableRecords($dataTableRecords, $this->maximumRows);
}
protected function aggregateByConfiguration()
{
$metrics = $this->getLogAggregator()->getMetricsFromVisitByDimension(self::CONFIGURATION_DIMENSION)->asDataTable();
$this->insertTable(self::CONFIGURATION_RECORD_NAME, $metrics);
}
protected function aggregateByResolutionAndScreenType()
{
$resolutions = $this->aggregateByResolution();
$this->aggregateByScreenType($resolutions);
}
protected function aggregateByResolution()
{
$table = $this->getLogAggregator()->getMetricsFromVisitByDimension(self::RESOLUTION_DIMENSION)->asDataTable();
$table->filter('ColumnCallbackDeleteRow', array('label', function ($value) {
return strlen($value) <= 5;
}));
$this->insertTable(self::RESOLUTION_RECORD_NAME, $table);
return $table;
}
protected function aggregateByScreenType(DataTable $resolutions)
{
$resolutions->filter('GroupBy', array('label', __NAMESPACE__ . '\getScreenTypeFromResolution'));
$this->insertTable(self::SCREEN_TYPE_RECORD_NAME, $resolutions);
}
protected function aggregateByPlugin()
{
$selects = array(
"sum(case log_visit.config_pdf when 1 then 1 else 0 end) as pdf",
"sum(case log_visit.config_flash when 1 then 1 else 0 end) as flash",
"sum(case log_visit.config_java when 1 then 1 else 0 end) as java",
"sum(case log_visit.config_director when 1 then 1 else 0 end) as director",
"sum(case log_visit.config_quicktime when 1 then 1 else 0 end) as quicktime",
"sum(case log_visit.config_realplayer when 1 then 1 else 0 end) as realplayer",
"sum(case log_visit.config_windowsmedia when 1 then 1 else 0 end) as windowsmedia",
"sum(case log_visit.config_gears when 1 then 1 else 0 end) as gears",
"sum(case log_visit.config_silverlight when 1 then 1 else 0 end) as silverlight",
"sum(case log_visit.config_cookie when 1 then 1 else 0 end) as cookie"
);
$query = $this->getLogAggregator()->queryVisitsByDimension(array(), false, $selects, $metrics = array());
$data = $query->fetch();
$cleanRow = LogAggregator::makeArrayOneColumn($data, Metrics::INDEX_NB_VISITS);
$table = DataTable::makeFromIndexedArray($cleanRow);
$this->insertTable(self::PLUGIN_RECORD_NAME, $table);
}
protected function aggregateByLanguage()
{
$query = $this->getLogAggregator()->queryVisitsByDimension(array("label" => self::LANGUAGE_DIMENSION));
$languageCodes = array_keys(Common::getLanguagesList());
$countryCodes = Common::getCountriesList($includeInternalCodes = true);
$metricsByLanguage = new DataArray();
while ($row = $query->fetch()) {
$langCode = Common::extractLanguageCodeFromBrowserLanguage($row['label'], $languageCodes);
$countryCode = Common::extractCountryCodeFromBrowserLanguage($row['label'], $countryCodes, $enableLanguageToCountryGuess = true);
if ($countryCode == 'xx' || $countryCode == $langCode) {
$metricsByLanguage->sumMetricsVisits($langCode, $row);
} else {
$metricsByLanguage->sumMetricsVisits($langCode . '-' . $countryCode, $row);
}
}
$report = $metricsByLanguage->asDataTable();
$this->insertTable(self::LANGUAGE_RECORD_NAME, $report);
}
protected function insertTable($recordName, DataTable $table)
{
$report = $table->getSerialized($this->maximumRows, null, Metrics::INDEX_NB_VISITS);
return $this->getProcessor()->insertBlobRecord($recordName, $report);
}
}
|