| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.1.0 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2023 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Analytics; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Data |
| 20 |
{ |
| 21 |
private $start_date = null; |
| 22 |
|
| 23 |
private $end_date = null; |
| 24 |
|
| 25 |
private $metrics = []; |
| 26 |
|
| 27 |
private $filters = []; |
| 28 |
|
| 29 |
private $offset = null; |
| 30 |
|
| 31 |
private $limit = null; |
| 32 |
|
| 33 |
protected $options = []; |
| 34 |
|
| 35 |
public function __construct($start_date = null, $end_date = null, $options = []) |
| 36 |
{ |
| 37 |
$this->start_date = $start_date; |
| 38 |
$this->end_date = $end_date; |
| 39 |
$this->options = $options; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Allowed metrics: |
| 44 |
* |
| 45 |
* [ |
| 46 |
* 'impressions', |
| 47 |
* 'averagetimeopen', |
| 48 |
* 'submissions', |
| 49 |
* 'conversionrate' |
| 50 |
* ] |
| 51 |
*/ |
| 52 |
public function setMetrics($metrics = []) |
| 53 |
{ |
| 54 |
$this->metrics = $metrics; |
| 55 |
} |
| 56 |
|
| 57 |
public function setFilters($filters = []) |
| 58 |
{ |
| 59 |
$this->filters = $filters; |
| 60 |
} |
| 61 |
|
| 62 |
public function setOffset($offset = null) |
| 63 |
{ |
| 64 |
$this->offset = (int) $offset; |
| 65 |
} |
| 66 |
|
| 67 |
public function setLimit($limit = null) |
| 68 |
{ |
| 69 |
$this->limit = (int) $limit; |
| 70 |
} |
| 71 |
|
| 72 |
public function getData($type = 'list') |
| 73 |
{ |
| 74 |
$data = array_fill_keys($this->metrics, []); |
| 75 |
|
| 76 |
foreach ($data as $metric_slug => &$metric_data) |
| 77 |
{ |
| 78 |
// Validate the given metric name and abort if unknown |
| 79 |
if (!$class_name = \FireBox\Core\Analytics\Helpers\Metrics::getClassFromSlug($metric_slug)) |
| 80 |
{ |
| 81 |
unset($data[$metric_slug]); |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
$class = '\FireBox\Core\Analytics\Metrics\\' . $class_name; |
| 86 |
$class = new $class($this->start_date, $this->end_date, $type, $this->options); |
| 87 |
|
| 88 |
if ($this->filters) |
| 89 |
{ |
| 90 |
$class->setFilters($this->filters); |
| 91 |
} |
| 92 |
|
| 93 |
if ($this->offset) |
| 94 |
{ |
| 95 |
$class->setOffset($this->offset); |
| 96 |
} |
| 97 |
|
| 98 |
if ($this->limit) |
| 99 |
{ |
| 100 |
$class->setLimit($this->limit); |
| 101 |
} |
| 102 |
|
| 103 |
$metric_data = $class->getData(); |
| 104 |
|
| 105 |
/** |
| 106 |
* Get list of impressions from start_date till end_date |
| 107 |
* $metric = new \FireBox\Core\Analytics\Metrics\Impressions($start_date, $end_date); |
| 108 |
* $metric->getData(); |
| 109 |
* |
| 110 |
* Output: |
| 111 |
* [ |
| 112 |
* 'date1' => 500, |
| 113 |
* 'date2' => 200 |
| 114 |
* ] |
| 115 |
* |
| 116 |
* Get total impressions from start_date till end_date |
| 117 |
* $metric = new \FireBox\Core\Analytics\Metrics\Impressions($start_date, $end_date); |
| 118 |
* $metric->getData('count'); |
| 119 |
* |
| 120 |
* Output: |
| 121 |
* 700 |
| 122 |
* |
| 123 |
* Get total impressions from start_date till end_date with the following filters: |
| 124 |
* $metric = new \FireBox\Core\Analytics\Metrics\Impressions($start_date, $end_date); |
| 125 |
* $metric->setFilters($filters); |
| 126 |
* $metric->getData('count'); |
| 127 |
* |
| 128 |
* Filters payload: |
| 129 |
* [ |
| 130 |
* 'campaigns' => [ |
| 131 |
* 'value' => [5, 10, 15], |
| 132 |
* ], |
| 133 |
* 'country' => [ |
| 134 |
* 'value' => ['GR', 'UK', 'USA', 'DE'], |
| 135 |
* ], |
| 136 |
* 'device' => [ |
| 137 |
* 'value' => ['desktop', 'tablet'], |
| 138 |
* ], |
| 139 |
* 'event' => [ |
| 140 |
* 'value' => ['close'], |
| 141 |
* ], |
| 142 |
* 'page' => [ |
| 143 |
* 'type' => 'contains', // contains, not_contains, equals |
| 144 |
* 'value' => ['/about', 'fireplugins'], |
| 145 |
* ], |
| 146 |
* 'referrer' => [ |
| 147 |
* 'type' => 'contains', // contains, not_contains, equals |
| 148 |
* 'value' => ['google.com', 'facebook.com'], |
| 149 |
* ] |
| 150 |
* ] |
| 151 |
* |
| 152 |
* Output: |
| 153 |
* 50 |
| 154 |
*/ |
| 155 |
|
| 156 |
$class->onAfterGetData($metric_data); |
| 157 |
} |
| 158 |
|
| 159 |
return $data; |
| 160 |
} |
| 161 |
} |