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
|
<?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\VisitFrequency;
use Piwik\API\Request;
use Piwik\Archive;
use Piwik\DataTable;
use Piwik\Piwik;
use Piwik\Plugins\VisitsSummary\API as APIVisitsSummary;
use Piwik\Segment\SegmentExpression;
/**
* VisitFrequency API lets you access a list of metrics related to Returning Visitors.
* @method static \Piwik\Plugins\VisitFrequency\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
// visitorType==returning,visitorType==returningCustomer
const RETURNING_VISITOR_SEGMENT = "visitorType%3D%3Dreturning%2CvisitorType%3D%3DreturningCustomer";
const COLUMN_SUFFIX = "_returning";
/**
* @param int $idSite
* @param string $period
* @param string $date
* @param bool|string $segment
* @param bool|array $columns
* @return mixed
*/
public function get($idSite, $period, $date, $segment = false, $columns = false)
{
$segment = $this->appendReturningVisitorSegment($segment);
$this->unprefixColumns($columns);
$params = array(
'idSite' => $idSite,
'period' => $period,
'date' => $date,
'segment' => $segment,
'columns' => implode(',', $columns),
'format' => 'original',
'serialize' => 0, // tests set this to 1
'format_metrics' => 0
);
$table = Request::processRequest('VisitsSummary.get', $params);
$this->prefixColumns($table, $period);
return $table;
}
protected function appendReturningVisitorSegment($segment)
{
if (empty($segment)) {
$segment = '';
} else {
$segment .= urlencode(SegmentExpression::AND_DELIMITER);
}
$segment .= self::RETURNING_VISITOR_SEGMENT;
return $segment;
}
protected function unprefixColumns(&$columns)
{
$columns = Piwik::getArrayFromApiParameter($columns);
foreach ($columns as &$column) {
$column = str_replace(self::COLUMN_SUFFIX, "", $column);
}
}
protected function prefixColumns($table, $period)
{
$rename = array();
foreach (APIVisitsSummary::getInstance()->getColumns($period) as $oldColumn) {
$rename[$oldColumn] = $oldColumn . self::COLUMN_SUFFIX;
}
$table->filter('ReplaceColumnNames', array($rename));
}
}
|