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