-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathAPI.php
More file actions
439 lines (408 loc) · 23.5 KB
/
API.php
File metadata and controls
439 lines (408 loc) · 23.5 KB
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
<?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\MarketingCampaignsReporting\DataTable\Filter\FormatCampaignLabels;
use Piwik\Plugins\Referrers\API as ReferrersAPI;
/**
* Exposes reporting API endpoints for marketing campaign dimensions and drill-down reports.
* Includes campaign IDs, names, keywords, source and medium dimensions, and hierarchical subtables.
*
* @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;
}
/**
* Returns campaign ID rows with standard marketing campaign metrics.
*
* @param int|string|int[] $idSite Website ID(s) to query.
* - Single site ID (e.g. 1)
* - Multiple site IDs (e.g. [1, 4, 5])
* - Comma-separated list ("1,4,5") or "all"
* @param 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data
* for the period containing the specified
* date.
* @param string $date The date or date range to process.
* 'YYYY-MM-DD', magic keywords (today, yesterday, lastWeek, lastMonth,
* lastYear), or date range (ie, 'YYYY-MM-DD,YYYY-MM-DD', lastX,
* previousX).
* @param string|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map Campaign ID report rows with visit, conversion, and revenue
* metrics.
*/
public function getId($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGN_ID_RECORD_NAME, $idSite, $period, $date, $segment);
$dataTable->filter('AddSegmentValue');
$dataTable->filter(FormatCampaignLabels::class);
return $dataTable;
}
/**
* Returns campaign name rows with standard marketing campaign metrics.
* Falls back to the Referrers campaigns report when no archived data is available.
*
* @param int|string|int[] $idSite Website ID(s) to query.
* - Single site ID (e.g. 1)
* - Multiple site IDs (e.g. [1, 4, 5])
* - Comma-separated list ("1,4,5") or "all"
* @param 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data
* for the period containing the specified
* date.
* @param string $date The date or date range to process.
* 'YYYY-MM-DD', magic keywords (today, yesterday, lastWeek, lastMonth,
* lastYear), or date range (ie, 'YYYY-MM-DD,YYYY-MM-DD', lastX,
* previousX).
* @param string|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @param bool $expanded Whether to expand keyword/content subtables for each campaign name.
* @param bool $flat Whether to flatten expanded subtables into a single table.
* @return DataTable|DataTable\Map Campaign name report rows, optionally including drill-down
* subtables.
*/
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);
}
$dataTable->filter(FormatCampaignLabels::class);
return $dataTable;
}
/**
* Returns keyword and content rows for a campaign name subtable.
* Falls back to Referrers campaign subtables and campaign label lookup when needed.
*
* @param int|string|int[] $idSite Website ID(s) to query.
* - Single site ID (e.g. 1)
* - Multiple site IDs (e.g. [1, 4, 5])
* - Comma-separated list ("1,4,5") or "all"
* @param 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data
* for the period containing the specified
* date.
* @param string $date The date or date range to process.
* 'YYYY-MM-DD', magic keywords (today, yesterday, lastWeek, lastMonth,
* lastYear), or date range (ie, 'YYYY-MM-DD,YYYY-MM-DD', lastX,
* previousX).
* @param int $idSubtable The subtable ID from a `getName` campaign row.
* @param string|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map Keyword and content rows for the selected campaign name.
*/
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)) {
$dataTable->filter(FormatCampaignLabels::class);
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)) {
$dataTable = $this->mergeDataTableMaps($dataTable, $referrersDataTable);
$dataTable->filter(FormatCampaignLabels::class);
return $dataTable;
}
// 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) {
$dataTable->filter(FormatCampaignLabels::class);
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);
$dataTable = $this->mergeDataTableMaps($dataTable, $referrersDataTable);
$dataTable->filter(FormatCampaignLabels::class);
return $dataTable;
}
$dataTable->filter(FormatCampaignLabels::class);
return $dataTable;
}
/**
* Returns campaign keyword rows with standard marketing campaign metrics.
* Falls back to merged Referrers campaign subtables when no archived keyword data is available.
*
* @param int|string|int[] $idSite Website ID(s) to query.
* - Single site ID (e.g. 1)
* - Multiple site IDs (e.g. [1, 4, 5])
* - Comma-separated list ("1,4,5") or "all"
* @param 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data
* for the period containing the specified
* date.
* @param string $date The date or date range to process.
* 'YYYY-MM-DD', magic keywords (today, yesterday, lastWeek, lastMonth,
* lastYear), or date range (ie, 'YYYY-MM-DD,YYYY-MM-DD', lastX,
* previousX).
* @param string|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map Campaign keyword report rows with visit, conversion, and
* revenue metrics.
*/
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);
}
$dataTable->filter(FormatCampaignLabels::class);
return $dataTable;
}
/**
* Returns campaign source rows with standard marketing campaign metrics.
*
* @param int|string|int[] $idSite Website ID(s) to query.
* - Single site ID (e.g. 1)
* - Multiple site IDs (e.g. [1, 4, 5])
* - Comma-separated list ("1,4,5") or "all"
* @param 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data
* for the period containing the specified
* date.
* @param string $date The date or date range to process.
* 'YYYY-MM-DD', magic keywords (today, yesterday, lastWeek, lastMonth,
* lastYear), or date range (ie, 'YYYY-MM-DD,YYYY-MM-DD', lastX,
* previousX).
* @param string|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map Campaign source report rows with visit, conversion, and
* revenue metrics.
*/
public function getSource($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGN_SOURCE_RECORD_NAME, $idSite, $period, $date, $segment);
$dataTable->filter('AddSegmentValue');
$dataTable->filter(FormatCampaignLabels::class);
return $dataTable;
}
/**
* Returns campaign medium rows with standard marketing campaign metrics.
*
* @param int|string|int[] $idSite Website ID(s) to query.
* - Single site ID (e.g. 1)
* - Multiple site IDs (e.g. [1, 4, 5])
* - Comma-separated list ("1,4,5") or "all"
* @param 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data
* for the period containing the specified
* date.
* @param string $date The date or date range to process.
* 'YYYY-MM-DD', magic keywords (today, yesterday, lastWeek, lastMonth,
* lastYear), or date range (ie, 'YYYY-MM-DD,YYYY-MM-DD', lastX,
* previousX).
* @param string|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map Campaign medium report rows with visit, conversion, and
* revenue metrics.
*/
public function getMedium($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGN_MEDIUM_RECORD_NAME, $idSite, $period, $date, $segment);
$dataTable->filter('AddSegmentValue');
$dataTable->filter(FormatCampaignLabels::class);
return $dataTable;
}
/**
* Returns campaign content rows with standard marketing campaign metrics.
*
* @param int|string|int[] $idSite Website ID(s) to query.
* - Single site ID (e.g. 1)
* - Multiple site IDs (e.g. [1, 4, 5])
* - Comma-separated list ("1,4,5") or "all"
* @param 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data
* for the period containing the specified
* date.
* @param string $date The date or date range to process.
* 'YYYY-MM-DD', magic keywords (today, yesterday, lastWeek, lastMonth,
* lastYear), or date range (ie, 'YYYY-MM-DD,YYYY-MM-DD', lastX,
* previousX).
* @param string|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map Campaign content report rows with visit, conversion, and
* revenue metrics.
*/
public function getContent($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGN_CONTENT_RECORD_NAME, $idSite, $period, $date, $segment);
$dataTable->filter('AddSegmentValue');
$dataTable->filter(FormatCampaignLabels::class);
return $dataTable;
}
/**
* Returns campaign group rows with standard marketing campaign metrics.
*
* @param int|string|int[] $idSite Website ID(s) to query.
* - Single site ID (e.g. 1)
* - Multiple site IDs (e.g. [1, 4, 5])
* - Comma-separated list ("1,4,5") or "all"
* @param 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data
* for the period containing the specified
* date.
* @param string $date The date or date range to process.
* 'YYYY-MM-DD', magic keywords (today, yesterday, lastWeek, lastMonth,
* lastYear), or date range (ie, 'YYYY-MM-DD,YYYY-MM-DD', lastX,
* previousX).
* @param string|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map Campaign group report rows with visit, conversion, and
* revenue metrics.
*/
public function getGroup($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGN_GROUP_RECORD_NAME, $idSite, $period, $date, $segment);
$dataTable->filter('AddSegmentValue');
$dataTable->filter(FormatCampaignLabels::class);
return $dataTable;
}
/**
* Returns campaign placement rows with standard marketing campaign metrics.
*
* @param int|string|int[] $idSite Website ID(s) to query.
* - Single site ID (e.g. 1)
* - Multiple site IDs (e.g. [1, 4, 5])
* - Comma-separated list ("1,4,5") or "all"
* @param 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data
* for the period containing the specified
* date.
* @param string $date The date or date range to process.
* 'YYYY-MM-DD', magic keywords (today, yesterday, lastWeek, lastMonth,
* lastYear), or date range (ie, 'YYYY-MM-DD,YYYY-MM-DD', lastX,
* previousX).
* @param string|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map Campaign placement report rows with visit, conversion, and
* revenue metrics.
*/
public function getPlacement($idSite, $period, $date, $segment = false)
{
$dataTable = $this->getDataTable(Archiver::CAMPAIGN_PLACEMENT_RECORD_NAME, $idSite, $period, $date, $segment);
$dataTable->filter('AddSegmentValue');
$dataTable->filter(FormatCampaignLabels::class);
return $dataTable;
}
/**
* Returns hierarchical source and medium report rows.
*
* @param int|string|int[] $idSite Website ID(s) to query.
* - Single site ID (e.g. 1)
* - Multiple site IDs (e.g. [1, 4, 5])
* - Comma-separated list ("1,4,5") or "all"
* @param 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data
* for the period containing the specified
* date.
* @param string $date The date or date range to process.
* 'YYYY-MM-DD', magic keywords (today, yesterday, lastWeek, lastMonth,
* lastYear), or date range (ie, 'YYYY-MM-DD,YYYY-MM-DD', lastX,
* previousX).
* @param string|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @param bool $expanded Whether to expand campaign-name subtables under each source/medium row.
* @param bool $flat Whether to flatten expanded subtables into a single table.
* @return DataTable|DataTable\Map Hierarchical source/medium rows, optionally including
* nested campaign-name subtables.
*/
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);
$dataTable->filter(FormatCampaignLabels::class);
return $dataTable;
}
/**
* Returns campaign names for a source and medium subtable.
*
* @param int|string|int[] $idSite Website ID(s) to query.
* - Single site ID (e.g. 1)
* - Multiple site IDs (e.g. [1, 4, 5])
* - Comma-separated list ("1,4,5") or "all"
* @param 'day'|'week'|'month'|'year'|'range' $period The period to process, processes data
* for the period containing the specified
* date.
* @param string $date The date or date range to process.
* 'YYYY-MM-DD', magic keywords (today, yesterday, lastWeek, lastMonth,
* lastYear), or date range (ie, 'YYYY-MM-DD,YYYY-MM-DD', lastX,
* previousX).
* @param int $idSubtable The subtable ID from a `getSourceMedium` source/medium row.
* @param string|null|false $segment Custom segment to filter the report.
* Example: "referrerName==example.com"
* Supports AND (;) and OR (,) operators.
* @return DataTable|DataTable\Map Campaign names for the selected source/medium row.
*/
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);
$dataTable->filter(FormatCampaignLabels::class);
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) . "'.");
}
}
}