false );
$summary = isset( $data['summary'] ) ? $data['summary'] : null;
$nonce = isset( $data['nonce'] ) ? (string) $data['nonce'] : '';
$fmt_dt = static function ( $v ) {
return $v ? get_date_from_gmt( (string) $v ) : '—';
};
?>
'session-analytics-overview-2',
'icon' => 'hash',
'title' => __( 'Session tracking totals', 'ai-eshop-optimizer' ),
'description' => __( 'Product AND content engagement + conversion derived from on-site session tracking. Rebuilt nightly; click Rebuild to refresh both now.', 'ai-eshop-optimizer' ),
) );
echo $aieo_sa_open;
$aieo_sa_job = \Rooster\Admin\States::job( array(
'id' => 'aieo-sa-job',
'state' => 'idle',
'run' => __( 'Rebuild now', 'ai-eshop-optimizer' ),
'run_attrs' => array( 'id' => 'aieo-sa-rebuild' ),
) );
echo $aieo_sa_job;
echo \Rooster\Admin\Guidance::text( __( 'Sessions tracked, raw events and journey rows currently stored, how many products have metrics, and when those metrics were last updated.', 'ai-eshop-optimizer' ) );
if ( $summary ) {
?>
total_sessions ?? 0 ) ) );
echo \Rooster\Admin\Ui::stat( __( 'Raw events', 'ai-eshop-optimizer' ), number_format_i18n( (int) ( $summary->total_events ?? 0 ) ) );
echo \Rooster\Admin\Ui::stat( __( 'Journey rows', 'ai-eshop-optimizer' ), number_format_i18n( (int) ( $summary->total_journey_records ?? 0 ) ) );
echo \Rooster\Admin\Ui::stat( __( 'Products with metrics', 'ai-eshop-optimizer' ), number_format_i18n( (int) ( $summary->products_with_metrics ?? 0 ) ) );
echo \Rooster\Admin\Ui::stat( __( 'Metrics last updated', 'ai-eshop-optimizer' ), $fmt_dt( $summary->metrics_last_updated ?? null ) );
?>
__( 'No analytics yet — click Rebuild now (needs tracked sessions).', 'ai-eshop-optimizer' ) ) );
}
echo $aieo_sa_close;
$sections = array(
array( __( 'Top converters (view → cart)', 'ai-eshop-optimizer' ), isset( $data['conversion'] ) ? $data['conversion'] : array(), 'conversion', 'session-analytics-top-converters-view-cart', 'trending-up' ),
array( __( 'Most engaging products', 'ai-eshop-optimizer' ), isset( $data['engagement'] ) ? $data['engagement'] : array(), 'engagement', 'session-analytics-most-engaging-products', 'activity' ),
array( __( 'Lowest bounce', 'ai-eshop-optimizer' ), isset( $data['bounce'] ) ? $data['bounce'] : array(), 'bounce', 'session-analytics-lowest-bounce', 'door-open' ),
);
$aieo_sa_descs = array(
'conversion' => __( 'Lists the products whose viewers most often add them to the cart, with views, add-to-cart clicks and the conversion rate.', 'ai-eshop-optimizer' ),
'engagement' => __( 'Ranks product pages by how long shoppers stay and how far they scroll, so you can see which pages hold their attention.', 'ai-eshop-optimizer' ),
'bounce' => __( 'Lists the products whose page, when a visit starts there, most often keeps shoppers browsing instead of leaving.', 'ai-eshop-optimizer' ),
);
foreach ( $sections as $sec ) :
list( $title, $rows, $type, $aieo_sa_anchor, $aieo_sa_icon ) = $sec;
$export_url = add_query_arg(
array(
'action' => 'aieo_session_analytics_export',
'type' => $type,
'_wpnonce' => $nonce,
),
admin_url( 'admin-ajax.php' )
);
$aieo_sa_rows = array();
foreach ( $rows as $r ) {
$pid = (int) ( $r->product_id ?? 0 );
$name = ! empty( $r->product_name ) ? (string) $r->product_name : ( '#' . $pid );
$one = array(
'product_id' => $pid,
'product' => $name,
'permalink' => $pid ? (string) get_permalink( $pid ) : '',
'views' => (int) ( $r->total_views ?? 0 ),
);
if ( 'conversion' === $type ) {
$one['add_to_cart'] = (int) ( $r->total_add_to_cart ?? 0 );
$one['conv_rate'] = (float) ( $r->conversion_rate_pct ?? 0 );
} elseif ( 'engagement' === $type ) {
$one['engagement'] = (float) ( $r->engagement_score ?? 0 );
$one['avg_sec'] = (float) ( $r->avg_seconds_on_page ?? 0 );
$one['avg_scroll'] = (float) ( $r->avg_scroll_pct ?? 0 );
} else {
$one['landings'] = (int) ( $r->landing_count ?? 0 );
$one['bounces'] = (int) ( $r->bounce_count ?? 0 );
$one['bounce_rate'] = (float) ( $r->bounce_rate_pct ?? 0 );
}
$aieo_sa_rows[] = $one;
}
$aieo_sa_cols = array(
array(
'key' => 'product',
'label' => __( 'Product', 'ai-eshop-optimizer' ),
'type' => 'link',
'format' => array( 'href' => 'permalink', 'external' => true ),
),
array( 'key' => 'views', 'label' => __( 'Views', 'ai-eshop-optimizer' ), 'type' => 'integer' ),
);
if ( 'conversion' === $type ) {
$aieo_sa_cols[] = array( 'key' => 'add_to_cart', 'label' => __( 'Add-to-cart', 'ai-eshop-optimizer' ), 'type' => 'integer' );
$aieo_sa_cols[] = array( 'key' => 'conv_rate', 'label' => __( 'Conv. rate %', 'ai-eshop-optimizer' ), 'type' => 'decimal', 'format' => array( 'precision' => 2 ) );
} elseif ( 'engagement' === $type ) {
$aieo_sa_cols[] = array( 'key' => 'engagement', 'label' => __( 'Engagement', 'ai-eshop-optimizer' ), 'type' => 'decimal', 'format' => array( 'precision' => 2 ) );
$aieo_sa_cols[] = array( 'key' => 'avg_sec', 'label' => __( 'Avg sec', 'ai-eshop-optimizer' ), 'type' => 'decimal', 'format' => array( 'precision' => 1 ) );
$aieo_sa_cols[] = array( 'key' => 'avg_scroll', 'label' => __( 'Avg scroll %', 'ai-eshop-optimizer' ), 'type' => 'decimal', 'format' => array( 'precision' => 1 ) );
} else {
$aieo_sa_cols[] = array( 'key' => 'landings', 'label' => __( 'Landings', 'ai-eshop-optimizer' ), 'type' => 'integer' );
$aieo_sa_cols[] = array( 'key' => 'bounces', 'label' => __( 'Bounces', 'ai-eshop-optimizer' ), 'type' => 'integer' );
$aieo_sa_cols[] = array( 'key' => 'bounce_rate', 'label' => __( 'Bounce %', 'ai-eshop-optimizer' ), 'type' => 'decimal', 'format' => array( 'precision' => 2 ) );
}
AIEO_Table::card(
array(
'anchor' => $aieo_sa_anchor,
'icon' => $aieo_sa_icon,
'heading' => 'h2',
'title' => $title,
'description' => isset( $aieo_sa_descs[ $type ] ) ? $aieo_sa_descs[ $type ] : '',
'toolbar' => ! empty( $rows ) ? AIEO_Kit::button( __( 'Export XLSX', 'ai-eshop-optimizer' ), array( 'href' => $export_url, 'size' => 'sm', 'icon' => 'download' ) ) : '',
'page_id' => 'session_analytics',
'row_key' => 'product_id',
'rows' => $aieo_sa_rows,
'caps' => array( 'search' => true ),
'per_page' => 0,
'cap' => 20,
'capped' => count( $aieo_sa_rows ) >= 20,
'empty' => __( 'No data yet (rankings need ≥10 views per product for significance).', 'ai-eshop-optimizer' ),
'columns' => $aieo_sa_cols,
)
);
endforeach;
$aieo_content_secs = array(
'engagement' => array( __( 'Top content by engagement', 'ai-eshop-optimizer' ), 'session-analytics-content-session-analytics', 'activity' ),
'demand' => array( __( 'Top content by demand', 'ai-eshop-optimizer' ), 'session-analytics-top-content-by-demand', 'chart-column' ),
'goals' => array( __( 'Top content by goal conversions', 'ai-eshop-optimizer' ), 'session-analytics-top-content-by-goal-conversions', 'target' ),
);
if ( empty( $data['content'] ) ) {
list( $aieo_sa_open, $aieo_sa_close ) = AIEO_DMM_UI::card( array(
'anchor' => 'session-analytics-content-session-analytics',
'icon' => 'file-pen-line',
'title' => __( 'Content sessions', 'ai-eshop-optimizer' ),
'description' => __( 'Ranks posts, pages and other content by engagement, demand and goal conversions over the last 30 days, from tracked page views.', 'ai-eshop-optimizer' ),
) );
echo $aieo_sa_open;
echo \Rooster\Admin\States::state( 'not-run', array( 'title' => __( 'No content vitals yet - they build from tracked page views (nightly, or click Rebuild now above). Once built: top content by engagement, demand and goal conversions, for every post type.', 'ai-eshop-optimizer' ) ) );
echo $aieo_sa_close;
} else {
if ( function_exists( 'rooster_admin_band_open' ) ) {
rooster_admin_band_open( array(
'title' => __( 'Content sessions', 'ai-eshop-optimizer' ),
'description' => __( 'Posts, pages and custom types — engagement, demand and goal conversions from the daily content vitals (rebuilt nightly from the same session stream).', 'ai-eshop-optimizer' ),
'cols' => 1,
) );
}
foreach ( $aieo_content_secs as $aieo_ck => $aieo_cs ) {
if ( empty( $data['content'][ $aieo_ck ] ) ) { continue; }
$aieo_c_rows = array();
foreach ( $data['content'][ $aieo_ck ] as $aieo_r ) {
$aieo_cid = (int) $aieo_r['content_id'];
$aieo_c_rows[] = array(
'content_id' => $aieo_cid,
'title' => '' !== (string) $aieo_r['title'] ? (string) $aieo_r['title'] : ( '#' . $aieo_cid ),
'permalink' => (string) get_permalink( $aieo_cid ),
'post_type' => (string) $aieo_r['post_type'],
'visitors' => (int) $aieo_r['visitors_30d'],
'engagement' => (float) $aieo_r['engagement_score'],
'goals' => (int) $aieo_r['goals_30d'],
'goal_rate' => 100 * (float) $aieo_r['goal_rate_30d'],
'demand' => (int) $aieo_r['content_demand'],
);
}
AIEO_Table::card(
array(
'anchor' => $aieo_cs[1],
'icon' => $aieo_cs[2],
'heading' => 'h2',
'title' => $aieo_cs[0],
'page_id' => 'session_analytics',
'row_key' => 'content_id',
'rows' => $aieo_c_rows,
'cap' => 20,
'capped' => count( $aieo_c_rows ) >= 20,
'empty' => __( 'No content vitals yet.', 'ai-eshop-optimizer' ),
'columns' => array(
array(
'key' => 'title',
'label' => __( 'Content', 'ai-eshop-optimizer' ),
'type' => 'link',
'format' => array( 'href' => 'permalink', 'external' => true ),
),
array( 'key' => 'post_type', 'label' => __( 'Type', 'ai-eshop-optimizer' ), 'type' => 'code' ),
array( 'key' => 'visitors', 'label' => __( 'Visitors 30d', 'ai-eshop-optimizer' ), 'type' => 'integer' ),
array( 'key' => 'engagement', 'label' => __( 'Engagement', 'ai-eshop-optimizer' ), 'type' => 'decimal', 'format' => array( 'precision' => 2 ) ),
array( 'key' => 'goals', 'label' => __( 'Goals 30d', 'ai-eshop-optimizer' ), 'type' => 'integer' ),
array( 'key' => 'goal_rate', 'label' => __( 'Goal rate %', 'ai-eshop-optimizer' ), 'type' => 'decimal', 'format' => array( 'precision' => 2 ) ),
array( 'key' => 'demand', 'label' => __( 'Demand', 'ai-eshop-optimizer' ), 'type' => 'integer' ),
),
)
);
}
if ( function_exists( 'rooster_admin_band_close' ) ) {
rooster_admin_band_close();
}
}
?>