Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.21% covered (warning)
84.21%
32 / 38
85.71% covered (warning)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Conversion_Rate_Over_Time_Controller
83.78% covered (warning)
83.78%
31 / 37
85.71% covered (warning)
85.71%
6 / 7
8.27
0.00% covered (danger)
0.00%
0 / 1
 get_report_key
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_report_label
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_data_endpoint
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_column_headers
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 get_default_values
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 get_fields
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 format_row_for_csv
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * REST API Reports Conversion Rate Over Time controller class.
4 *
5 * @package Automattic\Jetpack\PremiumAnalytics\Reports\Export\Exports
6 */
7
8declare( strict_types=1 );
9
10namespace Automattic\Jetpack\PremiumAnalytics\Reports\Export\Exports;
11
12defined( 'ABSPATH' ) || exit;
13
14use Automattic\Jetpack\PremiumAnalytics\Reports\Export\Abstract_Csv_Report_Controller;
15
16/**
17 * Conversion Rate Over Time CSV Export Controller.
18 *
19 * Handles CSV exports for the Conversion Rate Over Time report, supporting both
20 * single interval and comparison interval data.
21 *
22 * @since $$next-version$$
23 */
24class Conversion_Rate_Over_Time_Controller extends Abstract_Csv_Report_Controller {
25
26    /**
27     * Get the report key for this controller.
28     *
29     * @return string The report key.
30     */
31    public function get_report_key(): string {
32        return 'conversionrateovertime';
33    }
34
35    /**
36     * Get the report label for this controller.
37     *
38     * @return string The report label.
39     */
40    public function get_report_label(): string {
41        return __( 'Conversion Rate Over Time', 'jetpack-premium-analytics' );
42    }
43
44    /**
45     * Get the data endpoint for this controller.
46     *
47     * @return string The data endpoint.
48     */
49    public function get_data_endpoint(): string {
50        return 'reports/sessions/by-conversion-rate';
51    }
52
53    /**
54     * Get the column headers for this controller.
55     *
56     * @param string|null $interval Optional time interval for dynamic headers.
57     * @return array The column headers.
58     */
59    public function get_column_headers( ?string $interval = null ): array {
60        return array(
61            'time_interval'         => $this->get_interval_label( $interval ),
62            'sessions'              => __( 'Sessions', 'jetpack-premium-analytics' ),
63            'cart'                  => __( 'Cart', 'jetpack-premium-analytics' ),
64            'checkout'              => __( 'Checkout', 'jetpack-premium-analytics' ),
65            'purchase'              => __( 'Purchase', 'jetpack-premium-analytics' ),
66            'store_conversion_rate' => __( 'Store conversion rate', 'jetpack-premium-analytics' ),
67        );
68    }
69
70    /**
71     * Get default values for missing data fields.
72     *
73     * @return array Array of field_name => default_value pairs.
74     */
75    public function get_default_values(): array {
76        return array(
77            'active_sessions'    => 0,
78            'with_cart_addition' => 0,
79            'reached_checkout'   => 0,
80            'completed_checkout' => 0,
81        );
82    }
83
84    /**
85     * Get the list of API fields needed for this report.
86     *
87     * @return array
88     */
89    public function get_fields(): array {
90        // time_interval, date_start, and date_end are always returned by the
91        // API for time-series endpoints and do not need to be requested.
92        return array(
93            'active_sessions',
94            'with_cart_addition',
95            'reached_checkout',
96            'completed_checkout',
97        );
98    }
99
100    /**
101     * Format a row for CSV export.
102     *
103     * @param array       $item     The row data.
104     * @param string|null $interval Optional time interval for formatting.
105     * @return array The formatted row.
106     */
107    public function format_row_for_csv( array $item, ?string $interval = null ): array {
108        $defaults = $this->get_default_values();
109
110        $active_sessions    = (int) ( $item['active_sessions'] ?? $defaults['active_sessions'] );
111        $with_cart_addition = (int) ( $item['with_cart_addition'] ?? $defaults['with_cart_addition'] );
112        $reached_checkout   = (int) ( $item['reached_checkout'] ?? $defaults['reached_checkout'] );
113        $completed_checkout = (int) ( $item['completed_checkout'] ?? $defaults['completed_checkout'] );
114
115        $store_conversion_rate = $active_sessions > 0 ? ( $completed_checkout / $active_sessions ) * 100 : 0;
116
117        return array(
118            'time_interval'         => $this->format_time_interval( $item, $interval ),
119            'sessions'              => $active_sessions,
120            'cart'                  => $with_cart_addition,
121            'checkout'              => $reached_checkout,
122            'purchase'              => $completed_checkout,
123            'store_conversion_rate' => number_format( $store_conversion_rate, 2, '.', '' ) . '%',
124        );
125    }
126}