| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 3.1.14 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <[email protected]> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Analytics\QueryBuilders\Revenue; |
| 13 |
|
| 14 |
use FireBox\Core\Analytics\QueryBuilders\BaseQueryStrategy; |
| 15 |
|
| 16 |
if (!defined('ABSPATH')) |
| 17 |
{ |
| 18 |
exit; // Exit if accessed directly. |
| 19 |
} |
| 20 |
|
| 21 |
abstract class BaseRevenueQueryStrategy extends BaseQueryStrategy |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Revenue uses logs_details table as primary date column |
| 25 |
*/ |
| 26 |
protected function getDateColumn(): string |
| 27 |
{ |
| 28 |
return 'bld.date'; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Revenue-specific FROM/JOIN logic |
| 33 |
* Uses logs_details as primary table with JOIN to logs |
| 34 |
* Includes subquery for multi-touch attribution global counts (MySQL 5.5+ compatible) |
| 35 |
*/ |
| 36 |
protected function getDefaultFromAndJoins(): string |
| 37 |
{ |
| 38 |
$table_details = $this->metric->getTableDetails(); |
| 39 |
$table_logs = $this->metric->getTableLogs(); |
| 40 |
|
| 41 |
return "{$table_details} as bld |
| 42 |
LEFT JOIN {$table_logs} as bl ON bl.id = bld.log_id |
| 43 |
LEFT JOIN ( |
| 44 |
SELECT |
| 45 |
JSON_EXTRACT(event_label, '\$.order_id') as order_id, |
| 46 |
JSON_EXTRACT(event_label, '\$.order_type') as order_type, |
| 47 |
SUM(CASE WHEN event_source = 'conversion' THEN 1 ELSE 0 END) as global_conversion_count, |
| 48 |
SUM(CASE WHEN event_source = 'impression' THEN 1 ELSE 0 END) as global_impression_count, |
| 49 |
COUNT(*) as global_contribution_count |
| 50 |
FROM {$table_details} |
| 51 |
WHERE event = 'revenue' |
| 52 |
GROUP BY JSON_EXTRACT(event_label, '\$.order_id'), JSON_EXTRACT(event_label, '\$.order_type') |
| 53 |
) counts ON counts.order_id = JSON_EXTRACT(bld.event_label, '\$.order_id') |
| 54 |
AND counts.order_type = JSON_EXTRACT(bld.event_label, '\$.order_type')"; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Revenue metrics use 'bl' alias for logs table |
| 59 |
*/ |
| 60 |
protected function getTableAliases(): array |
| 61 |
{ |
| 62 |
return [ |
| 63 |
'logs' => 'bl', |
| 64 |
'details' => 'bld' |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Override getLimitOffset for Revenue-specific logic |
| 70 |
* For breakdown types that need revenue calculation and sorting, don't apply SQL LIMIT |
| 71 |
*/ |
| 72 |
public function getLimitOffset(): string |
| 73 |
{ |
| 74 |
$type = $this->metric->getType(); |
| 75 |
|
| 76 |
// For breakdown types that need revenue calculation and sorting, don't apply SQL LIMIT |
| 77 |
// The limit will be applied after PHP sorting in processResults |
| 78 |
if (in_array($type, ['top_campaign', 'countries', 'referrers', 'devices', 'pages', 'day_of_week'])) |
| 79 |
{ |
| 80 |
return ''; |
| 81 |
} |
| 82 |
|
| 83 |
// For other types, use the parent method |
| 84 |
return parent::getLimitOffset(); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Default processing for revenue strategies |
| 89 |
* Strategies can override this method for custom processing |
| 90 |
*/ |
| 91 |
public function processResults($results) |
| 92 |
{ |
| 93 |
return $this->metric->calculateRevenueGrouped($results); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get the column selection for global multi-touch attribution counts |
| 98 |
* These come from the subquery JOIN in getDefaultFromAndJoins() |
| 99 |
* Strategies should append this to their SELECT clause |
| 100 |
* |
| 101 |
* @return string SQL columns from counts subquery |
| 102 |
*/ |
| 103 |
protected function getGlobalCountColumns(): string |
| 104 |
{ |
| 105 |
return 'counts.global_contribution_count, |
| 106 |
counts.global_conversion_count, |
| 107 |
counts.global_impression_count'; |
| 108 |
} |
| 109 |
} |
| 110 |
|