PluginProbe
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) / trunk
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) vtrunk
9.1.2 9.1.1 9.1.0 9.0.2 9.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 All 153 releases
wp-analytify / classes / analytify-rest / endpoints-content.php

endpoints-content.php in Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) trunk, at classes/analytify-rest/endpoints-content.php

223 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Analytify REST Endpoints Content Trait
4 *
5 * This trait provides content-specific analytics endpoints for the Analytify REST API.
6 * It was created to separate content analytics functionality from the main REST class,
7 * offering endpoints for single post statistics and content performance analysis.
8 *
9 * PURPOSE:
10 * - Provides content analytics endpoints
11 * - Handles single post statistics
12 * - Manages content performance metrics
13 * - Offers content engagement analysis
14 *
15 * @package WP_Analytify
16 * @subpackage REST_API
17 * @since 8.0.0
18 */
19
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit; }
22
23 trait Analytify_Rest_Endpoints_Content {
24
25 /**
26 * Get top pages statistics
27 *
28 * Retrieves analytics data for the most visited pages on the website,
29 * including pageviews, unique pageviews, and bounce rates.
30 *
31 * @version 9.1.1
32 * @return array<string, mixed> Top pages statistics with performance metrics
33 */
34 private function top_pages_stats() {
35 $api_limit = apply_filters( 'analytify_api_limit_top_pages_stats', 100, 'dashboard' );
36 $site_url = $this->get_profile_info( 'website_url' );
37 if ( empty( $site_url ) ) {
38 $site_url = home_url();
39 }
40 $site_url = untrailingslashit( (string) $site_url );
41 $stats = array();
42 $stats_raw = $this->wp_analytify->get_reports(
43 'show-default-top-pages-dashboard',
44 array( 'screenPageViews', 'averageSessionDuration', 'bounceRate' ),
45 $this->get_dates(),
46 array( 'pageTitle', 'pagePath' ),
47 array(
48 'type' => 'metric',
49 'name' => 'screenPageViews',
50 'order' => 'desc',
51 ),
52 array(
53 'logic' => 'AND',
54 'filters' => array(
55 array(
56 'type' => 'dimension',
57 'name' => 'pageTitle',
58 'match_type' => 4,
59 'value' => '(not set)',
60 'not_expression' => true,
61 ),
62 array(
63 'type' => 'dimension',
64 'name' => 'pagePath',
65 'match_type' => 4,
66 'value' => '(not set)',
67 'not_expression' => true,
68 ),
69 ),
70 ),
71 $api_limit
72 );
73 if ( $stats_raw['rows'] ) {
74 foreach ( $stats_raw['rows'] as $row ) {
75 $views = $row['screenPageViews'] ? WPANALYTIFY_Utils::pretty_numbers( $row['screenPageViews'] ) : 0;
76 if ( $views < 1 ) {
77 continue; }
78 $page_path = isset( $row['pagePath'] ) ? (string) $row['pagePath'] : '';
79 if ( '' !== $page_path && '/' !== $page_path[0] ) {
80 $page_path = '/' . $page_path;
81 }
82 $page_url = esc_url( $site_url . $page_path );
83 $stats[] = array(
84 'no' => null,
85 'pageTitle' => '<a href="' . $page_url . '" target="_blank" rel="noopener noreferrer">' . esc_html( $row['pageTitle'] ) . '</a>',
86 'screenPageViews' => $views,
87 'userEngagementDuration' => $row['averageSessionDuration'] ? WPANALYTIFY_Utils::pretty_time( $row['averageSessionDuration'] ) : 0,
88 'bounceRate' => $row['bounceRate'] ? WPANALYTIFY_Utils::fraction_to_percentage( $row['bounceRate'] ) . '%' : 0,
89 );
90 }
91 }
92 return array(
93 'success' => true,
94 'headers' => array(
95 'no' => array(
96 'label' => esc_html__( '#', 'wp-analytify' ),
97 'type' => 'counter',
98 'th_class' => 'analytify_num_row',
99 'td_class' => 'analytify_txt_center',
100 ),
101 'pageTitle' => array(
102 'label' => esc_html__( 'Title', 'wp-analytify' ),
103 'th_class' => 'analytify_txt_left',
104 'td_class' => '',
105 ),
106 'screenPageViews' => array(
107 'label' => esc_html__( 'Views', 'wp-analytify' ),
108 'th_class' => 'analytify_value_row',
109 'td_class' => 'analytify_txt_center analytify_value_row',
110 ),
111 'userEngagementDuration' => array(
112 'label' => esc_html__( 'Avg. Time', 'wp-analytify' ),
113 'th_class' => 'analytify_value_row',
114 'td_class' => 'analytify_txt_center analytify_value_row',
115 ),
116 'bounceRate' => array(
117 'label' => esc_html__( 'Bounce Rate', 'wp-analytify' ),
118 'th_class' => 'analytify_value_row',
119 'td_class' => 'analytify_txt_center analytify_value_row',
120 ),
121 ),
122 'stats' => $stats,
123 'pagination' => true,
124 'footer' => apply_filters( 'analytify_top_pages_footer', __( 'Top pages and posts.', 'wp-analytify' ), array( $this->start_date, $this->end_date ) ),
125 );
126 }
127
128 /**
129 * Get single post analytics statistics
130 *
131 * Retrieves detailed analytics data for a specific post or page,
132 * including pageviews, time on page, and user engagement metrics.
133 *
134 * @return array<string, mixed> Single post statistics with section data
135 */
136 private function get_single_post_stats() {
137 $sections = array();
138
139 /**
140 * Sections added by the Core:
141 * 'General Statistics', 'Scroll Depth Reach'.
142 *
143 * Section added by Pro:
144 * 'Geographic', 'System Stats', 'How people are finding you (keywords)',
145 * 'Social Media', 'Top Referrers', 'What's happening when users come to your page'.
146 *
147 * More sections, can be added via the filter.
148 */
149 $sections = apply_filters( 'analytify_single_post_sections', $sections, $this->post_id, array( $this->start_date, $this->end_date ) );
150
151 if ( ! $sections || ! is_array( $sections ) ) {
152 $sections['success'] = false;
153 $sections['message'] = esc_html__( 'No sections found.', 'wp-analytify' );
154 } else {
155 $sections['success'] = true;
156 $start_timestamp = strtotime( $this->start_date );
157 $end_timestamp = strtotime( $this->end_date );
158 // translators: Analytics display - %1$s is start date, %2$s is end date.
159 $sections['heading'] = sprintf( esc_html__( 'Displaying Analytics of this page from %1$s to %2$s.', 'wp-analytify' ), wp_date( 'jS F, Y', $start_timestamp ? $start_timestamp : time() ), wp_date( 'jS F, Y', $end_timestamp ? $end_timestamp : time() ) );
160 }
161
162 return $sections;
163 }
164
165 /**
166 * Get single post sections for analytics display
167 *
168 * Processes and formats analytics sections for single post display,
169 * including general statistics and scroll depth metrics.
170 *
171 * @param array $sections Array of sections to process.
172 * @param int $post_id Post ID for analytics.
173 * @param array $date Start and end dates.
174 * @return array Processed sections with analytics data
175 */
176 public function single_post_sections( $sections, $post_id, $date ) {
177 $show_settings = $this->wp_analytify->settings->get_option( 'show_panels_back_end', 'wp-analytify-admin', array( 'show-overall-dashboard' ) );
178 if ( empty( $show_settings ) || ( ! in_array( 'show-overall-dashboard', $show_settings, true ) && ! in_array( 'show-scroll-depth-stats', $show_settings, true ) ) ) {
179 return $sections;
180 }
181 $report = new Analytify_Report(
182 array(
183 'dashboard_type' => 'single_post',
184 'start_date' => $date[0],
185 'end_date' => $date[1],
186 'post_id' => $post_id,
187 )
188 );
189 if ( in_array( 'show-overall-dashboard', $show_settings, true ) ) {
190 $general_stats = $report->get_general_stats();
191 $sections['general_stats'] = array(
192 'title' => esc_html__( 'General Statistics', 'wp-analytify' ),
193 'type' => 'boxes',
194 'stats' => $general_stats['boxes'],
195 'new_vs_returning' => $general_stats['new_vs_returning_boxes'],
196 'device_visitors' => $general_stats['device_visitors_boxes'],
197 );
198 }
199 if ( in_array( 'show-scroll-depth-stats', $show_settings, true ) && 'on' === $this->wp_analytify->settings->get_option( 'depth_percentage', 'wp-analytify-advanced' ) ) {
200 $scroll_depth_stats = $report->get_scroll_depth_stats();
201 $sections['scroll_depth'] = array(
202 'title' => esc_html__( 'Scroll Depth Reach', 'wp-analytify' ),
203 'type' => 'table',
204 'table_class' => 'analytify_bar_tables',
205 'headers' => array(
206 'percentage' => array(
207 'label' => esc_html__( 'Scroll Percentage', 'wp-analytify' ),
208 'th_class' => 'analytify_txt_left',
209 'td_class' => '',
210 ),
211 'events' => array(
212 'label' => esc_html__( 'Total Reached', 'wp-analytify' ),
213 'th_class' => '',
214 'td_class' => 'analytify_txt_center analytify_value_row',
215 ),
216 ),
217 'stats' => $scroll_depth_stats['stats'],
218 );
219 }
220 return $sections;
221 }
222 }
223