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