Welcome to mirror list, hosted at ThFree Co, Russian Federation.

API.php « MultiSites « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2906edc98106914b96e9a029ab76a548e0903a9a (plain)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Piwik_Plugins
 * @package Piwik_MultiSites
 */
use Piwik\Archive;
use Piwik\DataTable\Filter\CalculateEvolutionFilter;
use Piwik\Period\Range;
use Piwik\Piwik;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\Site;

/**
 * The MultiSites API lets you request the key metrics (visits, page views, revenue) for all Websites in Piwik.
 */
class Piwik_MultiSites_API
{
    const METRIC_TRANSLATION_KEY = 'translation';
    const METRIC_EVOLUTION_COL_NAME_KEY = 'evolution_column_name';
    const METRIC_RECORD_NAME_KEY = 'record_name';
    const METRIC_IS_ECOMMERCE_KEY = 'is_ecommerce';

    const NB_VISITS_METRIC = 'nb_visits';
    const NB_ACTIONS_METRIC = 'nb_actions';
    const NB_PAGEVIEWS_LABEL = 'nb_pageviews';
    const NB_PAGEVIEWS_METRIC = 'Actions_nb_pageviews';
    const GOAL_REVENUE_METRIC = 'revenue';
    const GOAL_CONVERSION_METRIC = 'nb_conversions';
    const ECOMMERCE_ORDERS_METRIC = 'orders';
    const ECOMMERCE_REVENUE_METRIC = 'ecommerce_revenue';

    static private $baseMetrics = array(
        self::NB_VISITS_METRIC   => array(
            self::METRIC_TRANSLATION_KEY        => 'General_ColumnNbVisits',
            self::METRIC_EVOLUTION_COL_NAME_KEY => 'visits_evolution',
            self::METRIC_RECORD_NAME_KEY        => self::NB_VISITS_METRIC,
            self::METRIC_IS_ECOMMERCE_KEY       => false,
        ),
        self::NB_ACTIONS_METRIC  => array(
            self::METRIC_TRANSLATION_KEY        => 'General_ColumnNbActions',
            self::METRIC_EVOLUTION_COL_NAME_KEY => 'actions_evolution',
            self::METRIC_RECORD_NAME_KEY        => self::NB_ACTIONS_METRIC,
            self::METRIC_IS_ECOMMERCE_KEY       => false,
        ),
        self::NB_PAGEVIEWS_LABEL => array(
            self::METRIC_TRANSLATION_KEY        => 'General_ColumnPageviews',
            self::METRIC_EVOLUTION_COL_NAME_KEY => 'pageviews_evolution',
            self::METRIC_RECORD_NAME_KEY        => self::NB_PAGEVIEWS_METRIC,
            self::METRIC_IS_ECOMMERCE_KEY       => false,
        )
    );

    /**
     * The singleton instance of this class.
     */
    static private $instance = null;

