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
|
<?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\Events;
use Piwik\Config;
use Piwik\DataArray;
use Piwik\DataTable;
use Piwik\Metrics;
use Piwik\Plugins\Actions\ArchivingHelper;
use Piwik\RankingQuery;
use Piwik\Tracker\Action;
/**
* Processing reports for Events
EVENT
- Category
- Action
- Name
- Value
METRICS (Events Overview report)
- Total number of events
- Unique events
- Visits with events
- Events/visit
- Event value
- Average event value AVG(custom_float)
MAIN REPORTS:
- Top Event Category (total events, unique events, event value, avg+min+max value)
- Top Event Action (total events, unique events, event value, avg+min+max value)
- Top Event Name (total events, unique events, event value, avg+min+max value)
COMPOSED REPORTS
- Top Category > Actions X
- Top Category > Names X
- Top Actions > Categories X
- Top Actions > Names X
- Top Names > Actions X
- Top Names > Categories X
UI
- Overview at the top (graph + Sparklines)
- Below show the left menu, defaults to Top Event Category
Not MVP:
- On hover on any row: Show % of total events
- Add min value metric, max value metric in tooltip
- List event scope Custom Variables Names > Custom variables values > Event Names > Event Actions
- List event scope Custom Variables Value > Event Category > Event Names > Event Actions
NOTES:
- For a given Name, Category is often constant
*/
class Archiver extends \Piwik\Plugin\Archiver
{
const EVENTS_CATEGORY_ACTION_RECORD_NAME = 'Events_category_action';
const EVENTS_CATEGORY_NAME_RECORD_NAME = 'Events_category_name';
const EVENTS_ACTION_CATEGORY_RECORD_NAME = 'Events_action_category';
const EVENTS_ACTION_NAME_RECORD_NAME = 'Events_action_name';
const EVENTS_NAME_ACTION_RECORD_NAME = 'Events_name_action';
const EVENTS_NAME_CATEGORY_RECORD_NAME = 'Events_name_category';
const EVENT_NAME_NOT_SET = 'Piwik_EventNameNotSet';
/**
* @var DataArray[]
*/
protected $arrays = array();
function __construct($processor)
{
parent::__construct($processor);
$this->columnToSortByBeforeTruncation = Metrics::INDEX_NB_VISITS;
$this->maximumRowsInDataTable = Config::getInstance()->General['datatable_archiving_maximum_rows_events'];
$this->maximumRowsInSubDataTable = Config::getInstance()->General['datatable_archiving_maximum_rows_subtable_events'];
}
protected function getRecordToDimensions()
{
return array(
self::EVENTS_CATEGORY_ACTION_RECORD_NAME => array("eventCategory", "eventAction"),
self::EVENTS_CATEGORY_NAME_RECORD_NAME => array("eventCategory", "eventName"),
self::EVENTS_ACTION_NAME_RECORD_NAME => array("eventAction", "eventName"),
self::EVENTS_ACTION_CATEGORY_RECORD_NAME => array("eventAction", "eventCategory"),
self::EVENTS_NAME_ACTION_RECORD_NAME => array("eventName", "eventAction"),
self::EVENTS_NAME_CATEGORY_RECORD_NAME => array("eventName", "eventCategory"),
);
}
public function aggregateMultipleReports()
{
$dataTableToSum = $this->getRecordNames();
$this->getProcessor()->aggregateDataTableRecords(
$dataTableToSum,
$this->maximumRowsInDataTable,
$this->maximumRowsInSubDataTable,
$this->columnToSortByBeforeTruncation,
$columnsAggregationOperation = null,
$columnsToRenameAfterAggregation = null,
$countRowsRecursive = false);
}
protected function getRecordNames()
{
$mapping = $this->getRecordToDimensions();
return array_keys($mapping);
}
public function aggregateDayReport()
{
$this->aggregateDayEvents();
$this->insertDayReports();
}
protected function aggregateDayEvents()
{
$select = "
log_action_event_category.name as eventCategory,
log_action_event_action.name as eventAction,
log_action_event_name.name as eventName,
count(distinct log_link_visit_action.idvisit) as `" . Metrics::INDEX_NB_VISITS . "`,
count(distinct log_link_visit_action.idvisitor) as `" . Metrics::INDEX_NB_UNIQ_VISITORS . "`,
count(*) as `" . Metrics::INDEX_EVENT_NB_HITS . "`,
sum(
case when " . Action::DB_COLUMN_CUSTOM_FLOAT . " is null
then 0
else " . Action::DB_COLUMN_CUSTOM_FLOAT . "
end
) as `" . Metrics::INDEX_EVENT_SUM_EVENT_VALUE . "`,
sum( case when " . Action::DB_COLUMN_CUSTOM_FLOAT . " is null then 0 else 1 end )
as `" . Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE . "`,
min(" . Action::DB_COLUMN_CUSTOM_FLOAT . ") as `" . Metrics::INDEX_EVENT_MIN_EVENT_VALUE . "`,
max(" . Action::DB_COLUMN_CUSTOM_FLOAT . ") as `" . Metrics::INDEX_EVENT_MAX_EVENT_VALUE . "`
";
$from = array(
"log_link_visit_action",
array(
"table" => "log_action",
"tableAlias" => "log_action_event_category",
"joinOn" => "log_link_visit_action.idaction_event_category = log_action_event_category.idaction"
),
array(
"table" => "log_action",
"tableAlias" => "log_action_event_action",
"joinOn" => "log_link_visit_action.idaction_event_action = log_action_event_action.idaction"
),
array(
"table" => "log_action",
"tableAlias" => "log_action_event_name",
"joinOn" => "log_link_visit_action.idaction_name = log_action_event_name.idaction"
)
);
$where = "log_link_visit_action.server_time >= ?
AND log_link_visit_action.server_time <= ?
AND log_link_visit_action.idsite = ?
AND log_link_visit_action.idaction_event_category IS NOT NULL";
$groupBy = "log_action_event_category.idaction,
log_action_event_action.idaction,
log_action_event_name.idaction";
$orderBy = "`" . Metrics::INDEX_NB_VISITS . "` DESC";
$rankingQueryLimit = ArchivingHelper::getRankingQueryLimit();
$rankingQuery = null;
if ($rankingQueryLimit > 0) {
$rankingQuery = new RankingQuery($rankingQueryLimit);
$rankingQuery->setOthersLabel(DataTable::LABEL_SUMMARY_ROW);
$rankingQuery->addLabelColumn(array('eventCategory', 'eventAction', 'eventName'));
$rankingQuery->addColumn(array(Metrics::INDEX_NB_UNIQ_VISITORS));
$rankingQuery->addColumn(array(Metrics::INDEX_EVENT_NB_HITS, Metrics::INDEX_NB_VISITS, Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE), 'sum');
$rankingQuery->addColumn(Metrics::INDEX_EVENT_SUM_EVENT_VALUE, 'sum');
$rankingQuery->addColumn(Metrics::INDEX_EVENT_MIN_EVENT_VALUE, 'min');
$rankingQuery->addColumn(Metrics::INDEX_EVENT_MAX_EVENT_VALUE, 'max');
}
$this->archiveDayQueryProcess($select, $from, $where, $groupBy, $orderBy, $rankingQuery);
}
protected function archiveDayQueryProcess($select, $from, $where, $groupBy, $orderBy, RankingQuery $rankingQuery)
{
// get query with segmentation
$query = $this->getLogAggregator()->generateQuery($select, $from, $where, $groupBy, $orderBy);
// apply ranking query
if ($rankingQuery) {
$query['sql'] = $rankingQuery->generateRankingQuery($query['sql']);
}
// get result
$resultSet = $this->getLogAggregator()->getDb()->query($query['sql'], $query['bind']);
if ($resultSet === false) {
return;
}
while ($row = $resultSet->fetch()) {
$this->aggregateEventRow($row);
}
}
/**
* Records the daily datatables
*/
protected function insertDayReports()
{
foreach ($this->arrays as $recordName => $dataArray) {
$dataTable = $dataArray->asDataTable();
$blob = $dataTable->getSerialized(
$this->maximumRowsInDataTable,
$this->maximumRowsInSubDataTable,
$this->columnToSortByBeforeTruncation);
$this->getProcessor()->insertBlobRecord($recordName, $blob);
}
}
/**
* @param string $name
* @return DataArray
*/
protected function getDataArray($name)
{
if (empty($this->arrays[$name])) {
$this->arrays[$name] = new DataArray();
}
return $this->arrays[$name];
}
protected function aggregateEventRow($row)
{
foreach ($this->getRecordToDimensions() as $record => $dimensions) {
$dataArray = $this->getDataArray($record);
$mainDimension = $dimensions[0];
$mainLabel = $row[$mainDimension];
// Event name is optional
if ($mainDimension == 'eventName'
&& empty($mainLabel)) {
$mainLabel = self::EVENT_NAME_NOT_SET;
}
$dataArray->sumMetricsEvents($mainLabel, $row);
$subDimension = $dimensions[1];
$subLabel = $row[$subDimension];
if (empty($subLabel)) {
continue;
}
$dataArray->sumMetricsEventsPivot($mainLabel, $subLabel, $row);
}
}
}
|