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-report-core.php

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

248 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- File naming is acceptable
2 /**
3 * Generates and returns reports.
4 *
5 * @package WP_Analytify
6 * @since 1.0.0
7 */
8
9 // Exit if accessed directly.
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- File name follows project convention
15 /**
16 * Report class.
17 *
18 * @package WP_Analytify
19 * @since 1.0.0
20 */
21 class Analytify_Report extends Analytify_Report_Abstract {
22
23 /**
24 * Hold numbers of general stats only.
25 * Can be used for generating footers.
26 *
27 * @var mixed
28 */
29 private $general_stats_num = null;
30
31 /**
32 * Undocumented function
33 *
34 * @return array<string, mixed>
35 */
36 public function get_general_stats(): array {
37
38 $cache_key = $this->cache_key( 'general-stats' );
39 $device_cache_key = $this->cache_key( 'device-stats' );
40
41 return $this->general_stats_ga4( $cache_key, $device_cache_key );
42 }
43
44 /**
45 * Generates browser stats - GA4.
46 *
47 * @version 8.2.0
48 * @param string $cache_key Cache key.
49 * @param string $device_cache_key Device cache key.
50 * @return array<string, mixed>
51 */
52 protected function general_stats_ga4( $cache_key, $device_cache_key ): array {
53
54 $boxes = $this->general_stats_boxes();
55 $send_email_total_time = $this->total_time_for_send_email();
56 unset( $boxes['new_sessions'] );
57 $new_vs_returning_boxes = $this->new_vs_returning();
58 $device_visitors_boxes = $this->visitor_devices();
59 $dimensions = array();
60 $device_dimensions = array(
61 'deviceCategory',
62 );
63 $filters = array();
64 $raw = $this->wp_analytify && method_exists( $this->wp_analytify, 'get_reports' ) ? $this->wp_analytify->get_reports(
65 $cache_key,
66 array(
67 'sessions',
68 'totalUsers',
69 'screenPageViews',
70 'bounceRate',
71 'screenPageViewsPerSession',
72 'engagedSessions',
73 'userEngagementDuration',
74 'newUsers',
75 'activeUsers',
76 'averageSessionDuration',
77 ),
78 $this->get_dates(),
79 $this->attach_post_url_dimension( $dimensions ),
80 array(),
81 array(
82 'logic' => 'AND',
83 'filters' => $this->attach_post_url_filter( $filters ),
84 )
85 ) : array();
86 if ( isset( $raw['aggregations']['sessions'] ) ) {
87 $boxes['sessions']['value'] = WPANALYTIFY_Utils::pretty_numbers( $raw['aggregations']['sessions'] );
88 $general_stats_num['sessions'] = $raw['aggregations']['sessions'];
89 }
90 if ( isset( $raw['aggregations']['totalUsers'] ) ) {
91 $boxes['visitors']['value'] = WPANALYTIFY_Utils::pretty_numbers( $raw['aggregations']['totalUsers'] );
92 $general_stats_num['visitors'] = $raw['aggregations']['totalUsers'];
93 }
94 if ( isset( $raw['aggregations']['screenPageViews'] ) ) {
95 $boxes['page_views']['value'] = WPANALYTIFY_Utils::pretty_numbers( $raw['aggregations']['screenPageViews'] );
96 $general_stats_num['page_views'] = $raw['aggregations']['screenPageViews'];
97 }
98 if ( isset( $raw['aggregations']['averageSessionDuration'] ) ) {
99 $boxes['avg_time_on_page']['value'] = WPANALYTIFY_Utils::pretty_time( $raw['aggregations']['averageSessionDuration'] );
100 $general_stats_num['avg_time_on_page'] = $raw['aggregations']['averageSessionDuration'];
101 }
102 if ( isset( $raw['aggregations']['bounceRate'] ) ) {
103 $boxes['bounce_rate']['value'] = WPANALYTIFY_Utils::fraction_to_percentage( $raw['aggregations']['bounceRate'] );
104 $general_stats_num['bounce_rate'] = $raw['aggregations']['bounceRate'];
105 }
106 if ( isset( $raw['aggregations']['screenPageViewsPerSession'] ) ) {
107 $boxes['view_per_session']['value'] = WPANALYTIFY_Utils::pretty_numbers( $raw['aggregations']['screenPageViewsPerSession'] );
108 $general_stats_num['view_per_session'] = $raw['aggregations']['screenPageViewsPerSession'];
109 }
110
111 if ( isset( $raw['aggregations']['engagedSessions'] ) ) {
112 $boxes['engaged_sessions']['value'] = WPANALYTIFY_Utils::pretty_numbers( $raw['aggregations']['engagedSessions'] );
113 $general_stats_num['engaged_sessions'] = $raw['aggregations']['engagedSessions'];
114 }
115
116 if ( isset( $raw['aggregations']['newUsers'] ) ) {
117 $new_vs_returning_boxes['new_vs_returning_visitors']['stats']['new']['number'] = WPANALYTIFY_Utils::pretty_numbers( $raw['aggregations']['newUsers'] );
118 }
119 if ( isset( $raw['aggregations']['activeUsers'] ) ) {
120 $new_vs_returning_boxes['new_vs_returning_visitors']['stats']['returning']['number'] = WPANALYTIFY_Utils::pretty_numbers( $raw['aggregations']['activeUsers'] );
121 }
122
123 $device_stats = $this->wp_analytify && method_exists( $this->wp_analytify, 'get_reports' ) ? $this->wp_analytify->get_reports(
124 $device_cache_key,
125 array( 'sessions' ),
126 $this->get_dates(),
127 $this->attach_post_url_dimension( $device_dimensions ),
128 array(),
129 array(
130 'logic' => 'AND',
131 'filters' => $this->attach_post_url_filter( $filters ),
132 )
133 ) : array();
134 if ( isset( $device_stats['rows'] ) && $device_stats['rows'] ) {
135 foreach ( $device_stats['rows'] as $device ) {
136 $device_visitors_boxes['visitor_devices']['stats'][ $device['deviceCategory'] ]['number'] = $device['sessions'];
137 }
138 }
139
140 if ( isset( $raw['aggregations']['userEngagementDuration'] ) ) {
141 $send_email_total_time['total_time']['value'] = WPANALYTIFY_Utils::pretty_time( $raw['aggregations']['userEngagementDuration'] );
142 }
143
144 return array(
145 'boxes' => $boxes,
146 'new_vs_returning_boxes' => $new_vs_returning_boxes,
147 'device_visitors_boxes' => $device_visitors_boxes,
148 'total_time_spent' => $send_email_total_time,
149 );
150 }
151
152 /**
153 * Returns the simple stats for general stats.
154 * This is intended to be used for the footer or in some calculation.
155 *
156 * @return array<string, mixed>|null
157 */
158 public function get_general_stats_num() {
159 return $this->general_stats_num;
160 }
161
162 /**
163 * Returns scroll depth stats.
164 *
165 * @return array<string, mixed>
166 */
167 public function get_scroll_depth_stats(): array {
168
169 $cache_key = $this->cache_key( 'scroll-depth' );
170
171 if ( $this->is_ga4 ) {
172 return $this->scroll_depth_ga4( $cache_key );
173 }
174
175 return array();
176 }
177
178 /**
179 * Generates scroll depth stats - GA4.
180 *
181 * @param string $cache_key Cache key.
182 * @return array<string, mixed>
183 */
184 protected function scroll_depth_ga4( $cache_key ): array {
185
186 $stats = array();
187
188 $dimensions = array(
189 'customEvent:wpa_category',
190 'customEvent:wpa_percentage',
191 );
192 $filters = array(
193 array(
194 'type' => 'dimension',
195 'name' => 'customEvent:wpa_category',
196 'match_type' => 1,
197 'value' => 'Analytify Scroll Depth',
198 ),
199 array(
200 'type' => 'dimension',
201 'name' => 'customEvent:wpa_percentage',
202 'match_type' => 4,
203 'value' => '(not set)',
204 'not_expression' => true,
205 ),
206 );
207
208 $raw = $this->wp_analytify && method_exists( $this->wp_analytify, 'get_reports' ) ? $this->wp_analytify->get_reports(
209 $cache_key,
210 array(
211 'eventCount',
212 ),
213 $this->get_dates(),
214 $this->attach_post_url_dimension( $dimensions ),
215 array(),
216 array(
217 'logic' => 'AND',
218 'filters' => $this->attach_post_url_filter( $filters ),
219 )
220 ) : array();
221
222 $total = 1;
223 if ( isset( $raw['aggregations']['eventCount'] ) && $raw['aggregations']['eventCount'] > 0 ) {
224 $total = $raw['aggregations']['eventCount'];
225 }
226
227 if ( isset( $raw['rows'] ) && $raw['rows'] ) {
228 foreach ( $raw['rows'] as $row ) {
229 if ( 'csv' === $this->dashboard_type ) {
230 $single_stat['percentage'] = $row['customEvent:wpa_percentage'] . esc_html__( '%', 'wp-analytify' );
231 } else {
232 $bar = is_numeric( $row['eventCount'] ) ? round( ( $row['eventCount'] / $total ) * 100 ) : 0;
233
234 $single_stat['percentage'] = esc_html( $row['customEvent:wpa_percentage'] ) . esc_html__( '%', 'wp-analytify' );
235 $single_stat['percentage'] .= '<span class="analytify_bar_graph"><span style="width:' . $bar . '%"></span></span>';
236 }
237 $single_stat['events'] = esc_html( $row['eventCount'] );
238
239 $stats[] = $single_stat;
240 }
241 }
242
243 return array(
244 'stats' => $stats,
245 );
246 }
247 }
248