    /**
     * Returns the singleton instance of this class. The instance is created
     * if it hasn't been already.
     *
     * @return Piwik_MultiSites_API
     */
    static public function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new self;
        }

        return self::$instance;
    }

    /**
     * Returns a report displaying the total visits, actions and revenue, as
     * well as the evolution of these values, of all existing sites over a
     * specified period of time.
     *
     * If the specified period is not a 'range', this function will calculcate
     * evolution metrics. Evolution metrics are metrics that display the
     * percent increase/decrease of another metric since the last period.
     *
     * This function will merge the result of the archive query so each
     * row in the result DataTable will correspond to the metrics of a single
     * site. If a date range is specified, the result will be a
     * DataTable_Array, but it will still be merged.
     *
     * @param string $period The period type to get data for.
     * @param string $date The date(s) to get data for.
     * @param bool|string $segment The segments to get data for.
     * @param bool|string $_restrictSitesToLogin Hack used to enforce we restrict the returned data to the specified username
     *                                        Only used when a scheduled task is running
     * @param bool|string $enhanced When true, return additional goal & ecommerce metrics
     * @param bool|string $pattern If specified, only the website which names (or site ID) match the pattern will be returned using SitesManager.getPatternMatchSites
     * @return DataTable
     */
    public function getAll($period, $date, $segment = false, $_restrictSitesToLogin = false, $enhanced = false, $pattern = false)
    {
        Piwik::checkUserHasSomeViewAccess();

        $idSites = $this->getSitesIdFromPattern($pattern);

        if(empty($idSites)) {
            return new DataTable();
        }
        return $this->buildDataTable(
            $idSites,
            $period,
            $date,
            $segment,
            $_restrictSitesToLogin,
            $enhanced,
            $multipleWebsitesRequested = true
        );
    }

    /**
     * Fetches the list of sites which names match the string pattern
     *
     * @param $pattern
     * @return array|string
     */
    private function getSitesIdFromPattern($pattern)
    {
        $idSites = 'all';
        if(empty($pattern)) {
            return $idSites;
        }
        $idSites = array();
        $sites = Piwik_API_Request::processRequest('SitesManager.getPatternMatchSites',
            array('pattern'   => $pattern,
                  // added because caller could overwrite these
                  'serialize' => 0,
                  'format'    => 'original'));
        if (!empty($sites)) {
            foreach ($sites as $site) {
                $idSites[] = $site['idsite'];
            }
        }
        return $idSites;
    }

    /**
     * Same as getAll but for a unique Piwik site
     * @see Piwik_MultiSites_API::getAll()
     *
     * @param int $idSite Id of the Piwik site
     * @param string $period The period type to get data for.
     * @param string $date The date(s) to get data for.
     * @param bool|string $segment The segments to get data for.
     * @param bool|string $_restrictSitesToLogin Hack used to enforce we restrict the returned data to the specified username
     *                                        Only used when a scheduled task is running
     * @param bool|string $enhanced When true, return additional goal & ecommerce metrics
     * @return DataTable
     */
    public function getOne($idSite, $period, $date, $segment = false, $_restrictSitesToLogin = false, $enhanced = false)
    {
        Piwik::checkUserHasViewAccess($idSite);
        return $this->buildDataTable(
            $idSite,
            $period,
            $date,
            $segment,
            $_restrictSitesToLogin,
            $enhanced,
            $multipleWebsitesRequested = false
        );
    }

    private function buildDataTable($sites, $period, $date, $segment, $_restrictSitesToLogin, $enhanced, $multipleWebsitesRequested)
    {
        $allWebsitesRequested = ($sites == 'all');
        if ($allWebsitesRequested) {
            if (Piwik::isUserIsSuperUser()
                // Hack: when this API function is called as a Scheduled Task, Super User status is enforced.
                // This means this function would return ALL websites in all cases.
                // Instead, we make sure that only the right set of data is returned
                && !Piwik_TaskScheduler::isTaskBeingExecuted()
            ) {
                Site::setSites(
                    Piwik_SitesManager_API::getInstance()->getAllSites()
                );
            } else {
                Site::setSitesFromArray(
                    Piwik_SitesManager_API::getInstance()->getSitesWithAtLeastViewAccess($limit = false, $_restrictSitesToLogin)
                );
            }
        }

        // build the archive type used to query archive data
        $archive = Archive::build(
            $sites,
            $period,
            $date,
            $segment,
            $_restrictSitesToLogin
        );

        // determine what data will be displayed
        $fieldsToGet = array();
        $columnNameRewrites = array();
        $apiECommerceMetrics = array();
        $apiMetrics = Piwik_MultiSites_API::getApiMetrics($enhanced);
        foreach ($apiMetrics as $metricName => $metricSettings) {
            $fieldsToGet[] = $metricSettings[self::METRIC_RECORD_NAME_KEY];
            $columnNameRewrites[$metricSettings[self::METRIC_RECORD_NAME_KEY]] = $metricName;

            if ($metricSettings[self::METRIC_IS_ECOMMERCE_KEY]) {
                $apiECommerceMetrics[$metricName] = $metricSettings;
            }
        }

        // get the data
        // $dataTable instanceOf Set
        $dataTable = $archive->getDataTableFromNumeric($fieldsToGet);

        // get rid of the DataTable_Array that is created by the IndexedBySite archive type
        if ($dataTable instanceof DataTable\Map
            && $multipleWebsitesRequested
        ) {
            $dataTable = $dataTable->mergeChildren();
        } else {
            if (!($dataTable instanceof DataTable\Map)
                && $dataTable->getRowsCount() > 0
            ) {
                $firstSite = is_array($sites) ? reset($sites) : $sites;
                
                $firstDataTableRow = $dataTable->getFirstRow();
                $firstDataTableRow->setColumn('label', $firstSite);
            }
        }

        // calculate total visits/actions/revenue
        $this->setMetricsTotalsMetadata($dataTable, $apiMetrics);

        // if the period isn't a range & a lastN/previousN date isn't used, we get the same
        // data for the last period to show the evolution of visits/actions/revenue
        list($strLastDate, $lastPeriod) = Range::getLastDate($date, $period);
        if ($strLastDate !== false) {
            if ($lastPeriod !== false) {
                // NOTE: no easy way to set last period date metadata when range of dates is requested.
                //       will be easier if DataTable_Array::metadata is removed, and metadata that is
                //       put there is put directly in DataTable::metadata.
                $dataTable->setMetadata(self::getLastPeriodMetadataName('date'), $lastPeriod);
            }
            $pastArchive = Archive::build($sites, $period, $strLastDate, $segment, $_restrictSitesToLogin);
            $pastData = $pastArchive->getDataTableFromNumeric($fieldsToGet);
            
            if ($pastData instanceof DataTable\Map
                && $multipleWebsitesRequested
            ) {
                $pastData = $pastData->mergeChildren();
            }

            // use past data to calculate evolution percentages
            $this->calculateEvolutionPercentages($dataTable, $pastData, $apiMetrics);

            $this->setPastDataMetadata($dataTable, $pastData, $apiMetrics);
        }

        // remove eCommerce related metrics on non eCommerce Piwik sites
        // note: this is not optimal in terms of performance: those metrics should not be retrieved in the first place
        if ($enhanced) {
            // $dataTableRows instanceOf Row[]
            $dataTableRows = $dataTable->getRows();

            foreach ($dataTableRows as $dataTableRow) {
                $siteId = $dataTableRow->getColumn('label');
                if (!Site::isEcommerceEnabledFor($siteId)) {
                    foreach ($apiECommerceMetrics as $metricSettings) {
                        $dataTableRow->deleteColumn($metricSettings[self::METRIC_RECORD_NAME_KEY]);
                        $dataTableRow->deleteColumn($metricSettings[self::METRIC_EVOLUTION_COL_NAME_KEY]);
                    }
                }
            }
        }

        // move the site id to a metadata column
        $dataTable->filter('ColumnCallbackAddMetadata', array('label', 'idsite'));

        // set the label of each row to the site name
        if ($multipleWebsitesRequested) {
            $dataTable->filter('ColumnCallbackReplace', array('label', '\Piwik\Site::getNameFor'));
        } else {
            $dataTable->filter('ColumnDelete', array('label'));
        }

        // replace record names with user friendly metric names
        $dataTable->filter('ReplaceColumnNames', array($columnNameRewrites));

        // Ensures data set sorted, for Metadata output
        $dataTable->filter('Sort', array(self::NB_VISITS_METRIC, 'desc', $naturalSort = false));

        // filter rows without visits
        // note: if only one website is queried and there are no visits, we can not remove the row otherwise
        // Piwik_API_ResponseBuilder throws 'Call to a member function getColumns() on a non-object'
        if ($multipleWebsitesRequested
        // We don't delete the 0 visits row, if "Enhanced" mode is on.
            && !$enhanced) {
            $dataTable->filter(
                'ColumnCallbackDeleteRow',
                array(
                     self::NB_VISITS_METRIC,
                     create_function('$value', 'return $value != 0;')
                )
            );
        }

        return $dataTable;
    }

    /**
     * Performs a binary filter of two
     * DataTables in order to correctly calculate evolution metrics.
     *
     * @param DataTable|DataTable\Map $currentData
     * @param DataTable|DataTable\Map $pastData
     * @param array $apiMetrics The array of string fields to calculate evolution
     *                          metrics for.
     * @throws Exception
     */
    private function calculateEvolutionPercentages($currentData, $pastData, $apiMetrics)
    {
        if (get_class($currentData) != get_class($pastData)) { // sanity check for regressions
            throw new Exception("Expected \$pastData to be of type ".get_class($currentData)." - got "
                               . get_class($pastData).".");
        }
        
        if ($currentData instanceof DataTable\Map) {
            $pastArray = $pastData->getArray();
            foreach ($currentData->getArray() as $subTable) {
                $this->calculateEvolutionPercentages($subTable, current($pastArray), $apiMetrics);
                next($pastArray);
            }
        } else {
            foreach ($apiMetrics as $metricSettings) {
                $currentData->filter(
                    'CalculateEvolutionFilter',
                    array(
                         $pastData,
                         $metricSettings[self::METRIC_EVOLUTION_COL_NAME_KEY],
                         $metricSettings[self::METRIC_RECORD_NAME_KEY],
                         $quotientPrecision = 1)
                );
            }
        }
    }

    /**
     * Sets the total visits, actions & revenue for a DataTable returned by
     * $this->buildDataTable.
     *
     * @param DataTable $dataTable
     * @param array $apiMetrics Metrics info.
     * @return array Array of three values: total visits, total actions, total revenue
     */
    private function setMetricsTotalsMetadata($dataTable, $apiMetrics)
    {
        if ($dataTable instanceof DataTable\Map) {
            foreach ($dataTable->getArray() as $table) {
                $this->setMetricsTotalsMetadata($table, $apiMetrics);
            }
        } else {
            $revenueMetric = '';
            if (Common::isGoalPluginEnabled()) {
                $revenueMetric = Piwik_Goals_Archiver::getRecordName(self::GOAL_REVENUE_METRIC);
            }

            $totals = array();
            foreach ($apiMetrics as $label => $metricInfo) {
                $totalMetadataName = self::getTotalMetadataName($label);
                $totals[$totalMetadataName] = 0;
            }

            foreach ($dataTable->getRows() as $row) {
                foreach ($apiMetrics as $label => $metricInfo) {
                    $totalMetadataName = self::getTotalMetadataName($label);
                    $totals[$totalMetadataName] += $row->getColumn($metricInfo[self::METRIC_RECORD_NAME_KEY]);
                }
            }

            foreach ($totals as $name => $value) {
                $dataTable->setMetadata($name, $value);
            }
        }
    }

    /**
     * Sets the total evolution metadata for a datatable returned by $this->buildDataTable
     * given data for the last period.
     *
     * @param DataTable $dataTable
     * @param DataTable $pastData
     * @param array $apiMetrics Metrics info.
     */
    private function setPastDataMetadata($dataTable, $pastData, $apiMetrics)
    {
        if ($dataTable instanceof DataTable\Map) {
            $pastArray = $pastData->getArray();
            foreach ($dataTable->getArray() as $subTable) {
                $this->setPastDataMetadata($subTable, current($pastArray), $apiMetrics);
                next($pastArray);
            }
        } else {
            // calculate total visits/actions/revenue for past data
            $this->setMetricsTotalsMetadata($pastData, $apiMetrics);

            foreach ($apiMetrics as $label => $metricInfo) {
                // get the names of metadata to set
                $totalMetadataName = self::getTotalMetadataName($label);
                $lastPeriodTotalMetadataName = self::getLastPeriodMetadataName($totalMetadataName);
                $totalEvolutionMetadataName =
                    self::getTotalMetadataName($metricInfo[self::METRIC_EVOLUTION_COL_NAME_KEY]);

                // set last period total
                $pastTotal = $pastData->getMetadata($totalMetadataName);
                $dataTable->setMetadata($lastPeriodTotalMetadataName, $pastTotal);

                // calculate & set evolution
                $currentTotal = $dataTable->getMetadata($totalMetadataName);
                $evolution = CalculateEvolutionFilter::calculate($currentTotal, $pastTotal);
                $dataTable->setMetadata($totalEvolutionMetadataName, $evolution);
            }
        }
    }

    /**
     * @ignore
     */
    public static function getApiMetrics($enhanced)
    {
        $metrics = self::$baseMetrics;
        if (Common::isGoalPluginEnabled()) {
            // goal revenue metric
            $metrics[self::GOAL_REVENUE_METRIC] = array(
                self::METRIC_TRANSLATION_KEY        => 'Goals_ColumnRevenue',
                self::METRIC_EVOLUTION_COL_NAME_KEY => self::GOAL_REVENUE_METRIC . '_evolution',
                self::METRIC_RECORD_NAME_KEY        => Piwik_Goals_Archiver::getRecordName(self::GOAL_REVENUE_METRIC),
                self::METRIC_IS_ECOMMERCE_KEY       => false,
            );

            if ($enhanced) {
                // number of goal conversions metric
                $metrics[self::GOAL_CONVERSION_METRIC] = array(
                    self::METRIC_TRANSLATION_KEY        => 'Goals_ColumnConversions',
                    self::METRIC_EVOLUTION_COL_NAME_KEY => self::GOAL_CONVERSION_METRIC . '_evolution',
                    self::METRIC_RECORD_NAME_KEY        => Piwik_Goals_Archiver::getRecordName(self::GOAL_CONVERSION_METRIC),
                    self::METRIC_IS_ECOMMERCE_KEY       => false,
                );

                // number of orders
                $metrics[self::ECOMMERCE_ORDERS_METRIC] = array(
                    self::METRIC_TRANSLATION_KEY        => 'General_EcommerceOrders',
                    self::METRIC_EVOLUTION_COL_NAME_KEY => self::ECOMMERCE_ORDERS_METRIC . '_evolution',
                    self::METRIC_RECORD_NAME_KEY        => Piwik_Goals_Archiver::getRecordName(self::GOAL_CONVERSION_METRIC, 0),
                    self::METRIC_IS_ECOMMERCE_KEY       => true,
                );

                // eCommerce revenue
                $metrics[self::ECOMMERCE_REVENUE_METRIC] = array(
                    self::METRIC_TRANSLATION_KEY        => 'General_ProductRevenue',
                    self::METRIC_EVOLUTION_COL_NAME_KEY => self::ECOMMERCE_REVENUE_METRIC . '_evolution',
                    self::METRIC_RECORD_NAME_KEY        => Piwik_Goals_Archiver::getRecordName(self::GOAL_REVENUE_METRIC, 0),
                    self::METRIC_IS_ECOMMERCE_KEY       => true,
                );
            }
        }

        return $metrics;
    }

    private static function getTotalMetadataName($name)
    {
        return 'total_' . $name;
    }

    private static function getLastPeriodMetadataName($name)
    {
        return 'last_period_' . $name;
    }
}