__( 'Last 7 days', 'ai-eshop-optimizer' ), '30' => __( 'Last 30 days', 'ai-eshop-optimizer' ), '90' => __( 'Last 90 days', 'ai-eshop-optimizer' ), '365' => __( 'Last 12 months', 'ai-eshop-optimizer' ), '0' => __( 'All time', 'ai-eshop-optimizer' ), ); } private function money( $v ) { if ( is_callable( array( 'AIEO_DMM_Ads', 'money' ) ) ) { return AIEO_DMM_Ads::money( $v ); } return AIEO_DMM_Conversions_Registry::money( $v ); } public function render_subtab() { global $wpdb; foreach ( array( 'class-aieo-dmm-conversions-registry.php', 'class-aieo-dmm-internal-promotions.php', 'class-aieo-dmm-contributors.php', ) as $f ) { $p = AIEO_DIR . 'includes/dmm/' . $f; if ( file_exists( $p ) ) { require_once $p; } } if ( ! class_exists( 'AIEO_DMM_Conversions_Registry' ) ) { list( $aieo_pr_open, $aieo_pr_close ) = AIEO_DMM_UI::card( array( 'icon' => 'megaphone', 'title' => $this->get_label() ) ); echo $aieo_pr_open; echo \Rooster\Admin\Ui::notice( esc_html__( 'The conversions registry is not loaded.', 'ai-eshop-optimizer' ), 'destructive', array( 'inline' => true ) ); echo $aieo_pr_close; return; } $conv = AIEO_DMM_Conversions_Registry::table(); $have = (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $conv ) ) === $conv; $rows = $have ? (int) $wpdb->get_var( "SELECT COUNT(*) FROM `{$conv}`" ) : 0; $days = isset( $_GET['pdays'] ) ? (int) $_GET['pdays'] : 30; $until = gmdate( 'Y-m-d H:i:s' ); $since = $days > 0 ? gmdate( 'Y-m-d H:i:s', time() - $days * DAY_IN_SECONDS ) : '1970-01-01 00:00:00'; $aieo_pr_period = array( 'anchor' => 'promotions-period', 'icon' => 'calendar-days', 'title' => __( 'Period', 'ai-eshop-optimizer' ), 'description' => __( 'All dates are GMT days. Figures come from one table, so every card below reconciles against the others.', 'ai-eshop-optimizer' ), 'col' => 1, ); if ( ! $have || 0 === $rows ) { list( $aieo_pr_open, $aieo_pr_close ) = AIEO_DMM_UI::card( $aieo_pr_period ); echo $aieo_pr_open; $aieo_pr_nohistory = \Rooster\Admin\States::state( 'not-run', array( 'title' => __( 'No conversion history yet.', 'ai-eshop-optimizer' ), 'text' => __( 'Run the one-off backfill to import every existing order into the registry, then reload this page:', 'ai-eshop-optimizer' ), 'action' => '
wp eval-file wp-content/plugins/ai-eshop-optimizer/bin/post-deploy-conversions-backfill.php', ) ); echo $aieo_pr_nohistory; echo $aieo_pr_close; return; } $band = function_exists( 'rooster_admin_band_open' ); if ( $band ) { rooster_admin_band_open( array( 'cols' => '1:2' ) ); } list( $aieo_pr_open, $aieo_pr_close ) = AIEO_DMM_UI::card( $aieo_pr_period ); echo $aieo_pr_open; echo ''; echo $aieo_pr_close; $this->card_headline( $conv, $since, $until ); if ( $band ) { rooster_admin_band_close(); } $this->card_channel_value( $conv, $since, $until, $days ); if ( $band ) { rooster_admin_band_open( array( 'cols' => 2 ) ); } $this->card_internal( $conv, $since, $until ); if ( $band ) { rooster_admin_band_close(); rooster_admin_band_open( array( 'cols' => 2 ) ); } $this->card_crosstab( $conv, $since, $until ); $this->card_contributors( $since, $until ); if ( $band ) { rooster_admin_band_close(); } } private function card_headline( $conv, $since, $until ) { global $wpdb; $r = $wpdb->get_row( $wpdb->prepare( "SELECT COUNT(*) n, ROUND(SUM(conversion_value),2) rev, SUM(int_surface <> '') int_n, ROUND(SUM(CASE WHEN int_surface <> '' THEN conversion_value ELSE 0 END),2) int_rev, SUM(utm_source <> '') ext_n, ROUND(SUM(CASE WHEN utm_source <> '' THEN conversion_value ELSE 0 END),2) ext_rev, SUM(int_surface <> '' AND utm_source <> '') both_n, SUM(attr_origin = 'overwritten') lost_n FROM `{$conv}` WHERE occurred_at BETWEEN %s AND %s", $since, $until ), ARRAY_A ); $glance = array( array( 'k' => 'n', 'metric' => __( 'Conversions in period', 'ai-eshop-optimizer' ), 'count' => (int) $r['n'], 'value' => (float) $r['rev'], 'note' => '' ), array( 'k' => 'int', 'metric' => __( 'Closed by one of OUR internal promotions', 'ai-eshop-optimizer' ), 'count' => (int) $r['int_n'], 'value' => (float) $r['int_rev'], 'note' => '' ), array( 'k' => 'ext', 'metric' => __( 'Brought in by an external promotion', 'ai-eshop-optimizer' ), 'count' => (int) $r['ext_n'], 'value' => (float) $r['ext_rev'], 'note' => '' ), array( 'k' => 'both', 'metric' => __( 'Carry BOTH (external brought, internal closed)', 'ai-eshop-optimizer' ), 'count' => (int) $r['both_n'], 'value' => null, 'note' => '' ), ); if ( (int) $r['lost_n'] > 0 ) { $glance[] = array( 'k' => 'lost', 'metric' => __( 'External promotion destroyed by legacy AIO* tagging', 'ai-eshop-optimizer' ), 'count' => (int) $r['lost_n'], 'value' => null, 'note' => __( 'internal credit preserved, campaign unrecoverable', 'ai-eshop-optimizer' ), ); } AIEO_Table::card( array( 'anchor' => 'promotions-at-a-glance', 'icon' => 'eye', 'heading' => 'h2', 'title' => __( 'At a glance', 'ai-eshop-optimizer' ), 'description' => __( 'Two kinds of promotion drive every sale, and they are independent. An EXTERNAL promotion brings someone to the shop (Google, Meta, a newsletter, a partner). An INTERNAL promotion moves them along once they are here (an upsell block, a related-products row, a category link, a hero slot). One order can carry one of each, and this screen keeps them separate so neither can overwrite the other.', 'ai-eshop-optimizer' ), 'footnote' => __( 'The two middle rows overlap deliberately. They are different questions, not a split of the total, so they do not sum to it. Only the "BOTH" row is the intersection.', 'ai-eshop-optimizer' ), 'col' => 2, 'narrow' => true, 'page_id' => $this->get_id(), 'row_key' => 'k', 'sortable' => false, 'density' => 'compact', 'rows' => $glance, 'row_class' => static function ( $row ) { return 'lost' === $row['k'] ? 'aieo-t-tone--warning' : ''; }, 'columns' => array( array( 'key' => 'metric', 'label' => __( 'Metric', 'ai-eshop-optimizer' ), 'type' => 'text', 'width' => '46%', 'subline' => 'note' ), array( 'key' => 'count', 'label' => __( 'Conversions', 'ai-eshop-optimizer' ), 'type' => 'integer' ), array( 'key' => 'value', 'label' => __( 'Revenue', 'ai-eshop-optimizer' ), 'type' => 'money' ), ), ) ); } private function card_channel_value( $conv, $since, $until, $days ) { global $wpdb; $lcd = $wpdb->prefix . 'aieo_landing_campaign_daily'; $cost = $wpdb->prefix . 'aieo_attribution_cost'; if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $lcd ) ) !== $lcd ) { return; } $d_from = $days > 0 ? gmdate( 'Y-m-d', time() - $days * DAY_IN_SECONDS ) : '1970-01-01'; $d_to = gmdate( 'Y-m-d', time() - DAY_IN_SECONDS ); $visits = array(); foreach ( (array) $wpdb->get_results( $wpdb->prepare( "SELECT channel, SUM(sessions) s FROM `{$lcd}` WHERE `date` BETWEEN %s AND %s GROUP BY channel", $d_from, $d_to ), ARRAY_A ) as $r ) { $visits[ self::norm_channel( $r['channel'] ) ] = ( $visits[ self::norm_channel( $r['channel'] ) ] ?? 0 ) + (int) $r['s']; } if ( ! $visits ) { return; } $spend = array(); if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $cost ) ) === $cost ) { foreach ( (array) $wpdb->get_results( $wpdb->prepare( "SELECT dimension_value c, SUM(amount) a FROM `{$cost}` WHERE dimension_type='channel' AND period_start BETWEEN %s AND %s GROUP BY dimension_value", $d_from, $d_to ), ARRAY_A ) as $r ) { $k = self::norm_channel( $r['c'] ); $spend[ $k ] = ( $spend[ $k ] ?? 0 ) + (float) $r['a']; } } $rev = array(); foreach ( (array) $wpdb->get_results( $wpdb->prepare( "SELECT channel c, SUM(conversion_value) r FROM `{$conv}` WHERE occurred_at BETWEEN %s AND %s AND conversion_type='order' GROUP BY channel", $since, $until ), ARRAY_A ) as $r ) { $k = self::norm_channel( $r['c'] ); $rev[ $k ] = ( $rev[ $k ] ?? 0 ) + (float) $r['r']; } $pv = 0; $ps = 0.0; foreach ( $visits as $k => $v ) { if ( 0 === strpos( $k, 'paid_' ) && isset( $spend[ $k ] ) ) { $pv += $v; $ps += $spend[ $k ]; } } $rate = $pv > 0 ? $ps / $pv : 0.0; list( $aieo_pr_open, $aieo_pr_close ) = AIEO_DMM_UI::card( array( 'anchor' => 'promotions-channel-value-and-what-your-free-traffic-would-cost-to-buy', 'icon' => 'banknote', 'title' => __( 'Value and cost per channel', 'ai-eshop-optimizer' ), 'description' => __( 'Revenue, spend and revenue per visit for every channel, plus the replacement cost of free organic-search and referral visits at your own measured cost per paid visit.', 'ai-eshop-optimizer' ), 'flush' => true, 'footer' => '' . esc_html__( 'Landing pages by income', 'ai-eshop-optimizer' ) . '' . '' . esc_html__( 'Break the same revenue down by the page the visit started on.', 'ai-eshop-optimizer' ) . '', 'footnote' => __( 'Replacement cost is shown for organic search and referral only. Direct traffic is excluded deliberately: it is the bucket every unidentified visit falls into, including crawlers, so a value quoted for it would not survive scrutiny. A paid channel with no ledger rows shows cost as "unknown", never zero.', 'ai-eshop-optimizer' ), ) ); echo $aieo_pr_open; arsort( $visits ); $free_repl = 0.0; $chan_rows = array(); foreach ( $visits as $k => $v ) { $r = $rev[ $k ] ?? 0.0; $sp = $spend[ $k ] ?? null; $is_paid = ( 0 === strpos( $k, 'paid_' ) ); $repl = ( ! $is_paid && in_array( $k, array( 'organic_search', 'referral' ), true ) && $rate > 0 ) ? $v * $rate : null; if ( null !== $repl ) { $free_repl += $repl; } $chan_rows[] = array( 'channel' => (string) $k, 'visits' => (int) $v, 'revenue' => (float) $r, 'rev_visit' => $v > 0 ? (float) $r / $v : null, 'paid' => ( null === $sp ) ? null : (float) $sp, 'paid_note' => ( null === $sp && $is_paid ) ? __( 'unknown', 'ai-eshop-optimizer' ) : '', 'cost_visit' => ( null !== $sp && $v > 0 ) ? (float) $sp / $v : null, 'repl' => $repl, ); } AIEO_Table::render( array( 'page_id' => $this->get_id(), 'row_key' => 'channel', 'density' => 'compact', 'rows' => $chan_rows, 'empty' => __( 'No channel traffic in this period.', 'ai-eshop-optimizer' ), 'columns' => array( array( 'key' => 'channel', 'label' => __( 'Channel', 'ai-eshop-optimizer' ), 'type' => 'text' ), array( 'key' => 'visits', 'label' => __( 'Visits', 'ai-eshop-optimizer' ), 'type' => 'integer' ), array( 'key' => 'revenue', 'label' => __( 'Revenue', 'ai-eshop-optimizer' ), 'type' => 'money' ), array( 'key' => 'rev_visit', 'label' => __( 'Revenue / visit', 'ai-eshop-optimizer' ), 'type' => 'money' ), array( 'key' => 'paid', 'label' => __( 'You paid', 'ai-eshop-optimizer' ), 'type' => 'money', 'subline' => 'paid_note', ), array( 'key' => 'cost_visit', 'label' => __( 'Cost / visit', 'ai-eshop-optimizer' ), 'type' => 'money' ), array( 'key' => 'repl', 'label' => __( 'Replacement cost', 'ai-eshop-optimizer' ), 'type' => 'money', 'tone' => 'success' ), ), ) ); if ( $rate > 0 ) { $aieo_pr_headline = \Rooster\Admin\Ui::notice( esc_html( sprintf( __( 'your free search and referral traffic would cost %1$s to buy at your own measured rate of %2$s per paid visit.', 'ai-eshop-optimizer' ), wp_strip_all_tags( $this->money( $free_repl ) ), wp_strip_all_tags( $this->money( $rate ) ) ) ), 'success', array( 'title' => __( 'The headline:', 'ai-eshop-optimizer' ), 'inline' => true ) ); echo $aieo_pr_headline; } echo $aieo_pr_close; } private static function norm_channel( $c ) { $c = strtolower( trim( (string) $c ) ); if ( '' === $c ) { return '(unknown)'; } if ( in_array( $c, array( 'organic', 'organic_search' ), true ) ) { return 'organic_search'; } if ( in_array( $c, array( 'paid_facebook', 'paid_fb', 'paid_ig', 'fb', 'ig', 'facebook', 'instagram' ), true ) ) { return 'paid_meta'; } if ( in_array( $c, array( 'paid_google', 'google', 'adwords' ), true ) ) { return 'paid_google'; } if ( in_array( $c, array( 'paid_tiktok', 'tiktok' ), true ) ) { return 'paid_tiktok'; } if ( in_array( $c, array( 'skroutz', 'shopflix', 'bestprice.gr', 'bestprice' ), true ) ) { return 'marketplace'; } return $c; } private function card_internal( $conv, $since, $until ) { global $wpdb; $rows = (array) $wpdb->get_results( $wpdb->prepare( "SELECT int_surface, COUNT(*) n, ROUND(SUM(conversion_value),2) rev, ROUND(AVG(conversion_value),2) aov, GROUP_CONCAT(DISTINCT int_origin) origins FROM `{$conv}` WHERE occurred_at BETWEEN %s AND %s AND int_surface <> '' GROUP BY int_surface ORDER BY rev DESC", $since, $until ), ARRAY_A ); $internal = array(); foreach ( (array) $rows as $r ) { $internal[] = array( 'surface' => (string) $r['int_surface'], 'label' => $this->surface_label( $r['int_surface'] ), 'n' => (int) $r['n'], 'rev' => (float) $r['rev'], 'aov' => (float) $r['aov'], 'evidence' => str_replace( array( 'reco_click', 'aio_import', 'addcart_src', 'url_param' ), array( 'measured click', 'imported history', 'measured add', 'link tag' ), (string) $r['origins'] ), ); } $anchors = (array) $wpdb->get_results( $wpdb->prepare( "SELECT int_surface, int_anchor_type, int_anchor_id, COUNT(*) n, ROUND(SUM(conversion_value),2) rev FROM `{$conv}` WHERE occurred_at BETWEEN %s AND %s AND int_surface <> '' AND int_anchor_id > 0 GROUP BY int_surface, int_anchor_type, int_anchor_id ORDER BY rev DESC LIMIT 15", $since, $until ), ARRAY_A ); AIEO_Table::card( array( 'anchor' => 'promotions-our-internal-promotions', 'icon' => 'megaphone', 'heading' => 'h2', 'title' => __( 'Our internal promotions', 'ai-eshop-optimizer' ), 'description' => __( 'What each of our own on-site surfaces earned. Credit is established by matching a recommendation CLICK to the cart-add that followed it in the same session, because the visitor clicks on one page and adds on another. Without that link every one of these sales is credited to the destination product page instead.', 'ai-eshop-optimizer' ), 'col' => 1, 'span' => $anchors ? '' : 'all', 'narrow' => ! $anchors, 'page_id' => $this->get_id(), 'row_key' => 'surface', 'density' => 'compact', 'rows' => $internal, 'empty' => __( 'No internal promotion conversions in this period.', 'ai-eshop-optimizer' ), 'columns' => array( array( 'key' => 'label', 'label' => __( 'Surface', 'ai-eshop-optimizer' ), 'type' => 'text', 'subline' => 'surface' ), array( 'key' => 'n', 'label' => __( 'Conversions', 'ai-eshop-optimizer' ), 'type' => 'integer' ), array( 'key' => 'rev', 'label' => __( 'Revenue', 'ai-eshop-optimizer' ), 'type' => 'money' ), array( 'key' => 'aov', 'label' => __( 'Avg order', 'ai-eshop-optimizer' ), 'type' => 'money' ), array( 'key' => 'evidence', 'label' => __( 'Evidence', 'ai-eshop-optimizer' ), 'type' => 'text', 'tone' => 'secondary' ), ), ) ); if ( $anchors ) { $anchor_rows = array(); foreach ( $anchors as $a ) { $anchor_rows[] = array( 'ref' => $a['int_surface'] . '|' . $a['int_anchor_type'] . '|' . (int) $a['int_anchor_id'], 'name' => $this->anchor_name( $a['int_anchor_type'], (int) $a['int_anchor_id'] ), 'which' => '(' . $a['int_anchor_type'] . ' #' . (int) $a['int_anchor_id'] . ')', 'surface' => $this->surface_label( $a['int_surface'] ), 'n' => (int) $a['n'], 'rev' => (float) $a['rev'], ); } AIEO_Table::card( array( 'anchor' => 'promotions-which-pages-promotion-block-earns-most', 'icon' => 'target', 'heading' => 'h2', 'title' => __( 'Which page\'s promotion block earns most', 'ai-eshop-optimizer' ), 'footnote' => __( 'Read this as: put your best cross-sells on these pages first, because their blocks already convert.', 'ai-eshop-optimizer' ), 'col' => 2, 'narrow' => true, 'page_id' => $this->get_id(), 'row_key' => 'ref', 'density' => 'compact', 'rows' => $anchor_rows, 'caps' => array( 'search' => true ), 'filters' => array( array( 'key' => 'surface', 'label' => __( 'Surface', 'ai-eshop-optimizer' ), 'kind' => 'values', 'control' => 'select' ) ), 'cap' => 15, 'capped' => count( $anchor_rows ) >= 15, 'empty' => __( 'No anchored promotion blocks in this period.', 'ai-eshop-optimizer' ), 'columns' => array( array( 'key' => 'name', 'label' => __( 'Page the block sits on', 'ai-eshop-optimizer' ), 'type' => 'text', 'suffix' => 'which' ), array( 'key' => 'surface', 'label' => __( 'Surface', 'ai-eshop-optimizer' ), 'type' => 'text' ), array( 'key' => 'n', 'label' => __( 'Conversions', 'ai-eshop-optimizer' ), 'type' => 'integer' ), array( 'key' => 'rev', 'label' => __( 'Revenue', 'ai-eshop-optimizer' ), 'type' => 'money' ), ), ) ); } } private function card_crosstab( $conv, $since, $until ) { global $wpdb; $rows = (array) $wpdb->get_results( $wpdb->prepare( "SELECT IF(utm_source = '', IF(attr_origin='overwritten','(destroyed)','(none / direct)'), utm_source) ext, int_surface, COUNT(*) n, ROUND(SUM(conversion_value),2) rev FROM `{$conv}` WHERE occurred_at BETWEEN %s AND %s AND int_surface <> '' GROUP BY ext, int_surface ORDER BY rev DESC LIMIT 25", $since, $until ), ARRAY_A ); $cross = array(); foreach ( (array) $rows as $r ) { $cross[] = array( 'pair' => $r['ext'] . '|' . $r['int_surface'], 'ext' => (string) $r['ext'], 'destroyed' => ( '(destroyed)' === $r['ext'] ), 'closed' => $this->surface_label( $r['int_surface'] ), 'n' => (int) $r['n'], 'rev' => (float) $r['rev'], ); } AIEO_Table::card( array( 'anchor' => 'promotions-who-brought-them-x-who-closed-them', 'icon' => 'arrow-right-left', 'heading' => 'h2', 'title' => __( 'Who brought them x who closed them', 'ai-eshop-optimizer' ), 'description' => __( 'The pairing the old setup could never show, because the internal tag used to overwrite the external one. Each row is a sentence: this external promotion brought the visitor, and this internal promotion closed the sale.', 'ai-eshop-optimizer' ), 'footnote' => __( '"(destroyed)" means the visitor\'s original campaign was wiped by the legacy internal UTM tagging before 2026-01-13 and cannot be recovered. It is shown rather than silently counted as direct traffic.', 'ai-eshop-optimizer' ), 'col' => 1, 'narrow' => true, 'page_id' => $this->get_id(), 'row_key' => 'pair', 'density' => 'compact', 'rows' => $cross, 'cap' => 25, 'capped' => count( $cross ) >= 25, 'empty' => __( 'Nothing in this period.', 'ai-eshop-optimizer' ), 'row_class' => static function ( $row ) { return ! empty( $row['destroyed'] ) ? 'aieo-t-tone--warning' : ''; }, 'columns' => array( array( 'key' => 'ext', 'label' => __( 'Brought them (external)', 'ai-eshop-optimizer' ), 'type' => 'text' ), array( 'key' => 'closed', 'label' => __( 'Closed them (internal)', 'ai-eshop-optimizer' ), 'type' => 'text' ), array( 'key' => 'n', 'label' => __( 'Orders', 'ai-eshop-optimizer' ), 'type' => 'integer' ), array( 'key' => 'rev', 'label' => __( 'Revenue', 'ai-eshop-optimizer' ), 'type' => 'money' ), ), ) ); } private function card_contributors( $since, $until ) { if ( ! class_exists( 'AIEO_DMM_Contributors' ) ) { return; } $board = AIEO_DMM_Contributors::leaderboard( $since, $until ); $rec = AIEO_DMM_Contributors::reconcile( $since, $until ); $open = AIEO_DMM_Contributors::unassigned( $since, $until, 25 ); $leaders = array(); foreach ( (array) $board as $b ) { $u = get_userdata( (int) $b['user_id'] ); $leaders[] = array( 'user_id' => (int) $b['user_id'], 'name' => $u ? $u->display_name : '#' . (int) $b['user_id'], 'leads' => (int) $b['leads'], 'orders' => (int) $b['orders'], 'revenue' => (float) $b['revenue'], 'campaigns' => (int) $b['campaigns'], 'last_seen' => $b['last_seen'], ); } $aieo_pr_recon = \Rooster\Admin\Ui::notice( '' . esc_html__( 'Reconciliation:', 'ai-eshop-optimizer' ) . ' ' . esc_html( sprintf( __( '%1$s credited + %2$s unassigned + %3$s not creditable = %4$s conversions carrying an external promotion.', 'ai-eshop-optimizer' ), number_format_i18n( $rec['credited'] ), number_format_i18n( $rec['unassigned'] ), number_format_i18n( $rec['rejected'] ), number_format_i18n( $rec['total'] ) ) ), $rec['balanced'] ? 'success' : 'destructive', array( 'inline' => true ) ); list( $aieo_pr_open, $aieo_pr_close ) = AIEO_DMM_UI::card( array( 'anchor' => 'promotions-contributors-external-partners', 'icon' => 'users-round', 'title' => __( 'Contributors (external partners)', 'ai-eshop-optimizer' ), 'description' => __( 'A contributor is a person or partner whose campaign brings you business. Nobody enrols and no keys are issued: you point an existing campaign value at a WordPress user, and all of that value\'s history credits to them immediately. Reporting only, no commissions or payouts.', 'ai-eshop-optimizer' ), 'flush' => true, 'footer' => '