-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathAPI.php
193 lines (162 loc) · 7.83 KB
/
API.php
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
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* Based on code from AdvancedCampaignReporting plugin by Piwik PRO released under GPL v3 or later:
* https://github.com/PiwikPRO/plugin-AdvancedCampaignReporting
*/
namespace Piwik\Plugins\MarketingCampaignsReporting;
use Piwik\Archive;
use Piwik\DataTable;
use Piwik\Metrics;
use Piwik\Piwik;
use Piwik\Plugins\Referrers\API as ReferrersAPI;
/**
* API for plugin MarketingCampaignsReporting
*
* @package MarketingCampaignsReporting
* @method static \Piwik\Plugins\MarketingCampaignsReporting\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
protected function getDataTable($name, $idSite, $period, $date, $segment, $expanded = false, $flat = false, $idSubtable = null)
{
Piwik::checkUserHasViewAccess($idSite);
$dataTable = Archive::createDataTableFromArchive($name, $idSite, $period, $date, $segment, $expanded, $flat, $idSubtable);
$dataTable->filter('Sort', array(Metrics::INDEX_NB_VISITS));
return $dataTable;
}
public function getId($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGN_ID_RECORD_NAME, $idSite, $period, $date, $segment);
$dataTable->filter('AddSegmentValue');
return $dataTable;
}
public function getName($idSite, $period, $date, $segment = false, $expanded = false, $flat = false)
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGN_NAME_RECORD_NAME, $idSite, $period, $date, $segment, $expanded, $flat);
$dataTable->filter('AddSegmentValue');
if ($this->isTableEmpty($dataTable)) {
$referrersDataTable = ReferrersAPI::getInstance()->getCampaigns($idSite, $period, $date, $segment, $expanded);
$dataTable = $this->mergeDataTableMaps($dataTable, $referrersDataTable);
}
return $dataTable;
}
public function getKeywordContentFromNameId($idSite, $period, $date, $idSubtable, $segment = false)
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGN_NAME_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = false, $flat = false, $idSubtable);
if (!$this->isTableEmpty($dataTable)) {
return $dataTable;
}
// try to load sub table from referrers api. That might work, if the report leading to this subtable was loaded using the referrers api fallback
$referrersDataTable = ReferrersAPI::getInstance()->getKeywordsFromCampaignId($idSite, $period, $date, $idSubtable, $segment);
if (!$this->isTableEmpty($referrersDataTable)) {
return $this->mergeDataTableMaps($dataTable, $referrersDataTable);
}
// if we can't find a subtable report using the id, try fetching the label to search for a subtable
$campaignNames = $this->getDataTable(Archiver::CAMPAIGN_NAME_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = false);
$row = $campaignNames->getRowFromIdSubDataTable($idSubtable);
if (!$row) {
return $dataTable;
}
$campaignName = $row->getColumn('label');
$campaignsDataTable = ReferrersAPI::getInstance()->getCampaigns($idSite, $period, $date, $segment, false);
$campaignRow = $campaignsDataTable->getRowFromLabel($campaignName);
if ($campaignRow && $idSubtable = $campaignRow->getIdSubDataTable()) {
$referrersDataTable = ReferrersAPI::getInstance()->getKeywordsFromCampaignId($idSite, $period, $date, $idSubtable, $segment);
return $this->mergeDataTableMaps($dataTable, $referrersDataTable);
}
return $dataTable;
}
public function getKeyword($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGN_KEYWORD_RECORD_NAME, $idSite, $period, $date, $segment);
$dataTable->filter('AddSegmentValue');
if ($this->isTableEmpty($dataTable)) {
$referrersDataTable = ReferrersAPI::getInstance()->getCampaigns($idSite, $period, $date, $segment, $expanded = true);
$referrersDataTable->applyQueuedFilters();
$referrersDataTable = $referrersDataTable->mergeSubtables();
$dataTable = $this->mergeDataTableMaps($dataTable, $referrersDataTable);
}
return $dataTable;
}
public function getSource($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGN_SOURCE_RECORD_NAME, $idSite, $period, $date, $segment);
$dataTable->filter('AddSegmentValue');
return $dataTable;
}
public function getMedium($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGN_MEDIUM_RECORD_NAME, $idSite, $period, $date, $segment);
$dataTable->filter('AddSegmentValue');
return $dataTable;
}
public function getContent($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGN_CONTENT_RECORD_NAME, $idSite, $period, $date, $segment);
$dataTable->filter('AddSegmentValue');
return $dataTable;
}
public function getGroup($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGN_GROUP_RECORD_NAME, $idSite, $period, $date, $segment);
$dataTable->filter('AddSegmentValue');
return $dataTable;
}
public function getPlacement($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGN_PLACEMENT_RECORD_NAME, $idSite, $period, $date, $segment);
$dataTable->filter('AddSegmentValue');
return $dataTable;
}
public function getSourceMedium($idSite, $period, $date, $segment = false, $expanded = false, $flat = false)
{
$dataTable = $this->getDataTable(Archiver::HIERARCHICAL_SOURCE_MEDIUM_RECORD_NAME, $idSite, $period, $date, $segment, $expanded, $flat);
return $dataTable;
}
public function getNameFromSourceMediumId($idSite, $period, $date, $idSubtable, $segment = false)
{
$dataTable = $this->getDataTable(Archiver::HIERARCHICAL_SOURCE_MEDIUM_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = false, $flat = false, $idSubtable);
return $dataTable;
}
private function isTableEmpty(DataTable\DataTableInterface $dataTable)
{
if ($dataTable instanceof DataTable) {
return $dataTable->getRowsCount() == 0;
} elseif ($dataTable instanceof DataTable\Map) {
foreach ($dataTable->getDataTables() as $label => $childTable) {
if ($this->isTableEmpty($childTable)) {
return true;
}
}
return false;
} else {
throw new \Exception("Sanity check: unknown datatable type '" . get_class($dataTable) . "'.");
}
}
private function mergeDataTableMaps(
DataTable\DataTableInterface $dataTable,
DataTable\DataTableInterface $referrersDataTable
) {
if ($dataTable instanceof DataTable) {
if ($this->isTableEmpty($dataTable)) {
$referrersDataTable->setAllTableMetadata($dataTable->getAllTableMetadata());
return $referrersDataTable;
} else {
return $dataTable;
}
} elseif ($dataTable instanceof DataTable\Map) {
foreach ($dataTable->getDataTables() as $label => $childTable) {
$newTable = $this->mergeDataTableMaps($childTable, $referrersDataTable->getTable($label));
$dataTable->addTable($newTable, $label);
}
return $dataTable;
} else {
throw new \Exception("Sanity check: unknown datatable type '" . get_class($dataTable) . "'.");
}
}
}