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