| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.1.14 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2024 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Analytics\Metrics; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class ConversionRate extends Metric |
| 20 |
{ |
| 21 |
public function getData() |
| 22 |
{ |
| 23 |
$this->applyFilters(); |
| 24 |
|
| 25 |
$sql = "SELECT |
| 26 |
{$this->getSelect()} |
| 27 |
FROM |
| 28 |
{$this->wpdb->prefix}firebox_logs as l |
| 29 |
LEFT JOIN {$this->wpdb->prefix}firebox_logs_details as bld ON bld.log_id = l.id AND bld.event = 'conversion' |
| 30 |
CROSS JOIN ( |
| 31 |
SELECT '%s' AS start_date, '%s' AS end_date |
| 32 |
) AS outer_query |
| 33 |
WHERE |
| 34 |
1 |
| 35 |
{$this->getWherePeriod()} |
| 36 |
{$this->sql_filters} |
| 37 |
{$this->getGroupBy()} |
| 38 |
{$this->getHaving()} |
| 39 |
{$this->getOrderBy()} |
| 40 |
{$this->getLimit()} |
| 41 |
{$this->getOffset()}"; |
| 42 |
|
| 43 |
$sql = $this->wpdb->prepare($sql, $this->query_placeholders); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 44 |
|
| 45 |
$data = $this->wpdb->get_results($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 46 |
|
| 47 |
if ($this->type === 'count') |
| 48 |
{ |
| 49 |
$data = isset($data[0]->total) ? (float) $data[0]->total : 0; |
| 50 |
} |
| 51 |
|
| 52 |
return $data; |
| 53 |
} |
| 54 |
|
| 55 |
private function getSelect() |
| 56 |
{ |
| 57 |
$select = ''; |
| 58 |
|
| 59 |
$total_select = '(COUNT(DISTINCT bld.id) / COUNT(DISTINCT l.id)) * 100 AS total'; |
| 60 |
|
| 61 |
switch ($this->type) |
| 62 |
{ |
| 63 |
case 'top_campaign': |
| 64 |
$select = 'l.box as id, (select p.post_title from ' . $this->wpdb->prefix . 'posts as p WHERE p.ID = l.box) as label, ' . $total_select; |
| 65 |
break; |
| 66 |
|
| 67 |
case 'countries': |
| 68 |
$select = 'l.country as label, ' . $total_select; |
| 69 |
break; |
| 70 |
|
| 71 |
case 'referrers': |
| 72 |
$select = 'l.referrer as label, ' . $total_select; |
| 73 |
break; |
| 74 |
|
| 75 |
case 'devices': |
| 76 |
$select = 'l.device as label, ' . $total_select; |
| 77 |
break; |
| 78 |
|
| 79 |
case 'pages': |
| 80 |
$select = 'l.page as label, ' . $total_select; |
| 81 |
break; |
| 82 |
|
| 83 |
case 'weekly': |
| 84 |
$select = 'DATE_FORMAT(STR_TO_DATE(CONCAT(yearweek(l.date), " ' . firebox()->_('FB_MONDAY') . '"), \'%%X%%V %%W\'), \'%%d %%b %%y\') as label, ' . $total_select; |
| 85 |
break; |
| 86 |
|
| 87 |
case 'monthly': |
| 88 |
$select = 'DATE_FORMAT(l.date, \'%%b %%Y\') as label, ' . $total_select; |
| 89 |
break; |
| 90 |
|
| 91 |
case 'day_of_week': |
| 92 |
$select = 'DAYNAME(l.date) as label, ' . $total_select; |
| 93 |
break; |
| 94 |
|
| 95 |
case 'list': |
| 96 |
default: |
| 97 |
$partA = 'date(l.date) AS label'; |
| 98 |
|
| 99 |
if ($this->isSingleDay()) |
| 100 |
{ |
| 101 |
$partA = 'CONCAT(DATE_FORMAT(l.date, \'%H\'), \':00\') as label'; |
| 102 |
} |
| 103 |
|
| 104 |
$select = $partA . ', ' . $total_select; |
| 105 |
break; |
| 106 |
|
| 107 |
case 'count': |
| 108 |
$select = $total_select; |
| 109 |
break; |
| 110 |
} |
| 111 |
|
| 112 |
return $select; |
| 113 |
} |
| 114 |
|
| 115 |
private function getHaving() |
| 116 |
{ |
| 117 |
$having = ''; |
| 118 |
|
| 119 |
switch ($this->type) |
| 120 |
{ |
| 121 |
case 'list': |
| 122 |
case 'top_campaign': |
| 123 |
$having = 'total > 0'; |
| 124 |
break; |
| 125 |
} |
| 126 |
|
| 127 |
$having = $having ? 'HAVING ' . $having : ''; |
| 128 |
|
| 129 |
return $having; |
| 130 |
} |
| 131 |
|
| 132 |
private function getGroupBy() |
| 133 |
{ |
| 134 |
if ($this->type === 'count') |
| 135 |
{ |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
$groupby = 'DATE(l.date)'; |
| 140 |
|
| 141 |
if ($this->isSingleDay() && $this->type !== 'count') |
| 142 |
{ |
| 143 |
$groupby = 'CONCAT(DATE_FORMAT(l.date, \'%H\'), \':00\')'; |
| 144 |
} |
| 145 |
|
| 146 |
if ($this->type === 'top_campaign') |
| 147 |
{ |
| 148 |
$groupby = 'l.box'; |
| 149 |
} |
| 150 |
else if ($this->type === 'countries') |
| 151 |
{ |
| 152 |
$groupby = 'l.country'; |
| 153 |
} |
| 154 |
else if ($this->type === 'referrers') |
| 155 |
{ |
| 156 |
$groupby = 'l.referrer'; |
| 157 |
} |
| 158 |
else if ($this->type === 'devices') |
| 159 |
{ |
| 160 |
$groupby = 'l.device'; |
| 161 |
} |
| 162 |
else if ($this->type === 'pages') |
| 163 |
{ |
| 164 |
$groupby = 'l.page'; |
| 165 |
} |
| 166 |
else if ($this->type === 'weekly') |
| 167 |
{ |
| 168 |
$groupby = 'yearweek(l.date)'; |
| 169 |
} |
| 170 |
else if ($this->type === 'monthly') |
| 171 |
{ |
| 172 |
$groupby = 'YEAR(l.date), MONTH(l.date)'; |
| 173 |
} |
| 174 |
else if ($this->type === 'day_of_week') |
| 175 |
{ |
| 176 |
$groupby = 'label'; |
| 177 |
} |
| 178 |
|
| 179 |
return 'GROUP BY ' . $groupby; |
| 180 |
} |
| 181 |
|
| 182 |
private function getOrderBy() |
| 183 |
{ |
| 184 |
if ($this->type === 'count') |
| 185 |
{ |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
$orderby = 'DATE(l.date) desc'; |
| 190 |
|
| 191 |
if (in_array($this->type, ['top_campaign', 'countries', 'referrers', 'devices', 'pages', 'day_of_week'])) |
| 192 |
{ |
| 193 |
$orderby = 'total desc'; |
| 194 |
} |
| 195 |
|
| 196 |
if (isset($this->options['orderby'])) |
| 197 |
{ |
| 198 |
$orderby = $this->options['orderby']; |
| 199 |
} |
| 200 |
|
| 201 |
return 'ORDER BY ' . $orderby; |
| 202 |
} |
| 203 |
} |