sidebar
2 months ago
appearance.php
2 months ago
date-custom-range-filter.php
2 months ago
demo-popup.php
2 months ago
demographic-chart.php
2 months ago
demographic.php
2 months ago
exclusions.php
2 months ago
ga4-dashboard.php
2 months ago
ga4-demographic-chart.php
2 months ago
gdpr-config.php
2 months ago
gdpr.php
2 months ago
purposes.php
2 months ago
ga4-dashboard.php
387 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | $ga_stats = new Ga_Stats; |
| 7 | |
| 8 | $ga4_demo_enabled = get_option( 'googleanalytics-ga4-demo' ); |
| 9 | $ga4_demo_enabled = 'on' === $ga4_demo_enabled || '1' === $ga4_demo_enabled; |
| 10 | $page_list_count_data = array(); |
| 11 | $gender_count_data = array(); |
| 12 | $age_count_data = array(); |
| 13 | $page_session_count_data = array(); |
| 14 | $user_count_data = array(); |
| 15 | $ga4_demo_device_data = array(); |
| 16 | |
| 17 | $from = false === empty( $date_range['from'] ) ? $date_range['from'] : '8daysAgo'; |
| 18 | $to = false === empty( $date_range['to'] ) ? $date_range['to'] : 'today'; |
| 19 | |
| 20 | $response = $ga_stats->ga4_run_report_rest( |
| 21 | $ga4_property, |
| 22 | array( |
| 23 | 'dateRanges' => array( |
| 24 | array( |
| 25 | 'startDate' => $from, |
| 26 | 'endDate' => $to, |
| 27 | ), |
| 28 | ), |
| 29 | 'dimensions' => array( |
| 30 | array( 'name' => 'date' ), |
| 31 | ), |
| 32 | 'metrics' => array( |
| 33 | array( 'name' => 'screenPageViews' ), |
| 34 | ), |
| 35 | 'orderBys' => array( |
| 36 | array( |
| 37 | 'dimension' => array( |
| 38 | 'dimensionName' => 'date', |
| 39 | 'orderType' => 'ALPHANUMERIC', |
| 40 | ), |
| 41 | 'desc' => false, |
| 42 | ), |
| 43 | ), |
| 44 | ) |
| 45 | ); |
| 46 | |
| 47 | $page_list_response = $ga_stats->ga4_run_report_rest( |
| 48 | $ga4_property, |
| 49 | array( |
| 50 | 'dateRanges' => array( |
| 51 | array( |
| 52 | 'startDate' => $from, |
| 53 | 'endDate' => $to, |
| 54 | ), |
| 55 | ), |
| 56 | 'dimensions' => array( |
| 57 | array( 'name' => 'landingPage' ), |
| 58 | ), |
| 59 | 'metrics' => array( |
| 60 | array( 'name' => 'screenPageViews' ), |
| 61 | ), |
| 62 | ) |
| 63 | ); |
| 64 | |
| 65 | foreach ( $page_list_response['rows'] ?? array() as $row ) { |
| 66 | $page = isset( $row['dimensionValues'][0]['value'] ) ? $row['dimensionValues'][0]['value'] : ''; |
| 67 | $metric = isset( $row['metricValues'][0]['value'] ) ? (int) $row['metricValues'][0]['value'] : 0; |
| 68 | |
| 69 | if ( '' !== $page ) { |
| 70 | $page_list_count_data[ $page ] = $metric; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | $pageViewCount = array_sum( array_values( $page_list_count_data ) ); |
| 75 | |
| 76 | $user_response = $ga_stats->ga4_run_report_rest( |
| 77 | $ga4_property, |
| 78 | array( |
| 79 | 'dateRanges' => array( |
| 80 | array( |
| 81 | 'startDate' => $from, |
| 82 | 'endDate' => $to, |
| 83 | ), |
| 84 | ), |
| 85 | 'dimensions' => array( |
| 86 | array( 'name' => 'date' ), |
| 87 | ), |
| 88 | 'metrics' => array( |
| 89 | array( 'name' => 'newUsers' ), |
| 90 | ), |
| 91 | 'orderBys' => array( |
| 92 | array( |
| 93 | 'dimension' => array( |
| 94 | 'dimensionName' => 'date', |
| 95 | 'orderType' => 'ALPHANUMERIC', |
| 96 | ), |
| 97 | 'desc' => false, |
| 98 | ), |
| 99 | ), |
| 100 | ) |
| 101 | ); |
| 102 | |
| 103 | $gender_chart_response = $ga_stats->ga4_run_report_rest( |
| 104 | $ga4_property, |
| 105 | array( |
| 106 | 'dateRanges' => array( |
| 107 | array( |
| 108 | 'startDate' => $from, |
| 109 | 'endDate' => $to, |
| 110 | ), |
| 111 | ), |
| 112 | 'dimensions' => array( |
| 113 | array( 'name' => 'userGender' ), |
| 114 | ), |
| 115 | 'metrics' => array( |
| 116 | array( 'name' => 'newUsers' ), |
| 117 | ), |
| 118 | ) |
| 119 | ); |
| 120 | |
| 121 | $age_chart_response = $ga_stats->ga4_run_report_rest( |
| 122 | $ga4_property, |
| 123 | array( |
| 124 | 'dateRanges' => array( |
| 125 | array( |
| 126 | 'startDate' => $from, |
| 127 | 'endDate' => $to, |
| 128 | ), |
| 129 | ), |
| 130 | 'dimensions' => array( |
| 131 | array( 'name' => 'userAgeBracket' ), |
| 132 | ), |
| 133 | 'metrics' => array( |
| 134 | array( 'name' => 'newUsers' ), |
| 135 | ), |
| 136 | ) |
| 137 | ); |
| 138 | |
| 139 | foreach ( $gender_chart_response['rows'] ?? array() as $row ) { |
| 140 | $label = isset( $row['dimensionValues'][0]['value'] ) ? $row['dimensionValues'][0]['value'] : ''; |
| 141 | $metric = isset( $row['metricValues'][0]['value'] ) ? (int) $row['metricValues'][0]['value'] : 0; |
| 142 | |
| 143 | if ( '' !== $label && 'unknown' !== $label ) { |
| 144 | $gender_count_data[ $label ] = $metric; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | $gender_count_data = array_reverse( $gender_count_data, true ); |
| 149 | |
| 150 | foreach ( $age_chart_response['rows'] ?? array() as $row ) { |
| 151 | $label = isset( $row['dimensionValues'][0]['value'] ) ? $row['dimensionValues'][0]['value'] : ''; |
| 152 | $metric = isset( $row['metricValues'][0]['value'] ) ? (int) $row['metricValues'][0]['value'] : 0; |
| 153 | |
| 154 | if ( '' !== $label && 'unknown' !== $label ) { |
| 155 | $age_count_data[ $label ] = $metric; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | $ga4_device_chart_response = $ga_stats->ga4_run_report_rest( |
| 160 | $ga4_property, |
| 161 | array( |
| 162 | 'dateRanges' => array( |
| 163 | array( |
| 164 | 'startDate' => $from, |
| 165 | 'endDate' => $to, |
| 166 | ), |
| 167 | ), |
| 168 | 'dimensions' => array( |
| 169 | array( 'name' => 'deviceCategory' ), |
| 170 | ), |
| 171 | 'metrics' => array( |
| 172 | array( 'name' => 'newUsers' ), |
| 173 | ), |
| 174 | ) |
| 175 | ); |
| 176 | |
| 177 | foreach ( $ga4_device_chart_response['rows'] ?? array() as $row ) { |
| 178 | $label = isset( $row['dimensionValues'][0]['value'] ) ? $row['dimensionValues'][0]['value'] : ''; |
| 179 | $metric = isset( $row['metricValues'][0]['value'] ) ? (int) $row['metricValues'][0]['value'] : 0; |
| 180 | |
| 181 | if ( '' !== $label ) { |
| 182 | $ga4_demo_device_data[ $label ] = $metric; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | foreach ( $response['rows'] ?? array() as $row ) { |
| 187 | $date_value = isset( $row['dimensionValues'][0]['value'] ) ? $row['dimensionValues'][0]['value'] : ''; |
| 188 | $metric = isset( $row['metricValues'][0]['value'] ) ? (int) $row['metricValues'][0]['value'] : 0; |
| 189 | |
| 190 | if ( '' !== $date_value ) { |
| 191 | $date = gmdate( 'M d', strtotime( $date_value ) ); |
| 192 | $page_session_count_data[ $date ] = $metric; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | foreach ( $user_response['rows'] ?? array() as $row ) { |
| 197 | $date_value = isset( $row['dimensionValues'][0]['value'] ) ? $row['dimensionValues'][0]['value'] : ''; |
| 198 | $metric = isset( $row['metricValues'][0]['value'] ) ? (int) $row['metricValues'][0]['value'] : 0; |
| 199 | |
| 200 | if ( '' !== $date_value ) { |
| 201 | $date = gmdate( 'M d', strtotime( $date_value ) ); |
| 202 | $user_count_data[ $date ] = $metric; |
| 203 | } |
| 204 | } |
| 205 | ?> |
| 206 | <script type="text/javascript"> |
| 207 | ga_charts.init( function() { |
| 208 | const pageSessionData = new google.visualization.DataTable(); |
| 209 | const userData = new google.visualization.DataTable(); |
| 210 | |
| 211 | pageSessionData.addColumn( 'string', '<?php echo esc_js( __( 'Day', 'googleanalytics' ) ); ?>' ); |
| 212 | pageSessionData.addColumn( 'number', '<?php echo esc_js( __( 'Page Views', 'googleanalytics' ) ); ?>' ); |
| 213 | pageSessionData.addColumn( { type: 'string', role: 'tooltip', 'p': { 'html': true } } ); |
| 214 | |
| 215 | userData.addColumn( 'string', '<?php echo esc_js( __( 'Day', 'googleanalytics' ) ); ?>' ); |
| 216 | userData.addColumn( 'number', '<?php echo esc_js( __( 'New Users', 'googleanalytics' ) ); ?>' ); |
| 217 | userData.addColumn( { type: 'string', role: 'tooltip', 'p': { 'html': true } } ); |
| 218 | |
| 219 | // Page Sessions. |
| 220 | <?php foreach ( $page_session_count_data as $date => $value ) : ?> |
| 221 | pageSessionData.addRow( [ |
| 222 | '<?php echo esc_js( $date ); ?>', |
| 223 | <?php echo esc_js( $value ); ?>, |
| 224 | ga_charts.createPageTooltip( |
| 225 | '<?php echo esc_js( $date ); ?>', |
| 226 | '<?php echo esc_js( $value ); ?>' |
| 227 | ) |
| 228 | ] ); |
| 229 | <?php endforeach; ?> |
| 230 | |
| 231 | // User data. |
| 232 | <?php foreach ( $user_count_data as $date => $value ) : ?> |
| 233 | userData.addRow( [ |
| 234 | '<?php echo esc_js( $date ); ?>', |
| 235 | <?php echo esc_js( $value ); ?>, |
| 236 | ga_charts.createUserTooltip( |
| 237 | '<?php echo esc_js( $date ); ?>', |
| 238 | '<?php echo esc_js( $value ); ?>' |
| 239 | ) |
| 240 | ] ); |
| 241 | <?php endforeach; ?> |
| 242 | |
| 243 | ga_charts.events( pageSessionData ); |
| 244 | ga_charts.drawPageSessionChart( pageSessionData ); |
| 245 | ga_charts.drawUserChart( userData ); |
| 246 | |
| 247 | // GA4 Demographic gender chart. |
| 248 | <?php |
| 249 | $demo_gender_data = array(); |
| 250 | $demo_gender_data[0] = array( 'Gender', 'The gender of visitors' ); |
| 251 | |
| 252 | $x = 1; |
| 253 | foreach ( $gender_count_data as $gender_type => $amount ) { |
| 254 | $demo_gender_data[ $x ] = array( ucfirst( $gender_type ), intval( $amount ) ); |
| 255 | $x++; |
| 256 | } |
| 257 | ?> |
| 258 | ga_charts.drawDemoGenderGa4Chart(<?php echo wp_json_encode( $demo_gender_data ); ?>); |
| 259 | ga_loader.hide(); |
| 260 | |
| 261 | // Demographic age chart. |
| 262 | <?php |
| 263 | $demo_ga4_age_data = array(); |
| 264 | $demo_ga4_age_data[0] = array( 'Age', 'Average age range of visitors' ); |
| 265 | |
| 266 | $x = 1; |
| 267 | foreach ( $age_count_data as $age_type => $amount ) { |
| 268 | $demo_ga4_age_data[ $x ] = array( $age_type, intval( $amount ) ); |
| 269 | $x++; |
| 270 | } |
| 271 | ?> |
| 272 | ga_charts.drawDemoAgeGa4Chart(<?php echo wp_json_encode( $demo_ga4_age_data ); ?>); |
| 273 | |
| 274 | // Device chart. |
| 275 | <?php |
| 276 | $ga4_demo_count_data = array(); |
| 277 | $ga4_demo_count_data[0] = array( |
| 278 | __( 'Device', 'googleanalytics' ), |
| 279 | __( 'Device Breakdown', 'googleanalytics' ), |
| 280 | ); |
| 281 | |
| 282 | $x = 1; |
| 283 | foreach ( $ga4_demo_device_data as $device_type => $amount ) { |
| 284 | $ga4_demo_count_data[ $x ] = array( $device_type, intval( $amount ) ); |
| 285 | $x++; |
| 286 | } |
| 287 | ?> |
| 288 | ga_charts.drawGa4DemoDeviceChart(<?php echo wp_json_encode( $ga4_demo_count_data ); ?>); |
| 289 | |
| 290 | ga_loader.hide(); |
| 291 | } ); |
| 292 | </script> |
| 293 | |
| 294 | <div class="dashboard-title">GA4 Dashboard</div> |
| 295 | |
| 296 | <?php if ( true === empty( $page_list_count_data ) ) : ?> |
| 297 | <?php |
| 298 | echo wp_kses( |
| 299 | Ga_Helper::ga_wp_notice( |
| 300 | __( 'You don\'t appear to have enough page view data. Please come back at a later date once you do.', 'googleanalytics' ), |
| 301 | 'warning', |
| 302 | false, |
| 303 | array() |
| 304 | ), |
| 305 | array( |
| 306 | 'button' => array( |
| 307 | 'class' => array(), |
| 308 | 'onclick' => array(), |
| 309 | ), |
| 310 | 'div' => array( |
| 311 | 'class' => array(), |
| 312 | ), |
| 313 | 'p' => array(), |
| 314 | ) |
| 315 | ); |
| 316 | ?> |
| 317 | <?php else : ?> |
| 318 | <div id="page_session_chart_div"></div> |
| 319 | |
| 320 | <?php require plugin_dir_path( __FILE__ ) . 'ga4-demographic-chart.php'; ?> |
| 321 | |
| 322 | <div class="ga-panel ga-panel-default" style="width:100%; max-width:1210px; margin-top: 2rem;"> |
| 323 | <div class="ga-panel-heading"> |
| 324 | <strong><?php echo esc_html( 'Top 10 Pages/Posts by page views' ); ?></strong> |
| 325 | </div> |
| 326 | <div class="ga-panel-body"> |
| 327 | <div id="table-container"> |
| 328 | <table class="ga-table"> |
| 329 | <tr> |
| 330 | <th style="text-align: right;"> |
| 331 | <?php echo esc_html( 'Url' ); ?> |
| 332 | </th> |
| 333 | <th style="text-align: right;"> |
| 334 | <?php echo esc_html( 'Pageviews' ); ?> |
| 335 | </th> |
| 336 | <th style="text-align: right;"> |
| 337 | <?php echo '%'; ?> |
| 338 | </th> |
| 339 | </tr> |
| 340 | <?php foreach ( array_slice( $page_list_count_data, 0, 10 ) as $page => $metric ) : ?> |
| 341 | <?php $percentage = $pageViewCount > 0 ? round( ( (float) $metric / $pageViewCount ) * 100 ) : 0; ?> |
| 342 | <tr> |
| 343 | <td class="ga-col-name"> |
| 344 | <?php if ( '(direct) / (none)' !== $page ) : ?> |
| 345 | <?php |
| 346 | $single_breakdown = false === empty( $ts ) ? |
| 347 | '/explorer-table.plotKeys=%5B%5D&_r.drilldown=analytics.sourceMedium:' : |
| 348 | '/explorer-table.plotKeys=%5B%5D&_r.drilldown=analytics.pagePath:'; |
| 349 | ?> |
| 350 | <a class="ga-source-name" |
| 351 | href="<?php echo esc_url( |
| 352 | $page . $single_breakdown . str_replace( |
| 353 | '+', |
| 354 | '%20', |
| 355 | str_replace( |
| 356 | '2F', |
| 357 | '~2F', |
| 358 | str_replace( '%', '', rawurlencode( $page ) ) |
| 359 | ) |
| 360 | ) |
| 361 | ); ?>/" |
| 362 | target="_blank"><?php echo esc_html( $page ); ?></a> |
| 363 | <?php else : ?> |
| 364 | <?php echo esc_html( $page ); ?> |
| 365 | <?php endif; ?> |
| 366 | </td> |
| 367 | <td style="text-align: right"><?php echo esc_html( $metric ); ?></td> |
| 368 | <td> |
| 369 | <div class="progress"> |
| 370 | <div class="progress-bar" role="progressbar" |
| 371 | aria-valuenow="<?php echo esc_attr( $percentage ); ?>" aria-valuemin="0" |
| 372 | aria-valuemax="100" |
| 373 | style="width: <?php echo esc_attr( $percentage ); ?>%;"></div> |
| 374 | <span style="margin-left: 10px;"> |
| 375 | <?php echo esc_html( Ga_Helper::format_percent( $percentage ) ); ?> |
| 376 | </span> |
| 377 | </div> |
| 378 | </td> |
| 379 | </tr> |
| 380 | <?php endforeach; ?> |
| 381 | </table> |
| 382 | </div> |
| 383 | </div> |
| 384 | </div> |
| 385 | <?php endif; ?> |
| 386 | |
| 387 | <div id="user_chart_div"></div> |