templates
2 months ago
ga-accounts-selector.php
2 months ago
ga-auth-button.php
2 months ago
ga-code.php
2 months ago
ga-dashboard-widget.php
2 months ago
ga-debug-modal.php
2 months ago
ga-ga4-settings.php
2 months ago
ga-googleanalytics-loader.php
2 months ago
ga-notice.php
2 months ago
ga-oauth-notice.php
2 months ago
ga-wp-notice.php
2 months ago
old-page.php
2 months ago
page.php
2 months ago
statistics.php
2 months ago
stats.php
2 months ago
trending.php
2 months ago
stats.php
391 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Stats view. |
| 4 | * |
| 5 | * @package GoogleAnalytics |
| 6 | */ |
| 7 | |
| 8 | if (!defined('ABSPATH')) exit; |
| 9 | |
| 10 | // Variables passed in (with fallbacks in case they aren't). |
| 11 | $age_chart = true === isset( $age_chart ) ? $age_chart : array(); |
| 12 | $gender_chart = true === isset( $gender_chart ) ? $gender_chart : array(); |
| 13 | $event_count_chart = true === isset( $event_count_chart ) ? $event_count_chart : array(); |
| 14 | |
| 15 | $account_data = json_decode( get_option( 'googleanalytics_account_data', true ), true ); |
| 16 | $selected_data = json_decode( get_option( 'googleanalytics_selected_account', true ), true ); |
| 17 | $demo_enabled = get_option( 'googleanalytics_demographic' ); |
| 18 | $demo_enabled = false === empty( $demo_enabled ); |
| 19 | $credentials = GOOGLE_APPLICATION_CREDENTIALS; |
| 20 | $myfile = file_get_contents( $credentials, 'r' ); // phpcs:ignore |
| 21 | $client_obj = json_decode( $myfile ); |
| 22 | $client = new Ga_Admin(); |
| 23 | $client = $client->getGa4Client(); |
| 24 | $token_response = $client->getAccessToken(); |
| 25 | $client_stuff = (array) $client; |
| 26 | $client_obj = array_values( $client_stuff )[4]; |
| 27 | $ga4_demo_enabled = get_option( 'googleanalytics-ga4-demo' ); |
| 28 | $ga4_demo_enabled = '1' === $ga4_demo_enabled || 'on' === $ga4_demo_enabled ? true : false; |
| 29 | $ga4_property = get_option( 'googleanalytics-ga4-property' ); |
| 30 | $ga4_property = true === isset( $ga4_property ) ? $ga4_property : false; |
| 31 | $internal_prop = $ga4_property; |
| 32 | $ua_prop_used = get_option( 'googleanalytics-view-id' ); |
| 33 | |
| 34 | if ( true === is_array( $account_data ) ) { |
| 35 | foreach ( $account_data as $properties ) { |
| 36 | if ( $properties['id'] === $selected_data[0] ) { |
| 37 | foreach ( $properties['webProperties'] as $web_property ) { |
| 38 | if ( $web_property['webPropertyId'] === $selected_data[1] ) { |
| 39 | $internal_prop = $web_property['internalWebPropertyId']; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | $ts = filter_input( INPUT_GET, 'ts', FILTER_UNSAFE_RAW ); |
| 47 | $selected_page = false === empty( $ts ) ? '' : 'selected'; |
| 48 | $selected_source = false === empty( $ts ) ? 'selected' : ''; |
| 49 | $report_url = ''; |
| 50 | |
| 51 | if ( true === is_array( $selected_data ) ) { |
| 52 | $report_url = 'https://analytics.google.com/analytics/web/#/report/content-pages/a' . $selected_data[0] . 'w' . $internal_prop . 'p' . $selected_data[2]; |
| 53 | } |
| 54 | |
| 55 | $source_page_url = false === empty( $ts ) ? str_replace( |
| 56 | 'content-pages', |
| 57 | 'trafficsources-all-traffic', |
| 58 | $report_url |
| 59 | ) : $report_url; |
| 60 | $demographic_page_url = str_replace( 'content-pages', 'visitors-demographics-overview', $report_url ); |
| 61 | $type_label = false === empty( $ts ) ? 'Traffic Sources' : 'Pages/Posts'; |
| 62 | $source_url = 'admin.php?page=googleanalytics&ts=1'; |
| 63 | $send_data = get_option( 'googleanalytics_send_data' ); |
| 64 | $need_account_demo_enable = array() === $gender_chart && array() === $age_chart; |
| 65 | |
| 66 | // Filter GA Action. |
| 67 | $ga_action = filter_input( INPUT_GET, 'ga_action', FILTER_UNSAFE_RAW ); |
| 68 | $date_range = Ga_Helper::get_date_range_from_request(); |
| 69 | $days_in_english = Ga_Helper::get_period_in_days_words( $date_range['from'], $date_range['to'] ); |
| 70 | |
| 71 | if ( false === $demo_enabled && false === $ga4_demo_enabled ) { |
| 72 | echo wp_kses( |
| 73 | Ga_Helper::ga_wp_notice( |
| 74 | __( 'Visualize gender and age data with our new demographic feature.', 'googleanalytics' ), |
| 75 | 'warning', |
| 76 | false, |
| 77 | array( |
| 78 | 'url' => Ga_Helper::create_url( |
| 79 | Ga_Helper::get_current_url(), |
| 80 | array( Ga_Controller_Core::ACTION_PARAM_NAME => 'demo-ad' ) |
| 81 | ), |
| 82 | 'label' => __( 'Access Now', 'googleanalytics' ), |
| 83 | ) |
| 84 | ), |
| 85 | array( |
| 86 | 'button' => array( |
| 87 | 'class' => array(), |
| 88 | 'onclick' => array(), |
| 89 | ), |
| 90 | 'div' => array( |
| 91 | 'class' => array(), |
| 92 | ), |
| 93 | 'p' => array(), |
| 94 | ) |
| 95 | ); |
| 96 | } |
| 97 | ?> |
| 98 | <div class="wrap ga-wrap" id="ga-stats-container" data-scroll="<?php echo esc_attr( $ga_action ); ?>"> |
| 99 | <?php if (false === empty($selected_data)) : ?> |
| 100 | <div class="dashboard-title">Universal Analytics</div> |
| 101 | <?php endif; ?> |
| 102 | <div class="filter-choices"> |
| 103 | <div> |
| 104 | <?php |
| 105 | Ga_Template::load( |
| 106 | 'templates/date-custom-range-filter', |
| 107 | array( |
| 108 | 'date_from' => $date_range['from'], |
| 109 | 'date_to' => $date_range['to'], |
| 110 | ) |
| 111 | ); |
| 112 | ?> |
| 113 | </div> |
| 114 | </div> |
| 115 | <?php |
| 116 | |
| 117 | if ( false === empty( $chart ) ) : ?> |
| 118 | <div class="ga-panel ga-panel-default"> |
| 119 | <div class="ga-panel-heading"> |
| 120 | <strong> |
| 121 | <?php echo esc_html( 'Pageviews - ' . $days_in_english ); ?> |
| 122 | </strong> |
| 123 | </div> |
| 124 | <div class="ga-panel-body ga-chart"> |
| 125 | <div id="chart_div" style="width: 100%;"></div> |
| 126 | <div class="ga-loader-wrapper stats-page"> |
| 127 | <div class="ga-loader stats-page-loader"></div> |
| 128 | </div> |
| 129 | </div> |
| 130 | </div> |
| 131 | <?php endif; ?> |
| 132 | |
| 133 | <?php if ( ! empty( $boxes ) ) : ?> |
| 134 | <div class="ga-panel ga-panel-default"> |
| 135 | <div class="ga-panel-heading"><strong><?php echo esc_html( 'Comparison - ' . $days_in_english ); ?></strong> |
| 136 | </div> |
| 137 | <div class="ga-panel-body"> |
| 138 | <div class="ga-row"> |
| 139 | <?php foreach ( $boxes as $box ) : ?> |
| 140 | <div class="ga-box"> |
| 141 | <div class="ga-panel ga-panel-default"> |
| 142 | <div class="ga-panel-body ga-box-centered"> |
| 143 | <div class="ga-box-label"><?php echo esc_html( $box['label'] ); ?></div> |
| 144 | <div class="ga-box-diff" style="color: <?php echo esc_attr( $box['color'] ); ?>;"> |
| 145 | <?php echo esc_html( Ga_Helper::format_percent( $box['diff'] ) ); ?> |
| 146 | </div> |
| 147 | <div class="ga-box-comparison"><?php echo esc_html( $box['comparison'] ); ?></div> |
| 148 | </div> |
| 149 | </div> |
| 150 | </div> |
| 151 | <?php endforeach; ?> |
| 152 | </div> |
| 153 | </div> |
| 154 | </div> |
| 155 | <?php |
| 156 | endif; |
| 157 | |
| 158 | if ( false === empty( get_option( 'ga4-token' ) ) && true === empty( get_option( 'googleanalytics-view-id' ) ) ) { |
| 159 | echo ''; |
| 160 | } else { |
| 161 | require plugin_dir_path( __FILE__ ) . '/templates/demographic-chart.php'; |
| 162 | } |
| 163 | |
| 164 | if ( ! empty( $sources ) ) : |
| 165 | ?> |
| 166 | <div class="filter-choices" id="traffic-sources"> |
| 167 | <a href="<?php echo esc_url( get_admin_url( '', 'admin.php?page=googleanalytics#traffic-sources' ) ); ?>" |
| 168 | class="<?php echo esc_attr( $selected_page ); ?>"> |
| 169 | <?php esc_html_e( 'Page View', 'googleanalytics' ); ?> |
| 170 | </a> |
| 171 | <a href="<?php echo esc_url( get_admin_url( '', 'admin.php?page=googleanalytics&ts=1#traffic-sources' ) ); ?>" |
| 172 | class="<?php echo esc_attr( $selected_source ); ?>"> |
| 173 | <?php esc_html_e( 'Traffic Source', 'googleanalytics' ); ?> |
| 174 | </a> |
| 175 | </div> |
| 176 | <div class="ga-panel ga-panel-default"> |
| 177 | <div class="ga-panel-heading"> |
| 178 | <strong><?php echo esc_html( 'Top 10 ' . $type_label . ' by page views' ); ?></strong> |
| 179 | </div> |
| 180 | <div class="ga-panel-body"> |
| 181 | |
| 182 | <div id="table-container"> |
| 183 | <table class="ga-table"> |
| 184 | <tr> |
| 185 | <td colspan="2"> |
| 186 | </td> |
| 187 | <th style="text-align: right;"> |
| 188 | <?php echo esc_html( 'Pageviews' ); ?> |
| 189 | </th> |
| 190 | <th style="text-align: right;"> |
| 191 | <?php echo '%'; ?> |
| 192 | </th> |
| 193 | </tr> |
| 194 | <tr> |
| 195 | <td colspan="2"></td> |
| 196 | <td class="ga-col-pageviews" style="text-align: right"> |
| 197 | <div style="font-size: 16px;"><?php echo esc_html( $sources['total'] ); ?></div> |
| 198 | <div style="color: grey; font-size: 10px;">% of |
| 199 | Total: |
| 200 | <?php |
| 201 | echo esc_html( |
| 202 | Ga_Helper::format_percent( |
| 203 | ( false === empty( $sources['total'] ) ) ? number_format( |
| 204 | $sources['sum'] / $sources['total'] * 100, |
| 205 | 2, |
| 206 | '.', |
| 207 | ' ' |
| 208 | ) : 100 |
| 209 | ) |
| 210 | ); |
| 211 | ?> |
| 212 | (<?php echo esc_html( $sources['sum'] ); ?>) |
| 213 | </div> |
| 214 | </td> |
| 215 | <td class="ga-col-progressbar" style="text-align: right"> |
| 216 | <div style="font-size: 16px;"><?php echo esc_html( $sources['total'] ); ?></div> |
| 217 | <div style="color: grey; font-size: 10px;">% of |
| 218 | Total: |
| 219 | <?php |
| 220 | echo esc_html( |
| 221 | Ga_Helper::format_percent( |
| 222 | ( false === empty( $sources['total'] ) ) ? number_format( |
| 223 | $sources['sum'] / $sources['total'] * 100, |
| 224 | 2, |
| 225 | '.', |
| 226 | ' ' |
| 227 | ) : 100 |
| 228 | ) |
| 229 | ); |
| 230 | ?> |
| 231 | (<?php echo esc_html( $sources['sum'] ); ?>) |
| 232 | </div> |
| 233 | </td> |
| 234 | </tr> |
| 235 | <?php foreach ( $sources['rows'] as $key => $source ) : ?> |
| 236 | <tr> |
| 237 | <td style="width: 5%;text-align: right"><?php echo esc_html( $key ); ?>.</td> |
| 238 | <td class="ga-col-name"> |
| 239 | <?php |
| 240 | if ( '(direct) / (none)' !== $source['name'] ) : |
| 241 | $single_breakdown = false === empty( $ts ) ? |
| 242 | '/explorer-table.plotKeys=%5B%5D&_r.drilldown=analytics.sourceMedium:' : |
| 243 | '/explorer-table.plotKeys=%5B%5D&_r.drilldown=analytics.pagePath:'; |
| 244 | ?> |
| 245 | <a class="ga-source-name" |
| 246 | href=" |
| 247 | <?php |
| 248 | echo esc_url( |
| 249 | $source_page_url . $single_breakdown . str_replace( |
| 250 | '+', |
| 251 | '%20', |
| 252 | str_replace( |
| 253 | '2F', |
| 254 | '~2F', |
| 255 | str_replace( '%', '', rawurlencode( $source['url'] ) ) |
| 256 | ) |
| 257 | ) |
| 258 | ); |
| 259 | ?> |
| 260 | /" |
| 261 | target="_blank"><?php echo esc_html( $source['name'] ); ?></a> |
| 262 | <?php else : ?> |
| 263 | <?php echo esc_html( $source['name'] ); ?> |
| 264 | <?php endif; ?> |
| 265 | </td> |
| 266 | <td style="text-align: right"><?php echo esc_html( $source['number'] ); ?></td> |
| 267 | <td> |
| 268 | <div class="progress"> |
| 269 | <div class="progress-bar" role="progressbar" |
| 270 | aria-valuenow="<?php echo esc_attr( $source['percent'] ); ?>" aria-valuemin="0" |
| 271 | aria-valuemax="100" |
| 272 | style="width: <?php echo esc_attr( $source['percent'] ); ?>%;"></div> |
| 273 | <span style="margin-left: 10px;"> |
| 274 | <?php echo esc_html( Ga_Helper::format_percent( $source['percent'] ) ); ?> |
| 275 | </span> |
| 276 | </div> |
| 277 | </td> |
| 278 | </tr> |
| 279 | <?php endforeach; ?> |
| 280 | </table> |
| 281 | </div> |
| 282 | </div> |
| 283 | </div> |
| 284 | <?php endif; ?> |
| 285 | |
| 286 | <?php |
| 287 | if ( ! empty( $chart ) ) : |
| 288 | ?> |
| 289 | <script type="text/javascript"> |
| 290 | |
| 291 | ga_charts.init( function() { |
| 292 | |
| 293 | var data = new google.visualization.DataTable(); |
| 294 | |
| 295 | data.addColumn( 'string', '<?php echo esc_js( __( 'Day', 'googleanalytics' ) ); ?>' ); |
| 296 | data.addColumn( 'number', '<?php echo esc_js( __( 'Pageviews', 'googleanalytics' ) ); ?>' ); |
| 297 | data.addColumn( { type: 'string', role: 'tooltip', 'p': { 'html': true } } ); |
| 298 | |
| 299 | <?php foreach ( $chart as $row ) : ?> |
| 300 | data.addRow( [ |
| 301 | '<?php echo esc_js( $row['day'] ); ?>', |
| 302 | <?php echo esc_js( $row['current'] ); ?>, |
| 303 | ga_charts.createTooltip( '<?php echo esc_js( $row['day'] ); ?>', |
| 304 | '<?php echo esc_js( $row['current'] ); ?>' |
| 305 | ) |
| 306 | ] ); |
| 307 | <?php endforeach; ?> |
| 308 | ga_charts.events( data ); |
| 309 | ga_charts.drawChart( data ); |
| 310 | ga_loader.hide(); |
| 311 | |
| 312 | <?php |
| 313 | // Event Count |
| 314 | $event_count_data = array(); |
| 315 | $event_count_data[0] = array( |
| 316 | __( 'Device', 'googleanalytics' ), |
| 317 | __( 'Device Breakdown', 'googleanalytics' ), |
| 318 | ); |
| 319 | |
| 320 | $x = 1; |
| 321 | foreach ($event_count_chart as $event_type => $amount) { |
| 322 | $event_count_data[ $x ] = array( $event_type, intval( $amount ) ); |
| 323 | $x ++; |
| 324 | } |
| 325 | ?> |
| 326 | // Demographic gender chart |
| 327 | <?php |
| 328 | $demo_gender_data[0] = array( 'Gender', 'The gender of visitors' ); |
| 329 | |
| 330 | $x = 1; |
| 331 | foreach ( $gender_chart as $gender_type => $amount ) { |
| 332 | $demo_gender_data[ $x ] = array( ucfirst( $gender_type ), intval( $amount ) ); |
| 333 | $x ++; |
| 334 | } |
| 335 | ?> |
| 336 | |
| 337 | ga_charts.drawDemoGenderChart(<?php echo wp_json_encode( $demo_gender_data ); ?>); |
| 338 | ga_loader.hide(); |
| 339 | |
| 340 | // Demographic age chart |
| 341 | <?php |
| 342 | $demo_age_data[0] = array( 'Age', 'Average age range of visitors' ); |
| 343 | |
| 344 | $x = 1; |
| 345 | |
| 346 | foreach ( $age_chart as $age_type => $amount ) { |
| 347 | $demo_age_data[ $x ] = array( $age_type, intval( $amount ) ); |
| 348 | $x ++; |
| 349 | } |
| 350 | ?> |
| 351 | ga_charts.drawDemoAgeChart(<?php echo wp_json_encode( $demo_age_data ); ?>); |
| 352 | |
| 353 | // Device chart. |
| 354 | <?php |
| 355 | $demo_device_data = array(); |
| 356 | $demo_device_data[0] = array( |
| 357 | __( 'Device', 'googleanalytics' ), |
| 358 | __( 'Device Breakdown', 'googleanalytics' ), |
| 359 | ); |
| 360 | |
| 361 | $x = 1; |
| 362 | foreach ( $device_chart as $age_type => $amount ) { |
| 363 | $demo_device_data[ $x ] = array( $age_type, intval( $amount ) ); |
| 364 | $x ++; |
| 365 | } |
| 366 | ?> |
| 367 | ga_charts.drawDemoDeviceChart(<?php echo wp_json_encode( $demo_device_data ); ?>); |
| 368 | |
| 369 | ga_loader.hide(); |
| 370 | |
| 371 | <?php if ( Ga_Helper::are_features_enabled() && ! empty( $send_data ) && 'true' === $send_data ) : ?> |
| 372 | ga_events.sendDemoData(<?php echo esc_js( get_option( 'googleanalytics_demo_data' ) ); ?>); |
| 373 | <?php |
| 374 | update_option( 'googleanalytics_demo_date', gmdate( 'Y-m-d' ) ); |
| 375 | update_option( 'googleanalytics_send_data', 'false' ); |
| 376 | endif; |
| 377 | ?> |
| 378 | }, |
| 379 | ); |
| 380 | </script> |
| 381 | <?php |
| 382 | endif; |
| 383 | |
| 384 | if ( false !== $ga4_property && true === empty( $ua_prop_used ) ) { |
| 385 | include 'templates/ga4-dashboard.php'; |
| 386 | } |
| 387 | |
| 388 | include 'templates/demo-popup.php'; |
| 389 | ?> |
| 390 | </div> |
| 391 |