| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FireBox |
| 4 | - * @version 3.1.13 Free | |
| 4 | + * @version 2.1.0 Free | |
| 5 | 5 | * |
| 6 | 6 | * @author FirePlugins <info@fireplugins.com> |
| 7 | 7 | * @link https://www.fireplugins.com |
| 8 | - * @copyright Copyright © 2026 FirePlugins All Rights Reserved | |
| 8 | + * @copyright Copyright © 2023 FirePlugins All Rights Reserved | |
| 9 | 9 | * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | namespace FireBox\Core\Analytics; |
| @@ -18,19 +18,23 @@ | ||
| 18 | 18 | |
| 19 | 19 | class Data |
| 20 | 20 | { |
| 21 | 21 | private $start_date = null; |
| 22 | + | |
| 22 | 23 | private $end_date = null; |
| 24 | + | |
| 23 | 25 | private $metrics = []; |
| 26 | + | |
| 24 | 27 | private $filters = []; |
| 28 | + | |
| 25 | 29 | private $offset = null; |
| 30 | + | |
| 26 | 31 | private $limit = null; |
| 32 | + | |
| 27 | 33 | protected $options = []; |
| 28 | 34 | |
| 29 | - public function __construct($start_date = '', $end_date = '', $options = []) | |
| 35 | + public function __construct($start_date = null, $end_date = null, $options = []) | |
| 30 | 36 | { |
| 31 | - Helpers\Date::transformStartEndDateToUTC($start_date, $end_date); | |
| 32 | - | |
| 33 | 37 | $this->start_date = $start_date; |
| 34 | 38 | $this->end_date = $end_date; |
| 35 | 39 | $this->options = $options; |
| 36 | 40 | } |
| @@ -35,200 +39,123 @@ | ||
| 35 | 39 | $this->options = $options; |
| 36 | 40 | } |
| 37 | 41 | |
| 38 | 42 | /** |
| 39 | - * Fluent interface for building analytics queries | |
| 43 | + * Allowed metrics: | |
| 40 | 44 | * |
| 41 | - * @param array $metrics Array of metric slugs | |
| 42 | - * @return self | |
| 45 | + * [ | |
| 46 | + * 'impressions', | |
| 47 | + * 'averagetimeopen', | |
| 48 | + * 'submissions', | |
| 49 | + * 'conversionrate' | |
| 50 | + * ] | |
| 43 | 51 | */ |
| 44 | - public function metrics(array $metrics) | |
| 52 | + public function setMetrics($metrics = []) | |
| 45 | 53 | { |
| 46 | 54 | $this->metrics = $metrics; |
| 47 | - return $this; | |
| 48 | 55 | } |
| 49 | 56 | |
| 50 | - /** | |
| 51 | - * Fluent interface for adding filters | |
| 52 | - * | |
| 53 | - * @param array $filters | |
| 54 | - * @return self | |
| 55 | - */ | |
| 56 | - public function filters(array $filters) | |
| 57 | + public function setFilters($filters = []) | |
| 57 | 58 | { |
| 58 | 59 | $this->filters = $filters; |
| 59 | - return $this; | |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - /** | |
| 63 | - * Fluent interface for pagination | |
| 64 | - * | |
| 65 | - * @param int $limit | |
| 66 | - * @param int $offset | |
| 67 | - * @return self | |
| 68 | - */ | |
| 69 | - public function paginate($limit, $offset = 0) | |
| 62 | + public function setOffset($offset = null) | |
| 70 | 63 | { |
| 71 | - $this->limit = (int) $limit; | |
| 72 | 64 | $this->offset = (int) $offset; |
| 73 | - return $this; | |
| 74 | 65 | } |
| 75 | 66 | |
| 76 | - /** | |
| 77 | - * Fluent interface for setting limit | |
| 78 | - * | |
| 79 | - * @param int|null $limit | |
| 80 | - * @return self | |
| 81 | - */ | |
| 82 | - public function limit($limit) | |
| 67 | + public function setLimit($limit = null) | |
| 83 | 68 | { |
| 84 | - $this->limit = $limit !== null ? (int) $limit : null; | |
| 85 | - return $this; | |
| 69 | + $this->limit = (int) $limit; | |
| 86 | 70 | } |
| 87 | 71 | |
| 88 | - /** | |
| 89 | - * Fluent interface for setting offset | |
| 90 | - * | |
| 91 | - * @param int $offset | |
| 92 | - * @return self | |
| 93 | - */ | |
| 94 | - public function offset($offset) | |
| 95 | - { | |
| 96 | - $this->offset = (int) $offset; | |
| 97 | - return $this; | |
| 98 | - } | |
| 99 | - | |
| 100 | - /** | |
| 101 | - * Get analytics data with improved error handling and performance | |
| 102 | - * | |
| 103 | - * @param string $type | |
| 104 | - * @return array | |
| 105 | - */ | |
| 106 | 72 | public function getData($type = 'list') |
| 107 | 73 | { |
| 108 | - if (empty($this->metrics)) { | |
| 109 | - return []; | |
| 110 | - } | |
| 74 | + $data = array_fill_keys($this->metrics, []); | |
| 111 | 75 | |
| 112 | - $data = []; | |
| 113 | - $metric_configs = $this->buildMetricConfigs($type); | |
| 114 | - | |
| 115 | - foreach ($metric_configs as $metric_slug => $config) { | |
| 116 | - try { | |
| 117 | - $data[$metric_slug] = $this->getMetricData($config); | |
| 118 | - } catch (\Exception $e) { | |
| 119 | - $data[$metric_slug] = $this->getEmptyMetricData($type); | |
| 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; | |
| 120 | 83 | } |
| 121 | - } | |
| 122 | 84 | |
| 123 | - return $data; | |
| 124 | - } | |
| 85 | + $class = '\FireBox\Core\Analytics\Metrics\\' . $class_name; | |
| 86 | + $class = new $class($this->start_date, $this->end_date, $type, $this->options); | |
| 125 | 87 | |
| 126 | - /** | |
| 127 | - * Build metric configurations for batch processing | |
| 128 | - * | |
| 129 | - * @param string $type | |
| 130 | - * @return array | |
| 131 | - */ | |
| 132 | - private function buildMetricConfigs($type) | |
| 133 | - { | |
| 134 | - $configs = []; | |
| 88 | + if ($this->filters) | |
| 89 | + { | |
| 90 | + $class->setFilters($this->filters); | |
| 91 | + } | |
| 135 | 92 | |
| 136 | - foreach ($this->metrics as $metric_slug) { | |
| 137 | - $class_name = \FireBox\Core\Analytics\Helpers\Metrics::getClassFromSlug($metric_slug); | |
| 138 | - | |
| 139 | - if (!$class_name) { | |
| 140 | - continue; | |
| 93 | + if ($this->offset) | |
| 94 | + { | |
| 95 | + $class->setOffset($this->offset); | |
| 141 | 96 | } |
| 142 | 97 | |
| 143 | - $class_path = '\FireBox\Core\Analytics\Metrics\\' . $class_name; | |
| 144 | - | |
| 145 | - if (!class_exists($class_path)) { | |
| 146 | - continue; | |
| 98 | + if ($this->limit) | |
| 99 | + { | |
| 100 | + $class->setLimit($this->limit); | |
| 147 | 101 | } |
| 148 | 102 | |
| 149 | - $configs[$metric_slug] = [ | |
| 150 | - 'class' => $class_path, | |
| 151 | - 'type' => $type, | |
| 152 | - 'start_date' => $this->start_date, | |
| 153 | - 'end_date' => $this->end_date, | |
| 154 | - 'options' => $this->options, | |
| 155 | - 'filters' => $this->filters, | |
| 156 | - 'limit' => $this->limit, | |
| 157 | - 'offset' => $this->offset | |
| 158 | - ]; | |
| 159 | - } | |
| 103 | + $metric_data = $class->getData(); | |
| 160 | 104 | |
| 161 | - return $configs; | |
| 162 | - } | |
| 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 | + */ | |
| 163 | 155 | |
| 164 | - /** | |
| 165 | - * Get data for a single metric using dependency injection | |
| 166 | - * | |
| 167 | - * @param array $config | |
| 168 | - * @return mixed | |
| 169 | - */ | |
| 170 | - private function getMetricData(array $config) | |
| 171 | - { | |
| 172 | - $metric = $this->createMetric($config); | |
| 173 | - $data = $metric->getData(); | |
| 156 | + $class->onAfterGetData($metric_data); | |
| 157 | + } | |
| 174 | 158 | |
| 175 | - // Apply transformations through the registry system | |
| 176 | - if (!empty($data)) { | |
| 177 | - \FireBox\Core\Analytics\Transformers\TransformerRegistry::transformData( | |
| 178 | - $data, | |
| 179 | - $config['type'], | |
| 180 | - array_merge($config['options'], [ | |
| 181 | - 'is_single_day' => \FireBox\Core\Analytics\Helpers\Date::isSingleDay( | |
| 182 | - $config['start_date'], | |
| 183 | - $config['end_date'] | |
| 184 | - ), | |
| 185 | - 'has_timezone_sql_conversion' => method_exists($metric, 'hasTimezoneSQLConversion') ? $metric->hasTimezoneSQLConversion() : false | |
| 186 | - ]) | |
| 187 | - ); | |
| 188 | - } | |
| 189 | - | |
| 190 | 159 | return $data; |
| 191 | - } | |
| 192 | - | |
| 193 | - /** | |
| 194 | - * Create metric instance with all dependencies injected | |
| 195 | - * | |
| 196 | - * @param array $config | |
| 197 | - * @return object | |
| 198 | - */ | |
| 199 | - private function createMetric(array $config) | |
| 200 | - { | |
| 201 | - $metric = new $config['class']( | |
| 202 | - $config['start_date'], | |
| 203 | - $config['end_date'], | |
| 204 | - $config['type'], | |
| 205 | - $config['options'] | |
| 206 | - ); | |
| 207 | - | |
| 208 | - // Inject dependencies in one go | |
| 209 | - if (!empty($config['filters'])) { | |
| 210 | - $metric->filters($config['filters']); | |
| 211 | - } | |
| 212 | - | |
| 213 | - if (!empty($config['limit'])) { | |
| 214 | - $metric->limit($config['limit']); | |
| 215 | - } | |
| 216 | - | |
| 217 | - if (!empty($config['offset'])) { | |
| 218 | - $metric->offset($config['offset']); | |
| 219 | - } | |
| 220 | - | |
| 221 | - return $metric; | |
| 222 | - } | |
| 223 | - | |
| 224 | - /** | |
| 225 | - * Get empty data structure for failed metrics | |
| 226 | - * | |
| 227 | - * @param string $type | |
| 228 | - * @return mixed | |
| 229 | - */ | |
| 230 | - private function getEmptyMetricData($type) | |
| 231 | - { | |
| 232 | - return $type === 'count' ? 0 : []; | |
| 233 | 160 | } |
| 234 | 161 | } |