firebox
/
Inc
/
Core
/
Analytics
/
QueryBuilders
/
Revenue
/
AttributionBreakdownByPeriodStrategy.php
AttributionBreakdownByPeriodStrategy.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 3.1.14, at Inc/Core/Analytics/QueryBuilders/Revenue/AttributionBreakdownByPeriodStrategy.php
| 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 | if (!defined('ABSPATH')) |
| 15 | { |
| 16 | exit; // Exit if accessed directly. |
| 17 | } |
| 18 | |
| 19 | class AttributionBreakdownByPeriodStrategy extends BaseRevenueQueryStrategy |
| 20 | { |
| 21 | public function getSelect(): string |
| 22 | { |
| 23 | $periodType = $this->determinePeriodType(); |
| 24 | $globalCounts = ', ' . $this->getGlobalCountColumns(); |
| 25 | |
| 26 | switch ($periodType) { |
| 27 | case 'hourly': |
| 28 | return 'CONCAT(' . $this->metric->getTimezoneDateSQL('DATE_FORMAT', 'bld.date', '\'%H\'') . ', \':00\') as label, bld.event_source, JSON_EXTRACT(bld.event_label, \'$.order_type\') as order_type, JSON_EXTRACT(bld.event_label, \'$.order_id\') as order_id' . $globalCounts; |
| 29 | |
| 30 | case 'weekly': |
| 31 | return 'DATE_FORMAT(STR_TO_DATE(CONCAT(yearweek(' . $this->metric->getTimezoneDateSQL('', 'bld.date') . '), " ' . firebox()->_('FB_MONDAY') . '"), \'%X%V %W\'), \'%d %b %y\') as label, bld.event_source, JSON_EXTRACT(bld.event_label, \'$.order_type\') as order_type, JSON_EXTRACT(bld.event_label, \'$.order_id\') as order_id' . $globalCounts; |
| 32 | |
| 33 | case 'monthly': |
| 34 | return $this->metric->getTimezoneDateSQL('DATE_FORMAT', 'bld.date', '\'%b %Y\'') . ' as label, bld.event_source, JSON_EXTRACT(bld.event_label, \'$.order_type\') as order_type, JSON_EXTRACT(bld.event_label, \'$.order_id\') as order_id' . $globalCounts; |
| 35 | |
| 36 | case 'daily': |
| 37 | default: |
| 38 | return $this->metric->getTimezoneDateSQL('date', 'bld.date') . ' as label, bld.event_source, JSON_EXTRACT(bld.event_label, \'$.order_type\') as order_type, JSON_EXTRACT(bld.event_label, \'$.order_id\') as order_id' . $globalCounts; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | public function getWhere(): string |
| 43 | { |
| 44 | return 'AND bld.event = \'revenue\''; |
| 45 | } |
| 46 | |
| 47 | public function getGroupBy(): string |
| 48 | { |
| 49 | $periodType = $this->determinePeriodType(); |
| 50 | |
| 51 | switch ($periodType) { |
| 52 | case 'hourly': |
| 53 | return 'GROUP BY CONCAT(' . $this->metric->getTimezoneDateSQL('DATE_FORMAT', 'bld.date', '\'%H\'') . ', \':00\'), bld.event_source, JSON_EXTRACT(bld.event_label, \'$.order_id\'), JSON_EXTRACT(bld.event_label, \'$.order_type\')'; |
| 54 | |
| 55 | case 'weekly': |
| 56 | return 'GROUP BY yearweek(' . $this->metric->getTimezoneDateSQL('', 'bld.date') . '), bld.event_source, JSON_EXTRACT(bld.event_label, \'$.order_id\'), JSON_EXTRACT(bld.event_label, \'$.order_type\')'; |
| 57 | |
| 58 | case 'monthly': |
| 59 | return 'GROUP BY YEAR(' . $this->metric->getTimezoneDateSQL('', 'bld.date') . '), MONTH(' . $this->metric->getTimezoneDateSQL('', 'bld.date') . '), bld.event_source, JSON_EXTRACT(bld.event_label, \'$.order_id\'), JSON_EXTRACT(bld.event_label, \'$.order_type\')'; |
| 60 | |
| 61 | case 'daily': |
| 62 | default: |
| 63 | return 'GROUP BY ' . $this->metric->getTimezoneDateSQL('date', 'bld.date') . ', bld.event_source, JSON_EXTRACT(bld.event_label, \'$.order_id\'), JSON_EXTRACT(bld.event_label, \'$.order_type\')'; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public function getOrderBy(): string |
| 68 | { |
| 69 | return ''; |
| 70 | } |
| 71 | |
| 72 | public function getHaving(): string |
| 73 | { |
| 74 | return ''; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Process results for attribution breakdown by period |
| 79 | */ |
| 80 | public function processResults($results) |
| 81 | { |
| 82 | return $this->metric->calculateAttributionBreakdownByPeriod($results); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Determine the period type based on chart filter and days between |
| 87 | */ |
| 88 | protected function determinePeriodType() |
| 89 | { |
| 90 | $options = $this->metric->getOptions(); |
| 91 | $days_between = $options['days_between'] ?? 1; |
| 92 | $chart_filter = $options['chart_filter'] ?? 'list'; |
| 93 | |
| 94 | // Same logic as chart endpoint |
| 95 | if ($days_between == 1) { |
| 96 | return 'hourly'; |
| 97 | } |
| 98 | |
| 99 | if ($days_between > 1) { |
| 100 | switch ($chart_filter) { |
| 101 | case 'weekly': |
| 102 | return 'weekly'; |
| 103 | case 'monthly': |
| 104 | return 'monthly'; |
| 105 | case 'list': |
| 106 | default: |
| 107 | return 'daily'; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return 'daily'; |
| 112 | } |
| 113 | } |
| 114 |