forked from nofrillsplugins/matomo-simple-ab-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.php
90 lines (82 loc) · 2.67 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
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\SimpleABTesting;
use Piwik\Common;
use Piwik\Piwik;
use Piwik\Db;
use Piwik\Plugins\SimpleABTesting\Dao\Experiments;
use Piwik\Container\StaticContainer;
use Piwik\DataTable;
use Piwik\Archive;
/**
* API for plugin SimpleABTesting.
*/
class API extends \Piwik\Plugin\API
{
/**
* @var Experiments
*/
private $experiments;
public function __construct()
{
$this->experiments = StaticContainer::get(Experiments::class);
}
/**
* Add an experiment
*/
public function insertExperiment(bool $idSite, string $name, string $hypothesis, string $description, string $fromDate, string $toDate, string $cssInsert, string $customJs): void
{
Piwik::checkUserHasSomeAdminAccess();
$this->experiments->insertExperiment($idSite, $name, $hypothesis, $description, $fromDate, $toDate, $cssInsert, $customJs);
}
public function deleteExperiment(bool $id): void
{
Piwik::checkUserHasSomeAdminAccess();
$this->experiments->deleteExperiment($id);
}
/**
* Get raw report data
* @todo - fix so we use date
*/
public function getExperimentReportData(int $idSite, string $period, string $date): array
{
Piwik::checkUserHasViewAccess($idSite);
$sql = "
SELECT
experiment_name AS `experiment_name`,
variant AS `variant`,
COUNT(DISTINCT idvisitor) AS `nb_unique_visitors`,
COUNT(*) AS `nb_visits`
FROM " . Common::prefixTable('simple_ab_testing_log') . "
WHERE idsite = ?
GROUP BY experiment_name
";
return Db::fetchAll($sql, [$idSite]);
}
/**
* Fetch experiment data from the archive blobs for reports or APIs.
*
* @param int $idSite The site ID.
* @param string $period The period (e.g., 'day', 'week', 'month').
* @param string $date The date range (e.g., 'today', 'last7', '2024-01-01').
* @param string|null $segment The segment string (optional, default is null).
* @return DataTable The archived experiment data grouped by experiment_name.
*/
public function getExperimentData(int $idSite, string $period, string $date, string $segment = null): DataTable
{
Piwik::checkUserHasViewAccess($idSite);
$dataTable = Archive::createDataTableFromArchive(
Archiver::RECORD_NAME,
$idSite,
$period,
$date,
$segment
);
return $dataTable;
}
}