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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\Referrers;
use Exception;
use Piwik\API\Request;
use Piwik\API\ResponseBuilder;
use Piwik\Archive;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\DataTable\Filter\ColumnCallbackAddColumnPercentage;
use Piwik\Date;
use Piwik\Metrics;
use Piwik\Piwik;
use Piwik\Plugins\Referrers\DataTable\Filter\GroupDifferentSocialWritings;
use Piwik\Site;
/**
* The Referrers API lets you access reports about Websites, Search engines, Keywords, Campaigns used to access your website.
*
* For example, "getKeywords" returns all search engine keywords (with <a href='http://matomo.org/docs/analytics-api/reference/#toc-metric-definitions' rel='noreferrer' target='_blank'>general analytics metrics</a> for each keyword), "getWebsites" returns referrer websites (along with the full Referrer URL if the parameter &expanded=1 is set).
* "getReferrerType" returns the Referrer overview report. "getCampaigns" returns the list of all campaigns (and all campaign keywords if the parameter &expanded=1 is set).
*
* @method static \Piwik\Plugins\Referrers\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
public function get($idSite, $period, $date, $segment = false, $columns = false)
{
Piwik::checkUserHasViewAccess($idSite);
$dataTableReferrersType = $this->getReferrerType($idSite, $period, $date, $segment);
$dataTable = $this->createReferrerTypeTable($dataTableReferrersType);
$archive = Archive::build($idSite, $period, $date, $segment);
$numericArchives = $archive->getDataTableFromNumeric([
Archiver::METRIC_DISTINCT_SEARCH_ENGINE_RECORD_NAME,
Archiver::METRIC_DISTINCT_SOCIAL_NETWORK_RECORD_NAME,
Archiver::METRIC_DISTINCT_KEYWORD_RECORD_NAME,
Archiver::METRIC_DISTINCT_WEBSITE_RECORD_NAME,
Archiver::METRIC_DISTINCT_URLS_RECORD_NAME,
Archiver::METRIC_DISTINCT_CAMPAIGN_RECORD_NAME,
]);
$this->mergeNumericArchives($dataTable, $numericArchives);
$totalVisits = array_sum($dataTableReferrersType->getColumn(Metrics::INDEX_NB_VISITS));
$percentColumns = [
'Referrers_visitorsFromDirectEntry',
'Referrers_visitorsFromSearchEngines',
'Referrers_visitorsFromCampaigns',
'Referrers_visitorsFromSocialNetworks',
'Referrers_visitorsFromWebsites',
];
foreach ($percentColumns as $column) {
$dataTable->filter(ColumnCallbackAddColumnPercentage::class, [
$column . '_percent',
$column,
$totalVisits,
]);
}
if (!empty($requestedColumns)) {
$requestedColumns = Piwik::getArrayFromApiParameter($columns);
$dataTable->filter(DataTable\Filter\ColumnDelete::class, [[], $requestedColumns]);
}
return $dataTable;
}
/**
* @param string $name
* @param int $idSite
* @param string $period
* @param string|Date $date
* @param string $segment
* @param bool $expanded
* @param int|null $idSubtable
* @return DataTable
*/
protected function getDataTable($name, $idSite, $period, $date, $segment, $expanded = false, $idSubtable = null)
{
$dataTable = Archive::createDataTableFromArchive($name, $idSite, $period, $date, $segment, $expanded, $flat=false, $idSubtable);
return $dataTable;
}
/**
* Returns a report describing visit information for each possible referrer type. The
* result is a datatable whose subtables are the reports for each parent row's referrer type.
*
* The subtable reports are: 'getKeywords' (for search engine referrer type), 'getWebsites',
* and 'getCampaigns'.
*
* @param string $idSite The site ID.
* @param string $period The period to get data for, either 'day', 'week', 'month', 'year',
* or 'range'.
* @param string $date The date of the period.
* @param bool|string $segment The segment to use.
* @param bool|int $typeReferrer (deprecated) If you want to get data only for a specific referrer
* type, supply a type for this parameter.
* @param bool|int $idSubtable For this report this value is a referrer type ID and not an actual
* subtable ID. The result when using this parameter will be the
* specific report for the given referrer type.
* @param bool $expanded Whether to get report w/ subtables loaded or not.
* @return DataTable
*/
public function getReferrerType($idSite, $period, $date, $segment = false, $typeReferrer = false,
$idSubtable = false, $expanded = false, $_setReferrerTypeLabel = true)
{
Piwik::checkUserHasViewAccess($idSite);
$this->checkSingleSite($idSite, 'getReferrerType');
// if idSubtable is supplied, interpret idSubtable as referrer type and return correct report
if ($idSubtable !== false) {
$result = false;
switch ($idSubtable) {
case Common::REFERRER_TYPE_SEARCH_ENGINE:
$result = $this->getKeywords($idSite, $period, $date, $segment);
break;
case Common::REFERRER_TYPE_SOCIAL_NETWORK:
$result = $this->getSocials($idSite, $period, $date, $segment);
break;
case Common::REFERRER_TYPE_WEBSITE:
$result = $this->getWebsites($idSite, $period, $date, $segment);
break;
case Common::REFERRER_TYPE_CAMPAIGN:
$result = $this->getCampaigns($idSite, $period, $date, $segment);
break;
default: // invalid idSubtable, return whole report
break;
}
if ($result) {
$result->filter('ColumnCallbackDeleteMetadata', array('segment'));
$result->filter('ColumnCallbackDeleteMetadata', array('segmentValue'));
return $this->removeSubtableIds($result); // this report won't return subtables of individual reports
}
}
// get visits by referrer type
$dataTable = $this->getDataTable(Archiver::REFERRER_TYPE_RECORD_NAME, $idSite, $period, $date, $segment);
if ($typeReferrer !== false) // filter for a specific referrer type
{
$dataTable->filter('Pattern', array('label', $typeReferrer));
}
// set subtable IDs for each row to the label (which holds the int referrer type)
$dataTable->filter('Piwik\Plugins\Referrers\DataTable\Filter\SetGetReferrerTypeSubtables', array($idSite, $period, $date, $segment, $expanded));
$dataTable->filter('AddSegmentByLabelMapping', array(
'referrerType',
array(
Common::REFERRER_TYPE_DIRECT_ENTRY => 'direct',
Common::REFERRER_TYPE_CAMPAIGN => 'campaign',
Common::REFERRER_TYPE_SEARCH_ENGINE => 'search',
Common::REFERRER_TYPE_SOCIAL_NETWORK => 'social',
Common::REFERRER_TYPE_WEBSITE => 'website',
)
));
// set referrer type column to readable value
if ($_setReferrerTypeLabel == 1) {
$dataTable->filter(DataTable\Filter\ColumnCallbackAddMetadata::class, ['label', 'referrer_type']);
$dataTable->filter('ColumnCallbackReplace', array('label', __NAMESPACE__ . '\getReferrerTypeLabel'));
}
return $dataTable;
}
private function checkSingleSite($idSite, $method)
{
$idSites = Site::getIdSitesFromIdSitesString($idSite);
if (count($idSites) > 1) {
throw new Exception("Referrers.$method with multiple sites is not supported (yet).");
}
}
/**
* Returns a report that shows
*/
public function getAll($idSite, $period, $date, $segment = false)
{
Piwik::checkUserHasViewAccess($idSite);
$this->checkSingleSite($idSite, 'getAll');
$dataTable = Request::processRequest('Referrers.getReferrerType', [
'idSite' => $idSite,
'period' => $period,
'date' => $date,
'segment' => $segment,
'expanded' => true,
'disable_generic_filters' => true,
'disable_queued_filters' => true,
'_setReferrerTypeLabel' => 0,
], []);
if ($dataTable instanceof DataTable\Map) {
throw new Exception("Referrers.getAll with multiple sites or dates is not supported (yet).");
}
$dataTable = $dataTable->mergeSubtables($labelColumn = 'referer_type', $useMetadataColumn = true);
$dataTable->queueFilter('ReplaceColumnNames');
$dataTable->queueFilter('ReplaceSummaryRowLabel');
return $dataTable;
}
public function getKeywords($idSite, $period, $date, $segment = false, $expanded = false, $flat = false)
{
Piwik::checkUserHasViewAccess($idSite);
$dataTable = Archive::createDataTableFromArchive(Archiver::KEYWORDS_RECORD_NAME, $idSite, $period, $date, $segment, $expanded, $flat);
if ($flat) {
$dataTable->filterSubtables('Piwik\Plugins\Referrers\DataTable\Filter\SearchEnginesFromKeywordId', array($dataTable));
} else {
$dataTable->filter('AddSegmentValue');
$dataTable->queueFilter('PrependSegment', array('referrerType==search;'));
}
$dataTable->queueFilter('Piwik\Plugins\Referrers\DataTable\Filter\KeywordNotDefined');
return $dataTable;
}
const LABEL_KEYWORD_NOT_DEFINED = "";
/**
* @ignore
*/
public static function getKeywordNotDefinedString()
{
return Piwik::translate('General_NotDefined', Piwik::translate('General_ColumnKeyword'));
}
/**
* @ignore
*/
public static function getCleanKeyword($label)
{
return $label == self::LABEL_KEYWORD_NOT_DEFINED
? self::getKeywordNotDefinedString()
: $label;
}
/**
* @param DataTable $table
*/
private function filterOutKeywordNotDefined($table)
{
if ($table instanceof DataTable) {
$row = $table->getRowIdFromLabel('');
if ($row) {
$table->deleteRow($row);
}
}
}
protected function getLabelsFromTable($table)
{
$request = $_GET;
$request['serialize'] = 0;
// Apply generic filters
$response = new ResponseBuilder($format = 'original', $request);
$table = $response->getResponse($table);
// If period=lastX we only keep the first resultset as we want to return a plain list
if ($table instanceof DataTable\Map) {
$tables = $table->getDataTables();
$table = current($tables);
}
// Keep the response simple, only include keywords
$keywords = $table->getColumn('label');
return $keywords;
}
public function getSearchEnginesFromKeywordId($idSite, $period, $date, $idSubtable, $segment = false)
{
Piwik::checkUserHasViewAccess($idSite);
$dataTable = $this->getDataTable(Archiver::KEYWORDS_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = false, $idSubtable);
$keywords = $this->getKeywords($idSite, $period, $date, $segment);
$keyword = $keywords->getRowFromIdSubDataTable($idSubtable)->getColumn('label');
$dataTable->filter('Piwik\Plugins\Referrers\DataTable\Filter\SearchEnginesFromKeywordId', array($keywords, $idSubtable));
$dataTable->filter('AddSegmentByLabel', array('referrerName'));
$dataTable->queueFilter('PrependSegment', array('referrerKeyword=='.$keyword.';referrerType==search;'));
return $dataTable;
}
public function getSearchEngines($idSite, $period, $date, $segment = false, $expanded = false, $flat = false)
{
Piwik::checkUserHasViewAccess($idSite);
$dataTable = Archive::createDataTableFromArchive(Archiver::SEARCH_ENGINES_RECORD_NAME, $idSite, $period, $date, $segment, $expanded, $flat);
if ($flat) {
$dataTable->filter('ColumnCallbackAddMetadata', array('label', 'url', function ($url) { return SearchEngine::getInstance()->getUrlFromName($url); }));
$dataTable->filter('MetadataCallbackAddMetadata', array('url', 'logo', function ($url) { return SearchEngine::getInstance()->getLogoFromUrl($url); }));
$dataTable->filterSubtables('Piwik\Plugins\Referrers\DataTable\Filter\KeywordsFromSearchEngineId', array($dataTable));
} else {
$dataTable->filter('AddSegmentByLabel', array('referrerName'));
$dataTable->queueFilter('PrependSegment', array('referrerType==search;'));
$dataTable->queueFilter('ColumnCallbackAddMetadata', array('label', 'url', function ($url) { return SearchEngine::getInstance()->getUrlFromName($url); }));
$dataTable->queueFilter('MetadataCallbackAddMetadata', array('url', 'logo', function ($url) { return SearchEngine::getInstance()->getLogoFromUrl($url); }));
}
return $dataTable;
}
public function getKeywordsFromSearchEngineId($idSite, $period, $date, $idSubtable, $segment = false)
{
Piwik::checkUserHasViewAccess($idSite);
$dataTable = $this->getDataTable(Archiver::SEARCH_ENGINES_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = false, $idSubtable);
// get the search engine and create the URL to the search result page
$searchEngines = $this->getSearchEngines($idSite, $period, $date, $segment);
$searchEngines->applyQueuedFilters();
$searchEngine = $searchEngines->getRowFromIdSubDataTable($idSubtable)->getColumn('label');
$dataTable->filter('Piwik\Plugins\Referrers\DataTable\Filter\KeywordsFromSearchEngineId', array($searchEngines, $idSubtable));
$dataTable->filter('AddSegmentByLabel', array('referrerKeyword'));
$dataTable->queueFilter('PrependSegment', array('referrerName=='.$searchEngine.';referrerType==search;'));
return $dataTable;
}
public function getCampaigns($idSite, $period, $date, $segment = false, $expanded = false)
{
Piwik::checkUserHasViewAccess($idSite);
$dataTable = $this->getDataTable(Archiver::CAMPAIGNS_RECORD_NAME, $idSite, $period, $date, $segment, $expanded);
$dataTable->filter('AddSegmentByLabel', array('referrerName'));
$dataTable->queueFilter('PrependSegment', array('referrerType==campaign;'));
return $dataTable;
}
public function getKeywordsFromCampaignId($idSite, $period, $date, $idSubtable, $segment = false)
{
Piwik::checkUserHasViewAccess($idSite);
$campaigns = $this->getCampaigns($idSite, $period, $date, $segment);
$campaigns->applyQueuedFilters();
$row = $campaigns->getRowFromIdSubDataTable($idSubtable);
$campaign = $row ? $row->getColumn('label') : '';
$dataTable = $this->getDataTable(Archiver::CAMPAIGNS_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = false, $idSubtable);
$dataTable->filter('AddSegmentByLabel', array('referrerKeyword'));
$dataTable->queueFilter('PrependSegment', array('referrerName=='.$campaign.';referrerType==campaign;'));
return $dataTable;
}
public function getWebsites($idSite, $period, $date, $segment = false, $expanded = false, $flat = false)
{
Piwik::checkUserHasViewAccess($idSite);
$dataTable = Archive::createDataTableFromArchive(Archiver::WEBSITES_RECORD_NAME, $idSite, $period, $date, $segment, $expanded, $flat, $idSubtable = null);
if ($flat) {
$dataTable->filterSubtables('Piwik\Plugins\Referrers\DataTable\Filter\UrlsFromWebsiteId');
} else {
$dataTable->filter('AddSegmentByLabel', array('referrerName'));
}
return $dataTable;
}
public function getUrlsFromWebsiteId($idSite, $period, $date, $idSubtable, $segment = false)
{
Piwik::checkUserHasViewAccess($idSite);
$dataTable = $this->getDataTable(Archiver::WEBSITES_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = false, $idSubtable);
$dataTable->filter('Piwik\Plugins\Referrers\DataTable\Filter\UrlsFromWebsiteId');
$dataTable->filter('MetadataCallbackAddMetadata', array('url', 'segment', function($url) {
return 'referrerUrl==' . urlencode($url);
}));
return $dataTable;
}
/**
* Returns report comparing the number of visits (and other info) for social network referrers.
* This is a view of the getWebsites report.
*
* @param string $idSite
* @param string $period
* @param string $date
* @param string|bool $segment
* @param bool $expanded
* @param bool $flat
* @return DataTable
*/
public function getSocials($idSite, $period, $date, $segment = false, $expanded = false, $flat = false)
{
Piwik::checkUserHasViewAccess($idSite);
$dataTable = Archive::createDataTableFromArchive(Archiver::SOCIAL_NETWORKS_RECORD_NAME, $idSite, $period, $date, $segment, $expanded, $flat);
$dataTable->filter(GroupDifferentSocialWritings::class);
$dataTable->filter('ColumnCallbackAddMetadata', array('label', 'url', function ($name) {
return Social::getInstance()->getMainUrlFromName($name);
}));
$dataTable = $this->completeSocialTablesWithOldReports($dataTable, $idSite, $period, $date, $segment, $expanded, $flat);
$dataTable->filter('MetadataCallbackAddMetadata', array('url', 'logo', function ($url) { return Social::getInstance()->getLogoFromUrl($url); }));
return $dataTable;
}
private function completeSocialTablesWithOldReports($dataTable, $idSite, $period, $date, $segment, $expanded, $flat)
{
return $this->combineDataTables($dataTable, function() use ($idSite, $period, $date, $segment, $expanded, $flat) {
$dataTableFiltered = Archive::createDataTableFromArchive(Archiver::WEBSITES_RECORD_NAME, $idSite, $period, $date, $segment, $expanded, false);
$this->filterWebsitesForSocials($dataTableFiltered, $idSite, $period, $date, $segment, $expanded, $flat);
return $dataTableFiltered;
});
}
protected function combineDataTables($dataTable, $callbackForAdditionalData)
{
$isMap = false;
$hasEmptyTable = false;
if ($dataTable instanceof DataTable\Map) {
$isMap = true;
$dataTables = $dataTable->getDataTables();
} else {
$dataTables = [$dataTable];
}
foreach ($dataTables as $table) {
if ($table instanceof DataTable && !$table->getRowsCountWithoutSummaryRow()) {
$hasEmptyTable = true;
break;
}
}
if ($hasEmptyTable) {
$dataTablesForCompletion = $callbackForAdditionalData();
if (!$isMap) {
$dataTable = $dataTablesForCompletion;
} else {
$filteredTables = $dataTablesForCompletion->getDataTables();
foreach ($dataTable as $label => $table) {
if ($table instanceof DataTable && !$table->getRowsCountWithoutSummaryRow() && !empty($filteredTables[$label])) {
$dataTable->addTable($filteredTables[$label], $label);
}
}
}
}
return $dataTable;
}
/**
* @param DataTable $dataTable
*/
protected function filterWebsitesForSocials($dataTable, $idSite, $period, $date, $segment, $expanded, $flat)
{
$dataTable->filter('ColumnCallbackDeleteRow', array('label', function ($url) {
return !Social::getInstance()->isSocialUrl($url);
}));
$dataTable->filter('ColumnCallbackAddMetadata', array('label', 'url', function ($url) {
return Social::getInstance()->getMainUrl($url);
}));
$dataTable->filter('GroupBy', array('label', function ($url) {
return Social::getInstance()->getSocialNetworkFromDomain($url);
}));
$this->setSocialIdSubtables($dataTable);
$this->removeSubtableMetadata($dataTable);
if ($flat) {
$this->buildExpandedTableForFlattenGetSocials($idSite, $period, $date, $segment, $expanded, $dataTable);
}
}
/**
* Returns report containing individual referrer URLs for a specific social networking
* site.
*
* @param string $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @param bool|int $idSubtable This ID does not reference a real DataTable record. Instead, it
* is the array index of an item in the Socials list file.
* The urls are filtered by the social network at this index.
* If false, no filtering is done and every social URL is returned.
* @return DataTable
*/
public function getUrlsForSocial($idSite, $period, $date, $segment = false, $idSubtable = false)
{
Piwik::checkUserHasViewAccess($idSite);
$dataTable = $this->getDataTable(Archiver::SOCIAL_NETWORKS_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = true, $idSubtable);
if (!$idSubtable) {
$dataTable = $dataTable->mergeSubtables();
}
$dataTable = $this->combineDataTables($dataTable, function() use ($idSite, $period, $date, $segment, $idSubtable) {
$dataTableFiltered = $this->getDataTable(Archiver::WEBSITES_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = true);
$socialNetworks = array_values(Social::getInstance()->getDefinitions());
$social = isset($socialNetworks[$idSubtable - 1]) ? $socialNetworks[$idSubtable - 1] : false;
// filter out everything but social network indicated by $idSubtable
$dataTableFiltered->filter(
'ColumnCallbackDeleteRow',
array('label',
function ($url) use ($social) {
return !Social::getInstance()->isSocialUrl($url, $social);
}
)
);
return $dataTableFiltered->mergeSubtables();
});
$dataTable->filter('AddSegmentByLabel', array('referrerUrl'));
$dataTable->filter('Piwik\Plugins\Referrers\DataTable\Filter\UrlsForSocial', array(true));
$dataTable->queueFilter('ReplaceColumnNames');
return $dataTable;
}
public function getNumberOfDistinctSearchEngines($idSite, $period, $date, $segment = false)
{
return $this->getNumeric(Archiver::METRIC_DISTINCT_SEARCH_ENGINE_RECORD_NAME, $idSite, $period, $date, $segment);
}
public function getNumberOfDistinctSocialNetworks($idSite, $period, $date, $segment = false)
{
return $this->getNumeric(Archiver::METRIC_DISTINCT_SOCIAL_NETWORK_RECORD_NAME, $idSite, $period, $date, $segment);
}
public function getNumberOfDistinctKeywords($idSite, $period, $date, $segment = false)
{
return $this->getNumeric(Archiver::METRIC_DISTINCT_KEYWORD_RECORD_NAME, $idSite, $period, $date, $segment);
}
public function getNumberOfDistinctCampaigns($idSite, $period, $date, $segment = false)
{
return $this->getNumeric(Archiver::METRIC_DISTINCT_CAMPAIGN_RECORD_NAME, $idSite, $period, $date, $segment);
}
public function getNumberOfDistinctWebsites($idSite, $period, $date, $segment = false)
{
return $this->getNumeric(Archiver::METRIC_DISTINCT_WEBSITE_RECORD_NAME, $idSite, $period, $date, $segment);
}
public function getNumberOfDistinctWebsitesUrls($idSite, $period, $date, $segment = false)
{
return $this->getNumeric(Archiver::METRIC_DISTINCT_URLS_RECORD_NAME, $idSite, $period, $date, $segment);
}
private function getNumeric($name, $idSite, $period, $date, $segment)
{
Piwik::checkUserHasViewAccess($idSite);
$archive = Archive::build($idSite, $period, $date, $segment);
return $archive->getDataTableFromNumeric($name);
}
/**
* Removes idsubdatatable_in_db metadata from a DataTable. Used by Social tables since
* they use fake subtable IDs.
*
* @param DataTable $dataTable
*/
private function removeSubtableMetadata($dataTable)
{
if ($dataTable instanceof DataTable\Map) {
foreach ($dataTable->getDataTables() as $childTable) {
$this->removeSubtableMetadata($childTable);
}
} else {
foreach ($dataTable->getRows() as $row) {
$row->deleteMetadata('idsubdatatable_in_db');
}
}
}
/**
* Sets the subtable IDs for the DataTable returned by getSocial.
*
* The IDs are int indexes into the array in of defined socials.
*
* @param DataTable $dataTable
*/
private function setSocialIdSubtables($dataTable)
{
if ($dataTable instanceof DataTable\Map) {
foreach ($dataTable->getDataTables() as $childTable) {
$this->setSocialIdSubtables($childTable);
}
} else {
foreach ($dataTable->getRows() as $row) {
$socialName = $row->getColumn('label');
$i = 1; // start at one because idSubtable=0 is equivalent to idSubtable=false
foreach (Social::getInstance()->getDefinitions() as $name) {
if ($name == $socialName) {
$row->setNonLoadedSubtableId($i);
break;
}
++$i;
}
}
}
}
/**
* Utility function that removes the subtable IDs for the subtables of the
* getReferrerType report. This avoids infinite recursion in said report (ie,
* the grandchildren of the report will be the original report, and it will
* recurse when trying to get a flat report).
*
* @param DataTable $table
* @return DataTable Returns $table for convenience.
*/
private function removeSubtableIds($table)
{
if ($table instanceof DataTable\Map) {
foreach ($table->getDataTables() as $childTable) {
$this->removeSubtableIds($childTable);
}
} else {
foreach ($table->getRows() as $row) {
$row->removeSubtable();
}
}
return $table;
}
/**
* @param int $idSite
* @param string $period
* @param string $date
* @param string|false $segment
* @param bool $expanded
* @param DataTable $dataTable
*/
private function buildExpandedTableForFlattenGetSocials($idSite, $period, $date, $segment, $expanded, $dataTable)
{
$urlsTable = Archive::createDataTableFromArchive(Archiver::WEBSITES_RECORD_NAME, $idSite, $period, $date, $segment, $expanded, $flat = true);
$urlsTable->filter('ColumnCallbackDeleteRow', array('label', function ($url) {
return !Social::getInstance()->isSocialUrl($url);
}));
$urlsTable = $urlsTable->mergeSubtables();
if ($dataTable instanceof DataTable\Map) {
$dataTables = $dataTable->getDataTables();
$urlsTables = $urlsTable->getDataTables();
} else {
$dataTables = [$dataTable];
$urlsTables = [$urlsTable];
}
foreach ($dataTables as $label => $dataTable) {
foreach ($dataTable->getRows() as $row) {
$row->removeSubtable();
$social = $row->getColumn('label');
$newTable = $urlsTables[$label]->getEmptyClone();
$rows = $urlsTables[$label]->getRows();
foreach ($rows as $id => $urlsTableRow) {
$url = $urlsTableRow->getColumn('label');
if (Social::getInstance()->isSocialUrl($url, $social)) {
$newTable->addRow($urlsTableRow);
$urlsTables[$label]->deleteRow($id);
}
}
if ($newTable->getRowsCount()) {
$newTable->filter('Piwik\Plugins\Referrers\DataTable\Filter\UrlsForSocial', array($expanded));
$row->setSubtable($newTable);
}
}
}
Common::destroy($urlsTable);
$urlsTable = null;
}
private function createReferrerTypeTable(DataTable\DataTableInterface $table)
{
if ($table instanceof DataTable) {
$nameToColumnId = array(
Common::REFERRER_TYPE_SEARCH_ENGINE => 'Referrers_visitorsFromSearchEngines',
Common::REFERRER_TYPE_SOCIAL_NETWORK => 'Referrers_visitorsFromSocialNetworks',
Common::REFERRER_TYPE_DIRECT_ENTRY => 'Referrers_visitorsFromDirectEntry',
Common::REFERRER_TYPE_WEBSITE => 'Referrers_visitorsFromWebsites',
Common::REFERRER_TYPE_CAMPAIGN => 'Referrers_visitorsFromCampaigns',
);
$newRow = array_fill_keys(array_values($nameToColumnId), 0);
foreach ($table->getRows() as $row) {
$referrerType = $row->getMetadata('referrer_type');
if (empty($nameToColumnId[$referrerType])) {
continue;
}
$nameVar = $nameToColumnId[$referrerType];
$value = $row->getColumn(Metrics::INDEX_NB_VISITS);
$newRow[$nameVar] = $value;
}
$result = new DataTable\Simple();
$result->addRowFromSimpleArray($newRow);
return $result;
} else if ($table instanceof DataTable\Map) {
$result = new DataTable\Map();
$result->setKeyName($table->getKeyName());
foreach ($table->getDataTables() as $label => $childTable) {
if ($childTable->getRowsCount() > 0) {
$referrerTypeTable = $this->createReferrerTypeTable($childTable);
$result->addTable($referrerTypeTable, $label);
} else {
$result->addTable(new DataTable(), $label);
}
}
} else {
throw new \Exception("Unexpected DataTable type: " . get_class($table)); // sanity check
}
return $result;
}
private function mergeNumericArchives(DataTable\DataTableInterface $table, DataTable\DataTableInterface $numericArchives = null)
{
if ($table instanceof DataTable) {
/** @var DataTable $numericArchives */
if (empty($numericArchives)) {
return;
}
$table->setAllTableMetadata($numericArchives->getAllTableMetadata());
if ($table->getRows() == 0) {
$table->addRow(new DataTable\Row());
}
if ($numericArchives->getRowsCount() == 0) {
return;
}
$row = $table->getFirstRow();
foreach ($numericArchives->getFirstRow() as $name => $value) {
$row->setColumn($name, $value);
}
} else if ($table instanceof DataTable\Map) {
foreach ($table->getDataTables() as $label => $childTable) {
$numericArchiveChildTable = $numericArchives->getTable($label);
$this->mergeNumericArchives($childTable, $numericArchiveChildTable);
}
} else {
throw new \Exception("Unexpected DataTable type: " . get_class($table)); // sanity check
}
}
}
|